blob: 8a548cf51732393342587714b9a51d548c3ec7cd [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"
John McCall19510852010-08-20 18:27:03 +000016#include "clang/Sema/Scope.h"
17#include "clang/Sema/ParsedTemplate.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
Douglas Gregor23c94db2010-07-02 17:43:08 +000045 return Actions.ActOnTypeName(getCurScope(), 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:
Fariborz Jahanian1b72fa72010-08-17 23:19:16 +0000177 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
178 0, SourceLocation(), 0, 0, CurrAttr);
179 if (CurrAttr->getKind() == AttributeList::AT_IBOutletCollection)
180 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000181 // If it's a builtin type name, eat it and expect a rparen
182 // __attribute__(( vec_type_hint(char) ))
183 ConsumeToken();
Nate Begeman6f3d8382009-06-26 06:32:41 +0000184 if (Tok.is(tok::r_paren))
185 ConsumeParen();
186 break;
187 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000189 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 // now parse the list of expressions
193 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000194 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000195 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 ArgExprsOk = false;
197 SkipUntil(tok::r_paren);
198 break;
199 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000200 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 }
Chris Lattner04d66662007-10-09 17:33:22 +0000202 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 break;
204 ConsumeToken(); // Eat the comma, move to the next argument
205 }
206 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000207 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000209 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
Sean Huntbbd37c62009-11-21 08:43:09 +0000210 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
211 ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 CurrAttr);
213 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000214 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 }
216 }
217 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000218 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 0, SourceLocation(), 0, 0, CurrAttr);
220 }
221 }
222 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000224 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000225 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
226 SkipUntil(tok::r_paren, false);
227 }
228 if (EndLoc)
229 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 }
231 return CurrAttr;
232}
233
Eli Friedmana23b4852009-06-08 07:21:15 +0000234/// ParseMicrosoftDeclSpec - Parse an __declspec construct
235///
236/// [MS] decl-specifier:
237/// __declspec ( extended-decl-modifier-seq )
238///
239/// [MS] extended-decl-modifier-seq:
240/// extended-decl-modifier[opt]
241/// extended-decl-modifier extended-decl-modifier-seq
242
Eli Friedman290eeb02009-06-08 23:27:34 +0000243AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000244 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000245
Steve Narofff59e17e2008-12-24 20:59:21 +0000246 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000247 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
248 "declspec")) {
249 SkipUntil(tok::r_paren, true); // skip until ) or ;
250 return CurrAttr;
251 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000252 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000253 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
254 SourceLocation AttrNameLoc = ConsumeToken();
255 if (Tok.is(tok::l_paren)) {
256 ConsumeParen();
257 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
258 // correctly.
259 OwningExprResult ArgExpr(ParseAssignmentExpression());
260 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000261 Expr *ExprList = ArgExpr.take();
Sean Huntbbd37c62009-11-21 08:43:09 +0000262 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedmana23b4852009-06-08 07:21:15 +0000263 SourceLocation(), &ExprList, 1,
264 CurrAttr, true);
265 }
266 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
267 SkipUntil(tok::r_paren, false);
268 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000269 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
270 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000271 }
272 }
273 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
274 SkipUntil(tok::r_paren, false);
Eli Friedman290eeb02009-06-08 23:27:34 +0000275 return CurrAttr;
276}
277
278AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
279 // Treat these like attributes
280 // FIXME: Allow Sema to distinguish between these and real attributes!
281 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000282 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
283 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000284 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
285 SourceLocation AttrNameLoc = ConsumeToken();
286 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
287 // FIXME: Support these properly!
288 continue;
Sean Huntbbd37c62009-11-21 08:43:09 +0000289 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman290eeb02009-06-08 23:27:34 +0000290 SourceLocation(), 0, 0, CurrAttr, true);
291 }
292 return CurrAttr;
Steve Narofff59e17e2008-12-24 20:59:21 +0000293}
294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295/// ParseDeclaration - Parse a full 'declaration', which consists of
296/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000297/// 'Context' should be a Declarator::TheContext value. This returns the
298/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000299///
300/// declaration: [C99 6.7]
301/// block-declaration ->
302/// simple-declaration
303/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000304/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000305/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000306/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000307/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000308/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000309/// others... [FIXME]
310///
Chris Lattner97144fc2009-04-02 04:16:50 +0000311Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000312 SourceLocation &DeclEnd,
313 CXX0XAttributeList Attr) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000314 ParenBraceBracketBalancer BalancerRAIIObj(*this);
315
John McCalld226f652010-08-21 09:40:31 +0000316 Decl *SingleDecl = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000317 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000318 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000319 case tok::kw_export:
Sean Huntbbd37c62009-11-21 08:43:09 +0000320 if (Attr.HasAttr)
321 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
322 << Attr.Range;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000323 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000324 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000325 case tok::kw_namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +0000326 if (Attr.HasAttr)
327 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
328 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000329 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000330 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000331 case tok::kw_using:
Sean Huntbbd37c62009-11-21 08:43:09 +0000332 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner682bf922009-03-29 16:50:03 +0000333 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000334 case tok::kw_static_assert:
Sean Huntbbd37c62009-11-21 08:43:09 +0000335 if (Attr.HasAttr)
336 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
337 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000338 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000339 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000340 default:
Chris Lattner5c5db552010-04-05 18:18:31 +0000341 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000342 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000343
Chris Lattner682bf922009-03-29 16:50:03 +0000344 // This routine returns a DeclGroup, if the thing we parsed only contains a
345 // single decl, convert it now.
346 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000347}
348
349/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
350/// declaration-specifiers init-declarator-list[opt] ';'
351///[C90/C++]init-declarator-list ';' [TODO]
352/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000353///
354/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000355/// declaration. If it is true, it checks for and eats it.
Chris Lattnercd147752009-03-29 17:27:48 +0000356Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000357 SourceLocation &DeclEnd,
Chris Lattner5c5db552010-04-05 18:18:31 +0000358 AttributeList *Attr,
359 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000361 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000362 if (Attr)
363 DS.AddAttributes(Attr);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000364 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
365 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
368 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000369 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000370 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000371 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallaec03712010-05-21 20:45:30 +0000372 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000373 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000374 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Chris Lattner5c5db552010-04-05 18:18:31 +0000377 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld8ac0572009-11-03 19:26:08 +0000378}
Mike Stump1eb44332009-09-09 15:08:12 +0000379
John McCalld8ac0572009-11-03 19:26:08 +0000380/// ParseDeclGroup - Having concluded that this is either a function
381/// definition or a group of object declarations, actually parse the
382/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000383Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
384 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000385 bool AllowFunctionDefinitions,
386 SourceLocation *DeclEnd) {
387 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000388 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000389 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000390
John McCalld8ac0572009-11-03 19:26:08 +0000391 // Bail out if the first declarator didn't seem well-formed.
392 if (!D.hasName() && !D.mayOmitIdentifier()) {
393 // Skip until ; or }.
394 SkipUntil(tok::r_brace, true, true);
395 if (Tok.is(tok::semi))
396 ConsumeToken();
397 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattnerc82daef2010-07-11 22:24:20 +0000400 // Check to see if we have a function *definition* which must have a body.
401 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
402 // Look at the next token to make sure that this isn't a function
403 // declaration. We have to check this because __attribute__ might be the
404 // start of a function definition in GCC-extended K&R C.
405 !isDeclarationAfterDeclarator()) {
406
Chris Lattner004659a2010-07-11 22:42:07 +0000407 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000408 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
409 Diag(Tok, diag::err_function_declared_typedef);
410
411 // Recover by treating the 'typedef' as spurious.
412 DS.ClearStorageClassSpecs();
413 }
414
John McCalld226f652010-08-21 09:40:31 +0000415 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000416 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000417 }
418
419 if (isDeclarationSpecifier()) {
420 // If there is an invalid declaration specifier right after the function
421 // prototype, then we must be in a missing semicolon case where this isn't
422 // actually a body. Just fall through into the code that handles it as a
423 // prototype, and let the top-level code handle the erroneous declspec
424 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000425 } else {
426 Diag(Tok, diag::err_expected_fn_body);
427 SkipUntil(tok::semi);
428 return DeclGroupPtrTy();
429 }
430 }
431
John McCalld226f652010-08-21 09:40:31 +0000432 llvm::SmallVector<Decl *, 8> DeclsInGroup;
433 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000434 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000435 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000436 DeclsInGroup.push_back(FirstDecl);
437
438 // If we don't have a comma, it is either the end of the list (a ';') or an
439 // error, bail out.
440 while (Tok.is(tok::comma)) {
441 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000442 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000443
444 // Parse the next declarator.
445 D.clear();
446
447 // Accept attributes in an init-declarator. In the first declarator in a
448 // declaration, these would be part of the declspec. In subsequent
449 // declarators, they become part of the declarator itself, so that they
450 // don't apply to declarators after *this* one. Examples:
451 // short __attribute__((common)) var; -> declspec
452 // short var __attribute__((common)); -> declarator
453 // short x, __attribute__((common)) var; -> declarator
454 if (Tok.is(tok::kw___attribute)) {
455 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000456 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld8ac0572009-11-03 19:26:08 +0000457 D.AddAttributes(AttrList, Loc);
458 }
459
460 ParseDeclarator(D);
461
John McCalld226f652010-08-21 09:40:31 +0000462 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000463 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000464 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000465 DeclsInGroup.push_back(ThisDecl);
466 }
467
468 if (DeclEnd)
469 *DeclEnd = Tok.getLocation();
470
471 if (Context != Declarator::ForContext &&
472 ExpectAndConsume(tok::semi,
473 Context == Declarator::FileContext
474 ? diag::err_invalid_token_after_toplevel_declarator
475 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000476 // Okay, there was no semicolon and one was expected. If we see a
477 // declaration specifier, just assume it was missing and continue parsing.
478 // Otherwise things are very confused and we skip to recover.
479 if (!isDeclarationSpecifier()) {
480 SkipUntil(tok::r_brace, true, true);
481 if (Tok.is(tok::semi))
482 ConsumeToken();
483 }
John McCalld8ac0572009-11-03 19:26:08 +0000484 }
485
Douglas Gregor23c94db2010-07-02 17:43:08 +0000486 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000487 DeclsInGroup.data(),
488 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000489}
490
Douglas Gregor1426e532009-05-12 21:31:51 +0000491/// \brief Parse 'declaration' after parsing 'declaration-specifiers
492/// declarator'. This method parses the remainder of the declaration
493/// (including any attributes or initializer, among other things) and
494/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000495///
Reid Spencer5f016e22007-07-11 17:01:13 +0000496/// init-declarator: [C99 6.7]
497/// declarator
498/// declarator '=' initializer
499/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
500/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000501/// [C++] declarator initializer[opt]
502///
503/// [C++] initializer:
504/// [C++] '=' initializer-clause
505/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000506/// [C++0x] '=' 'default' [TODO]
507/// [C++0x] '=' 'delete'
508///
509/// According to the standard grammar, =default and =delete are function
510/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000511///
John McCalld226f652010-08-21 09:40:31 +0000512Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000513 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000514 // If a simple-asm-expr is present, parse it.
515 if (Tok.is(tok::kw_asm)) {
516 SourceLocation Loc;
517 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
518 if (AsmLabel.isInvalid()) {
519 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000520 return 0;
Douglas Gregor1426e532009-05-12 21:31:51 +0000521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Douglas Gregor1426e532009-05-12 21:31:51 +0000523 D.setAsmLabel(AsmLabel.release());
524 D.SetRangeEnd(Loc);
525 }
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Douglas Gregor1426e532009-05-12 21:31:51 +0000527 // If attributes are present, parse them.
528 if (Tok.is(tok::kw___attribute)) {
529 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000530 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000531 D.AddAttributes(AttrList, Loc);
532 }
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Douglas Gregor1426e532009-05-12 21:31:51 +0000534 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000535 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000536 switch (TemplateInfo.Kind) {
537 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000538 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000539 break;
540
541 case ParsedTemplateInfo::Template:
542 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000543 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Douglas Gregore542c862009-06-23 23:11:28 +0000544 Action::MultiTemplateParamsArg(Actions,
545 TemplateInfo.TemplateParams->data(),
546 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000547 D);
548 break;
549
550 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000551 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000552 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000553 TemplateInfo.ExternLoc,
554 TemplateInfo.TemplateLoc,
555 D);
556 if (ThisRes.isInvalid()) {
557 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000558 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000559 }
560
561 ThisDecl = ThisRes.get();
562 break;
563 }
564 }
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Douglas Gregor1426e532009-05-12 21:31:51 +0000566 // Parse declarator '=' initializer.
567 if (Tok.is(tok::equal)) {
568 ConsumeToken();
569 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
570 SourceLocation DelLoc = ConsumeToken();
571 Actions.SetDeclDeleted(ThisDecl, DelLoc);
572 } else {
John McCall731ad842009-12-19 09:28:58 +0000573 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
574 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000575 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000576 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000577
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000578 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000579 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000580 ConsumeCodeCompletionToken();
581 SkipUntil(tok::comma, true, true);
582 return ThisDecl;
583 }
584
Douglas Gregor1426e532009-05-12 21:31:51 +0000585 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000586
John McCall731ad842009-12-19 09:28:58 +0000587 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000588 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000589 ExitScope();
590 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000591
Douglas Gregor1426e532009-05-12 21:31:51 +0000592 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000593 SkipUntil(tok::comma, true, true);
594 Actions.ActOnInitializerError(ThisDecl);
595 } else
596 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000597 }
598 } else if (Tok.is(tok::l_paren)) {
599 // Parse C++ direct initializer: '(' expression-list ')'
600 SourceLocation LParenLoc = ConsumeParen();
601 ExprVector Exprs(Actions);
602 CommaLocsTy CommaLocs;
603
Douglas Gregorb4debae2009-12-22 17:47:17 +0000604 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
605 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000606 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000607 }
608
Douglas Gregor1426e532009-05-12 21:31:51 +0000609 if (ParseExpressionList(Exprs, CommaLocs)) {
610 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000611
612 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000613 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000614 ExitScope();
615 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000616 } else {
617 // Match the ')'.
618 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
619
620 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
621 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000622
623 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000624 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000625 ExitScope();
626 }
627
Douglas Gregor1426e532009-05-12 21:31:51 +0000628 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
629 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000630 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000631 }
632 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000633 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000634 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
635 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000636 }
637
638 return ThisDecl;
639}
640
Reid Spencer5f016e22007-07-11 17:01:13 +0000641/// ParseSpecifierQualifierList
642/// specifier-qualifier-list:
643/// type-specifier specifier-qualifier-list[opt]
644/// type-qualifier specifier-qualifier-list[opt]
645/// [GNU] attributes specifier-qualifier-list[opt]
646///
647void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
648 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
649 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Reid Spencer5f016e22007-07-11 17:01:13 +0000652 // Validate declspec for type-name.
653 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000654 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
655 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 // Issue diagnostic and remove storage class if present.
659 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
660 if (DS.getStorageClassSpecLoc().isValid())
661 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
662 else
663 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
664 DS.ClearStorageClassSpecs();
665 }
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 // Issue diagnostic and remove function specfier if present.
668 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000669 if (DS.isInlineSpecified())
670 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
671 if (DS.isVirtualSpecified())
672 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
673 if (DS.isExplicitSpecified())
674 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 DS.ClearFunctionSpecs();
676 }
677}
678
Chris Lattnerc199ab32009-04-12 20:42:31 +0000679/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
680/// specified token is valid after the identifier in a declarator which
681/// immediately follows the declspec. For example, these things are valid:
682///
683/// int x [ 4]; // direct-declarator
684/// int x ( int y); // direct-declarator
685/// int(int x ) // direct-declarator
686/// int x ; // simple-declaration
687/// int x = 17; // init-declarator-list
688/// int x , y; // init-declarator-list
689/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000690/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000691/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000692///
693/// This is not, because 'x' does not immediately follow the declspec (though
694/// ')' happens to be valid anyway).
695/// int (x)
696///
697static bool isValidAfterIdentifierInDeclarator(const Token &T) {
698 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
699 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000700 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000701}
702
Chris Lattnere40c2952009-04-14 21:34:55 +0000703
704/// ParseImplicitInt - This method is called when we have an non-typename
705/// identifier in a declspec (which normally terminates the decl spec) when
706/// the declspec has no type specifier. In this case, the declspec is either
707/// malformed or is "implicit int" (in K&R and C89).
708///
709/// This method handles diagnosing this prettily and returns false if the
710/// declspec is done being processed. If it recovers and thinks there may be
711/// other pieces of declspec after it, it returns true.
712///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000713bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000714 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000715 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000716 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Chris Lattnere40c2952009-04-14 21:34:55 +0000718 SourceLocation Loc = Tok.getLocation();
719 // If we see an identifier that is not a type name, we normally would
720 // parse it as the identifer being declared. However, when a typename
721 // is typo'd or the definition is not included, this will incorrectly
722 // parse the typename as the identifier name and fall over misparsing
723 // later parts of the diagnostic.
724 //
725 // As such, we try to do some look-ahead in cases where this would
726 // otherwise be an "implicit-int" case to see if this is invalid. For
727 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
728 // an identifier with implicit int, we'd get a parse error because the
729 // next token is obviously invalid for a type. Parse these as a case
730 // with an invalid type specifier.
731 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Chris Lattnere40c2952009-04-14 21:34:55 +0000733 // Since we know that this either implicit int (which is rare) or an
734 // error, we'd do lookahead to try to do better recovery.
735 if (isValidAfterIdentifierInDeclarator(NextToken())) {
736 // If this token is valid for implicit int, e.g. "static x = 4", then
737 // we just avoid eating the identifier, so it will be parsed as the
738 // identifier in the declarator.
739 return false;
740 }
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Chris Lattnere40c2952009-04-14 21:34:55 +0000742 // Otherwise, if we don't consume this token, we are going to emit an
743 // error anyway. Try to recover from various common problems. Check
744 // to see if this was a reference to a tag name without a tag specified.
745 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000746 //
747 // C++ doesn't need this, and isTagName doesn't take SS.
748 if (SS == 0) {
749 const char *TagName = 0;
750 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Douglas Gregor23c94db2010-07-02 17:43:08 +0000752 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +0000753 default: break;
754 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
755 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
756 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
757 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
758 }
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Chris Lattnerf4382f52009-04-14 22:17:06 +0000760 if (TagName) {
761 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +0000762 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +0000763 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Chris Lattnerf4382f52009-04-14 22:17:06 +0000765 // Parse this as a tag as if the missing tag were present.
766 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +0000767 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000768 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000769 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000770 return true;
771 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000772 }
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Douglas Gregora786fdb2009-10-13 23:27:22 +0000774 // This is almost certainly an invalid type name. Let the action emit a
775 // diagnostic and attempt to recover.
776 Action::TypeTy *T = 0;
777 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +0000778 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +0000779 // The action emitted a diagnostic, so we don't have to.
780 if (T) {
781 // The action has suggested that the type T could be used. Set that as
782 // the type in the declaration specifiers, consume the would-be type
783 // name token, and we're done.
784 const char *PrevSpec;
785 unsigned DiagID;
786 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
787 false);
788 DS.SetRangeEnd(Tok.getLocation());
789 ConsumeToken();
790
791 // There may be other declaration specifiers after this.
792 return true;
793 }
794
795 // Fall through; the action had no suggestion for us.
796 } else {
797 // The action did not emit a diagnostic, so emit one now.
798 SourceRange R;
799 if (SS) R = SS->getRange();
800 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Douglas Gregora786fdb2009-10-13 23:27:22 +0000803 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000804 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000805 unsigned DiagID;
806 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000807 DS.SetRangeEnd(Tok.getLocation());
808 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Chris Lattnere40c2952009-04-14 21:34:55 +0000810 // TODO: Could inject an invalid typedef decl in an enclosing scope to
811 // avoid rippling error messages on subsequent uses of the same type,
812 // could be useful if #include was forgotten.
813 return false;
814}
815
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000816/// \brief Determine the declaration specifier context from the declarator
817/// context.
818///
819/// \param Context the declarator context, which is one of the
820/// Declarator::TheContext enumerator values.
821Parser::DeclSpecContext
822Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
823 if (Context == Declarator::MemberContext)
824 return DSC_class;
825 if (Context == Declarator::FileContext)
826 return DSC_top_level;
827 return DSC_normal;
828}
829
Reid Spencer5f016e22007-07-11 17:01:13 +0000830/// ParseDeclarationSpecifiers
831/// declaration-specifiers: [C99 6.7]
832/// storage-class-specifier declaration-specifiers[opt]
833/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000834/// [C99] function-specifier declaration-specifiers[opt]
835/// [GNU] attributes declaration-specifiers[opt]
836///
837/// storage-class-specifier: [C99 6.7.1]
838/// 'typedef'
839/// 'extern'
840/// 'static'
841/// 'auto'
842/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000843/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000844/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000845/// function-specifier: [C99 6.7.4]
846/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000847/// [C++] 'virtual'
848/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000849/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000850/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000851
Reid Spencer5f016e22007-07-11 17:01:13 +0000852///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000853void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000854 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000855 AccessSpecifier AS,
856 DeclSpecContext DSContext) {
Douglas Gregor791215b2009-09-21 20:51:25 +0000857 if (Tok.is(tok::code_completion)) {
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000858 Action::ParserCompletionContext CCC = Action::PCC_Namespace;
Douglas Gregor01dfea02010-01-10 23:08:15 +0000859 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000860 CCC = DSContext == DSC_class? Action::PCC_MemberTemplate
861 : Action::PCC_Template;
Douglas Gregor01dfea02010-01-10 23:08:15 +0000862 else if (DSContext == DSC_class)
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000863 CCC = Action::PCC_Class;
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000864 else if (ObjCImpDecl)
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000865 CCC = Action::PCC_ObjCImplementation;
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000866
Douglas Gregor23c94db2010-07-02 17:43:08 +0000867 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Douglas Gregordc845342010-05-25 05:58:43 +0000868 ConsumeCodeCompletionToken();
Douglas Gregor791215b2009-09-21 20:51:25 +0000869 }
870
Chris Lattner81c018d2008-03-13 06:29:04 +0000871 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000873 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000875 unsigned DiagID = 0;
876
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000878
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000880 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000881 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 // If this is not a declaration specifier token, we're done reading decl
883 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000884 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Chris Lattner5e02c472009-01-05 00:07:25 +0000887 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +0000888 // C++ scope specifier. Annotate and loop, or bail out on error.
889 if (TryAnnotateCXXScopeToken(true)) {
890 if (!DS.hasTypeSpecifier())
891 DS.SetTypeSpecError();
892 goto DoneWithDeclSpec;
893 }
John McCall2e0a7152010-03-01 18:20:46 +0000894 if (Tok.is(tok::coloncolon)) // ::new or ::delete
895 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +0000896 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000897
898 case tok::annot_cxxscope: {
899 if (DS.hasTypeSpecifier())
900 goto DoneWithDeclSpec;
901
John McCallaa87d332009-12-12 11:40:51 +0000902 CXXScopeSpec SS;
John McCallca0408f2010-08-23 06:44:23 +0000903 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCallaa87d332009-12-12 11:40:51 +0000904 SS.setRange(Tok.getAnnotationRange());
905
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000906 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000907 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000908 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000909 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000910 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000911 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000912
913 // C++ [class.qual]p2:
914 // In a lookup in which the constructor is an acceptable lookup
915 // result and the nested-name-specifier nominates a class C:
916 //
917 // - if the name specified after the
918 // nested-name-specifier, when looked up in C, is the
919 // injected-class-name of C (Clause 9), or
920 //
921 // - if the name specified after the nested-name-specifier
922 // is the same as the identifier or the
923 // simple-template-id's template-name in the last
924 // component of the nested-name-specifier,
925 //
926 // the name is instead considered to name the constructor of
927 // class C.
928 //
929 // Thus, if the template-name is actually the constructor
930 // name, then the code is ill-formed; this interpretation is
931 // reinforced by the NAD status of core issue 635.
932 TemplateIdAnnotation *TemplateId
933 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +0000934 if ((DSContext == DSC_top_level ||
935 (DSContext == DSC_class && DS.isFriendSpecified())) &&
936 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000937 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000938 if (isConstructorDeclarator()) {
939 // The user meant this to be an out-of-line constructor
940 // definition, but template arguments are not allowed
941 // there. Just allow this as a constructor; we'll
942 // complain about it later.
943 goto DoneWithDeclSpec;
944 }
945
946 // The user meant this to name a type, but it actually names
947 // a constructor with some extraneous template
948 // arguments. Complain, then parse it as a type as the user
949 // intended.
950 Diag(TemplateId->TemplateNameLoc,
951 diag::err_out_of_line_template_id_names_constructor)
952 << TemplateId->Name;
953 }
954
John McCallaa87d332009-12-12 11:40:51 +0000955 DS.getTypeSpecScope() = SS;
956 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +0000957 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000958 "ParseOptionalCXXScopeSpecifier not working");
959 AnnotateTemplateIdTokenAsType(&SS);
960 continue;
961 }
962
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000963 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +0000964 DS.getTypeSpecScope() = SS;
965 ConsumeToken(); // The C++ scope.
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000966 if (Tok.getAnnotationValue())
967 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
968 PrevSpec, DiagID,
969 Tok.getAnnotationValue());
970 else
971 DS.SetTypeSpecError();
972 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
973 ConsumeToken(); // The typename
974 }
975
Douglas Gregor9135c722009-03-25 15:40:00 +0000976 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000977 goto DoneWithDeclSpec;
978
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000979 // If we're in a context where the identifier could be a class name,
980 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +0000981 if ((DSContext == DSC_top_level ||
982 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000983 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000984 &SS)) {
985 if (isConstructorDeclarator())
986 goto DoneWithDeclSpec;
987
988 // As noted in C++ [class.qual]p2 (cited above), when the name
989 // of the class is qualified in a context where it could name
990 // a constructor, its a constructor name. However, we've
991 // looked at the declarator, and the user probably meant this
992 // to be a type. Complain that it isn't supposed to be treated
993 // as a type, then proceed to parse it as a type.
994 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
995 << Next.getIdentifierInfo();
996 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000997
Douglas Gregorb696ea32009-02-04 17:00:24 +0000998 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
Douglas Gregor23c94db2010-07-02 17:43:08 +0000999 Next.getLocation(), getCurScope(), &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001000
Chris Lattnerf4382f52009-04-14 22:17:06 +00001001 // If the referenced identifier is not a type, then this declspec is
1002 // erroneous: We already checked about that it has no type specifier, and
1003 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001004 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001005 if (TypeRep == 0) {
1006 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001007 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001008 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001009 }
Mike Stump1eb44332009-09-09 15:08:12 +00001010
John McCallaa87d332009-12-12 11:40:51 +00001011 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001012 ConsumeToken(); // The C++ scope.
1013
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001014 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001015 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001016 if (isInvalid)
1017 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001019 DS.SetRangeEnd(Tok.getLocation());
1020 ConsumeToken(); // The typename.
1021
1022 continue;
1023 }
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Chris Lattner80d0c892009-01-21 19:48:37 +00001025 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001026 if (Tok.getAnnotationValue())
1027 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001028 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001029 else
1030 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001031
1032 if (isInvalid)
1033 break;
1034
Chris Lattner80d0c892009-01-21 19:48:37 +00001035 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1036 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Chris Lattner80d0c892009-01-21 19:48:37 +00001038 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1039 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1040 // Objective-C interface. If we don't have Objective-C or a '<', this is
1041 // just a normal reference to a typedef name.
1042 if (!Tok.is(tok::less) || !getLang().ObjC1)
1043 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001045 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001046 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001047 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1048 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1049 LAngleLoc, EndProtoLoc);
1050 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1051 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Chris Lattner80d0c892009-01-21 19:48:37 +00001053 DS.SetRangeEnd(EndProtoLoc);
1054 continue;
1055 }
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Chris Lattner3bd934a2008-07-26 01:18:38 +00001057 // typedef-name
1058 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001059 // In C++, check to see if this is a scope specifier like foo::bar::, if
1060 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001061 if (getLang().CPlusPlus) {
1062 if (TryAnnotateCXXScopeToken(true)) {
1063 if (!DS.hasTypeSpecifier())
1064 DS.SetTypeSpecError();
1065 goto DoneWithDeclSpec;
1066 }
1067 if (!Tok.is(tok::identifier))
1068 continue;
1069 }
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Chris Lattner3bd934a2008-07-26 01:18:38 +00001071 // This identifier can only be a typedef name if we haven't already seen
1072 // a type-specifier. Without this check we misparse:
1073 // typedef int X; struct Y { short X; }; as 'short int'.
1074 if (DS.hasTypeSpecifier())
1075 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001076
John Thompson82287d12010-02-05 00:12:22 +00001077 // Check for need to substitute AltiVec keyword tokens.
1078 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1079 break;
1080
Chris Lattner3bd934a2008-07-26 01:18:38 +00001081 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +00001082 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor23c94db2010-07-02 17:43:08 +00001083 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001084
Chris Lattnerc199ab32009-04-12 20:42:31 +00001085 // If this is not a typedef name, don't parse it as part of the declspec,
1086 // it must be an implicit int or an error.
1087 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001088 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001089 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001090 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001091
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001092 // If we're in a context where the identifier could be a class name,
1093 // check whether this is a constructor declaration.
1094 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001095 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001096 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001097 goto DoneWithDeclSpec;
1098
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001099 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001100 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001101 if (isInvalid)
1102 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Chris Lattner3bd934a2008-07-26 01:18:38 +00001104 DS.SetRangeEnd(Tok.getLocation());
1105 ConsumeToken(); // The identifier
1106
1107 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1108 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1109 // Objective-C interface. If we don't have Objective-C or a '<', this is
1110 // just a normal reference to a typedef name.
1111 if (!Tok.is(tok::less) || !getLang().ObjC1)
1112 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001114 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001115 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001116 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1117 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1118 LAngleLoc, EndProtoLoc);
1119 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1120 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Chris Lattner3bd934a2008-07-26 01:18:38 +00001122 DS.SetRangeEnd(EndProtoLoc);
1123
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001124 // Need to support trailing type qualifiers (e.g. "id<p> const").
1125 // If a type specifier follows, it will be diagnosed elsewhere.
1126 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001127 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001128
1129 // type-name
1130 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001131 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001132 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001133 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001134 // This template-id does not refer to a type name, so we're
1135 // done with the type-specifiers.
1136 goto DoneWithDeclSpec;
1137 }
1138
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001139 // If we're in a context where the template-id could be a
1140 // constructor name or specialization, check whether this is a
1141 // constructor declaration.
1142 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001143 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001144 isConstructorDeclarator())
1145 goto DoneWithDeclSpec;
1146
Douglas Gregor39a8de12009-02-25 19:37:18 +00001147 // Turn the template-id annotation token into a type annotation
1148 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001149 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001150 continue;
1151 }
1152
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 // GNU attributes support.
1154 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001155 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001157
1158 // Microsoft declspec support.
1159 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001160 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001161 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Steve Naroff239f0732008-12-25 14:16:32 +00001163 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001164 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001165 // FIXME: Add handling here!
1166 break;
1167
1168 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001169 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001170 case tok::kw___cdecl:
1171 case tok::kw___stdcall:
1172 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001173 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001174 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1175 continue;
1176
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 // storage-class-specifier
1178 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001179 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1180 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 break;
1182 case tok::kw_extern:
1183 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001184 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001185 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1186 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001188 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001189 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001190 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001191 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 case tok::kw_static:
1193 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001194 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001195 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1196 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 break;
1198 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001199 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001200 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1201 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001202 else
John McCallfec54012009-08-03 20:12:06 +00001203 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1204 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 break;
1206 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001207 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1208 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001210 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001211 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1212 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001213 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001215 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 // function-specifier
1219 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001220 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001222 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001223 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001224 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001225 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001226 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001227 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001228
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001229 // friend
1230 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001231 if (DSContext == DSC_class)
1232 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1233 else {
1234 PrevSpec = ""; // not actually used by the diagnostic
1235 DiagID = diag::err_friend_invalid_in_context;
1236 isInvalid = true;
1237 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001238 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Sebastian Redl2ac67232009-11-05 15:47:02 +00001240 // constexpr
1241 case tok::kw_constexpr:
1242 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1243 break;
1244
Chris Lattner80d0c892009-01-21 19:48:37 +00001245 // type-specifier
1246 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001247 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1248 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001249 break;
1250 case tok::kw_long:
1251 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001252 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1253 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001254 else
John McCallfec54012009-08-03 20:12:06 +00001255 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1256 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001257 break;
1258 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001259 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1260 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001261 break;
1262 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001263 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1264 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001265 break;
1266 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001267 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1268 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001269 break;
1270 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001271 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1272 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001273 break;
1274 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001275 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1276 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001277 break;
1278 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001279 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1280 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001281 break;
1282 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001283 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1284 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001285 break;
1286 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001287 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1288 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001289 break;
1290 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001291 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1292 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001293 break;
1294 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001295 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1296 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001297 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001298 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001299 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1300 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001301 break;
1302 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001303 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1304 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001305 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001306 case tok::kw_bool:
1307 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001308 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1309 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001310 break;
1311 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001312 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1313 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001314 break;
1315 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001316 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1317 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001318 break;
1319 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001320 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1321 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001322 break;
John Thompson82287d12010-02-05 00:12:22 +00001323 case tok::kw___vector:
1324 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1325 break;
1326 case tok::kw___pixel:
1327 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1328 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001329
1330 // class-specifier:
1331 case tok::kw_class:
1332 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001333 case tok::kw_union: {
1334 tok::TokenKind Kind = Tok.getKind();
1335 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001336 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001337 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001338 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001339
1340 // enum-specifier:
1341 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001342 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001343 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001344 continue;
1345
1346 // cv-qualifier:
1347 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001348 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1349 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001350 break;
1351 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001352 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1353 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001354 break;
1355 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001356 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1357 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001358 break;
1359
Douglas Gregord57959a2009-03-27 23:10:48 +00001360 // C++ typename-specifier:
1361 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001362 if (TryAnnotateTypeOrScopeToken()) {
1363 DS.SetTypeSpecError();
1364 goto DoneWithDeclSpec;
1365 }
1366 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001367 continue;
1368 break;
1369
Chris Lattner80d0c892009-01-21 19:48:37 +00001370 // GNU typeof support.
1371 case tok::kw_typeof:
1372 ParseTypeofSpecifier(DS);
1373 continue;
1374
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001375 case tok::kw_decltype:
1376 ParseDecltypeSpecifier(DS);
1377 continue;
1378
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001379 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001380 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001381 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1382 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001383 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001384 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Chris Lattnerbce61352008-07-26 00:20:22 +00001386 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001387 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001388 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001389 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1390 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1391 LAngleLoc, EndProtoLoc);
1392 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1393 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001394 DS.SetRangeEnd(EndProtoLoc);
1395
Chris Lattner1ab3b962008-11-18 07:48:38 +00001396 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregor849b2432010-03-31 17:46:05 +00001397 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001398 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001399 // Need to support trailing type qualifiers (e.g. "id<p> const").
1400 // If a type specifier follows, it will be diagnosed elsewhere.
1401 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001402 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001403 }
John McCallfec54012009-08-03 20:12:06 +00001404 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001405 if (isInvalid) {
1406 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001407 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001408 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001409 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001410 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001411 ConsumeToken();
1412 }
1413}
Douglas Gregoradcac882008-12-01 23:54:00 +00001414
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001415/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001416/// primarily follow the C++ grammar with additions for C99 and GNU,
1417/// which together subsume the C grammar. Note that the C++
1418/// type-specifier also includes the C type-qualifier (for const,
1419/// volatile, and C99 restrict). Returns true if a type-specifier was
1420/// found (and parsed), false otherwise.
1421///
1422/// type-specifier: [C++ 7.1.5]
1423/// simple-type-specifier
1424/// class-specifier
1425/// enum-specifier
1426/// elaborated-type-specifier [TODO]
1427/// cv-qualifier
1428///
1429/// cv-qualifier: [C++ 7.1.5.1]
1430/// 'const'
1431/// 'volatile'
1432/// [C99] 'restrict'
1433///
1434/// simple-type-specifier: [ C++ 7.1.5.2]
1435/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1436/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1437/// 'char'
1438/// 'wchar_t'
1439/// 'bool'
1440/// 'short'
1441/// 'int'
1442/// 'long'
1443/// 'signed'
1444/// 'unsigned'
1445/// 'float'
1446/// 'double'
1447/// 'void'
1448/// [C99] '_Bool'
1449/// [C99] '_Complex'
1450/// [C99] '_Imaginary' // Removed in TC2?
1451/// [GNU] '_Decimal32'
1452/// [GNU] '_Decimal64'
1453/// [GNU] '_Decimal128'
1454/// [GNU] typeof-specifier
1455/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1456/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001457/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001458/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001459bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001460 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001461 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001462 const ParsedTemplateInfo &TemplateInfo,
1463 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001464 SourceLocation Loc = Tok.getLocation();
1465
1466 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001467 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001468 // If we already have a type specifier, this identifier is not a type.
1469 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1470 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1471 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1472 return false;
John Thompson82287d12010-02-05 00:12:22 +00001473 // Check for need to substitute AltiVec keyword tokens.
1474 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1475 break;
1476 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001477 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001478 // Annotate typenames and C++ scope specifiers. If we get one, just
1479 // recurse to handle whatever we get.
1480 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001481 return true;
1482 if (Tok.is(tok::identifier))
1483 return false;
1484 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1485 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001486 case tok::coloncolon: // ::foo::bar
1487 if (NextToken().is(tok::kw_new) || // ::new
1488 NextToken().is(tok::kw_delete)) // ::delete
1489 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Chris Lattner166a8fc2009-01-04 23:41:41 +00001491 // Annotate typenames and C++ scope specifiers. If we get one, just
1492 // recurse to handle whatever we get.
1493 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001494 return true;
1495 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1496 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Douglas Gregor12e083c2008-11-07 15:42:26 +00001498 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001499 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001500 if (Tok.getAnnotationValue())
1501 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001502 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001503 else
1504 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001505 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1506 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Douglas Gregor12e083c2008-11-07 15:42:26 +00001508 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1509 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1510 // Objective-C interface. If we don't have Objective-C or a '<', this is
1511 // just a normal reference to a typedef name.
1512 if (!Tok.is(tok::less) || !getLang().ObjC1)
1513 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001515 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001516 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001517 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1518 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1519 LAngleLoc, EndProtoLoc);
1520 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1521 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Douglas Gregor12e083c2008-11-07 15:42:26 +00001523 DS.SetRangeEnd(EndProtoLoc);
1524 return true;
1525 }
1526
1527 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001528 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001529 break;
1530 case tok::kw_long:
1531 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001532 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1533 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001534 else
John McCallfec54012009-08-03 20:12:06 +00001535 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1536 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001537 break;
1538 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001539 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001540 break;
1541 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001542 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1543 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001544 break;
1545 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001546 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1547 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001548 break;
1549 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001550 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1551 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001552 break;
1553 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001554 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001555 break;
1556 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001557 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001558 break;
1559 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001560 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001561 break;
1562 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001563 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001564 break;
1565 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001566 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001567 break;
1568 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001569 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001570 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001571 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001572 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001573 break;
1574 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001575 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001576 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001577 case tok::kw_bool:
1578 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001579 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001580 break;
1581 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001582 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1583 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001584 break;
1585 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001586 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1587 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001588 break;
1589 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001590 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1591 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001592 break;
John Thompson82287d12010-02-05 00:12:22 +00001593 case tok::kw___vector:
1594 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1595 break;
1596 case tok::kw___pixel:
1597 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1598 break;
1599
Douglas Gregor12e083c2008-11-07 15:42:26 +00001600 // class-specifier:
1601 case tok::kw_class:
1602 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001603 case tok::kw_union: {
1604 tok::TokenKind Kind = Tok.getKind();
1605 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001606 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1607 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001608 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001609 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001610
1611 // enum-specifier:
1612 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001613 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001614 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001615 return true;
1616
1617 // cv-qualifier:
1618 case tok::kw_const:
1619 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001620 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001621 break;
1622 case tok::kw_volatile:
1623 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001624 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001625 break;
1626 case tok::kw_restrict:
1627 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001628 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001629 break;
1630
1631 // GNU typeof support.
1632 case tok::kw_typeof:
1633 ParseTypeofSpecifier(DS);
1634 return true;
1635
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001636 // C++0x decltype support.
1637 case tok::kw_decltype:
1638 ParseDecltypeSpecifier(DS);
1639 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001640
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001641 // C++0x auto support.
1642 case tok::kw_auto:
1643 if (!getLang().CPlusPlus0x)
1644 return false;
1645
John McCallfec54012009-08-03 20:12:06 +00001646 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001647 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001648 case tok::kw___ptr64:
1649 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001650 case tok::kw___cdecl:
1651 case tok::kw___stdcall:
1652 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001653 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001654 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001655 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001656
Douglas Gregor12e083c2008-11-07 15:42:26 +00001657 default:
1658 // Not a type-specifier; do nothing.
1659 return false;
1660 }
1661
1662 // If the specifier combination wasn't legal, issue a diagnostic.
1663 if (isInvalid) {
1664 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001665 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001666 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001667 }
1668 DS.SetRangeEnd(Tok.getLocation());
1669 ConsumeToken(); // whatever we parsed above.
1670 return true;
1671}
Reid Spencer5f016e22007-07-11 17:01:13 +00001672
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001673/// ParseStructDeclaration - Parse a struct declaration without the terminating
1674/// semicolon.
1675///
Reid Spencer5f016e22007-07-11 17:01:13 +00001676/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001677/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001678/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001679/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001680/// struct-declarator-list:
1681/// struct-declarator
1682/// struct-declarator-list ',' struct-declarator
1683/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1684/// struct-declarator:
1685/// declarator
1686/// [GNU] declarator attributes[opt]
1687/// declarator[opt] ':' constant-expression
1688/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1689///
Chris Lattnere1359422008-04-10 06:46:29 +00001690void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001691ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001692 if (Tok.is(tok::kw___extension__)) {
1693 // __extension__ silences extension warnings in the subexpression.
1694 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001695 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001696 return ParseStructDeclaration(DS, Fields);
1697 }
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Steve Naroff28a7ca82007-08-20 22:28:22 +00001699 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001700 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001701 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001703 // If there are no declarators, this is a free-standing declaration
1704 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001705 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001706 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001707 return;
1708 }
1709
1710 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001711 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001712 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001713 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001714 FieldDeclarator DeclaratorInfo(DS);
1715
1716 // Attributes are only allowed here on successive declarators.
1717 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1718 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001719 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001720 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1721 }
Mike Stump1eb44332009-09-09 15:08:12 +00001722
Steve Naroff28a7ca82007-08-20 22:28:22 +00001723 /// struct-declarator: declarator
1724 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001725 if (Tok.isNot(tok::colon)) {
1726 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1727 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001728 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001729 }
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Chris Lattner04d66662007-10-09 17:33:22 +00001731 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001732 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001733 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001734 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001735 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001736 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001737 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001738 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001739
Steve Naroff28a7ca82007-08-20 22:28:22 +00001740 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001741 if (Tok.is(tok::kw___attribute)) {
1742 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001743 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001744 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1745 }
1746
John McCallbdd563e2009-11-03 02:38:08 +00001747 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00001748 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00001749 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001750
Steve Naroff28a7ca82007-08-20 22:28:22 +00001751 // If we don't have a comma, it is either the end of the list (a ';')
1752 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001753 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001754 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001755
Steve Naroff28a7ca82007-08-20 22:28:22 +00001756 // Consume the comma.
1757 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001758
John McCallbdd563e2009-11-03 02:38:08 +00001759 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001760 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001761}
1762
1763/// ParseStructUnionBody
1764/// struct-contents:
1765/// struct-declaration-list
1766/// [EXT] empty
1767/// [GNU] "struct-declaration-list" without terminatoring ';'
1768/// struct-declaration-list:
1769/// struct-declaration
1770/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001771/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001772///
Reid Spencer5f016e22007-07-11 17:01:13 +00001773void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001774 unsigned TagType, Decl *TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001775 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1776 PP.getSourceManager(),
1777 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Reid Spencer5f016e22007-07-11 17:01:13 +00001779 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001780
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001781 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001782 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00001783
Reid Spencer5f016e22007-07-11 17:01:13 +00001784 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1785 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001786 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00001787 Diag(Tok, diag::ext_empty_struct_union)
1788 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00001789
John McCalld226f652010-08-21 09:40:31 +00001790 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001791
Reid Spencer5f016e22007-07-11 17:01:13 +00001792 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001793 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001794 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001795
Reid Spencer5f016e22007-07-11 17:01:13 +00001796 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001797 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001798 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00001799 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00001800 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001801 ConsumeToken();
1802 continue;
1803 }
Chris Lattnere1359422008-04-10 06:46:29 +00001804
1805 // Parse all the comma separated declarators.
1806 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001807
John McCallbdd563e2009-11-03 02:38:08 +00001808 if (!Tok.is(tok::at)) {
1809 struct CFieldCallback : FieldCallback {
1810 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001811 Decl *TagDecl;
1812 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001813
John McCalld226f652010-08-21 09:40:31 +00001814 CFieldCallback(Parser &P, Decl *TagDecl,
1815 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001816 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1817
John McCalld226f652010-08-21 09:40:31 +00001818 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001819 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00001820 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00001821 FD.D.getDeclSpec().getSourceRange().getBegin(),
1822 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001823 FieldDecls.push_back(Field);
1824 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001825 }
John McCallbdd563e2009-11-03 02:38:08 +00001826 } Callback(*this, TagDecl, FieldDecls);
1827
1828 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001829 } else { // Handle @defs
1830 ConsumeToken();
1831 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1832 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001833 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001834 continue;
1835 }
1836 ConsumeToken();
1837 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1838 if (!Tok.is(tok::identifier)) {
1839 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001840 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001841 continue;
1842 }
John McCalld226f652010-08-21 09:40:31 +00001843 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001844 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001845 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001846 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1847 ConsumeToken();
1848 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001849 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001850
Chris Lattner04d66662007-10-09 17:33:22 +00001851 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001852 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001853 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001854 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001855 break;
1856 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001857 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1858 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001859 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001860 // If we stopped at a ';', eat it.
1861 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001862 }
1863 }
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Steve Naroff60fccee2007-10-29 21:38:07 +00001865 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001866
Ted Kremenek1e377652010-02-11 02:19:13 +00001867 llvm::OwningPtr<AttributeList> AttrList;
Reid Spencer5f016e22007-07-11 17:01:13 +00001868 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001869 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001870 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001871
Douglas Gregor23c94db2010-07-02 17:43:08 +00001872 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001873 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001874 LBraceLoc, RBraceLoc,
Ted Kremenek1e377652010-02-11 02:19:13 +00001875 AttrList.get());
Douglas Gregor72de6672009-01-08 20:45:30 +00001876 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001877 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001878}
1879
1880
1881/// ParseEnumSpecifier
1882/// enum-specifier: [C99 6.7.2.2]
1883/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001884///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001885/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1886/// '}' attributes[opt]
1887/// 'enum' identifier
1888/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001889///
1890/// [C++] elaborated-type-specifier:
1891/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1892///
Chris Lattner4c97d762009-04-12 21:49:30 +00001893void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001894 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00001895 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001896 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001897 if (Tok.is(tok::code_completion)) {
1898 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001899 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00001900 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00001901 }
1902
Ted Kremenek1e377652010-02-11 02:19:13 +00001903 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001904 // If attributes exist after tag, parse them.
1905 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001906 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001907
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001908 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00001909 if (getLang().CPlusPlus) {
1910 if (ParseOptionalCXXScopeSpecifier(SS, 0, false))
1911 return;
1912
1913 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001914 Diag(Tok, diag::err_expected_ident);
1915 if (Tok.isNot(tok::l_brace)) {
1916 // Has no name and is not a definition.
1917 // Skip the rest of this declarator, up until the comma or semicolon.
1918 SkipUntil(tok::comma, true);
1919 return;
1920 }
1921 }
1922 }
Mike Stump1eb44332009-09-09 15:08:12 +00001923
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001924 // Must have either 'enum name' or 'enum {...}'.
1925 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1926 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001928 // Skip the rest of this declarator, up until the comma or semicolon.
1929 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001930 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001931 }
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001933 // If an identifier is present, consume and remember it.
1934 IdentifierInfo *Name = 0;
1935 SourceLocation NameLoc;
1936 if (Tok.is(tok::identifier)) {
1937 Name = Tok.getIdentifierInfo();
1938 NameLoc = ConsumeToken();
1939 }
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001941 // There are three options here. If we have 'enum foo;', then this is a
1942 // forward declaration. If we have 'enum foo {...' then this is a
1943 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1944 //
1945 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1946 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1947 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1948 //
John McCall0f434ec2009-07-31 02:45:11 +00001949 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001950 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001951 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001952 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001953 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001954 else
John McCall0f434ec2009-07-31 02:45:11 +00001955 TUK = Action::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00001956
1957 // enums cannot be templates, although they can be referenced from a
1958 // template.
1959 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
1960 TUK != Action::TUK_Reference) {
1961 Diag(Tok, diag::err_enum_template);
1962
1963 // Skip the rest of this declarator, up until the comma or semicolon.
1964 SkipUntil(tok::comma, true);
1965 return;
1966 }
1967
Douglas Gregor402abb52009-05-28 23:31:59 +00001968 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001969 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00001970 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
1971 const char *PrevSpec = 0;
1972 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00001973 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
1974 StartLoc, SS, Name, NameLoc, Attr.get(),
1975 AS,
1976 Action::MultiTemplateParamsArg(Actions),
1977 Owned, IsDependent);
Douglas Gregor48c89f42010-04-24 16:38:41 +00001978 if (IsDependent) {
1979 // This enum has a dependent nested-name-specifier. Handle it as a
1980 // dependent tag.
1981 if (!Name) {
1982 DS.SetTypeSpecError();
1983 Diag(Tok, diag::err_expected_type_name_after_typename);
1984 return;
1985 }
1986
Douglas Gregor23c94db2010-07-02 17:43:08 +00001987 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00001988 TUK, SS, Name, StartLoc,
1989 NameLoc);
1990 if (Type.isInvalid()) {
1991 DS.SetTypeSpecError();
1992 return;
1993 }
1994
1995 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
1996 Type.get(), false))
1997 Diag(StartLoc, DiagID) << PrevSpec;
1998
1999 return;
2000 }
Mike Stump1eb44332009-09-09 15:08:12 +00002001
John McCalld226f652010-08-21 09:40:31 +00002002 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002003 // The action failed to produce an enumeration tag. If this is a
2004 // definition, consume the entire definition.
2005 if (Tok.is(tok::l_brace)) {
2006 ConsumeBrace();
2007 SkipUntil(tok::r_brace);
2008 }
2009
2010 DS.SetTypeSpecError();
2011 return;
2012 }
2013
Chris Lattner04d66662007-10-09 17:33:22 +00002014 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002015 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Douglas Gregorb988f9c2010-01-25 16:33:23 +00002017 // FIXME: The DeclSpec should keep the locations of both the keyword and the
2018 // name (if there is one).
Douglas Gregorb988f9c2010-01-25 16:33:23 +00002019 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCalld226f652010-08-21 09:40:31 +00002020 TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002021 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002022}
2023
2024/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2025/// enumerator-list:
2026/// enumerator
2027/// enumerator-list ',' enumerator
2028/// enumerator:
2029/// enumeration-constant
2030/// enumeration-constant '=' constant-expression
2031/// enumeration-constant:
2032/// identifier
2033///
John McCalld226f652010-08-21 09:40:31 +00002034void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002035 // Enter the scope of the enum body and start the definition.
2036 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002037 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002038
Reid Spencer5f016e22007-07-11 17:01:13 +00002039 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002040
Chris Lattner7946dd32007-08-27 17:24:30 +00002041 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002042 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002043 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002044
John McCalld226f652010-08-21 09:40:31 +00002045 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002046
John McCalld226f652010-08-21 09:40:31 +00002047 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002048
Reid Spencer5f016e22007-07-11 17:01:13 +00002049 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002050 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002051 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2052 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002053
Reid Spencer5f016e22007-07-11 17:01:13 +00002054 SourceLocation EqualLoc;
John McCallca0408f2010-08-23 06:44:23 +00002055 OwningExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002056 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002057 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002058 AssignedVal = ParseConstantExpression();
2059 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002060 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002061 }
Mike Stump1eb44332009-09-09 15:08:12 +00002062
Reid Spencer5f016e22007-07-11 17:01:13 +00002063 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002064 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2065 LastEnumConstDecl,
2066 IdentLoc, Ident,
2067 EqualLoc,
2068 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002069 EnumConstantDecls.push_back(EnumConstDecl);
2070 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002071
Chris Lattner04d66662007-10-09 17:33:22 +00002072 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002073 break;
2074 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002075
2076 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002077 !(getLang().C99 || getLang().CPlusPlus0x))
2078 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2079 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002080 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002081 }
Mike Stump1eb44332009-09-09 15:08:12 +00002082
Reid Spencer5f016e22007-07-11 17:01:13 +00002083 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002084 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002085
Ted Kremenek1e377652010-02-11 02:19:13 +00002086 llvm::OwningPtr<AttributeList> Attr;
Reid Spencer5f016e22007-07-11 17:01:13 +00002087 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00002088 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00002089 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00002090
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002091 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2092 EnumConstantDecls.data(), EnumConstantDecls.size(),
Douglas Gregor23c94db2010-07-02 17:43:08 +00002093 getCurScope(), Attr.get());
Mike Stump1eb44332009-09-09 15:08:12 +00002094
Douglas Gregor72de6672009-01-08 20:45:30 +00002095 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002096 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002097}
2098
2099/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002100/// start of a type-qualifier-list.
2101bool Parser::isTypeQualifier() const {
2102 switch (Tok.getKind()) {
2103 default: return false;
2104 // type-qualifier
2105 case tok::kw_const:
2106 case tok::kw_volatile:
2107 case tok::kw_restrict:
2108 return true;
2109 }
2110}
2111
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002112/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2113/// is definitely a type-specifier. Return false if it isn't part of a type
2114/// specifier or if we're not sure.
2115bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2116 switch (Tok.getKind()) {
2117 default: return false;
2118 // type-specifiers
2119 case tok::kw_short:
2120 case tok::kw_long:
2121 case tok::kw_signed:
2122 case tok::kw_unsigned:
2123 case tok::kw__Complex:
2124 case tok::kw__Imaginary:
2125 case tok::kw_void:
2126 case tok::kw_char:
2127 case tok::kw_wchar_t:
2128 case tok::kw_char16_t:
2129 case tok::kw_char32_t:
2130 case tok::kw_int:
2131 case tok::kw_float:
2132 case tok::kw_double:
2133 case tok::kw_bool:
2134 case tok::kw__Bool:
2135 case tok::kw__Decimal32:
2136 case tok::kw__Decimal64:
2137 case tok::kw__Decimal128:
2138 case tok::kw___vector:
2139
2140 // struct-or-union-specifier (C99) or class-specifier (C++)
2141 case tok::kw_class:
2142 case tok::kw_struct:
2143 case tok::kw_union:
2144 // enum-specifier
2145 case tok::kw_enum:
2146
2147 // typedef-name
2148 case tok::annot_typename:
2149 return true;
2150 }
2151}
2152
Steve Naroff5f8aa692008-02-11 23:15:56 +00002153/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002154/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002155bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002156 switch (Tok.getKind()) {
2157 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002158
Chris Lattner166a8fc2009-01-04 23:41:41 +00002159 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002160 if (TryAltiVecVectorToken())
2161 return true;
2162 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002163 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002164 // Annotate typenames and C++ scope specifiers. If we get one, just
2165 // recurse to handle whatever we get.
2166 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002167 return true;
2168 if (Tok.is(tok::identifier))
2169 return false;
2170 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002171
Chris Lattner166a8fc2009-01-04 23:41:41 +00002172 case tok::coloncolon: // ::foo::bar
2173 if (NextToken().is(tok::kw_new) || // ::new
2174 NextToken().is(tok::kw_delete)) // ::delete
2175 return false;
2176
Chris Lattner166a8fc2009-01-04 23:41:41 +00002177 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002178 return true;
2179 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002180
Reid Spencer5f016e22007-07-11 17:01:13 +00002181 // GNU attributes support.
2182 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002183 // GNU typeof support.
2184 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Reid Spencer5f016e22007-07-11 17:01:13 +00002186 // type-specifiers
2187 case tok::kw_short:
2188 case tok::kw_long:
2189 case tok::kw_signed:
2190 case tok::kw_unsigned:
2191 case tok::kw__Complex:
2192 case tok::kw__Imaginary:
2193 case tok::kw_void:
2194 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002195 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002196 case tok::kw_char16_t:
2197 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002198 case tok::kw_int:
2199 case tok::kw_float:
2200 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002201 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002202 case tok::kw__Bool:
2203 case tok::kw__Decimal32:
2204 case tok::kw__Decimal64:
2205 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002206 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002207
Chris Lattner99dc9142008-04-13 18:59:07 +00002208 // struct-or-union-specifier (C99) or class-specifier (C++)
2209 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002210 case tok::kw_struct:
2211 case tok::kw_union:
2212 // enum-specifier
2213 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002214
Reid Spencer5f016e22007-07-11 17:01:13 +00002215 // type-qualifier
2216 case tok::kw_const:
2217 case tok::kw_volatile:
2218 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002219
2220 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002221 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002222 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Chris Lattner7c186be2008-10-20 00:25:30 +00002224 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2225 case tok::less:
2226 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002227
Steve Naroff239f0732008-12-25 14:16:32 +00002228 case tok::kw___cdecl:
2229 case tok::kw___stdcall:
2230 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002231 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002232 case tok::kw___w64:
2233 case tok::kw___ptr64:
2234 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002235 }
2236}
2237
2238/// isDeclarationSpecifier() - Return true if the current token is part of a
2239/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002240bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002241 switch (Tok.getKind()) {
2242 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002243
Chris Lattner166a8fc2009-01-04 23:41:41 +00002244 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002245 // Unfortunate hack to support "Class.factoryMethod" notation.
2246 if (getLang().ObjC1 && NextToken().is(tok::period))
2247 return false;
John Thompson82287d12010-02-05 00:12:22 +00002248 if (TryAltiVecVectorToken())
2249 return true;
2250 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002251 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002252 // Annotate typenames and C++ scope specifiers. If we get one, just
2253 // recurse to handle whatever we get.
2254 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002255 return true;
2256 if (Tok.is(tok::identifier))
2257 return false;
2258 return isDeclarationSpecifier();
2259
Chris Lattner166a8fc2009-01-04 23:41:41 +00002260 case tok::coloncolon: // ::foo::bar
2261 if (NextToken().is(tok::kw_new) || // ::new
2262 NextToken().is(tok::kw_delete)) // ::delete
2263 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002264
Chris Lattner166a8fc2009-01-04 23:41:41 +00002265 // Annotate typenames and C++ scope specifiers. If we get one, just
2266 // recurse to handle whatever we get.
2267 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002268 return true;
2269 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002270
Reid Spencer5f016e22007-07-11 17:01:13 +00002271 // storage-class-specifier
2272 case tok::kw_typedef:
2273 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002274 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002275 case tok::kw_static:
2276 case tok::kw_auto:
2277 case tok::kw_register:
2278 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002279
Reid Spencer5f016e22007-07-11 17:01:13 +00002280 // type-specifiers
2281 case tok::kw_short:
2282 case tok::kw_long:
2283 case tok::kw_signed:
2284 case tok::kw_unsigned:
2285 case tok::kw__Complex:
2286 case tok::kw__Imaginary:
2287 case tok::kw_void:
2288 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002289 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002290 case tok::kw_char16_t:
2291 case tok::kw_char32_t:
2292
Reid Spencer5f016e22007-07-11 17:01:13 +00002293 case tok::kw_int:
2294 case tok::kw_float:
2295 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002296 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002297 case tok::kw__Bool:
2298 case tok::kw__Decimal32:
2299 case tok::kw__Decimal64:
2300 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002301 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002302
Chris Lattner99dc9142008-04-13 18:59:07 +00002303 // struct-or-union-specifier (C99) or class-specifier (C++)
2304 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002305 case tok::kw_struct:
2306 case tok::kw_union:
2307 // enum-specifier
2308 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002309
Reid Spencer5f016e22007-07-11 17:01:13 +00002310 // type-qualifier
2311 case tok::kw_const:
2312 case tok::kw_volatile:
2313 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002314
Reid Spencer5f016e22007-07-11 17:01:13 +00002315 // function-specifier
2316 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002317 case tok::kw_virtual:
2318 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002319
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002320 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002321 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002322
Chris Lattner1ef08762007-08-09 17:01:07 +00002323 // GNU typeof support.
2324 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002325
Chris Lattner1ef08762007-08-09 17:01:07 +00002326 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002327 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002328 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002329
Chris Lattnerf3948c42008-07-26 03:38:44 +00002330 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2331 case tok::less:
2332 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002333
Steve Naroff47f52092009-01-06 19:34:12 +00002334 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002335 case tok::kw___cdecl:
2336 case tok::kw___stdcall:
2337 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002338 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002339 case tok::kw___w64:
2340 case tok::kw___ptr64:
2341 case tok::kw___forceinline:
2342 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002343 }
2344}
2345
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002346bool Parser::isConstructorDeclarator() {
2347 TentativeParsingAction TPA(*this);
2348
2349 // Parse the C++ scope specifier.
2350 CXXScopeSpec SS;
John McCall9ba61662010-02-26 08:45:28 +00002351 if (ParseOptionalCXXScopeSpecifier(SS, 0, true)) {
2352 TPA.Revert();
2353 return false;
2354 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002355
2356 // Parse the constructor name.
2357 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2358 // We already know that we have a constructor name; just consume
2359 // the token.
2360 ConsumeToken();
2361 } else {
2362 TPA.Revert();
2363 return false;
2364 }
2365
2366 // Current class name must be followed by a left parentheses.
2367 if (Tok.isNot(tok::l_paren)) {
2368 TPA.Revert();
2369 return false;
2370 }
2371 ConsumeParen();
2372
2373 // A right parentheses or ellipsis signals that we have a constructor.
2374 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2375 TPA.Revert();
2376 return true;
2377 }
2378
2379 // If we need to, enter the specified scope.
2380 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002381 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002382 DeclScopeObj.EnterDeclaratorScope();
2383
2384 // Check whether the next token(s) are part of a declaration
2385 // specifier, in which case we have the start of a parameter and,
2386 // therefore, we know that this is a constructor.
2387 bool IsConstructor = isDeclarationSpecifier();
2388 TPA.Revert();
2389 return IsConstructor;
2390}
Reid Spencer5f016e22007-07-11 17:01:13 +00002391
2392/// ParseTypeQualifierListOpt
2393/// type-qualifier-list: [C99 6.7.5]
2394/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002395/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002396/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002397/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Sean Huntbbd37c62009-11-21 08:43:09 +00002398/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2399/// if CXX0XAttributesAllowed = true
Reid Spencer5f016e22007-07-11 17:01:13 +00002400///
Sean Huntbbd37c62009-11-21 08:43:09 +00002401void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2402 bool CXX0XAttributesAllowed) {
2403 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2404 SourceLocation Loc = Tok.getLocation();
2405 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2406 if (CXX0XAttributesAllowed)
2407 DS.AddAttributes(Attr.AttrList);
2408 else
2409 Diag(Loc, diag::err_attributes_not_allowed);
2410 }
2411
Reid Spencer5f016e22007-07-11 17:01:13 +00002412 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002413 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002414 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002415 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002416 SourceLocation Loc = Tok.getLocation();
2417
2418 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002419 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002420 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2421 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002422 break;
2423 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002424 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2425 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002426 break;
2427 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002428 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2429 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002430 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002431 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002432 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002433 case tok::kw___cdecl:
2434 case tok::kw___stdcall:
2435 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002436 case tok::kw___thiscall:
Sean Huntbbd37c62009-11-21 08:43:09 +00002437 if (GNUAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002438 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2439 continue;
2440 }
2441 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002442 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002443 if (GNUAttributesAllowed) {
2444 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002445 continue; // do *not* consume the next token!
2446 }
2447 // otherwise, FALL THROUGH!
2448 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002449 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002450 // If this is not a type-qualifier token, we're done reading type
2451 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002452 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002453 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002454 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002455
Reid Spencer5f016e22007-07-11 17:01:13 +00002456 // If the specifier combination wasn't legal, issue a diagnostic.
2457 if (isInvalid) {
2458 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002459 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002460 }
2461 ConsumeToken();
2462 }
2463}
2464
2465
2466/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2467///
2468void Parser::ParseDeclarator(Declarator &D) {
2469 /// This implements the 'declarator' production in the C grammar, then checks
2470 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002471 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002472}
2473
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002474/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2475/// is parsed by the function passed to it. Pass null, and the direct-declarator
2476/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002477/// ptr-operator production.
2478///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002479/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2480/// [C] pointer[opt] direct-declarator
2481/// [C++] direct-declarator
2482/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002483///
2484/// pointer: [C99 6.7.5]
2485/// '*' type-qualifier-list[opt]
2486/// '*' type-qualifier-list[opt] pointer
2487///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002488/// ptr-operator:
2489/// '*' cv-qualifier-seq[opt]
2490/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002491/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002492/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002493/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002494/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002495void Parser::ParseDeclaratorInternal(Declarator &D,
2496 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002497 if (Diags.hasAllExtensionsSilenced())
2498 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002499 // C++ member pointers start with a '::' or a nested-name.
2500 // Member pointers get special handling, since there's no place for the
2501 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002502 if (getLang().CPlusPlus &&
2503 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2504 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002505 CXXScopeSpec SS;
John McCall9ba61662010-02-26 08:45:28 +00002506 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true); // ignore fail
2507
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002508 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002509 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002510 // The scope spec really belongs to the direct-declarator.
2511 D.getCXXScopeSpec() = SS;
2512 if (DirectDeclParser)
2513 (this->*DirectDeclParser)(D);
2514 return;
2515 }
2516
2517 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002518 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002519 DeclSpec DS;
2520 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002521 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002522
2523 // Recurse to parse whatever is left.
2524 ParseDeclaratorInternal(D, DirectDeclParser);
2525
2526 // Sema will have to catch (syntactically invalid) pointers into global
2527 // scope. It has to catch pointers into namespace scope anyway.
2528 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002529 Loc, DS.TakeAttributes()),
2530 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002531 return;
2532 }
2533 }
2534
2535 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002536 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002537 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002538 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002539 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002540 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002541 if (DirectDeclParser)
2542 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002543 return;
2544 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002545
Sebastian Redl05532f22009-03-15 22:02:01 +00002546 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2547 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002548 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002549 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002550
Chris Lattner9af55002009-03-27 04:18:06 +00002551 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002552 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002553 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002554
Reid Spencer5f016e22007-07-11 17:01:13 +00002555 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002556 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002557
Reid Spencer5f016e22007-07-11 17:01:13 +00002558 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002559 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002560 if (Kind == tok::star)
2561 // Remember that we parsed a pointer type, and remember the type-quals.
2562 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002563 DS.TakeAttributes()),
2564 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002565 else
2566 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002567 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002568 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002569 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002570 } else {
2571 // Is a reference
2572 DeclSpec DS;
2573
Sebastian Redl743de1f2009-03-23 00:00:23 +00002574 // Complain about rvalue references in C++03, but then go on and build
2575 // the declarator.
2576 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2577 Diag(Loc, diag::err_rvalue_reference);
2578
Reid Spencer5f016e22007-07-11 17:01:13 +00002579 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2580 // cv-qualifiers are introduced through the use of a typedef or of a
2581 // template type argument, in which case the cv-qualifiers are ignored.
2582 //
2583 // [GNU] Retricted references are allowed.
2584 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002585 // [C++0x] Attributes on references are not allowed.
2586 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002587 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002588
2589 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2590 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2591 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002592 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002593 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2594 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002595 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002596 }
2597
2598 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002599 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002600
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002601 if (D.getNumTypeObjects() > 0) {
2602 // C++ [dcl.ref]p4: There shall be no references to references.
2603 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2604 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002605 if (const IdentifierInfo *II = D.getIdentifier())
2606 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2607 << II;
2608 else
2609 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2610 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002611
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002612 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002613 // can go ahead and build the (technically ill-formed)
2614 // declarator: reference collapsing will take care of it.
2615 }
2616 }
2617
Reid Spencer5f016e22007-07-11 17:01:13 +00002618 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002619 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002620 DS.TakeAttributes(),
2621 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002622 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002623 }
2624}
2625
2626/// ParseDirectDeclarator
2627/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002628/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002629/// '(' declarator ')'
2630/// [GNU] '(' attributes declarator ')'
2631/// [C90] direct-declarator '[' constant-expression[opt] ']'
2632/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2633/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2634/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2635/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2636/// direct-declarator '(' parameter-type-list ')'
2637/// direct-declarator '(' identifier-list[opt] ')'
2638/// [GNU] direct-declarator '(' parameter-forward-declarations
2639/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002640/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2641/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002642/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002643///
2644/// declarator-id: [C++ 8]
2645/// id-expression
2646/// '::'[opt] nested-name-specifier[opt] type-name
2647///
2648/// id-expression: [C++ 5.1]
2649/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002650/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002651///
2652/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002653/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002654/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002655/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002656/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002657/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002658///
Reid Spencer5f016e22007-07-11 17:01:13 +00002659void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002660 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002661
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002662 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2663 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002664 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002665 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2666 true);
John McCall9ba61662010-02-26 08:45:28 +00002667 }
2668
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002669 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002670 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00002671 // Change the declaration context for name lookup, until this function
2672 // is exited (and the declarator has been parsed).
2673 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002674 }
2675
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002676 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2677 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2678 // We found something that indicates the start of an unqualified-id.
2679 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00002680 bool AllowConstructorName;
2681 if (D.getDeclSpec().hasTypeSpecifier())
2682 AllowConstructorName = false;
2683 else if (D.getCXXScopeSpec().isSet())
2684 AllowConstructorName =
2685 (D.getContext() == Declarator::FileContext ||
2686 (D.getContext() == Declarator::MemberContext &&
2687 D.getDeclSpec().isFriendSpecified()));
2688 else
2689 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2690
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002691 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2692 /*EnteringContext=*/true,
2693 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002694 AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002695 /*ObjectType=*/0,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002696 D.getName()) ||
2697 // Once we're past the identifier, if the scope was bad, mark the
2698 // whole declarator bad.
2699 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002700 D.SetIdentifier(0, Tok.getLocation());
2701 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002702 } else {
2703 // Parsed the unqualified-id; update range information and move along.
2704 if (D.getSourceRange().getBegin().isInvalid())
2705 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2706 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002707 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002708 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002709 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002710 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002711 assert(!getLang().CPlusPlus &&
2712 "There's a C++-specific check for tok::identifier above");
2713 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2714 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2715 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002716 goto PastIdentifier;
2717 }
2718
2719 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002720 // direct-declarator: '(' declarator ')'
2721 // direct-declarator: '(' attributes declarator ')'
2722 // Example: 'char (*X)' or 'int (*XX)(void)'
2723 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002724
2725 // If the declarator was parenthesized, we entered the declarator
2726 // scope when parsing the parenthesized declarator, then exited
2727 // the scope already. Re-enter the scope, if we need to.
2728 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002729 // If there was an error parsing parenthesized declarator, declarator
2730 // scope may have been enterred before. Don't do it again.
2731 if (!D.isInvalidType() &&
2732 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002733 // Change the declaration context for name lookup, until this function
2734 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002735 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002736 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002737 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002738 // This could be something simple like "int" (in which case the declarator
2739 // portion is empty), if an abstract-declarator is allowed.
2740 D.SetIdentifier(0, Tok.getLocation());
2741 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002742 if (D.getContext() == Declarator::MemberContext)
2743 Diag(Tok, diag::err_expected_member_name_or_semi)
2744 << D.getDeclSpec().getSourceRange();
2745 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002746 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002747 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002748 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002749 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002750 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002751 }
Mike Stump1eb44332009-09-09 15:08:12 +00002752
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002753 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002754 assert(D.isPastIdentifier() &&
2755 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002756
Sean Huntbbd37c62009-11-21 08:43:09 +00002757 // Don't parse attributes unless we have an identifier.
Douglas Gregor3c3aaf92010-02-19 16:47:56 +00002758 if (D.getIdentifier() && getLang().CPlusPlus0x
Sean Huntbbd37c62009-11-21 08:43:09 +00002759 && isCXX0XAttributeSpecifier(true)) {
2760 SourceLocation AttrEndLoc;
2761 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2762 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2763 }
2764
Reid Spencer5f016e22007-07-11 17:01:13 +00002765 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002766 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002767 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2768 // In such a case, check if we actually have a function declarator; if it
2769 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002770 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2771 // When not in file scope, warn for ambiguous function declarators, just
2772 // in case the author intended it as a variable definition.
2773 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2774 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2775 break;
2776 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002777 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002778 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002779 ParseBracketDeclarator(D);
2780 } else {
2781 break;
2782 }
2783 }
2784}
2785
Chris Lattneref4715c2008-04-06 05:45:57 +00002786/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2787/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002788/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002789/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2790///
2791/// direct-declarator:
2792/// '(' declarator ')'
2793/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002794/// direct-declarator '(' parameter-type-list ')'
2795/// direct-declarator '(' identifier-list[opt] ')'
2796/// [GNU] direct-declarator '(' parameter-forward-declarations
2797/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002798///
2799void Parser::ParseParenDeclarator(Declarator &D) {
2800 SourceLocation StartLoc = ConsumeParen();
2801 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002802
Chris Lattner7399ee02008-10-20 02:05:46 +00002803 // Eat any attributes before we look at whether this is a grouping or function
2804 // declarator paren. If this is a grouping paren, the attribute applies to
2805 // the type being built up, for example:
2806 // int (__attribute__(()) *x)(long y)
2807 // If this ends up not being a grouping paren, the attribute applies to the
2808 // first argument, for example:
2809 // int (__attribute__(()) int x)
2810 // In either case, we need to eat any attributes to be able to determine what
2811 // sort of paren this is.
2812 //
Ted Kremenek1e377652010-02-11 02:19:13 +00002813 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner7399ee02008-10-20 02:05:46 +00002814 bool RequiresArg = false;
2815 if (Tok.is(tok::kw___attribute)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002816 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +00002817
Chris Lattner7399ee02008-10-20 02:05:46 +00002818 // We require that the argument list (if this is a non-grouping paren) be
2819 // present even if the attribute list was empty.
2820 RequiresArg = true;
2821 }
Steve Naroff239f0732008-12-25 14:16:32 +00002822 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002823 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002824 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2825 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002826 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman290eeb02009-06-08 23:27:34 +00002827 }
Mike Stump1eb44332009-09-09 15:08:12 +00002828
Chris Lattneref4715c2008-04-06 05:45:57 +00002829 // If we haven't past the identifier yet (or where the identifier would be
2830 // stored, if this is an abstract declarator), then this is probably just
2831 // grouping parens. However, if this could be an abstract-declarator, then
2832 // this could also be the start of function arguments (consider 'void()').
2833 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002834
Chris Lattneref4715c2008-04-06 05:45:57 +00002835 if (!D.mayOmitIdentifier()) {
2836 // If this can't be an abstract-declarator, this *must* be a grouping
2837 // paren, because we haven't seen the identifier yet.
2838 isGrouping = true;
2839 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002840 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002841 isDeclarationSpecifier()) { // 'int(int)' is a function.
2842 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2843 // considered to be a type, not a K&R identifier-list.
2844 isGrouping = false;
2845 } else {
2846 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2847 isGrouping = true;
2848 }
Mike Stump1eb44332009-09-09 15:08:12 +00002849
Chris Lattneref4715c2008-04-06 05:45:57 +00002850 // If this is a grouping paren, handle:
2851 // direct-declarator: '(' declarator ')'
2852 // direct-declarator: '(' attributes declarator ')'
2853 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002854 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002855 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002856 if (AttrList)
Ted Kremenek1e377652010-02-11 02:19:13 +00002857 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002858
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002859 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002860 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002861 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002862
2863 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002864 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002865 return;
2866 }
Mike Stump1eb44332009-09-09 15:08:12 +00002867
Chris Lattneref4715c2008-04-06 05:45:57 +00002868 // Okay, if this wasn't a grouping paren, it must be the start of a function
2869 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002870 // identifier (and remember where it would have been), then call into
2871 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002872 D.SetIdentifier(0, Tok.getLocation());
2873
Ted Kremenek1e377652010-02-11 02:19:13 +00002874 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002875}
2876
2877/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2878/// declarator D up to a paren, which indicates that we are parsing function
2879/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002880///
Chris Lattner7399ee02008-10-20 02:05:46 +00002881/// If AttrList is non-null, then the caller parsed those arguments immediately
2882/// after the open paren - they should be considered to be the first argument of
2883/// a parameter. If RequiresArg is true, then the first argument of the
2884/// function is required to be present and required to not be an identifier
2885/// list.
2886///
Reid Spencer5f016e22007-07-11 17:01:13 +00002887/// This method also handles this portion of the grammar:
2888/// parameter-type-list: [C99 6.7.5]
2889/// parameter-list
2890/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002891/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002892///
2893/// parameter-list: [C99 6.7.5]
2894/// parameter-declaration
2895/// parameter-list ',' parameter-declaration
2896///
2897/// parameter-declaration: [C99 6.7.5]
2898/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002899/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002900/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002901/// declaration-specifiers abstract-declarator[opt]
2902/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002903/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002904/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2905///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002906/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002907/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002908///
Chris Lattner7399ee02008-10-20 02:05:46 +00002909void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2910 AttributeList *AttrList,
2911 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002912 // lparen is already consumed!
2913 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002914
Chris Lattner7399ee02008-10-20 02:05:46 +00002915 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002916 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002917 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002918 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002919 delete AttrList;
2920 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002921
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002922 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2923 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002924
2925 // cv-qualifier-seq[opt].
2926 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002927 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002928 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002929 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002930 llvm::SmallVector<TypeTy*, 2> Exceptions;
2931 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002932 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002933 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002934 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002935 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002936
2937 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002938 if (Tok.is(tok::kw_throw)) {
2939 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002940 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002941 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002942 hasAnyExceptionSpec);
2943 assert(Exceptions.size() == ExceptionRanges.size() &&
2944 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002945 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002946 }
2947
Chris Lattnerf97409f2008-04-06 06:57:35 +00002948 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002949 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002950 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002951 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002952 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002953 /*arglist*/ 0, 0,
2954 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002955 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002956 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002957 Exceptions.data(),
2958 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002959 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002960 LParenLoc, RParenLoc, D),
2961 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002962 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002963 }
2964
Chris Lattner7399ee02008-10-20 02:05:46 +00002965 // Alternatively, this parameter list may be an identifier list form for a
2966 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00002967 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
2968 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00002969 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002970 // K&R identifier lists can't have typedefs as identifiers, per
2971 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002972 if (RequiresArg) {
2973 Diag(Tok, diag::err_argument_required_after_attribute);
2974 delete AttrList;
2975 }
Chris Lattner83a94472010-05-14 17:23:36 +00002976
Steve Naroff2d081c42009-01-28 19:16:40 +00002977 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00002978 // normal declarators, not for abstract-declarators. Get the first
2979 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00002980 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00002981 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00002982
2983 // Identifier lists follow a really simple grammar: the identifiers can
2984 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
2985 // identifier lists are really rare in the brave new modern world, and it
2986 // is very common for someone to typo a type in a non-k&r style list. If
2987 // we are presented with something like: "void foo(intptr x, float y)",
2988 // we don't want to start parsing the function declarator as though it is
2989 // a K&R style declarator just because intptr is an invalid type.
2990 //
2991 // To handle this, we check to see if the token after the first identifier
2992 // is a "," or ")". Only if so, do we parse it as an identifier list.
2993 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
2994 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
2995 FirstTok.getIdentifierInfo(),
2996 FirstTok.getLocation(), D);
2997
2998 // If we get here, the code is invalid. Push the first identifier back
2999 // into the token stream and parse the first argument as an (invalid)
3000 // normal argument declarator.
3001 PP.EnterToken(Tok);
3002 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003003 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003004 }
Mike Stump1eb44332009-09-09 15:08:12 +00003005
Chris Lattnerf97409f2008-04-06 06:57:35 +00003006 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003007
Chris Lattnerf97409f2008-04-06 06:57:35 +00003008 // Build up an array of information about the parsed arguments.
3009 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003010
3011 // Enter function-declaration scope, limiting any declarators to the
3012 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003013 ParseScope PrototypeScope(this,
3014 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003015
Chris Lattnerf97409f2008-04-06 06:57:35 +00003016 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003017 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003018 while (1) {
3019 if (Tok.is(tok::ellipsis)) {
3020 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003021 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003022 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003023 }
Mike Stump1eb44332009-09-09 15:08:12 +00003024
Chris Lattnerf97409f2008-04-06 06:57:35 +00003025 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00003026
Chris Lattnerf97409f2008-04-06 06:57:35 +00003027 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003028 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003029 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00003030
3031 // If the caller parsed attributes for the first argument, add them now.
3032 if (AttrList) {
3033 DS.AddAttributes(AttrList);
3034 AttrList = 0; // Only apply the attributes to the first parameter.
3035 }
Chris Lattnere64c5492009-02-27 18:38:20 +00003036 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003037
Chris Lattnerf97409f2008-04-06 06:57:35 +00003038 // Parse the declarator. This is "PrototypeContext", because we must
3039 // accept either 'declarator' or 'abstract-declarator' here.
3040 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3041 ParseDeclarator(ParmDecl);
3042
3043 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003044 if (Tok.is(tok::kw___attribute)) {
3045 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00003046 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003047 ParmDecl.AddAttributes(AttrList, Loc);
3048 }
Mike Stump1eb44332009-09-09 15:08:12 +00003049
Chris Lattnerf97409f2008-04-06 06:57:35 +00003050 // Remember this parsed parameter in ParamInfo.
3051 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003052
Douglas Gregor72b505b2008-12-16 21:30:33 +00003053 // DefArgToks is used when the parsing of default arguments needs
3054 // to be delayed.
3055 CachedTokens *DefArgToks = 0;
3056
Chris Lattnerf97409f2008-04-06 06:57:35 +00003057 // If no parameter was specified, verify that *something* was specified,
3058 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003059 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3060 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003061 // Completely missing, emit error.
3062 Diag(DSStart, diag::err_missing_param);
3063 } else {
3064 // Otherwise, we have something. Add it and let semantic analysis try
3065 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003066
Chris Lattnerf97409f2008-04-06 06:57:35 +00003067 // Inform the actions module about the parameter declarator, so it gets
3068 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003069 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003070
3071 // Parse the default argument, if any. We parse the default
3072 // arguments in all dialects; the semantic analysis in
3073 // ActOnParamDefaultArgument will reject the default argument in
3074 // C.
3075 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003076 SourceLocation EqualLoc = Tok.getLocation();
3077
Chris Lattner04421082008-04-08 04:40:51 +00003078 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003079 if (D.getContext() == Declarator::MemberContext) {
3080 // If we're inside a class definition, cache the tokens
3081 // corresponding to the default argument. We'll actually parse
3082 // them when we see the end of the class definition.
3083 // FIXME: Templates will require something similar.
3084 // FIXME: Can we use a smart pointer for Toks?
3085 DefArgToks = new CachedTokens;
3086
Mike Stump1eb44332009-09-09 15:08:12 +00003087 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003088 /*StopAtSemi=*/true,
3089 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003090 delete DefArgToks;
3091 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003092 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003093 } else {
3094 // Mark the end of the default argument so that we know when to
3095 // stop when we parse it later on.
3096 Token DefArgEnd;
3097 DefArgEnd.startToken();
3098 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3099 DefArgEnd.setLocation(Tok.getLocation());
3100 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003101 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003102 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003103 }
Chris Lattner04421082008-04-08 04:40:51 +00003104 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003105 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003106 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003107
Douglas Gregor72b505b2008-12-16 21:30:33 +00003108 OwningExprResult DefArgResult(ParseAssignmentExpression());
3109 if (DefArgResult.isInvalid()) {
3110 Actions.ActOnParamDefaultArgumentError(Param);
3111 SkipUntil(tok::comma, tok::r_paren, true, true);
3112 } else {
3113 // Inform the actions module about the default argument
3114 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00003115 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00003116 }
Chris Lattner04421082008-04-08 04:40:51 +00003117 }
3118 }
Mike Stump1eb44332009-09-09 15:08:12 +00003119
3120 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3121 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003122 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003123 }
3124
3125 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003126 if (Tok.isNot(tok::comma)) {
3127 if (Tok.is(tok::ellipsis)) {
3128 IsVariadic = true;
3129 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3130
3131 if (!getLang().CPlusPlus) {
3132 // We have ellipsis without a preceding ',', which is ill-formed
3133 // in C. Complain and provide the fix.
3134 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003135 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003136 }
3137 }
3138
3139 break;
3140 }
Mike Stump1eb44332009-09-09 15:08:12 +00003141
Chris Lattnerf97409f2008-04-06 06:57:35 +00003142 // Consume the comma.
3143 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003144 }
Mike Stump1eb44332009-09-09 15:08:12 +00003145
Chris Lattnerf97409f2008-04-06 06:57:35 +00003146 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00003147 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00003148
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003149 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003150 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3151 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003152
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003153 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003154 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003155 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003156 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00003157 llvm::SmallVector<TypeTy*, 2> Exceptions;
3158 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00003159
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003160 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003161 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003162 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003163 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003164 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003165
3166 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003167 if (Tok.is(tok::kw_throw)) {
3168 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003169 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003170 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003171 hasAnyExceptionSpec);
3172 assert(Exceptions.size() == ExceptionRanges.size() &&
3173 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003174 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003175 }
3176
Reid Spencer5f016e22007-07-11 17:01:13 +00003177 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003178 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003179 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003180 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003181 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003182 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003183 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003184 Exceptions.data(),
3185 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003186 Exceptions.size(),
3187 LParenLoc, RParenLoc, D),
3188 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003189}
3190
Chris Lattner66d28652008-04-06 06:34:08 +00003191/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3192/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003193/// first identifier has already been consumed, and the current token is the
3194/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003195///
3196/// identifier-list: [C99 6.7.5]
3197/// identifier
3198/// identifier-list ',' identifier
3199///
3200void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003201 IdentifierInfo *FirstIdent,
3202 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003203 Declarator &D) {
3204 // Build up an array of information about the parsed arguments.
3205 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3206 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003207
Chris Lattner66d28652008-04-06 06:34:08 +00003208 // If there was no identifier specified for the declarator, either we are in
3209 // an abstract-declarator, or we are in a parameter declarator which was found
3210 // to be abstract. In abstract-declarators, identifier lists are not valid:
3211 // diagnose this.
3212 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003213 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003214
Chris Lattner83a94472010-05-14 17:23:36 +00003215 // The first identifier was already read, and is known to be the first
3216 // identifier in the list. Remember this identifier in ParamInfo.
3217 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00003218 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00003219
Chris Lattner66d28652008-04-06 06:34:08 +00003220 while (Tok.is(tok::comma)) {
3221 // Eat the comma.
3222 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003223
Chris Lattner50c64772008-04-06 06:39:19 +00003224 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003225 if (Tok.isNot(tok::identifier)) {
3226 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003227 SkipUntil(tok::r_paren);
3228 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003229 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003230
Chris Lattner66d28652008-04-06 06:34:08 +00003231 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003232
3233 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003234 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003235 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003236
Chris Lattner66d28652008-04-06 06:34:08 +00003237 // Verify that the argument identifier has not already been mentioned.
3238 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003239 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003240 } else {
3241 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003242 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003243 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00003244 0));
Chris Lattner50c64772008-04-06 06:39:19 +00003245 }
Mike Stump1eb44332009-09-09 15:08:12 +00003246
Chris Lattner66d28652008-04-06 06:34:08 +00003247 // Eat the identifier.
3248 ConsumeToken();
3249 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003250
3251 // If we have the closing ')', eat it and we're done.
3252 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3253
Chris Lattner50c64772008-04-06 06:39:19 +00003254 // Remember that we parsed a function type, and remember the attributes. This
3255 // function type is always a K&R style function type, which is not varargs and
3256 // has no prototype.
3257 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003258 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003259 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003260 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003261 /*exception*/false,
3262 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003263 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003264 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003265}
Chris Lattneref4715c2008-04-06 05:45:57 +00003266
Reid Spencer5f016e22007-07-11 17:01:13 +00003267/// [C90] direct-declarator '[' constant-expression[opt] ']'
3268/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3269/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3270/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3271/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3272void Parser::ParseBracketDeclarator(Declarator &D) {
3273 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003274
Chris Lattner378c7e42008-12-18 07:27:21 +00003275 // C array syntax has many features, but by-far the most common is [] and [4].
3276 // This code does a fast path to handle some of the most obvious cases.
3277 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003278 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003279 //FIXME: Use these
3280 CXX0XAttributeList Attr;
3281 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3282 Attr = ParseCXX0XAttributes();
3283 }
3284
Chris Lattner378c7e42008-12-18 07:27:21 +00003285 // Remember that we parsed the empty array type.
John McCallca0408f2010-08-23 06:44:23 +00003286 OwningExprResult NumElements;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003287 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3288 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003289 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003290 return;
3291 } else if (Tok.getKind() == tok::numeric_constant &&
3292 GetLookAheadToken(1).is(tok::r_square)) {
3293 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00003294 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003295 ConsumeToken();
3296
Sebastian Redlab197ba2009-02-09 18:23:29 +00003297 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003298 //FIXME: Use these
3299 CXX0XAttributeList Attr;
3300 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3301 Attr = ParseCXX0XAttributes();
3302 }
Chris Lattner378c7e42008-12-18 07:27:21 +00003303
3304 // If there was an error parsing the assignment-expression, recover.
3305 if (ExprRes.isInvalid())
3306 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00003307
Chris Lattner378c7e42008-12-18 07:27:21 +00003308 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003309 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3310 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003311 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003312 return;
3313 }
Mike Stump1eb44332009-09-09 15:08:12 +00003314
Reid Spencer5f016e22007-07-11 17:01:13 +00003315 // If valid, this location is the position where we read the 'static' keyword.
3316 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003317 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003318 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003319
Reid Spencer5f016e22007-07-11 17:01:13 +00003320 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003321 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003322 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003323 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003324
Reid Spencer5f016e22007-07-11 17:01:13 +00003325 // If we haven't already read 'static', check to see if there is one after the
3326 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003327 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003328 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003329
Reid Spencer5f016e22007-07-11 17:01:13 +00003330 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3331 bool isStar = false;
John McCallca0408f2010-08-23 06:44:23 +00003332 OwningExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00003333
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003334 // Handle the case where we have '[*]' as the array size. However, a leading
3335 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3336 // the the token after the star is a ']'. Since stars in arrays are
3337 // infrequent, use of lookahead is not costly here.
3338 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003339 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003340
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003341 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003342 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003343 StaticLoc = SourceLocation(); // Drop the static.
3344 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003345 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003346 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003347 // Note, in C89, this production uses the constant-expr production instead
3348 // of assignment-expr. The only difference is that assignment-expr allows
3349 // things like '=' and '*='. Sema rejects these in C89 mode because they
3350 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003351
Douglas Gregore0762c92009-06-19 23:52:42 +00003352 // Parse the constant-expression or assignment-expression now (depending
3353 // on dialect).
3354 if (getLang().CPlusPlus)
3355 NumElements = ParseConstantExpression();
3356 else
3357 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003358 }
Mike Stump1eb44332009-09-09 15:08:12 +00003359
Reid Spencer5f016e22007-07-11 17:01:13 +00003360 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003361 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003362 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003363 // If the expression was invalid, skip it.
3364 SkipUntil(tok::r_square);
3365 return;
3366 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003367
3368 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3369
Sean Huntbbd37c62009-11-21 08:43:09 +00003370 //FIXME: Use these
3371 CXX0XAttributeList Attr;
3372 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3373 Attr = ParseCXX0XAttributes();
3374 }
3375
Chris Lattner378c7e42008-12-18 07:27:21 +00003376 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00003377 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3378 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003379 NumElements.release(),
3380 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003381 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003382}
3383
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003384/// [GNU] typeof-specifier:
3385/// typeof ( expressions )
3386/// typeof ( type-name )
3387/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003388///
3389void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003390 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003391 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003392 SourceLocation StartLoc = ConsumeToken();
3393
John McCallcfb708c2010-01-13 20:03:27 +00003394 const bool hasParens = Tok.is(tok::l_paren);
3395
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003396 bool isCastExpr;
3397 TypeTy *CastTy;
3398 SourceRange CastRange;
3399 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3400 isCastExpr,
3401 CastTy,
3402 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003403 if (hasParens)
3404 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003405
3406 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003407 // FIXME: Not accurate, the range gets one token more than it should.
3408 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003409 else
3410 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003411
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003412 if (isCastExpr) {
3413 if (!CastTy) {
3414 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003415 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003416 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003417
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003418 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003419 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003420 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3421 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003422 DiagID, CastTy))
3423 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003424 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003425 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003426
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003427 // If we get here, the operand to the typeof was an expresion.
3428 if (Operand.isInvalid()) {
3429 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003430 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003431 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003432
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003433 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003434 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003435 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3436 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003437 DiagID, Operand.release()))
3438 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003439}
Chris Lattner1b492422010-02-28 18:33:55 +00003440
3441
3442/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3443/// from TryAltiVecVectorToken.
3444bool Parser::TryAltiVecVectorTokenOutOfLine() {
3445 Token Next = NextToken();
3446 switch (Next.getKind()) {
3447 default: return false;
3448 case tok::kw_short:
3449 case tok::kw_long:
3450 case tok::kw_signed:
3451 case tok::kw_unsigned:
3452 case tok::kw_void:
3453 case tok::kw_char:
3454 case tok::kw_int:
3455 case tok::kw_float:
3456 case tok::kw_double:
3457 case tok::kw_bool:
3458 case tok::kw___pixel:
3459 Tok.setKind(tok::kw___vector);
3460 return true;
3461 case tok::identifier:
3462 if (Next.getIdentifierInfo() == Ident_pixel) {
3463 Tok.setKind(tok::kw___vector);
3464 return true;
3465 }
3466 return false;
3467 }
3468}
3469
3470bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3471 const char *&PrevSpec, unsigned &DiagID,
3472 bool &isInvalid) {
3473 if (Tok.getIdentifierInfo() == Ident_vector) {
3474 Token Next = NextToken();
3475 switch (Next.getKind()) {
3476 case tok::kw_short:
3477 case tok::kw_long:
3478 case tok::kw_signed:
3479 case tok::kw_unsigned:
3480 case tok::kw_void:
3481 case tok::kw_char:
3482 case tok::kw_int:
3483 case tok::kw_float:
3484 case tok::kw_double:
3485 case tok::kw_bool:
3486 case tok::kw___pixel:
3487 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3488 return true;
3489 case tok::identifier:
3490 if (Next.getIdentifierInfo() == Ident_pixel) {
3491 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3492 return true;
3493 }
3494 break;
3495 default:
3496 break;
3497 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00003498 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00003499 DS.isTypeAltiVecVector()) {
3500 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3501 return true;
3502 }
3503 return false;
3504}
3505