blob: b0d9da5845790cfcfcf8cc5e0119a4a132e6bf3c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000017#include "clang/Parse/Template.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000018#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Sebastian Redlef65f062009-05-29 18:02:33 +000031Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000035
Reid Spencer5f016e22007-07-11 17:01:13 +000036 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000039 if (Range)
40 *Range = DeclaratorInfo.getSourceRange();
41
Chris Lattnereaaebc72009-04-25 08:06:05 +000042 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000043 return true;
44
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:
177 // If it's a builtin type name, eat it and expect a rparen
178 // __attribute__(( vec_type_hint(char) ))
179 ConsumeToken();
Sean Huntbbd37c62009-11-21 08:43:09 +0000180 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Nate Begeman6f3d8382009-06-26 06:32:41 +0000181 0, SourceLocation(), 0, 0, CurrAttr);
182 if (Tok.is(tok::r_paren))
183 ConsumeParen();
184 break;
185 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000187 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 // now parse the list of expressions
191 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000192 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000193 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 ArgExprsOk = false;
195 SkipUntil(tok::r_paren);
196 break;
197 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000198 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 }
Chris Lattner04d66662007-10-09 17:33:22 +0000200 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 break;
202 ConsumeToken(); // Eat the comma, move to the next argument
203 }
204 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000205 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000207 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
Sean Huntbbd37c62009-11-21 08:43:09 +0000208 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
209 ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 CurrAttr);
211 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000212 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 }
214 }
215 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000216 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 0, SourceLocation(), 0, 0, CurrAttr);
218 }
219 }
220 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000222 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000223 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
224 SkipUntil(tok::r_paren, false);
225 }
226 if (EndLoc)
227 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 }
229 return CurrAttr;
230}
231
Eli Friedmana23b4852009-06-08 07:21:15 +0000232/// ParseMicrosoftDeclSpec - Parse an __declspec construct
233///
234/// [MS] decl-specifier:
235/// __declspec ( extended-decl-modifier-seq )
236///
237/// [MS] extended-decl-modifier-seq:
238/// extended-decl-modifier[opt]
239/// extended-decl-modifier extended-decl-modifier-seq
240
Eli Friedman290eeb02009-06-08 23:27:34 +0000241AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000242 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000243
Steve Narofff59e17e2008-12-24 20:59:21 +0000244 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000245 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
246 "declspec")) {
247 SkipUntil(tok::r_paren, true); // skip until ) or ;
248 return CurrAttr;
249 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000250 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000251 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
252 SourceLocation AttrNameLoc = ConsumeToken();
253 if (Tok.is(tok::l_paren)) {
254 ConsumeParen();
255 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
256 // correctly.
257 OwningExprResult ArgExpr(ParseAssignmentExpression());
258 if (!ArgExpr.isInvalid()) {
259 ExprTy* ExprList = ArgExpr.take();
Sean Huntbbd37c62009-11-21 08:43:09 +0000260 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedmana23b4852009-06-08 07:21:15 +0000261 SourceLocation(), &ExprList, 1,
262 CurrAttr, true);
263 }
264 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
265 SkipUntil(tok::r_paren, false);
266 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000267 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
268 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000269 }
270 }
271 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
272 SkipUntil(tok::r_paren, false);
Eli Friedman290eeb02009-06-08 23:27:34 +0000273 return CurrAttr;
274}
275
276AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
277 // Treat these like attributes
278 // FIXME: Allow Sema to distinguish between these and real attributes!
279 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000280 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
281 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000282 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
283 SourceLocation AttrNameLoc = ConsumeToken();
284 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
285 // FIXME: Support these properly!
286 continue;
Sean Huntbbd37c62009-11-21 08:43:09 +0000287 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman290eeb02009-06-08 23:27:34 +0000288 SourceLocation(), 0, 0, CurrAttr, true);
289 }
290 return CurrAttr;
Steve Narofff59e17e2008-12-24 20:59:21 +0000291}
292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293/// ParseDeclaration - Parse a full 'declaration', which consists of
294/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000295/// 'Context' should be a Declarator::TheContext value. This returns the
296/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000297///
298/// declaration: [C99 6.7]
299/// block-declaration ->
300/// simple-declaration
301/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000302/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000303/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000304/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000305/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000306/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000307/// others... [FIXME]
308///
Chris Lattner97144fc2009-04-02 04:16:50 +0000309Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000310 SourceLocation &DeclEnd,
311 CXX0XAttributeList Attr) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000312 ParenBraceBracketBalancer BalancerRAIIObj(*this);
313
Chris Lattner682bf922009-03-29 16:50:03 +0000314 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000315 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000316 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000317 case tok::kw_export:
Sean Huntbbd37c62009-11-21 08:43:09 +0000318 if (Attr.HasAttr)
319 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
320 << Attr.Range;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000321 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000322 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000323 case tok::kw_namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +0000324 if (Attr.HasAttr)
325 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
326 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000327 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000328 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000329 case tok::kw_using:
Sean Huntbbd37c62009-11-21 08:43:09 +0000330 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner682bf922009-03-29 16:50:03 +0000331 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000332 case tok::kw_static_assert:
Sean Huntbbd37c62009-11-21 08:43:09 +0000333 if (Attr.HasAttr)
334 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
335 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000336 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000337 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000338 default:
Chris Lattner5c5db552010-04-05 18:18:31 +0000339 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000340 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000341
Chris Lattner682bf922009-03-29 16:50:03 +0000342 // This routine returns a DeclGroup, if the thing we parsed only contains a
343 // single decl, convert it now.
344 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000345}
346
347/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
348/// declaration-specifiers init-declarator-list[opt] ';'
349///[C90/C++]init-declarator-list ';' [TODO]
350/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000351///
352/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000353/// declaration. If it is true, it checks for and eats it.
Chris Lattnercd147752009-03-29 17:27:48 +0000354Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000355 SourceLocation &DeclEnd,
Chris Lattner5c5db552010-04-05 18:18:31 +0000356 AttributeList *Attr,
357 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000359 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000360 if (Attr)
361 DS.AddAttributes(Attr);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000362 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
363 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
366 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000367 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000368 if (RequireSemi) ConsumeToken();
Douglas Gregor23c94db2010-07-02 17:43:08 +0000369 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallaec03712010-05-21 20:45:30 +0000370 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000371 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000372 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Chris Lattner5c5db552010-04-05 18:18:31 +0000375 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld8ac0572009-11-03 19:26:08 +0000376}
Mike Stump1eb44332009-09-09 15:08:12 +0000377
John McCalld8ac0572009-11-03 19:26:08 +0000378/// ParseDeclGroup - Having concluded that this is either a function
379/// definition or a group of object declarations, actually parse the
380/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000381Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
382 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000383 bool AllowFunctionDefinitions,
384 SourceLocation *DeclEnd) {
385 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000386 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000387 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000388
John McCalld8ac0572009-11-03 19:26:08 +0000389 // Bail out if the first declarator didn't seem well-formed.
390 if (!D.hasName() && !D.mayOmitIdentifier()) {
391 // Skip until ; or }.
392 SkipUntil(tok::r_brace, true, true);
393 if (Tok.is(tok::semi))
394 ConsumeToken();
395 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Chris Lattnerc82daef2010-07-11 22:24:20 +0000398 // Check to see if we have a function *definition* which must have a body.
399 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
400 // Look at the next token to make sure that this isn't a function
401 // declaration. We have to check this because __attribute__ might be the
402 // start of a function definition in GCC-extended K&R C.
403 !isDeclarationAfterDeclarator()) {
404
Chris Lattner004659a2010-07-11 22:42:07 +0000405 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000406 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
407 Diag(Tok, diag::err_function_declared_typedef);
408
409 // Recover by treating the 'typedef' as spurious.
410 DS.ClearStorageClassSpecs();
411 }
412
413 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
414 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000415 }
416
417 if (isDeclarationSpecifier()) {
418 // If there is an invalid declaration specifier right after the function
419 // prototype, then we must be in a missing semicolon case where this isn't
420 // actually a body. Just fall through into the code that handles it as a
421 // prototype, and let the top-level code handle the erroneous declspec
422 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000423 } else {
424 Diag(Tok, diag::err_expected_fn_body);
425 SkipUntil(tok::semi);
426 return DeclGroupPtrTy();
427 }
428 }
429
430 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
431 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000432 D.complete(FirstDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000433 if (FirstDecl.get())
434 DeclsInGroup.push_back(FirstDecl);
435
436 // If we don't have a comma, it is either the end of the list (a ';') or an
437 // error, bail out.
438 while (Tok.is(tok::comma)) {
439 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000440 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000441
442 // Parse the next declarator.
443 D.clear();
444
445 // Accept attributes in an init-declarator. In the first declarator in a
446 // declaration, these would be part of the declspec. In subsequent
447 // declarators, they become part of the declarator itself, so that they
448 // don't apply to declarators after *this* one. Examples:
449 // short __attribute__((common)) var; -> declspec
450 // short var __attribute__((common)); -> declarator
451 // short x, __attribute__((common)) var; -> declarator
452 if (Tok.is(tok::kw___attribute)) {
453 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000454 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld8ac0572009-11-03 19:26:08 +0000455 D.AddAttributes(AttrList, Loc);
456 }
457
458 ParseDeclarator(D);
459
460 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000461 D.complete(ThisDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000462 if (ThisDecl.get())
463 DeclsInGroup.push_back(ThisDecl);
464 }
465
466 if (DeclEnd)
467 *DeclEnd = Tok.getLocation();
468
469 if (Context != Declarator::ForContext &&
470 ExpectAndConsume(tok::semi,
471 Context == Declarator::FileContext
472 ? diag::err_invalid_token_after_toplevel_declarator
473 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000474 // Okay, there was no semicolon and one was expected. If we see a
475 // declaration specifier, just assume it was missing and continue parsing.
476 // Otherwise things are very confused and we skip to recover.
477 if (!isDeclarationSpecifier()) {
478 SkipUntil(tok::r_brace, true, true);
479 if (Tok.is(tok::semi))
480 ConsumeToken();
481 }
John McCalld8ac0572009-11-03 19:26:08 +0000482 }
483
Douglas Gregor23c94db2010-07-02 17:43:08 +0000484 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000485 DeclsInGroup.data(),
486 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000487}
488
Douglas Gregor1426e532009-05-12 21:31:51 +0000489/// \brief Parse 'declaration' after parsing 'declaration-specifiers
490/// declarator'. This method parses the remainder of the declaration
491/// (including any attributes or initializer, among other things) and
492/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000493///
Reid Spencer5f016e22007-07-11 17:01:13 +0000494/// init-declarator: [C99 6.7]
495/// declarator
496/// declarator '=' initializer
497/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
498/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000499/// [C++] declarator initializer[opt]
500///
501/// [C++] initializer:
502/// [C++] '=' initializer-clause
503/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000504/// [C++0x] '=' 'default' [TODO]
505/// [C++0x] '=' 'delete'
506///
507/// According to the standard grammar, =default and =delete are function
508/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000509///
Douglas Gregore542c862009-06-23 23:11:28 +0000510Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
511 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000512 // If a simple-asm-expr is present, parse it.
513 if (Tok.is(tok::kw_asm)) {
514 SourceLocation Loc;
515 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
516 if (AsmLabel.isInvalid()) {
517 SkipUntil(tok::semi, true, true);
518 return DeclPtrTy();
519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Douglas Gregor1426e532009-05-12 21:31:51 +0000521 D.setAsmLabel(AsmLabel.release());
522 D.SetRangeEnd(Loc);
523 }
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Douglas Gregor1426e532009-05-12 21:31:51 +0000525 // If attributes are present, parse them.
526 if (Tok.is(tok::kw___attribute)) {
527 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000528 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000529 D.AddAttributes(AttrList, Loc);
530 }
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Douglas Gregor1426e532009-05-12 21:31:51 +0000532 // Inform the current actions module that we just parsed this declarator.
Douglas Gregord5a423b2009-09-25 18:43:00 +0000533 DeclPtrTy ThisDecl;
534 switch (TemplateInfo.Kind) {
535 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000536 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000537 break;
538
539 case ParsedTemplateInfo::Template:
540 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000541 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Douglas Gregore542c862009-06-23 23:11:28 +0000542 Action::MultiTemplateParamsArg(Actions,
543 TemplateInfo.TemplateParams->data(),
544 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000545 D);
546 break;
547
548 case ParsedTemplateInfo::ExplicitInstantiation: {
549 Action::DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000550 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000551 TemplateInfo.ExternLoc,
552 TemplateInfo.TemplateLoc,
553 D);
554 if (ThisRes.isInvalid()) {
555 SkipUntil(tok::semi, true, true);
556 return DeclPtrTy();
557 }
558
559 ThisDecl = ThisRes.get();
560 break;
561 }
562 }
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Douglas Gregor1426e532009-05-12 21:31:51 +0000564 // Parse declarator '=' initializer.
565 if (Tok.is(tok::equal)) {
566 ConsumeToken();
567 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
568 SourceLocation DelLoc = ConsumeToken();
569 Actions.SetDeclDeleted(ThisDecl, DelLoc);
570 } else {
John McCall731ad842009-12-19 09:28:58 +0000571 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
572 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000573 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000574 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000575
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000576 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000577 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000578 ConsumeCodeCompletionToken();
579 SkipUntil(tok::comma, true, true);
580 return ThisDecl;
581 }
582
Douglas Gregor1426e532009-05-12 21:31:51 +0000583 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000584
John McCall731ad842009-12-19 09:28:58 +0000585 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000586 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000587 ExitScope();
588 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000589
Douglas Gregor1426e532009-05-12 21:31:51 +0000590 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000591 SkipUntil(tok::comma, true, true);
592 Actions.ActOnInitializerError(ThisDecl);
593 } else
594 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000595 }
596 } else if (Tok.is(tok::l_paren)) {
597 // Parse C++ direct initializer: '(' expression-list ')'
598 SourceLocation LParenLoc = ConsumeParen();
599 ExprVector Exprs(Actions);
600 CommaLocsTy CommaLocs;
601
Douglas Gregorb4debae2009-12-22 17:47:17 +0000602 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
603 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000604 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000605 }
606
Douglas Gregor1426e532009-05-12 21:31:51 +0000607 if (ParseExpressionList(Exprs, CommaLocs)) {
608 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000609
610 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000611 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000612 ExitScope();
613 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000614 } else {
615 // Match the ')'.
616 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
617
618 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
619 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000620
621 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000622 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000623 ExitScope();
624 }
625
Douglas Gregor1426e532009-05-12 21:31:51 +0000626 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
627 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000628 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000629 }
630 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000631 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000632 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
633 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000634 }
635
636 return ThisDecl;
637}
638
Reid Spencer5f016e22007-07-11 17:01:13 +0000639/// ParseSpecifierQualifierList
640/// specifier-qualifier-list:
641/// type-specifier specifier-qualifier-list[opt]
642/// type-qualifier specifier-qualifier-list[opt]
643/// [GNU] attributes specifier-qualifier-list[opt]
644///
645void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
646 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
647 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 // Validate declspec for type-name.
651 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000652 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
653 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 // Issue diagnostic and remove storage class if present.
657 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
658 if (DS.getStorageClassSpecLoc().isValid())
659 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
660 else
661 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
662 DS.ClearStorageClassSpecs();
663 }
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 // Issue diagnostic and remove function specfier if present.
666 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000667 if (DS.isInlineSpecified())
668 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
669 if (DS.isVirtualSpecified())
670 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
671 if (DS.isExplicitSpecified())
672 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 DS.ClearFunctionSpecs();
674 }
675}
676
Chris Lattnerc199ab32009-04-12 20:42:31 +0000677/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
678/// specified token is valid after the identifier in a declarator which
679/// immediately follows the declspec. For example, these things are valid:
680///
681/// int x [ 4]; // direct-declarator
682/// int x ( int y); // direct-declarator
683/// int(int x ) // direct-declarator
684/// int x ; // simple-declaration
685/// int x = 17; // init-declarator-list
686/// int x , y; // init-declarator-list
687/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000688/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000689/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000690///
691/// This is not, because 'x' does not immediately follow the declspec (though
692/// ')' happens to be valid anyway).
693/// int (x)
694///
695static bool isValidAfterIdentifierInDeclarator(const Token &T) {
696 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
697 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000698 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000699}
700
Chris Lattnere40c2952009-04-14 21:34:55 +0000701
702/// ParseImplicitInt - This method is called when we have an non-typename
703/// identifier in a declspec (which normally terminates the decl spec) when
704/// the declspec has no type specifier. In this case, the declspec is either
705/// malformed or is "implicit int" (in K&R and C89).
706///
707/// This method handles diagnosing this prettily and returns false if the
708/// declspec is done being processed. If it recovers and thinks there may be
709/// other pieces of declspec after it, it returns true.
710///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000711bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000712 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000713 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000714 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Chris Lattnere40c2952009-04-14 21:34:55 +0000716 SourceLocation Loc = Tok.getLocation();
717 // If we see an identifier that is not a type name, we normally would
718 // parse it as the identifer being declared. However, when a typename
719 // is typo'd or the definition is not included, this will incorrectly
720 // parse the typename as the identifier name and fall over misparsing
721 // later parts of the diagnostic.
722 //
723 // As such, we try to do some look-ahead in cases where this would
724 // otherwise be an "implicit-int" case to see if this is invalid. For
725 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
726 // an identifier with implicit int, we'd get a parse error because the
727 // next token is obviously invalid for a type. Parse these as a case
728 // with an invalid type specifier.
729 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Chris Lattnere40c2952009-04-14 21:34:55 +0000731 // Since we know that this either implicit int (which is rare) or an
732 // error, we'd do lookahead to try to do better recovery.
733 if (isValidAfterIdentifierInDeclarator(NextToken())) {
734 // If this token is valid for implicit int, e.g. "static x = 4", then
735 // we just avoid eating the identifier, so it will be parsed as the
736 // identifier in the declarator.
737 return false;
738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Chris Lattnere40c2952009-04-14 21:34:55 +0000740 // Otherwise, if we don't consume this token, we are going to emit an
741 // error anyway. Try to recover from various common problems. Check
742 // to see if this was a reference to a tag name without a tag specified.
743 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000744 //
745 // C++ doesn't need this, and isTagName doesn't take SS.
746 if (SS == 0) {
747 const char *TagName = 0;
748 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Douglas Gregor23c94db2010-07-02 17:43:08 +0000750 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +0000751 default: break;
752 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
753 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
754 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
755 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
756 }
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Chris Lattnerf4382f52009-04-14 22:17:06 +0000758 if (TagName) {
759 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +0000760 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +0000761 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Chris Lattnerf4382f52009-04-14 22:17:06 +0000763 // Parse this as a tag as if the missing tag were present.
764 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +0000765 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000766 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000767 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000768 return true;
769 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000770 }
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Douglas Gregora786fdb2009-10-13 23:27:22 +0000772 // This is almost certainly an invalid type name. Let the action emit a
773 // diagnostic and attempt to recover.
774 Action::TypeTy *T = 0;
775 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +0000776 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +0000777 // The action emitted a diagnostic, so we don't have to.
778 if (T) {
779 // The action has suggested that the type T could be used. Set that as
780 // the type in the declaration specifiers, consume the would-be type
781 // name token, and we're done.
782 const char *PrevSpec;
783 unsigned DiagID;
784 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
785 false);
786 DS.SetRangeEnd(Tok.getLocation());
787 ConsumeToken();
788
789 // There may be other declaration specifiers after this.
790 return true;
791 }
792
793 // Fall through; the action had no suggestion for us.
794 } else {
795 // The action did not emit a diagnostic, so emit one now.
796 SourceRange R;
797 if (SS) R = SS->getRange();
798 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
799 }
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Douglas Gregora786fdb2009-10-13 23:27:22 +0000801 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000802 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000803 unsigned DiagID;
804 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000805 DS.SetRangeEnd(Tok.getLocation());
806 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Chris Lattnere40c2952009-04-14 21:34:55 +0000808 // TODO: Could inject an invalid typedef decl in an enclosing scope to
809 // avoid rippling error messages on subsequent uses of the same type,
810 // could be useful if #include was forgotten.
811 return false;
812}
813
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000814/// \brief Determine the declaration specifier context from the declarator
815/// context.
816///
817/// \param Context the declarator context, which is one of the
818/// Declarator::TheContext enumerator values.
819Parser::DeclSpecContext
820Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
821 if (Context == Declarator::MemberContext)
822 return DSC_class;
823 if (Context == Declarator::FileContext)
824 return DSC_top_level;
825 return DSC_normal;
826}
827
Reid Spencer5f016e22007-07-11 17:01:13 +0000828/// ParseDeclarationSpecifiers
829/// declaration-specifiers: [C99 6.7]
830/// storage-class-specifier declaration-specifiers[opt]
831/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000832/// [C99] function-specifier declaration-specifiers[opt]
833/// [GNU] attributes declaration-specifiers[opt]
834///
835/// storage-class-specifier: [C99 6.7.1]
836/// 'typedef'
837/// 'extern'
838/// 'static'
839/// 'auto'
840/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000841/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000842/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000843/// function-specifier: [C99 6.7.4]
844/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000845/// [C++] 'virtual'
846/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000847/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000848/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000849
Reid Spencer5f016e22007-07-11 17:01:13 +0000850///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000851void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000852 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000853 AccessSpecifier AS,
854 DeclSpecContext DSContext) {
Douglas Gregor791215b2009-09-21 20:51:25 +0000855 if (Tok.is(tok::code_completion)) {
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000856 Action::ParserCompletionContext CCC = Action::PCC_Namespace;
Douglas Gregor01dfea02010-01-10 23:08:15 +0000857 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000858 CCC = DSContext == DSC_class? Action::PCC_MemberTemplate
859 : Action::PCC_Template;
Douglas Gregor01dfea02010-01-10 23:08:15 +0000860 else if (DSContext == DSC_class)
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000861 CCC = Action::PCC_Class;
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000862 else if (ObjCImpDecl)
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000863 CCC = Action::PCC_ObjCImplementation;
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000864
Douglas Gregor23c94db2010-07-02 17:43:08 +0000865 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Douglas Gregordc845342010-05-25 05:58:43 +0000866 ConsumeCodeCompletionToken();
Douglas Gregor791215b2009-09-21 20:51:25 +0000867 }
868
Chris Lattner81c018d2008-03-13 06:29:04 +0000869 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000871 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000873 unsigned DiagID = 0;
874
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000876
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000878 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000879 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 // If this is not a declaration specifier token, we're done reading decl
881 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000882 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000883 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Chris Lattner5e02c472009-01-05 00:07:25 +0000885 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +0000886 // C++ scope specifier. Annotate and loop, or bail out on error.
887 if (TryAnnotateCXXScopeToken(true)) {
888 if (!DS.hasTypeSpecifier())
889 DS.SetTypeSpecError();
890 goto DoneWithDeclSpec;
891 }
John McCall2e0a7152010-03-01 18:20:46 +0000892 if (Tok.is(tok::coloncolon)) // ::new or ::delete
893 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +0000894 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000895
896 case tok::annot_cxxscope: {
897 if (DS.hasTypeSpecifier())
898 goto DoneWithDeclSpec;
899
John McCallaa87d332009-12-12 11:40:51 +0000900 CXXScopeSpec SS;
901 SS.setScopeRep(Tok.getAnnotationValue());
902 SS.setRange(Tok.getAnnotationRange());
903
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000904 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000905 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000906 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000907 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000908 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000909 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000910
911 // C++ [class.qual]p2:
912 // In a lookup in which the constructor is an acceptable lookup
913 // result and the nested-name-specifier nominates a class C:
914 //
915 // - if the name specified after the
916 // nested-name-specifier, when looked up in C, is the
917 // injected-class-name of C (Clause 9), or
918 //
919 // - if the name specified after the nested-name-specifier
920 // is the same as the identifier or the
921 // simple-template-id's template-name in the last
922 // component of the nested-name-specifier,
923 //
924 // the name is instead considered to name the constructor of
925 // class C.
926 //
927 // Thus, if the template-name is actually the constructor
928 // name, then the code is ill-formed; this interpretation is
929 // reinforced by the NAD status of core issue 635.
930 TemplateIdAnnotation *TemplateId
931 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +0000932 if ((DSContext == DSC_top_level ||
933 (DSContext == DSC_class && DS.isFriendSpecified())) &&
934 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000935 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000936 if (isConstructorDeclarator()) {
937 // The user meant this to be an out-of-line constructor
938 // definition, but template arguments are not allowed
939 // there. Just allow this as a constructor; we'll
940 // complain about it later.
941 goto DoneWithDeclSpec;
942 }
943
944 // The user meant this to name a type, but it actually names
945 // a constructor with some extraneous template
946 // arguments. Complain, then parse it as a type as the user
947 // intended.
948 Diag(TemplateId->TemplateNameLoc,
949 diag::err_out_of_line_template_id_names_constructor)
950 << TemplateId->Name;
951 }
952
John McCallaa87d332009-12-12 11:40:51 +0000953 DS.getTypeSpecScope() = SS;
954 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +0000955 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000956 "ParseOptionalCXXScopeSpecifier not working");
957 AnnotateTemplateIdTokenAsType(&SS);
958 continue;
959 }
960
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000961 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +0000962 DS.getTypeSpecScope() = SS;
963 ConsumeToken(); // The C++ scope.
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000964 if (Tok.getAnnotationValue())
965 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
966 PrevSpec, DiagID,
967 Tok.getAnnotationValue());
968 else
969 DS.SetTypeSpecError();
970 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
971 ConsumeToken(); // The typename
972 }
973
Douglas Gregor9135c722009-03-25 15:40:00 +0000974 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000975 goto DoneWithDeclSpec;
976
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000977 // If we're in a context where the identifier could be a class name,
978 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +0000979 if ((DSContext == DSC_top_level ||
980 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000981 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000982 &SS)) {
983 if (isConstructorDeclarator())
984 goto DoneWithDeclSpec;
985
986 // As noted in C++ [class.qual]p2 (cited above), when the name
987 // of the class is qualified in a context where it could name
988 // a constructor, its a constructor name. However, we've
989 // looked at the declarator, and the user probably meant this
990 // to be a type. Complain that it isn't supposed to be treated
991 // as a type, then proceed to parse it as a type.
992 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
993 << Next.getIdentifierInfo();
994 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000995
Douglas Gregorb696ea32009-02-04 17:00:24 +0000996 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
Douglas Gregor23c94db2010-07-02 17:43:08 +0000997 Next.getLocation(), getCurScope(), &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000998
Chris Lattnerf4382f52009-04-14 22:17:06 +0000999 // If the referenced identifier is not a type, then this declspec is
1000 // erroneous: We already checked about that it has no type specifier, and
1001 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001002 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001003 if (TypeRep == 0) {
1004 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001005 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001006 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001007 }
Mike Stump1eb44332009-09-09 15:08:12 +00001008
John McCallaa87d332009-12-12 11:40:51 +00001009 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001010 ConsumeToken(); // The C++ scope.
1011
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001012 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001013 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001014 if (isInvalid)
1015 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001017 DS.SetRangeEnd(Tok.getLocation());
1018 ConsumeToken(); // The typename.
1019
1020 continue;
1021 }
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Chris Lattner80d0c892009-01-21 19:48:37 +00001023 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001024 if (Tok.getAnnotationValue())
1025 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001026 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001027 else
1028 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001029
1030 if (isInvalid)
1031 break;
1032
Chris Lattner80d0c892009-01-21 19:48:37 +00001033 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1034 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Chris Lattner80d0c892009-01-21 19:48:37 +00001036 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1037 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1038 // Objective-C interface. If we don't have Objective-C or a '<', this is
1039 // just a normal reference to a typedef name.
1040 if (!Tok.is(tok::less) || !getLang().ObjC1)
1041 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001043 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001044 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001045 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1046 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1047 LAngleLoc, EndProtoLoc);
1048 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1049 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Chris Lattner80d0c892009-01-21 19:48:37 +00001051 DS.SetRangeEnd(EndProtoLoc);
1052 continue;
1053 }
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Chris Lattner3bd934a2008-07-26 01:18:38 +00001055 // typedef-name
1056 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001057 // In C++, check to see if this is a scope specifier like foo::bar::, if
1058 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001059 if (getLang().CPlusPlus) {
1060 if (TryAnnotateCXXScopeToken(true)) {
1061 if (!DS.hasTypeSpecifier())
1062 DS.SetTypeSpecError();
1063 goto DoneWithDeclSpec;
1064 }
1065 if (!Tok.is(tok::identifier))
1066 continue;
1067 }
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Chris Lattner3bd934a2008-07-26 01:18:38 +00001069 // This identifier can only be a typedef name if we haven't already seen
1070 // a type-specifier. Without this check we misparse:
1071 // typedef int X; struct Y { short X; }; as 'short int'.
1072 if (DS.hasTypeSpecifier())
1073 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001074
John Thompson82287d12010-02-05 00:12:22 +00001075 // Check for need to substitute AltiVec keyword tokens.
1076 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1077 break;
1078
Chris Lattner3bd934a2008-07-26 01:18:38 +00001079 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +00001080 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor23c94db2010-07-02 17:43:08 +00001081 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001082
Chris Lattnerc199ab32009-04-12 20:42:31 +00001083 // If this is not a typedef name, don't parse it as part of the declspec,
1084 // it must be an implicit int or an error.
1085 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001086 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001087 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001088 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001089
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001090 // If we're in a context where the identifier could be a class name,
1091 // check whether this is a constructor declaration.
1092 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001093 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001094 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001095 goto DoneWithDeclSpec;
1096
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001097 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001098 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001099 if (isInvalid)
1100 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Chris Lattner3bd934a2008-07-26 01:18:38 +00001102 DS.SetRangeEnd(Tok.getLocation());
1103 ConsumeToken(); // The identifier
1104
1105 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1106 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1107 // Objective-C interface. If we don't have Objective-C or a '<', this is
1108 // just a normal reference to a typedef name.
1109 if (!Tok.is(tok::less) || !getLang().ObjC1)
1110 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001112 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001113 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001114 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1115 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1116 LAngleLoc, EndProtoLoc);
1117 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1118 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Chris Lattner3bd934a2008-07-26 01:18:38 +00001120 DS.SetRangeEnd(EndProtoLoc);
1121
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001122 // Need to support trailing type qualifiers (e.g. "id<p> const").
1123 // If a type specifier follows, it will be diagnosed elsewhere.
1124 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001125 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001126
1127 // type-name
1128 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001129 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001130 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001131 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001132 // This template-id does not refer to a type name, so we're
1133 // done with the type-specifiers.
1134 goto DoneWithDeclSpec;
1135 }
1136
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001137 // If we're in a context where the template-id could be a
1138 // constructor name or specialization, check whether this is a
1139 // constructor declaration.
1140 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001141 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001142 isConstructorDeclarator())
1143 goto DoneWithDeclSpec;
1144
Douglas Gregor39a8de12009-02-25 19:37:18 +00001145 // Turn the template-id annotation token into a type annotation
1146 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001147 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001148 continue;
1149 }
1150
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 // GNU attributes support.
1152 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001153 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001155
1156 // Microsoft declspec support.
1157 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001158 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001159 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Steve Naroff239f0732008-12-25 14:16:32 +00001161 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001162 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001163 // FIXME: Add handling here!
1164 break;
1165
1166 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001167 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001168 case tok::kw___cdecl:
1169 case tok::kw___stdcall:
1170 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001171 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001172 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1173 continue;
1174
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 // storage-class-specifier
1176 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001177 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1178 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 break;
1180 case tok::kw_extern:
1181 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001182 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001183 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1184 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001185 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001186 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001187 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001188 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001189 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 case tok::kw_static:
1191 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001192 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001193 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1194 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 break;
1196 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001197 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001198 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1199 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001200 else
John McCallfec54012009-08-03 20:12:06 +00001201 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1202 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001203 break;
1204 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001205 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1206 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001208 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001209 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1210 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001211 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001213 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 // function-specifier
1217 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001218 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001220 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001221 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001222 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001223 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001224 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001225 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001226
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001227 // friend
1228 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001229 if (DSContext == DSC_class)
1230 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1231 else {
1232 PrevSpec = ""; // not actually used by the diagnostic
1233 DiagID = diag::err_friend_invalid_in_context;
1234 isInvalid = true;
1235 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001236 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Sebastian Redl2ac67232009-11-05 15:47:02 +00001238 // constexpr
1239 case tok::kw_constexpr:
1240 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1241 break;
1242
Chris Lattner80d0c892009-01-21 19:48:37 +00001243 // type-specifier
1244 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001245 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1246 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001247 break;
1248 case tok::kw_long:
1249 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001250 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1251 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001252 else
John McCallfec54012009-08-03 20:12:06 +00001253 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1254 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001255 break;
1256 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001257 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1258 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001259 break;
1260 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001261 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1262 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001263 break;
1264 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001265 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1266 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001267 break;
1268 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001269 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1270 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001271 break;
1272 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001273 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1274 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001275 break;
1276 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001277 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1278 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001279 break;
1280 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001281 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1282 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001283 break;
1284 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001285 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1286 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001287 break;
1288 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001289 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1290 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001291 break;
1292 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001293 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1294 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001295 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001296 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001297 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1298 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001299 break;
1300 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001301 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1302 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001303 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001304 case tok::kw_bool:
1305 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001306 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1307 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001308 break;
1309 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001310 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1311 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001312 break;
1313 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001314 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1315 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001316 break;
1317 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001318 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1319 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001320 break;
John Thompson82287d12010-02-05 00:12:22 +00001321 case tok::kw___vector:
1322 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1323 break;
1324 case tok::kw___pixel:
1325 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1326 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001327
1328 // class-specifier:
1329 case tok::kw_class:
1330 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001331 case tok::kw_union: {
1332 tok::TokenKind Kind = Tok.getKind();
1333 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001334 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001335 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001336 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001337
1338 // enum-specifier:
1339 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001340 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001341 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001342 continue;
1343
1344 // cv-qualifier:
1345 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001346 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1347 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001348 break;
1349 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001350 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1351 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001352 break;
1353 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001354 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1355 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001356 break;
1357
Douglas Gregord57959a2009-03-27 23:10:48 +00001358 // C++ typename-specifier:
1359 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001360 if (TryAnnotateTypeOrScopeToken()) {
1361 DS.SetTypeSpecError();
1362 goto DoneWithDeclSpec;
1363 }
1364 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001365 continue;
1366 break;
1367
Chris Lattner80d0c892009-01-21 19:48:37 +00001368 // GNU typeof support.
1369 case tok::kw_typeof:
1370 ParseTypeofSpecifier(DS);
1371 continue;
1372
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001373 case tok::kw_decltype:
1374 ParseDecltypeSpecifier(DS);
1375 continue;
1376
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001377 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001378 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001379 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1380 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001381 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001382 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Chris Lattnerbce61352008-07-26 00:20:22 +00001384 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001385 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001386 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001387 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1388 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1389 LAngleLoc, EndProtoLoc);
1390 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1391 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001392 DS.SetRangeEnd(EndProtoLoc);
1393
Chris Lattner1ab3b962008-11-18 07:48:38 +00001394 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregor849b2432010-03-31 17:46:05 +00001395 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001396 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001397 // Need to support trailing type qualifiers (e.g. "id<p> const").
1398 // If a type specifier follows, it will be diagnosed elsewhere.
1399 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001400 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 }
John McCallfec54012009-08-03 20:12:06 +00001402 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001403 if (isInvalid) {
1404 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001405 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001406 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001407 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001408 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001409 ConsumeToken();
1410 }
1411}
Douglas Gregoradcac882008-12-01 23:54:00 +00001412
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001413/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001414/// primarily follow the C++ grammar with additions for C99 and GNU,
1415/// which together subsume the C grammar. Note that the C++
1416/// type-specifier also includes the C type-qualifier (for const,
1417/// volatile, and C99 restrict). Returns true if a type-specifier was
1418/// found (and parsed), false otherwise.
1419///
1420/// type-specifier: [C++ 7.1.5]
1421/// simple-type-specifier
1422/// class-specifier
1423/// enum-specifier
1424/// elaborated-type-specifier [TODO]
1425/// cv-qualifier
1426///
1427/// cv-qualifier: [C++ 7.1.5.1]
1428/// 'const'
1429/// 'volatile'
1430/// [C99] 'restrict'
1431///
1432/// simple-type-specifier: [ C++ 7.1.5.2]
1433/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1434/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1435/// 'char'
1436/// 'wchar_t'
1437/// 'bool'
1438/// 'short'
1439/// 'int'
1440/// 'long'
1441/// 'signed'
1442/// 'unsigned'
1443/// 'float'
1444/// 'double'
1445/// 'void'
1446/// [C99] '_Bool'
1447/// [C99] '_Complex'
1448/// [C99] '_Imaginary' // Removed in TC2?
1449/// [GNU] '_Decimal32'
1450/// [GNU] '_Decimal64'
1451/// [GNU] '_Decimal128'
1452/// [GNU] typeof-specifier
1453/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1454/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001455/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001456/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001457bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001458 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001459 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001460 const ParsedTemplateInfo &TemplateInfo,
1461 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001462 SourceLocation Loc = Tok.getLocation();
1463
1464 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001465 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001466 // If we already have a type specifier, this identifier is not a type.
1467 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1468 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1469 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1470 return false;
John Thompson82287d12010-02-05 00:12:22 +00001471 // Check for need to substitute AltiVec keyword tokens.
1472 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1473 break;
1474 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001475 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001476 // Annotate typenames and C++ scope specifiers. If we get one, just
1477 // recurse to handle whatever we get.
1478 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001479 return true;
1480 if (Tok.is(tok::identifier))
1481 return false;
1482 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1483 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001484 case tok::coloncolon: // ::foo::bar
1485 if (NextToken().is(tok::kw_new) || // ::new
1486 NextToken().is(tok::kw_delete)) // ::delete
1487 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Chris Lattner166a8fc2009-01-04 23:41:41 +00001489 // Annotate typenames and C++ scope specifiers. If we get one, just
1490 // recurse to handle whatever we get.
1491 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001492 return true;
1493 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1494 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Douglas Gregor12e083c2008-11-07 15:42:26 +00001496 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001497 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001498 if (Tok.getAnnotationValue())
1499 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001500 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001501 else
1502 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001503 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1504 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Douglas Gregor12e083c2008-11-07 15:42:26 +00001506 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1507 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1508 // Objective-C interface. If we don't have Objective-C or a '<', this is
1509 // just a normal reference to a typedef name.
1510 if (!Tok.is(tok::less) || !getLang().ObjC1)
1511 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001513 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001514 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001515 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1516 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1517 LAngleLoc, EndProtoLoc);
1518 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1519 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Douglas Gregor12e083c2008-11-07 15:42:26 +00001521 DS.SetRangeEnd(EndProtoLoc);
1522 return true;
1523 }
1524
1525 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001526 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001527 break;
1528 case tok::kw_long:
1529 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001530 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1531 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001532 else
John McCallfec54012009-08-03 20:12:06 +00001533 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1534 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001535 break;
1536 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001537 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001538 break;
1539 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001540 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1541 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001542 break;
1543 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001544 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1545 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001546 break;
1547 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001548 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1549 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001550 break;
1551 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001552 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001553 break;
1554 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001555 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001556 break;
1557 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001558 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001559 break;
1560 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001561 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001562 break;
1563 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001564 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001565 break;
1566 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001567 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001568 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001569 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001570 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001571 break;
1572 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001573 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001574 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001575 case tok::kw_bool:
1576 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001577 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001578 break;
1579 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001580 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1581 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001582 break;
1583 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001584 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1585 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001586 break;
1587 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001588 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1589 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001590 break;
John Thompson82287d12010-02-05 00:12:22 +00001591 case tok::kw___vector:
1592 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1593 break;
1594 case tok::kw___pixel:
1595 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1596 break;
1597
Douglas Gregor12e083c2008-11-07 15:42:26 +00001598 // class-specifier:
1599 case tok::kw_class:
1600 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001601 case tok::kw_union: {
1602 tok::TokenKind Kind = Tok.getKind();
1603 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001604 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1605 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001606 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001607 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001608
1609 // enum-specifier:
1610 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001611 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001612 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001613 return true;
1614
1615 // cv-qualifier:
1616 case tok::kw_const:
1617 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001618 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001619 break;
1620 case tok::kw_volatile:
1621 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001622 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001623 break;
1624 case tok::kw_restrict:
1625 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001626 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001627 break;
1628
1629 // GNU typeof support.
1630 case tok::kw_typeof:
1631 ParseTypeofSpecifier(DS);
1632 return true;
1633
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001634 // C++0x decltype support.
1635 case tok::kw_decltype:
1636 ParseDecltypeSpecifier(DS);
1637 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001639 // C++0x auto support.
1640 case tok::kw_auto:
1641 if (!getLang().CPlusPlus0x)
1642 return false;
1643
John McCallfec54012009-08-03 20:12:06 +00001644 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001645 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001646 case tok::kw___ptr64:
1647 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001648 case tok::kw___cdecl:
1649 case tok::kw___stdcall:
1650 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001651 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001652 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001653 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001654
Douglas Gregor12e083c2008-11-07 15:42:26 +00001655 default:
1656 // Not a type-specifier; do nothing.
1657 return false;
1658 }
1659
1660 // If the specifier combination wasn't legal, issue a diagnostic.
1661 if (isInvalid) {
1662 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001663 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001664 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001665 }
1666 DS.SetRangeEnd(Tok.getLocation());
1667 ConsumeToken(); // whatever we parsed above.
1668 return true;
1669}
Reid Spencer5f016e22007-07-11 17:01:13 +00001670
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001671/// ParseStructDeclaration - Parse a struct declaration without the terminating
1672/// semicolon.
1673///
Reid Spencer5f016e22007-07-11 17:01:13 +00001674/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001675/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001676/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001677/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001678/// struct-declarator-list:
1679/// struct-declarator
1680/// struct-declarator-list ',' struct-declarator
1681/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1682/// struct-declarator:
1683/// declarator
1684/// [GNU] declarator attributes[opt]
1685/// declarator[opt] ':' constant-expression
1686/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1687///
Chris Lattnere1359422008-04-10 06:46:29 +00001688void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001689ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001690 if (Tok.is(tok::kw___extension__)) {
1691 // __extension__ silences extension warnings in the subexpression.
1692 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001693 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001694 return ParseStructDeclaration(DS, Fields);
1695 }
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Steve Naroff28a7ca82007-08-20 22:28:22 +00001697 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001698 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001699 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001701 // If there are no declarators, this is a free-standing declaration
1702 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001703 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001704 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001705 return;
1706 }
1707
1708 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001709 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001710 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001711 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001712 FieldDeclarator DeclaratorInfo(DS);
1713
1714 // Attributes are only allowed here on successive declarators.
1715 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1716 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001717 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001718 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1719 }
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Steve Naroff28a7ca82007-08-20 22:28:22 +00001721 /// struct-declarator: declarator
1722 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001723 if (Tok.isNot(tok::colon)) {
1724 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1725 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001726 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001727 }
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Chris Lattner04d66662007-10-09 17:33:22 +00001729 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001730 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001731 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001732 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001733 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001734 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001735 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001736 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001737
Steve Naroff28a7ca82007-08-20 22:28:22 +00001738 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001739 if (Tok.is(tok::kw___attribute)) {
1740 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001741 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001742 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1743 }
1744
John McCallbdd563e2009-11-03 02:38:08 +00001745 // We're done with this declarator; invoke the callback.
John McCall54abf7d2009-11-04 02:18:39 +00001746 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1747 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001748
Steve Naroff28a7ca82007-08-20 22:28:22 +00001749 // If we don't have a comma, it is either the end of the list (a ';')
1750 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001751 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001752 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001753
Steve Naroff28a7ca82007-08-20 22:28:22 +00001754 // Consume the comma.
1755 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001756
John McCallbdd563e2009-11-03 02:38:08 +00001757 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001758 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001759}
1760
1761/// ParseStructUnionBody
1762/// struct-contents:
1763/// struct-declaration-list
1764/// [EXT] empty
1765/// [GNU] "struct-declaration-list" without terminatoring ';'
1766/// struct-declaration-list:
1767/// struct-declaration
1768/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001769/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001770///
Reid Spencer5f016e22007-07-11 17:01:13 +00001771void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001772 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001773 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1774 PP.getSourceManager(),
1775 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001776
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001779 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001780 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00001781
Reid Spencer5f016e22007-07-11 17:01:13 +00001782 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1783 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001784 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00001785 Diag(Tok, diag::ext_empty_struct_union)
1786 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00001787
Chris Lattnerb28317a2009-03-28 19:18:32 +00001788 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001789
Reid Spencer5f016e22007-07-11 17:01:13 +00001790 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001791 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001792 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Reid Spencer5f016e22007-07-11 17:01:13 +00001794 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001795 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001796 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00001797 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00001798 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001799 ConsumeToken();
1800 continue;
1801 }
Chris Lattnere1359422008-04-10 06:46:29 +00001802
1803 // Parse all the comma separated declarators.
1804 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001805
John McCallbdd563e2009-11-03 02:38:08 +00001806 if (!Tok.is(tok::at)) {
1807 struct CFieldCallback : FieldCallback {
1808 Parser &P;
1809 DeclPtrTy TagDecl;
1810 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1811
1812 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1813 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1814 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1815
1816 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001817 // Install the declarator into the current TagDecl.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001818 DeclPtrTy Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00001819 FD.D.getDeclSpec().getSourceRange().getBegin(),
1820 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001821 FieldDecls.push_back(Field);
1822 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001823 }
John McCallbdd563e2009-11-03 02:38:08 +00001824 } Callback(*this, TagDecl, FieldDecls);
1825
1826 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001827 } else { // Handle @defs
1828 ConsumeToken();
1829 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1830 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001831 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001832 continue;
1833 }
1834 ConsumeToken();
1835 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1836 if (!Tok.is(tok::identifier)) {
1837 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001838 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001839 continue;
1840 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001841 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001842 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001843 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001844 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1845 ConsumeToken();
1846 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001847 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001848
Chris Lattner04d66662007-10-09 17:33:22 +00001849 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001850 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001851 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001852 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001853 break;
1854 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001855 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1856 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001857 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001858 // If we stopped at a ';', eat it.
1859 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001860 }
1861 }
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Steve Naroff60fccee2007-10-29 21:38:07 +00001863 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Ted Kremenek1e377652010-02-11 02:19:13 +00001865 llvm::OwningPtr<AttributeList> AttrList;
Reid Spencer5f016e22007-07-11 17:01:13 +00001866 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001867 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001868 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001869
Douglas Gregor23c94db2010-07-02 17:43:08 +00001870 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001871 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001872 LBraceLoc, RBraceLoc,
Ted Kremenek1e377652010-02-11 02:19:13 +00001873 AttrList.get());
Douglas Gregor72de6672009-01-08 20:45:30 +00001874 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001875 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001876}
1877
1878
1879/// ParseEnumSpecifier
1880/// enum-specifier: [C99 6.7.2.2]
1881/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001882///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001883/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1884/// '}' attributes[opt]
1885/// 'enum' identifier
1886/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001887///
1888/// [C++] elaborated-type-specifier:
1889/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1890///
Chris Lattner4c97d762009-04-12 21:49:30 +00001891void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001892 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00001893 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001894 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001895 if (Tok.is(tok::code_completion)) {
1896 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001897 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00001898 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00001899 }
1900
Ted Kremenek1e377652010-02-11 02:19:13 +00001901 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001902 // If attributes exist after tag, parse them.
1903 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001904 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001905
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001906 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00001907 if (getLang().CPlusPlus) {
1908 if (ParseOptionalCXXScopeSpecifier(SS, 0, false))
1909 return;
1910
1911 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001912 Diag(Tok, diag::err_expected_ident);
1913 if (Tok.isNot(tok::l_brace)) {
1914 // Has no name and is not a definition.
1915 // Skip the rest of this declarator, up until the comma or semicolon.
1916 SkipUntil(tok::comma, true);
1917 return;
1918 }
1919 }
1920 }
Mike Stump1eb44332009-09-09 15:08:12 +00001921
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001922 // Must have either 'enum name' or 'enum {...}'.
1923 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1924 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001925
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001926 // Skip the rest of this declarator, up until the comma or semicolon.
1927 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001928 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001929 }
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001931 // If an identifier is present, consume and remember it.
1932 IdentifierInfo *Name = 0;
1933 SourceLocation NameLoc;
1934 if (Tok.is(tok::identifier)) {
1935 Name = Tok.getIdentifierInfo();
1936 NameLoc = ConsumeToken();
1937 }
Mike Stump1eb44332009-09-09 15:08:12 +00001938
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001939 // There are three options here. If we have 'enum foo;', then this is a
1940 // forward declaration. If we have 'enum foo {...' then this is a
1941 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1942 //
1943 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1944 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1945 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1946 //
John McCall0f434ec2009-07-31 02:45:11 +00001947 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001948 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001949 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001950 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001951 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001952 else
John McCall0f434ec2009-07-31 02:45:11 +00001953 TUK = Action::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00001954
1955 // enums cannot be templates, although they can be referenced from a
1956 // template.
1957 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
1958 TUK != Action::TUK_Reference) {
1959 Diag(Tok, diag::err_enum_template);
1960
1961 // Skip the rest of this declarator, up until the comma or semicolon.
1962 SkipUntil(tok::comma, true);
1963 return;
1964 }
1965
Douglas Gregor402abb52009-05-28 23:31:59 +00001966 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001967 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00001968 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
1969 const char *PrevSpec = 0;
1970 unsigned DiagID;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001971 DeclPtrTy TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
Ted Kremenek1e377652010-02-11 02:19:13 +00001972 StartLoc, SS, Name, NameLoc, Attr.get(),
1973 AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001974 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001975 Owned, IsDependent);
Douglas Gregor48c89f42010-04-24 16:38:41 +00001976 if (IsDependent) {
1977 // This enum has a dependent nested-name-specifier. Handle it as a
1978 // dependent tag.
1979 if (!Name) {
1980 DS.SetTypeSpecError();
1981 Diag(Tok, diag::err_expected_type_name_after_typename);
1982 return;
1983 }
1984
Douglas Gregor23c94db2010-07-02 17:43:08 +00001985 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00001986 TUK, SS, Name, StartLoc,
1987 NameLoc);
1988 if (Type.isInvalid()) {
1989 DS.SetTypeSpecError();
1990 return;
1991 }
1992
1993 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
1994 Type.get(), false))
1995 Diag(StartLoc, DiagID) << PrevSpec;
1996
1997 return;
1998 }
Mike Stump1eb44332009-09-09 15:08:12 +00001999
Douglas Gregor48c89f42010-04-24 16:38:41 +00002000 if (!TagDecl.get()) {
2001 // The action failed to produce an enumeration tag. If this is a
2002 // definition, consume the entire definition.
2003 if (Tok.is(tok::l_brace)) {
2004 ConsumeBrace();
2005 SkipUntil(tok::r_brace);
2006 }
2007
2008 DS.SetTypeSpecError();
2009 return;
2010 }
2011
Chris Lattner04d66662007-10-09 17:33:22 +00002012 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002013 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002014
Douglas Gregorb988f9c2010-01-25 16:33:23 +00002015 // FIXME: The DeclSpec should keep the locations of both the keyword and the
2016 // name (if there is one).
Douglas Gregorb988f9c2010-01-25 16:33:23 +00002017 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00002018 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00002019 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002020}
2021
2022/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2023/// enumerator-list:
2024/// enumerator
2025/// enumerator-list ',' enumerator
2026/// enumerator:
2027/// enumeration-constant
2028/// enumeration-constant '=' constant-expression
2029/// enumeration-constant:
2030/// identifier
2031///
Chris Lattnerb28317a2009-03-28 19:18:32 +00002032void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002033 // Enter the scope of the enum body and start the definition.
2034 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002035 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002036
Reid Spencer5f016e22007-07-11 17:01:13 +00002037 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002038
Chris Lattner7946dd32007-08-27 17:24:30 +00002039 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002040 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002041 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002042
Chris Lattnerb28317a2009-03-28 19:18:32 +00002043 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002044
Chris Lattnerb28317a2009-03-28 19:18:32 +00002045 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002046
Reid Spencer5f016e22007-07-11 17:01:13 +00002047 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002048 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002049 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2050 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002051
Reid Spencer5f016e22007-07-11 17:01:13 +00002052 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002053 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00002054 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002055 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002056 AssignedVal = ParseConstantExpression();
2057 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002058 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002059 }
Mike Stump1eb44332009-09-09 15:08:12 +00002060
Reid Spencer5f016e22007-07-11 17:01:13 +00002061 // Install the enumerator constant into EnumDecl.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002062 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002063 LastEnumConstDecl,
2064 IdentLoc, Ident,
2065 EqualLoc,
2066 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002067 EnumConstantDecls.push_back(EnumConstDecl);
2068 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Chris Lattner04d66662007-10-09 17:33:22 +00002070 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002071 break;
2072 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002073
2074 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002075 !(getLang().C99 || getLang().CPlusPlus0x))
2076 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2077 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002078 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002079 }
Mike Stump1eb44332009-09-09 15:08:12 +00002080
Reid Spencer5f016e22007-07-11 17:01:13 +00002081 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002082 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002083
Ted Kremenek1e377652010-02-11 02:19:13 +00002084 llvm::OwningPtr<AttributeList> Attr;
Reid Spencer5f016e22007-07-11 17:01:13 +00002085 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00002086 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00002087 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00002088
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002089 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2090 EnumConstantDecls.data(), EnumConstantDecls.size(),
Douglas Gregor23c94db2010-07-02 17:43:08 +00002091 getCurScope(), Attr.get());
Mike Stump1eb44332009-09-09 15:08:12 +00002092
Douglas Gregor72de6672009-01-08 20:45:30 +00002093 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002094 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002095}
2096
2097/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002098/// start of a type-qualifier-list.
2099bool Parser::isTypeQualifier() const {
2100 switch (Tok.getKind()) {
2101 default: return false;
2102 // type-qualifier
2103 case tok::kw_const:
2104 case tok::kw_volatile:
2105 case tok::kw_restrict:
2106 return true;
2107 }
2108}
2109
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002110/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2111/// is definitely a type-specifier. Return false if it isn't part of a type
2112/// specifier or if we're not sure.
2113bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2114 switch (Tok.getKind()) {
2115 default: return false;
2116 // type-specifiers
2117 case tok::kw_short:
2118 case tok::kw_long:
2119 case tok::kw_signed:
2120 case tok::kw_unsigned:
2121 case tok::kw__Complex:
2122 case tok::kw__Imaginary:
2123 case tok::kw_void:
2124 case tok::kw_char:
2125 case tok::kw_wchar_t:
2126 case tok::kw_char16_t:
2127 case tok::kw_char32_t:
2128 case tok::kw_int:
2129 case tok::kw_float:
2130 case tok::kw_double:
2131 case tok::kw_bool:
2132 case tok::kw__Bool:
2133 case tok::kw__Decimal32:
2134 case tok::kw__Decimal64:
2135 case tok::kw__Decimal128:
2136 case tok::kw___vector:
2137
2138 // struct-or-union-specifier (C99) or class-specifier (C++)
2139 case tok::kw_class:
2140 case tok::kw_struct:
2141 case tok::kw_union:
2142 // enum-specifier
2143 case tok::kw_enum:
2144
2145 // typedef-name
2146 case tok::annot_typename:
2147 return true;
2148 }
2149}
2150
Steve Naroff5f8aa692008-02-11 23:15:56 +00002151/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002152/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002153bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002154 switch (Tok.getKind()) {
2155 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002156
Chris Lattner166a8fc2009-01-04 23:41:41 +00002157 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002158 if (TryAltiVecVectorToken())
2159 return true;
2160 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002161 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002162 // Annotate typenames and C++ scope specifiers. If we get one, just
2163 // recurse to handle whatever we get.
2164 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002165 return true;
2166 if (Tok.is(tok::identifier))
2167 return false;
2168 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002169
Chris Lattner166a8fc2009-01-04 23:41:41 +00002170 case tok::coloncolon: // ::foo::bar
2171 if (NextToken().is(tok::kw_new) || // ::new
2172 NextToken().is(tok::kw_delete)) // ::delete
2173 return false;
2174
Chris Lattner166a8fc2009-01-04 23:41:41 +00002175 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002176 return true;
2177 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002178
Reid Spencer5f016e22007-07-11 17:01:13 +00002179 // GNU attributes support.
2180 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002181 // GNU typeof support.
2182 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002183
Reid Spencer5f016e22007-07-11 17:01:13 +00002184 // type-specifiers
2185 case tok::kw_short:
2186 case tok::kw_long:
2187 case tok::kw_signed:
2188 case tok::kw_unsigned:
2189 case tok::kw__Complex:
2190 case tok::kw__Imaginary:
2191 case tok::kw_void:
2192 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002193 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002194 case tok::kw_char16_t:
2195 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002196 case tok::kw_int:
2197 case tok::kw_float:
2198 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002199 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002200 case tok::kw__Bool:
2201 case tok::kw__Decimal32:
2202 case tok::kw__Decimal64:
2203 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002204 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002205
Chris Lattner99dc9142008-04-13 18:59:07 +00002206 // struct-or-union-specifier (C99) or class-specifier (C++)
2207 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002208 case tok::kw_struct:
2209 case tok::kw_union:
2210 // enum-specifier
2211 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002212
Reid Spencer5f016e22007-07-11 17:01:13 +00002213 // type-qualifier
2214 case tok::kw_const:
2215 case tok::kw_volatile:
2216 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002217
2218 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002219 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002220 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002221
Chris Lattner7c186be2008-10-20 00:25:30 +00002222 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2223 case tok::less:
2224 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002225
Steve Naroff239f0732008-12-25 14:16:32 +00002226 case tok::kw___cdecl:
2227 case tok::kw___stdcall:
2228 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002229 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002230 case tok::kw___w64:
2231 case tok::kw___ptr64:
2232 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002233 }
2234}
2235
2236/// isDeclarationSpecifier() - Return true if the current token is part of a
2237/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002238bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002239 switch (Tok.getKind()) {
2240 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002241
Chris Lattner166a8fc2009-01-04 23:41:41 +00002242 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002243 // Unfortunate hack to support "Class.factoryMethod" notation.
2244 if (getLang().ObjC1 && NextToken().is(tok::period))
2245 return false;
John Thompson82287d12010-02-05 00:12:22 +00002246 if (TryAltiVecVectorToken())
2247 return true;
2248 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002249 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002250 // Annotate typenames and C++ scope specifiers. If we get one, just
2251 // recurse to handle whatever we get.
2252 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002253 return true;
2254 if (Tok.is(tok::identifier))
2255 return false;
2256 return isDeclarationSpecifier();
2257
Chris Lattner166a8fc2009-01-04 23:41:41 +00002258 case tok::coloncolon: // ::foo::bar
2259 if (NextToken().is(tok::kw_new) || // ::new
2260 NextToken().is(tok::kw_delete)) // ::delete
2261 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002262
Chris Lattner166a8fc2009-01-04 23:41:41 +00002263 // Annotate typenames and C++ scope specifiers. If we get one, just
2264 // recurse to handle whatever we get.
2265 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002266 return true;
2267 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002268
Reid Spencer5f016e22007-07-11 17:01:13 +00002269 // storage-class-specifier
2270 case tok::kw_typedef:
2271 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002272 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002273 case tok::kw_static:
2274 case tok::kw_auto:
2275 case tok::kw_register:
2276 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002277
Reid Spencer5f016e22007-07-11 17:01:13 +00002278 // type-specifiers
2279 case tok::kw_short:
2280 case tok::kw_long:
2281 case tok::kw_signed:
2282 case tok::kw_unsigned:
2283 case tok::kw__Complex:
2284 case tok::kw__Imaginary:
2285 case tok::kw_void:
2286 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002287 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002288 case tok::kw_char16_t:
2289 case tok::kw_char32_t:
2290
Reid Spencer5f016e22007-07-11 17:01:13 +00002291 case tok::kw_int:
2292 case tok::kw_float:
2293 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002294 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002295 case tok::kw__Bool:
2296 case tok::kw__Decimal32:
2297 case tok::kw__Decimal64:
2298 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002299 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002300
Chris Lattner99dc9142008-04-13 18:59:07 +00002301 // struct-or-union-specifier (C99) or class-specifier (C++)
2302 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002303 case tok::kw_struct:
2304 case tok::kw_union:
2305 // enum-specifier
2306 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002307
Reid Spencer5f016e22007-07-11 17:01:13 +00002308 // type-qualifier
2309 case tok::kw_const:
2310 case tok::kw_volatile:
2311 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002312
Reid Spencer5f016e22007-07-11 17:01:13 +00002313 // function-specifier
2314 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002315 case tok::kw_virtual:
2316 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002317
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002318 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002319 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002320
Chris Lattner1ef08762007-08-09 17:01:07 +00002321 // GNU typeof support.
2322 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002323
Chris Lattner1ef08762007-08-09 17:01:07 +00002324 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002325 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002326 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002327
Chris Lattnerf3948c42008-07-26 03:38:44 +00002328 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2329 case tok::less:
2330 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Steve Naroff47f52092009-01-06 19:34:12 +00002332 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002333 case tok::kw___cdecl:
2334 case tok::kw___stdcall:
2335 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002336 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002337 case tok::kw___w64:
2338 case tok::kw___ptr64:
2339 case tok::kw___forceinline:
2340 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002341 }
2342}
2343
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002344bool Parser::isConstructorDeclarator() {
2345 TentativeParsingAction TPA(*this);
2346
2347 // Parse the C++ scope specifier.
2348 CXXScopeSpec SS;
John McCall9ba61662010-02-26 08:45:28 +00002349 if (ParseOptionalCXXScopeSpecifier(SS, 0, true)) {
2350 TPA.Revert();
2351 return false;
2352 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002353
2354 // Parse the constructor name.
2355 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2356 // We already know that we have a constructor name; just consume
2357 // the token.
2358 ConsumeToken();
2359 } else {
2360 TPA.Revert();
2361 return false;
2362 }
2363
2364 // Current class name must be followed by a left parentheses.
2365 if (Tok.isNot(tok::l_paren)) {
2366 TPA.Revert();
2367 return false;
2368 }
2369 ConsumeParen();
2370
2371 // A right parentheses or ellipsis signals that we have a constructor.
2372 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2373 TPA.Revert();
2374 return true;
2375 }
2376
2377 // If we need to, enter the specified scope.
2378 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002379 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002380 DeclScopeObj.EnterDeclaratorScope();
2381
2382 // Check whether the next token(s) are part of a declaration
2383 // specifier, in which case we have the start of a parameter and,
2384 // therefore, we know that this is a constructor.
2385 bool IsConstructor = isDeclarationSpecifier();
2386 TPA.Revert();
2387 return IsConstructor;
2388}
Reid Spencer5f016e22007-07-11 17:01:13 +00002389
2390/// ParseTypeQualifierListOpt
2391/// type-qualifier-list: [C99 6.7.5]
2392/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002393/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002394/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002395/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Sean Huntbbd37c62009-11-21 08:43:09 +00002396/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2397/// if CXX0XAttributesAllowed = true
Reid Spencer5f016e22007-07-11 17:01:13 +00002398///
Sean Huntbbd37c62009-11-21 08:43:09 +00002399void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2400 bool CXX0XAttributesAllowed) {
2401 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2402 SourceLocation Loc = Tok.getLocation();
2403 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2404 if (CXX0XAttributesAllowed)
2405 DS.AddAttributes(Attr.AttrList);
2406 else
2407 Diag(Loc, diag::err_attributes_not_allowed);
2408 }
2409
Reid Spencer5f016e22007-07-11 17:01:13 +00002410 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002411 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002412 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002413 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002414 SourceLocation Loc = Tok.getLocation();
2415
2416 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002417 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002418 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2419 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002420 break;
2421 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002422 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2423 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002424 break;
2425 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002426 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2427 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002428 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002429 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002430 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002431 case tok::kw___cdecl:
2432 case tok::kw___stdcall:
2433 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002434 case tok::kw___thiscall:
Sean Huntbbd37c62009-11-21 08:43:09 +00002435 if (GNUAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002436 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2437 continue;
2438 }
2439 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002440 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002441 if (GNUAttributesAllowed) {
2442 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002443 continue; // do *not* consume the next token!
2444 }
2445 // otherwise, FALL THROUGH!
2446 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002447 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002448 // If this is not a type-qualifier token, we're done reading type
2449 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002450 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002451 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002452 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002453
Reid Spencer5f016e22007-07-11 17:01:13 +00002454 // If the specifier combination wasn't legal, issue a diagnostic.
2455 if (isInvalid) {
2456 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002457 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002458 }
2459 ConsumeToken();
2460 }
2461}
2462
2463
2464/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2465///
2466void Parser::ParseDeclarator(Declarator &D) {
2467 /// This implements the 'declarator' production in the C grammar, then checks
2468 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002469 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002470}
2471
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002472/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2473/// is parsed by the function passed to it. Pass null, and the direct-declarator
2474/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002475/// ptr-operator production.
2476///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002477/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2478/// [C] pointer[opt] direct-declarator
2479/// [C++] direct-declarator
2480/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002481///
2482/// pointer: [C99 6.7.5]
2483/// '*' type-qualifier-list[opt]
2484/// '*' type-qualifier-list[opt] pointer
2485///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002486/// ptr-operator:
2487/// '*' cv-qualifier-seq[opt]
2488/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002489/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002490/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002491/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002492/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002493void Parser::ParseDeclaratorInternal(Declarator &D,
2494 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002495 if (Diags.hasAllExtensionsSilenced())
2496 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002497 // C++ member pointers start with a '::' or a nested-name.
2498 // Member pointers get special handling, since there's no place for the
2499 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002500 if (getLang().CPlusPlus &&
2501 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2502 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002503 CXXScopeSpec SS;
John McCall9ba61662010-02-26 08:45:28 +00002504 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true); // ignore fail
2505
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002506 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002507 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002508 // The scope spec really belongs to the direct-declarator.
2509 D.getCXXScopeSpec() = SS;
2510 if (DirectDeclParser)
2511 (this->*DirectDeclParser)(D);
2512 return;
2513 }
2514
2515 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002516 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002517 DeclSpec DS;
2518 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002519 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002520
2521 // Recurse to parse whatever is left.
2522 ParseDeclaratorInternal(D, DirectDeclParser);
2523
2524 // Sema will have to catch (syntactically invalid) pointers into global
2525 // scope. It has to catch pointers into namespace scope anyway.
2526 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002527 Loc, DS.TakeAttributes()),
2528 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002529 return;
2530 }
2531 }
2532
2533 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002534 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002535 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002536 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002537 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002538 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002539 if (DirectDeclParser)
2540 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002541 return;
2542 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002543
Sebastian Redl05532f22009-03-15 22:02:01 +00002544 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2545 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002546 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002547 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002548
Chris Lattner9af55002009-03-27 04:18:06 +00002549 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002550 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002551 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002552
Reid Spencer5f016e22007-07-11 17:01:13 +00002553 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002554 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002555
Reid Spencer5f016e22007-07-11 17:01:13 +00002556 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002557 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002558 if (Kind == tok::star)
2559 // Remember that we parsed a pointer type, and remember the type-quals.
2560 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002561 DS.TakeAttributes()),
2562 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002563 else
2564 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002565 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002566 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002567 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002568 } else {
2569 // Is a reference
2570 DeclSpec DS;
2571
Sebastian Redl743de1f2009-03-23 00:00:23 +00002572 // Complain about rvalue references in C++03, but then go on and build
2573 // the declarator.
2574 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2575 Diag(Loc, diag::err_rvalue_reference);
2576
Reid Spencer5f016e22007-07-11 17:01:13 +00002577 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2578 // cv-qualifiers are introduced through the use of a typedef or of a
2579 // template type argument, in which case the cv-qualifiers are ignored.
2580 //
2581 // [GNU] Retricted references are allowed.
2582 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002583 // [C++0x] Attributes on references are not allowed.
2584 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002585 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002586
2587 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2588 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2589 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002590 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002591 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2592 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002593 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002594 }
2595
2596 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002597 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002598
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002599 if (D.getNumTypeObjects() > 0) {
2600 // C++ [dcl.ref]p4: There shall be no references to references.
2601 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2602 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002603 if (const IdentifierInfo *II = D.getIdentifier())
2604 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2605 << II;
2606 else
2607 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2608 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002609
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002610 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002611 // can go ahead and build the (technically ill-formed)
2612 // declarator: reference collapsing will take care of it.
2613 }
2614 }
2615
Reid Spencer5f016e22007-07-11 17:01:13 +00002616 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002617 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002618 DS.TakeAttributes(),
2619 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002620 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002621 }
2622}
2623
2624/// ParseDirectDeclarator
2625/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002626/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002627/// '(' declarator ')'
2628/// [GNU] '(' attributes declarator ')'
2629/// [C90] direct-declarator '[' constant-expression[opt] ']'
2630/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2631/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2632/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2633/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2634/// direct-declarator '(' parameter-type-list ')'
2635/// direct-declarator '(' identifier-list[opt] ')'
2636/// [GNU] direct-declarator '(' parameter-forward-declarations
2637/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002638/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2639/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002640/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002641///
2642/// declarator-id: [C++ 8]
2643/// id-expression
2644/// '::'[opt] nested-name-specifier[opt] type-name
2645///
2646/// id-expression: [C++ 5.1]
2647/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002648/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002649///
2650/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002651/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002652/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002653/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002654/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002655/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002656///
Reid Spencer5f016e22007-07-11 17:01:13 +00002657void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002658 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002659
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002660 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2661 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002662 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002663 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2664 true);
John McCall9ba61662010-02-26 08:45:28 +00002665 }
2666
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002667 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002668 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00002669 // Change the declaration context for name lookup, until this function
2670 // is exited (and the declarator has been parsed).
2671 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002672 }
2673
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002674 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2675 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2676 // We found something that indicates the start of an unqualified-id.
2677 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00002678 bool AllowConstructorName;
2679 if (D.getDeclSpec().hasTypeSpecifier())
2680 AllowConstructorName = false;
2681 else if (D.getCXXScopeSpec().isSet())
2682 AllowConstructorName =
2683 (D.getContext() == Declarator::FileContext ||
2684 (D.getContext() == Declarator::MemberContext &&
2685 D.getDeclSpec().isFriendSpecified()));
2686 else
2687 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2688
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002689 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2690 /*EnteringContext=*/true,
2691 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002692 AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002693 /*ObjectType=*/0,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002694 D.getName()) ||
2695 // Once we're past the identifier, if the scope was bad, mark the
2696 // whole declarator bad.
2697 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002698 D.SetIdentifier(0, Tok.getLocation());
2699 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002700 } else {
2701 // Parsed the unqualified-id; update range information and move along.
2702 if (D.getSourceRange().getBegin().isInvalid())
2703 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2704 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002705 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002706 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002707 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002708 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002709 assert(!getLang().CPlusPlus &&
2710 "There's a C++-specific check for tok::identifier above");
2711 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2712 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2713 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002714 goto PastIdentifier;
2715 }
2716
2717 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002718 // direct-declarator: '(' declarator ')'
2719 // direct-declarator: '(' attributes declarator ')'
2720 // Example: 'char (*X)' or 'int (*XX)(void)'
2721 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002722
2723 // If the declarator was parenthesized, we entered the declarator
2724 // scope when parsing the parenthesized declarator, then exited
2725 // the scope already. Re-enter the scope, if we need to.
2726 if (D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002727 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002728 // Change the declaration context for name lookup, until this function
2729 // is exited (and the declarator has been parsed).
2730 DeclScopeObj.EnterDeclaratorScope();
2731 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002732 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002733 // This could be something simple like "int" (in which case the declarator
2734 // portion is empty), if an abstract-declarator is allowed.
2735 D.SetIdentifier(0, Tok.getLocation());
2736 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002737 if (D.getContext() == Declarator::MemberContext)
2738 Diag(Tok, diag::err_expected_member_name_or_semi)
2739 << D.getDeclSpec().getSourceRange();
2740 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002741 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002742 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002743 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002744 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002745 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002746 }
Mike Stump1eb44332009-09-09 15:08:12 +00002747
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002748 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002749 assert(D.isPastIdentifier() &&
2750 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002751
Sean Huntbbd37c62009-11-21 08:43:09 +00002752 // Don't parse attributes unless we have an identifier.
Douglas Gregor3c3aaf92010-02-19 16:47:56 +00002753 if (D.getIdentifier() && getLang().CPlusPlus0x
Sean Huntbbd37c62009-11-21 08:43:09 +00002754 && isCXX0XAttributeSpecifier(true)) {
2755 SourceLocation AttrEndLoc;
2756 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2757 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2758 }
2759
Reid Spencer5f016e22007-07-11 17:01:13 +00002760 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002761 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002762 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2763 // In such a case, check if we actually have a function declarator; if it
2764 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002765 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2766 // When not in file scope, warn for ambiguous function declarators, just
2767 // in case the author intended it as a variable definition.
2768 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2769 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2770 break;
2771 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002772 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002773 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002774 ParseBracketDeclarator(D);
2775 } else {
2776 break;
2777 }
2778 }
2779}
2780
Chris Lattneref4715c2008-04-06 05:45:57 +00002781/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2782/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002783/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002784/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2785///
2786/// direct-declarator:
2787/// '(' declarator ')'
2788/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002789/// direct-declarator '(' parameter-type-list ')'
2790/// direct-declarator '(' identifier-list[opt] ')'
2791/// [GNU] direct-declarator '(' parameter-forward-declarations
2792/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002793///
2794void Parser::ParseParenDeclarator(Declarator &D) {
2795 SourceLocation StartLoc = ConsumeParen();
2796 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002797
Chris Lattner7399ee02008-10-20 02:05:46 +00002798 // Eat any attributes before we look at whether this is a grouping or function
2799 // declarator paren. If this is a grouping paren, the attribute applies to
2800 // the type being built up, for example:
2801 // int (__attribute__(()) *x)(long y)
2802 // If this ends up not being a grouping paren, the attribute applies to the
2803 // first argument, for example:
2804 // int (__attribute__(()) int x)
2805 // In either case, we need to eat any attributes to be able to determine what
2806 // sort of paren this is.
2807 //
Ted Kremenek1e377652010-02-11 02:19:13 +00002808 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner7399ee02008-10-20 02:05:46 +00002809 bool RequiresArg = false;
2810 if (Tok.is(tok::kw___attribute)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002811 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +00002812
Chris Lattner7399ee02008-10-20 02:05:46 +00002813 // We require that the argument list (if this is a non-grouping paren) be
2814 // present even if the attribute list was empty.
2815 RequiresArg = true;
2816 }
Steve Naroff239f0732008-12-25 14:16:32 +00002817 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002818 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002819 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2820 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002821 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman290eeb02009-06-08 23:27:34 +00002822 }
Mike Stump1eb44332009-09-09 15:08:12 +00002823
Chris Lattneref4715c2008-04-06 05:45:57 +00002824 // If we haven't past the identifier yet (or where the identifier would be
2825 // stored, if this is an abstract declarator), then this is probably just
2826 // grouping parens. However, if this could be an abstract-declarator, then
2827 // this could also be the start of function arguments (consider 'void()').
2828 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002829
Chris Lattneref4715c2008-04-06 05:45:57 +00002830 if (!D.mayOmitIdentifier()) {
2831 // If this can't be an abstract-declarator, this *must* be a grouping
2832 // paren, because we haven't seen the identifier yet.
2833 isGrouping = true;
2834 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002835 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002836 isDeclarationSpecifier()) { // 'int(int)' is a function.
2837 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2838 // considered to be a type, not a K&R identifier-list.
2839 isGrouping = false;
2840 } else {
2841 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2842 isGrouping = true;
2843 }
Mike Stump1eb44332009-09-09 15:08:12 +00002844
Chris Lattneref4715c2008-04-06 05:45:57 +00002845 // If this is a grouping paren, handle:
2846 // direct-declarator: '(' declarator ')'
2847 // direct-declarator: '(' attributes declarator ')'
2848 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002849 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002850 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002851 if (AttrList)
Ted Kremenek1e377652010-02-11 02:19:13 +00002852 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002853
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002854 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002855 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002856 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002857
2858 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002859 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002860 return;
2861 }
Mike Stump1eb44332009-09-09 15:08:12 +00002862
Chris Lattneref4715c2008-04-06 05:45:57 +00002863 // Okay, if this wasn't a grouping paren, it must be the start of a function
2864 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002865 // identifier (and remember where it would have been), then call into
2866 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002867 D.SetIdentifier(0, Tok.getLocation());
2868
Ted Kremenek1e377652010-02-11 02:19:13 +00002869 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002870}
2871
2872/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2873/// declarator D up to a paren, which indicates that we are parsing function
2874/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002875///
Chris Lattner7399ee02008-10-20 02:05:46 +00002876/// If AttrList is non-null, then the caller parsed those arguments immediately
2877/// after the open paren - they should be considered to be the first argument of
2878/// a parameter. If RequiresArg is true, then the first argument of the
2879/// function is required to be present and required to not be an identifier
2880/// list.
2881///
Reid Spencer5f016e22007-07-11 17:01:13 +00002882/// This method also handles this portion of the grammar:
2883/// parameter-type-list: [C99 6.7.5]
2884/// parameter-list
2885/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002886/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002887///
2888/// parameter-list: [C99 6.7.5]
2889/// parameter-declaration
2890/// parameter-list ',' parameter-declaration
2891///
2892/// parameter-declaration: [C99 6.7.5]
2893/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002894/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002895/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002896/// declaration-specifiers abstract-declarator[opt]
2897/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002898/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002899/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2900///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002901/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002902/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002903///
Chris Lattner7399ee02008-10-20 02:05:46 +00002904void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2905 AttributeList *AttrList,
2906 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002907 // lparen is already consumed!
2908 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002909
Chris Lattner7399ee02008-10-20 02:05:46 +00002910 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002911 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002912 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002913 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002914 delete AttrList;
2915 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002916
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002917 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2918 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002919
2920 // cv-qualifier-seq[opt].
2921 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002922 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002923 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002924 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002925 llvm::SmallVector<TypeTy*, 2> Exceptions;
2926 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002927 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002928 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002929 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002930 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002931
2932 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002933 if (Tok.is(tok::kw_throw)) {
2934 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002935 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002936 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002937 hasAnyExceptionSpec);
2938 assert(Exceptions.size() == ExceptionRanges.size() &&
2939 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002940 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002941 }
2942
Chris Lattnerf97409f2008-04-06 06:57:35 +00002943 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002944 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002945 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002946 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002947 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002948 /*arglist*/ 0, 0,
2949 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002950 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002951 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002952 Exceptions.data(),
2953 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002954 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002955 LParenLoc, RParenLoc, D),
2956 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002957 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002958 }
2959
Chris Lattner7399ee02008-10-20 02:05:46 +00002960 // Alternatively, this parameter list may be an identifier list form for a
2961 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00002962 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
2963 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00002964 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002965 // K&R identifier lists can't have typedefs as identifiers, per
2966 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002967 if (RequiresArg) {
2968 Diag(Tok, diag::err_argument_required_after_attribute);
2969 delete AttrList;
2970 }
Chris Lattner83a94472010-05-14 17:23:36 +00002971
Steve Naroff2d081c42009-01-28 19:16:40 +00002972 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00002973 // normal declarators, not for abstract-declarators. Get the first
2974 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00002975 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00002976 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00002977
2978 // Identifier lists follow a really simple grammar: the identifiers can
2979 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
2980 // identifier lists are really rare in the brave new modern world, and it
2981 // is very common for someone to typo a type in a non-k&r style list. If
2982 // we are presented with something like: "void foo(intptr x, float y)",
2983 // we don't want to start parsing the function declarator as though it is
2984 // a K&R style declarator just because intptr is an invalid type.
2985 //
2986 // To handle this, we check to see if the token after the first identifier
2987 // is a "," or ")". Only if so, do we parse it as an identifier list.
2988 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
2989 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
2990 FirstTok.getIdentifierInfo(),
2991 FirstTok.getLocation(), D);
2992
2993 // If we get here, the code is invalid. Push the first identifier back
2994 // into the token stream and parse the first argument as an (invalid)
2995 // normal argument declarator.
2996 PP.EnterToken(Tok);
2997 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00002998 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002999 }
Mike Stump1eb44332009-09-09 15:08:12 +00003000
Chris Lattnerf97409f2008-04-06 06:57:35 +00003001 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003002
Chris Lattnerf97409f2008-04-06 06:57:35 +00003003 // Build up an array of information about the parsed arguments.
3004 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003005
3006 // Enter function-declaration scope, limiting any declarators to the
3007 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003008 ParseScope PrototypeScope(this,
3009 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003010
Chris Lattnerf97409f2008-04-06 06:57:35 +00003011 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003012 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003013 while (1) {
3014 if (Tok.is(tok::ellipsis)) {
3015 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003016 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003017 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003018 }
Mike Stump1eb44332009-09-09 15:08:12 +00003019
Chris Lattnerf97409f2008-04-06 06:57:35 +00003020 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00003021
Chris Lattnerf97409f2008-04-06 06:57:35 +00003022 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003023 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003024 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00003025
3026 // If the caller parsed attributes for the first argument, add them now.
3027 if (AttrList) {
3028 DS.AddAttributes(AttrList);
3029 AttrList = 0; // Only apply the attributes to the first parameter.
3030 }
Chris Lattnere64c5492009-02-27 18:38:20 +00003031 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003032
Chris Lattnerf97409f2008-04-06 06:57:35 +00003033 // Parse the declarator. This is "PrototypeContext", because we must
3034 // accept either 'declarator' or 'abstract-declarator' here.
3035 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3036 ParseDeclarator(ParmDecl);
3037
3038 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003039 if (Tok.is(tok::kw___attribute)) {
3040 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00003041 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003042 ParmDecl.AddAttributes(AttrList, Loc);
3043 }
Mike Stump1eb44332009-09-09 15:08:12 +00003044
Chris Lattnerf97409f2008-04-06 06:57:35 +00003045 // Remember this parsed parameter in ParamInfo.
3046 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003047
Douglas Gregor72b505b2008-12-16 21:30:33 +00003048 // DefArgToks is used when the parsing of default arguments needs
3049 // to be delayed.
3050 CachedTokens *DefArgToks = 0;
3051
Chris Lattnerf97409f2008-04-06 06:57:35 +00003052 // If no parameter was specified, verify that *something* was specified,
3053 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003054 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3055 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003056 // Completely missing, emit error.
3057 Diag(DSStart, diag::err_missing_param);
3058 } else {
3059 // Otherwise, we have something. Add it and let semantic analysis try
3060 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003061
Chris Lattnerf97409f2008-04-06 06:57:35 +00003062 // Inform the actions module about the parameter declarator, so it gets
3063 // added to the current scope.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003064 DeclPtrTy Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003065
3066 // Parse the default argument, if any. We parse the default
3067 // arguments in all dialects; the semantic analysis in
3068 // ActOnParamDefaultArgument will reject the default argument in
3069 // C.
3070 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003071 SourceLocation EqualLoc = Tok.getLocation();
3072
Chris Lattner04421082008-04-08 04:40:51 +00003073 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003074 if (D.getContext() == Declarator::MemberContext) {
3075 // If we're inside a class definition, cache the tokens
3076 // corresponding to the default argument. We'll actually parse
3077 // them when we see the end of the class definition.
3078 // FIXME: Templates will require something similar.
3079 // FIXME: Can we use a smart pointer for Toks?
3080 DefArgToks = new CachedTokens;
3081
Mike Stump1eb44332009-09-09 15:08:12 +00003082 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003083 /*StopAtSemi=*/true,
3084 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003085 delete DefArgToks;
3086 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003087 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003088 } else {
3089 // Mark the end of the default argument so that we know when to
3090 // stop when we parse it later on.
3091 Token DefArgEnd;
3092 DefArgEnd.startToken();
3093 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3094 DefArgEnd.setLocation(Tok.getLocation());
3095 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003096 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003097 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003098 }
Chris Lattner04421082008-04-08 04:40:51 +00003099 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003100 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003101 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003102
Douglas Gregor72b505b2008-12-16 21:30:33 +00003103 OwningExprResult DefArgResult(ParseAssignmentExpression());
3104 if (DefArgResult.isInvalid()) {
3105 Actions.ActOnParamDefaultArgumentError(Param);
3106 SkipUntil(tok::comma, tok::r_paren, true, true);
3107 } else {
3108 // Inform the actions module about the default argument
3109 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00003110 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00003111 }
Chris Lattner04421082008-04-08 04:40:51 +00003112 }
3113 }
Mike Stump1eb44332009-09-09 15:08:12 +00003114
3115 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3116 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003117 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003118 }
3119
3120 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003121 if (Tok.isNot(tok::comma)) {
3122 if (Tok.is(tok::ellipsis)) {
3123 IsVariadic = true;
3124 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3125
3126 if (!getLang().CPlusPlus) {
3127 // We have ellipsis without a preceding ',', which is ill-formed
3128 // in C. Complain and provide the fix.
3129 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003130 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003131 }
3132 }
3133
3134 break;
3135 }
Mike Stump1eb44332009-09-09 15:08:12 +00003136
Chris Lattnerf97409f2008-04-06 06:57:35 +00003137 // Consume the comma.
3138 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003139 }
Mike Stump1eb44332009-09-09 15:08:12 +00003140
Chris Lattnerf97409f2008-04-06 06:57:35 +00003141 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00003142 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00003143
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003144 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003145 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3146 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003147
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003148 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003149 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003150 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003151 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00003152 llvm::SmallVector<TypeTy*, 2> Exceptions;
3153 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00003154
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003155 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003156 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003157 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003158 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003159 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003160
3161 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003162 if (Tok.is(tok::kw_throw)) {
3163 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003164 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003165 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003166 hasAnyExceptionSpec);
3167 assert(Exceptions.size() == ExceptionRanges.size() &&
3168 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003169 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003170 }
3171
Reid Spencer5f016e22007-07-11 17:01:13 +00003172 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003173 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003174 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003175 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003176 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003177 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003178 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003179 Exceptions.data(),
3180 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003181 Exceptions.size(),
3182 LParenLoc, RParenLoc, D),
3183 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003184}
3185
Chris Lattner66d28652008-04-06 06:34:08 +00003186/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3187/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003188/// first identifier has already been consumed, and the current token is the
3189/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003190///
3191/// identifier-list: [C99 6.7.5]
3192/// identifier
3193/// identifier-list ',' identifier
3194///
3195void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003196 IdentifierInfo *FirstIdent,
3197 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003198 Declarator &D) {
3199 // Build up an array of information about the parsed arguments.
3200 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3201 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003202
Chris Lattner66d28652008-04-06 06:34:08 +00003203 // If there was no identifier specified for the declarator, either we are in
3204 // an abstract-declarator, or we are in a parameter declarator which was found
3205 // to be abstract. In abstract-declarators, identifier lists are not valid:
3206 // diagnose this.
3207 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003208 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003209
Chris Lattner83a94472010-05-14 17:23:36 +00003210 // The first identifier was already read, and is known to be the first
3211 // identifier in the list. Remember this identifier in ParamInfo.
3212 ParamsSoFar.insert(FirstIdent);
3213 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003214 DeclPtrTy()));
Mike Stump1eb44332009-09-09 15:08:12 +00003215
Chris Lattner66d28652008-04-06 06:34:08 +00003216 while (Tok.is(tok::comma)) {
3217 // Eat the comma.
3218 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003219
Chris Lattner50c64772008-04-06 06:39:19 +00003220 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003221 if (Tok.isNot(tok::identifier)) {
3222 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003223 SkipUntil(tok::r_paren);
3224 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003225 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003226
Chris Lattner66d28652008-04-06 06:34:08 +00003227 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003228
3229 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003230 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003231 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003232
Chris Lattner66d28652008-04-06 06:34:08 +00003233 // Verify that the argument identifier has not already been mentioned.
3234 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003235 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003236 } else {
3237 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003238 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003239 Tok.getLocation(),
3240 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00003241 }
Mike Stump1eb44332009-09-09 15:08:12 +00003242
Chris Lattner66d28652008-04-06 06:34:08 +00003243 // Eat the identifier.
3244 ConsumeToken();
3245 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003246
3247 // If we have the closing ')', eat it and we're done.
3248 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3249
Chris Lattner50c64772008-04-06 06:39:19 +00003250 // Remember that we parsed a function type, and remember the attributes. This
3251 // function type is always a K&R style function type, which is not varargs and
3252 // has no prototype.
3253 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003254 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003255 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003256 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003257 /*exception*/false,
3258 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003259 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003260 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003261}
Chris Lattneref4715c2008-04-06 05:45:57 +00003262
Reid Spencer5f016e22007-07-11 17:01:13 +00003263/// [C90] direct-declarator '[' constant-expression[opt] ']'
3264/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3265/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3266/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3267/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3268void Parser::ParseBracketDeclarator(Declarator &D) {
3269 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003270
Chris Lattner378c7e42008-12-18 07:27:21 +00003271 // C array syntax has many features, but by-far the most common is [] and [4].
3272 // This code does a fast path to handle some of the most obvious cases.
3273 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003274 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003275 //FIXME: Use these
3276 CXX0XAttributeList Attr;
3277 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3278 Attr = ParseCXX0XAttributes();
3279 }
3280
Chris Lattner378c7e42008-12-18 07:27:21 +00003281 // Remember that we parsed the empty array type.
3282 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003283 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3284 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003285 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003286 return;
3287 } else if (Tok.getKind() == tok::numeric_constant &&
3288 GetLookAheadToken(1).is(tok::r_square)) {
3289 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00003290 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003291 ConsumeToken();
3292
Sebastian Redlab197ba2009-02-09 18:23:29 +00003293 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003294 //FIXME: Use these
3295 CXX0XAttributeList Attr;
3296 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3297 Attr = ParseCXX0XAttributes();
3298 }
Chris Lattner378c7e42008-12-18 07:27:21 +00003299
3300 // If there was an error parsing the assignment-expression, recover.
3301 if (ExprRes.isInvalid())
3302 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00003303
Chris Lattner378c7e42008-12-18 07:27:21 +00003304 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003305 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3306 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003307 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003308 return;
3309 }
Mike Stump1eb44332009-09-09 15:08:12 +00003310
Reid Spencer5f016e22007-07-11 17:01:13 +00003311 // If valid, this location is the position where we read the 'static' keyword.
3312 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003313 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003314 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003315
Reid Spencer5f016e22007-07-11 17:01:13 +00003316 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003317 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003318 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003319 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003320
Reid Spencer5f016e22007-07-11 17:01:13 +00003321 // If we haven't already read 'static', check to see if there is one after the
3322 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003323 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003324 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003325
Reid Spencer5f016e22007-07-11 17:01:13 +00003326 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3327 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00003328 OwningExprResult NumElements(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00003329
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003330 // Handle the case where we have '[*]' as the array size. However, a leading
3331 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3332 // the the token after the star is a ']'. Since stars in arrays are
3333 // infrequent, use of lookahead is not costly here.
3334 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003335 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003336
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003337 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003338 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003339 StaticLoc = SourceLocation(); // Drop the static.
3340 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003341 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003342 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003343 // Note, in C89, this production uses the constant-expr production instead
3344 // of assignment-expr. The only difference is that assignment-expr allows
3345 // things like '=' and '*='. Sema rejects these in C89 mode because they
3346 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003347
Douglas Gregore0762c92009-06-19 23:52:42 +00003348 // Parse the constant-expression or assignment-expression now (depending
3349 // on dialect).
3350 if (getLang().CPlusPlus)
3351 NumElements = ParseConstantExpression();
3352 else
3353 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003354 }
Mike Stump1eb44332009-09-09 15:08:12 +00003355
Reid Spencer5f016e22007-07-11 17:01:13 +00003356 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003357 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003358 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003359 // If the expression was invalid, skip it.
3360 SkipUntil(tok::r_square);
3361 return;
3362 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003363
3364 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3365
Sean Huntbbd37c62009-11-21 08:43:09 +00003366 //FIXME: Use these
3367 CXX0XAttributeList Attr;
3368 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3369 Attr = ParseCXX0XAttributes();
3370 }
3371
Chris Lattner378c7e42008-12-18 07:27:21 +00003372 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00003373 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3374 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003375 NumElements.release(),
3376 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003377 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003378}
3379
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003380/// [GNU] typeof-specifier:
3381/// typeof ( expressions )
3382/// typeof ( type-name )
3383/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003384///
3385void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003386 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003387 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003388 SourceLocation StartLoc = ConsumeToken();
3389
John McCallcfb708c2010-01-13 20:03:27 +00003390 const bool hasParens = Tok.is(tok::l_paren);
3391
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003392 bool isCastExpr;
3393 TypeTy *CastTy;
3394 SourceRange CastRange;
3395 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3396 isCastExpr,
3397 CastTy,
3398 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003399 if (hasParens)
3400 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003401
3402 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003403 // FIXME: Not accurate, the range gets one token more than it should.
3404 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003405 else
3406 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003407
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003408 if (isCastExpr) {
3409 if (!CastTy) {
3410 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003411 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003412 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003413
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003414 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003415 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003416 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3417 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003418 DiagID, CastTy))
3419 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003420 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003421 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003422
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003423 // If we get here, the operand to the typeof was an expresion.
3424 if (Operand.isInvalid()) {
3425 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003426 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003427 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003428
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003429 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003430 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003431 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3432 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003433 DiagID, Operand.release()))
3434 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003435}
Chris Lattner1b492422010-02-28 18:33:55 +00003436
3437
3438/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3439/// from TryAltiVecVectorToken.
3440bool Parser::TryAltiVecVectorTokenOutOfLine() {
3441 Token Next = NextToken();
3442 switch (Next.getKind()) {
3443 default: return false;
3444 case tok::kw_short:
3445 case tok::kw_long:
3446 case tok::kw_signed:
3447 case tok::kw_unsigned:
3448 case tok::kw_void:
3449 case tok::kw_char:
3450 case tok::kw_int:
3451 case tok::kw_float:
3452 case tok::kw_double:
3453 case tok::kw_bool:
3454 case tok::kw___pixel:
3455 Tok.setKind(tok::kw___vector);
3456 return true;
3457 case tok::identifier:
3458 if (Next.getIdentifierInfo() == Ident_pixel) {
3459 Tok.setKind(tok::kw___vector);
3460 return true;
3461 }
3462 return false;
3463 }
3464}
3465
3466bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3467 const char *&PrevSpec, unsigned &DiagID,
3468 bool &isInvalid) {
3469 if (Tok.getIdentifierInfo() == Ident_vector) {
3470 Token Next = NextToken();
3471 switch (Next.getKind()) {
3472 case tok::kw_short:
3473 case tok::kw_long:
3474 case tok::kw_signed:
3475 case tok::kw_unsigned:
3476 case tok::kw_void:
3477 case tok::kw_char:
3478 case tok::kw_int:
3479 case tok::kw_float:
3480 case tok::kw_double:
3481 case tok::kw_bool:
3482 case tok::kw___pixel:
3483 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3484 return true;
3485 case tok::identifier:
3486 if (Next.getIdentifierInfo() == Ident_pixel) {
3487 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3488 return true;
3489 }
3490 break;
3491 default:
3492 break;
3493 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00003494 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00003495 DS.isTypeAltiVecVector()) {
3496 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3497 return true;
3498 }
3499 return false;
3500}
3501