blob: ec485d2d964682d7d7569bb44c30537fac429a45 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner1a76a3c2007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Douglas Gregorb53edfb2009-11-10 19:49:08 +000017#include "clang/Parse/Template.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000018#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000019#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
Chris Lattnerf5fbd792006-08-10 23:56:11 +000026/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Sebastian Redld6434562009-05-29 18:02:33 +000031Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000034 ParseSpecifierQualifierList(DS);
Sebastian Redld6434562009-05-29 18:02:33 +000035
Chris Lattnerf5fbd792006-08-10 23:56:11 +000036 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000039 if (Range)
40 *Range = DeclaratorInfo.getSourceRange();
41
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000042 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000043 return true;
44
45 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000046}
47
Alexis Hunt96d5c762009-11-21 08:43:09 +000048/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +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
Steve Naroff0f2fe172007-06-01 17:11:19 +000063/// attrib-name
64/// attrib-name '(' identifier ')'
65/// attrib-name '(' identifier ',' nonempty-expr-list ')'
66/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000067///
Steve Naroff0f2fe172007-06-01 17:11:19 +000068/// [GNU] attrib-name:
69/// identifier
70/// typespec
71/// typequal
72/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000073///
Steve Naroff0f2fe172007-06-01 17:11:19 +000074/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-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
Steve Naroff0f2fe172007-06-01 17:11:19 +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.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000083
Alexis Hunt96d5c762009-11-21 08:43:09 +000084AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) {
85 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +000086
Steve Naroffb8371e12007-06-09 03:39:29 +000087 AttributeList *CurrAttr = 0;
Mike Stump11289f42009-09-09 15:08:12 +000088
Chris Lattner76c72282007-10-09 17:33:22 +000089 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +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 Lattner76c72282007-10-09 17:33:22 +0000101 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
102 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000103
104 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +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 Stump11289f42009-09-09 15:08:12 +0000112
Douglas Gregora2f49452010-03-16 19:09:18 +0000113 // check if we have a "parameterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000114 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000115 ConsumeParen(); // ignore the left paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000116
Chris Lattner76c72282007-10-09 17:33:22 +0000117 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000118 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
119 SourceLocation ParmLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000120
121 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000122 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000123 ConsumeParen(); // ignore the right paren loc for now
Alexis Hunt96d5c762009-11-21 08:43:09 +0000124 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000125 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner76c72282007-10-09 17:33:22 +0000126 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000127 ConsumeToken();
128 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000129 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000130 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000131
Steve Naroff0f2fe172007-06-01 17:11:19 +0000132 // now parse the non-empty comma separated list of expressions
133 while (1) {
Sebastian Redl59b5e512008-12-11 21:36:32 +0000134 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000135 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000136 ArgExprsOk = false;
137 SkipUntil(tok::r_paren);
138 break;
139 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000140 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000141 }
Chris Lattner76c72282007-10-09 17:33:22 +0000142 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000143 break;
144 ConsumeToken(); // Eat the comma, move to the next argument
145 }
Chris Lattner76c72282007-10-09 17:33:22 +0000146 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000147 ConsumeParen(); // ignore the right paren loc for now
Alexis Hunt96d5c762009-11-21 08:43:09 +0000148 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
149 AttrNameLoc, ParmName, ParmLoc,
150 ArgExprs.take(), ArgExprs.size(),
151 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000152 }
153 }
154 } else { // not an identifier
Nate Begemanf2758702009-06-26 06:32:41 +0000155 switch (Tok.getKind()) {
156 case tok::r_paren:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000157 // parse a possibly empty comma separated list of expressions
Steve Naroff0f2fe172007-06-01 17:11:19 +0000158 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000159 ConsumeParen(); // ignore the right paren loc for now
Alexis Hunt96d5c762009-11-21 08:43:09 +0000160 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000161 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begemanf2758702009-06-26 06:32:41 +0000162 break;
163 case tok::kw_char:
164 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000165 case tok::kw_char16_t:
166 case tok::kw_char32_t:
Nate Begemanf2758702009-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();
Alexis Hunt96d5c762009-11-21 08:43:09 +0000180 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Nate Begemanf2758702009-06-26 06:32:41 +0000181 0, SourceLocation(), 0, 0, CurrAttr);
182 if (Tok.is(tok::r_paren))
183 ConsumeParen();
184 break;
185 default:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000186 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000187 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000188 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000189
Steve Naroff0f2fe172007-06-01 17:11:19 +0000190 // now parse the list of expressions
191 while (1) {
Sebastian Redl59b5e512008-12-11 21:36:32 +0000192 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000193 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000194 ArgExprsOk = false;
195 SkipUntil(tok::r_paren);
196 break;
197 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000198 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000199 }
Chris Lattner76c72282007-10-09 17:33:22 +0000200 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000201 break;
202 ConsumeToken(); // Eat the comma, move to the next argument
203 }
204 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000205 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000206 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl511ed552008-11-25 22:21:31 +0000207 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000208 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
209 ArgExprs.size(),
Steve Naroffb8371e12007-06-09 03:39:29 +0000210 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000211 }
Nate Begemanf2758702009-06-26 06:32:41 +0000212 break;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000213 }
214 }
215 } else {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000216 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000217 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000218 }
219 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000220 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000221 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000222 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-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;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000228 }
229 return CurrAttr;
230}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000231
Eli Friedman06de2b52009-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 Friedman53339e02009-06-08 23:27:34 +0000241AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000242 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000243
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000244 ConsumeToken();
Eli Friedman06de2b52009-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 Friedman53339e02009-06-08 23:27:34 +0000250 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-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();
Alexis Hunt96d5c762009-11-21 08:43:09 +0000260 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman06de2b52009-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 {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000267 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
268 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000269 }
270 }
271 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
272 SkipUntil(tok::r_paren, false);
Eli Friedman53339e02009-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 Gregora941dca2010-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 Friedman53339e02009-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;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000287 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman53339e02009-06-08 23:27:34 +0000288 SourceLocation(), 0, 0, CurrAttr, true);
289 }
290 return CurrAttr;
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000291}
292
Chris Lattner53361ac2006-08-10 05:19:57 +0000293/// ParseDeclaration - Parse a full 'declaration', which consists of
294/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000295/// 'Context' should be a Declarator::TheContext value. This returns the
296/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000297///
298/// declaration: [C99 6.7]
299/// block-declaration ->
300/// simple-declaration
301/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000302/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000303/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000304/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000305/// [C++] using-declaration
Sebastian Redlf769df52009-03-24 22:27:57 +0000306/// [C++0x] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000307/// others... [FIXME]
308///
Chris Lattner49836b42009-04-02 04:16:50 +0000309Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000310 SourceLocation &DeclEnd,
311 CXX0XAttributeList Attr) {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000312 DeclPtrTy SingleDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000313 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000314 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000315 case tok::kw_export:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000316 if (Attr.HasAttr)
317 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
318 << Attr.Range;
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000319 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000320 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000321 case tok::kw_namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000322 if (Attr.HasAttr)
323 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
324 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000325 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000326 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000327 case tok::kw_using:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000328 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000329 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000330 case tok::kw_static_assert:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000331 if (Attr.HasAttr)
332 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
333 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000334 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000335 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000336 default:
Chris Lattner005fc1b2010-04-05 18:18:31 +0000337 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000338 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000339
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000340 // This routine returns a DeclGroup, if the thing we parsed only contains a
341 // single decl, convert it now.
342 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000343}
344
345/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
346/// declaration-specifiers init-declarator-list[opt] ';'
347///[C90/C++]init-declarator-list ';' [TODO]
348/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000349///
350/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000351/// declaration. If it is true, it checks for and eats it.
Chris Lattner32dc41c2009-03-29 17:27:48 +0000352Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000353 SourceLocation &DeclEnd,
Chris Lattner005fc1b2010-04-05 18:18:31 +0000354 AttributeList *Attr,
355 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000356 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000357 ParsingDeclSpec DS(*this);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000358 if (Attr)
359 DS.AddAttributes(Attr);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000360 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
361 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump11289f42009-09-09 15:08:12 +0000362
Chris Lattner0e894622006-08-13 19:58:17 +0000363 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
364 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000365 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000366 if (RequireSemi) ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000367 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall28a6aea2009-11-04 02:18:39 +0000368 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000369 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000370 }
Mike Stump11289f42009-09-09 15:08:12 +0000371
Chris Lattner005fc1b2010-04-05 18:18:31 +0000372 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld5a36322009-11-03 19:26:08 +0000373}
Mike Stump11289f42009-09-09 15:08:12 +0000374
John McCalld5a36322009-11-03 19:26:08 +0000375/// ParseDeclGroup - Having concluded that this is either a function
376/// definition or a group of object declarations, actually parse the
377/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000378Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
379 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000380 bool AllowFunctionDefinitions,
381 SourceLocation *DeclEnd) {
382 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000383 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000384 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000385
John McCalld5a36322009-11-03 19:26:08 +0000386 // Bail out if the first declarator didn't seem well-formed.
387 if (!D.hasName() && !D.mayOmitIdentifier()) {
388 // Skip until ; or }.
389 SkipUntil(tok::r_brace, true, true);
390 if (Tok.is(tok::semi))
391 ConsumeToken();
392 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000393 }
Mike Stump11289f42009-09-09 15:08:12 +0000394
John McCalld5a36322009-11-03 19:26:08 +0000395 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
396 if (isDeclarationAfterDeclarator()) {
397 // Fall though. We have to check this first, though, because
398 // __attribute__ might be the start of a function definition in
399 // (extended) K&R C.
400 } else if (isStartOfFunctionDefinition()) {
401 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
402 Diag(Tok, diag::err_function_declared_typedef);
403
404 // Recover by treating the 'typedef' as spurious.
405 DS.ClearStorageClassSpecs();
406 }
407
408 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
409 return Actions.ConvertDeclToDeclGroup(TheDecl);
410 } else {
411 Diag(Tok, diag::err_expected_fn_body);
412 SkipUntil(tok::semi);
413 return DeclGroupPtrTy();
414 }
415 }
416
417 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
418 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000419 D.complete(FirstDecl);
John McCalld5a36322009-11-03 19:26:08 +0000420 if (FirstDecl.get())
421 DeclsInGroup.push_back(FirstDecl);
422
423 // If we don't have a comma, it is either the end of the list (a ';') or an
424 // error, bail out.
425 while (Tok.is(tok::comma)) {
426 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000427 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000428
429 // Parse the next declarator.
430 D.clear();
431
432 // Accept attributes in an init-declarator. In the first declarator in a
433 // declaration, these would be part of the declspec. In subsequent
434 // declarators, they become part of the declarator itself, so that they
435 // don't apply to declarators after *this* one. Examples:
436 // short __attribute__((common)) var; -> declspec
437 // short var __attribute__((common)); -> declarator
438 // short x, __attribute__((common)) var; -> declarator
439 if (Tok.is(tok::kw___attribute)) {
440 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000441 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld5a36322009-11-03 19:26:08 +0000442 D.AddAttributes(AttrList, Loc);
443 }
444
445 ParseDeclarator(D);
446
447 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000448 D.complete(ThisDecl);
John McCalld5a36322009-11-03 19:26:08 +0000449 if (ThisDecl.get())
450 DeclsInGroup.push_back(ThisDecl);
451 }
452
453 if (DeclEnd)
454 *DeclEnd = Tok.getLocation();
455
456 if (Context != Declarator::ForContext &&
457 ExpectAndConsume(tok::semi,
458 Context == Declarator::FileContext
459 ? diag::err_invalid_token_after_toplevel_declarator
460 : diag::err_expected_semi_declaration)) {
461 SkipUntil(tok::r_brace, true, true);
462 if (Tok.is(tok::semi))
463 ConsumeToken();
464 }
465
466 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
467 DeclsInGroup.data(),
468 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000469}
470
Douglas Gregor23996282009-05-12 21:31:51 +0000471/// \brief Parse 'declaration' after parsing 'declaration-specifiers
472/// declarator'. This method parses the remainder of the declaration
473/// (including any attributes or initializer, among other things) and
474/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000475///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000476/// init-declarator: [C99 6.7]
477/// declarator
478/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000479/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
480/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000481/// [C++] declarator initializer[opt]
482///
483/// [C++] initializer:
484/// [C++] '=' initializer-clause
485/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000486/// [C++0x] '=' 'default' [TODO]
487/// [C++0x] '=' 'delete'
488///
489/// According to the standard grammar, =default and =delete are function
490/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000491///
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000492Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
493 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000494 // If a simple-asm-expr is present, parse it.
495 if (Tok.is(tok::kw_asm)) {
496 SourceLocation Loc;
497 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
498 if (AsmLabel.isInvalid()) {
499 SkipUntil(tok::semi, true, true);
500 return DeclPtrTy();
501 }
Mike Stump11289f42009-09-09 15:08:12 +0000502
Douglas Gregor23996282009-05-12 21:31:51 +0000503 D.setAsmLabel(AsmLabel.release());
504 D.SetRangeEnd(Loc);
505 }
Mike Stump11289f42009-09-09 15:08:12 +0000506
Douglas Gregor23996282009-05-12 21:31:51 +0000507 // If attributes are present, parse them.
508 if (Tok.is(tok::kw___attribute)) {
509 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000510 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor23996282009-05-12 21:31:51 +0000511 D.AddAttributes(AttrList, Loc);
512 }
Mike Stump11289f42009-09-09 15:08:12 +0000513
Douglas Gregor23996282009-05-12 21:31:51 +0000514 // Inform the current actions module that we just parsed this declarator.
Douglas Gregor450f00842009-09-25 18:43:00 +0000515 DeclPtrTy ThisDecl;
516 switch (TemplateInfo.Kind) {
517 case ParsedTemplateInfo::NonTemplate:
518 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
519 break;
520
521 case ParsedTemplateInfo::Template:
522 case ParsedTemplateInfo::ExplicitSpecialization:
523 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000524 Action::MultiTemplateParamsArg(Actions,
525 TemplateInfo.TemplateParams->data(),
526 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000527 D);
528 break;
529
530 case ParsedTemplateInfo::ExplicitInstantiation: {
531 Action::DeclResult ThisRes
532 = Actions.ActOnExplicitInstantiation(CurScope,
533 TemplateInfo.ExternLoc,
534 TemplateInfo.TemplateLoc,
535 D);
536 if (ThisRes.isInvalid()) {
537 SkipUntil(tok::semi, true, true);
538 return DeclPtrTy();
539 }
540
541 ThisDecl = ThisRes.get();
542 break;
543 }
544 }
Mike Stump11289f42009-09-09 15:08:12 +0000545
Douglas Gregor23996282009-05-12 21:31:51 +0000546 // Parse declarator '=' initializer.
547 if (Tok.is(tok::equal)) {
548 ConsumeToken();
549 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
550 SourceLocation DelLoc = ConsumeToken();
551 Actions.SetDeclDeleted(ThisDecl, DelLoc);
552 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000553 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
554 EnterScope(0);
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000555 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000556 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000557
Douglas Gregor23996282009-05-12 21:31:51 +0000558 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000559
John McCall1f4ee7b2009-12-19 09:28:58 +0000560 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000561 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000562 ExitScope();
563 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000564
Douglas Gregor23996282009-05-12 21:31:51 +0000565 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000566 SkipUntil(tok::comma, true, true);
567 Actions.ActOnInitializerError(ThisDecl);
568 } else
569 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor23996282009-05-12 21:31:51 +0000570 }
571 } else if (Tok.is(tok::l_paren)) {
572 // Parse C++ direct initializer: '(' expression-list ')'
573 SourceLocation LParenLoc = ConsumeParen();
574 ExprVector Exprs(Actions);
575 CommaLocsTy CommaLocs;
576
Douglas Gregor613bf102009-12-22 17:47:17 +0000577 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
578 EnterScope(0);
579 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
580 }
581
Douglas Gregor23996282009-05-12 21:31:51 +0000582 if (ParseExpressionList(Exprs, CommaLocs)) {
583 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000584
585 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
586 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
587 ExitScope();
588 }
Douglas Gregor23996282009-05-12 21:31:51 +0000589 } else {
590 // Match the ')'.
591 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
592
593 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
594 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000595
596 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
597 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
598 ExitScope();
599 }
600
Douglas Gregor23996282009-05-12 21:31:51 +0000601 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
602 move_arg(Exprs),
Jay Foad7d0479f2009-05-21 09:52:38 +0000603 CommaLocs.data(), RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000604 }
605 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000606 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000607 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
608 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000609 }
610
611 return ThisDecl;
612}
613
Chris Lattner1890ac82006-08-13 01:16:23 +0000614/// ParseSpecifierQualifierList
615/// specifier-qualifier-list:
616/// type-specifier specifier-qualifier-list[opt]
617/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000618/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000619///
620void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
621 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
622 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000623 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000624
Chris Lattner1890ac82006-08-13 01:16:23 +0000625 // Validate declspec for type-name.
626 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000627 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
628 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000629 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000630
Chris Lattner1b22eed2006-11-28 05:12:07 +0000631 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000632 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000633 if (DS.getStorageClassSpecLoc().isValid())
634 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
635 else
636 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000637 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000638 }
Mike Stump11289f42009-09-09 15:08:12 +0000639
Chris Lattner1b22eed2006-11-28 05:12:07 +0000640 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000641 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000642 if (DS.isInlineSpecified())
643 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
644 if (DS.isVirtualSpecified())
645 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
646 if (DS.isExplicitSpecified())
647 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000648 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000649 }
650}
Chris Lattner53361ac2006-08-10 05:19:57 +0000651
Chris Lattner6cc055a2009-04-12 20:42:31 +0000652/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
653/// specified token is valid after the identifier in a declarator which
654/// immediately follows the declspec. For example, these things are valid:
655///
656/// int x [ 4]; // direct-declarator
657/// int x ( int y); // direct-declarator
658/// int(int x ) // direct-declarator
659/// int x ; // simple-declaration
660/// int x = 17; // init-declarator-list
661/// int x , y; // init-declarator-list
662/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000663/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000664/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000665///
666/// This is not, because 'x' does not immediately follow the declspec (though
667/// ')' happens to be valid anyway).
668/// int (x)
669///
670static bool isValidAfterIdentifierInDeclarator(const Token &T) {
671 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
672 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000673 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000674}
675
Chris Lattner20a0c612009-04-14 21:34:55 +0000676
677/// ParseImplicitInt - This method is called when we have an non-typename
678/// identifier in a declspec (which normally terminates the decl spec) when
679/// the declspec has no type specifier. In this case, the declspec is either
680/// malformed or is "implicit int" (in K&R and C89).
681///
682/// This method handles diagnosing this prettily and returns false if the
683/// declspec is done being processed. If it recovers and thinks there may be
684/// other pieces of declspec after it, it returns true.
685///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000686bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000687 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000688 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000689 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000690
Chris Lattner20a0c612009-04-14 21:34:55 +0000691 SourceLocation Loc = Tok.getLocation();
692 // If we see an identifier that is not a type name, we normally would
693 // parse it as the identifer being declared. However, when a typename
694 // is typo'd or the definition is not included, this will incorrectly
695 // parse the typename as the identifier name and fall over misparsing
696 // later parts of the diagnostic.
697 //
698 // As such, we try to do some look-ahead in cases where this would
699 // otherwise be an "implicit-int" case to see if this is invalid. For
700 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
701 // an identifier with implicit int, we'd get a parse error because the
702 // next token is obviously invalid for a type. Parse these as a case
703 // with an invalid type specifier.
704 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000705
Chris Lattner20a0c612009-04-14 21:34:55 +0000706 // Since we know that this either implicit int (which is rare) or an
707 // error, we'd do lookahead to try to do better recovery.
708 if (isValidAfterIdentifierInDeclarator(NextToken())) {
709 // If this token is valid for implicit int, e.g. "static x = 4", then
710 // we just avoid eating the identifier, so it will be parsed as the
711 // identifier in the declarator.
712 return false;
713 }
Mike Stump11289f42009-09-09 15:08:12 +0000714
Chris Lattner20a0c612009-04-14 21:34:55 +0000715 // Otherwise, if we don't consume this token, we are going to emit an
716 // error anyway. Try to recover from various common problems. Check
717 // to see if this was a reference to a tag name without a tag specified.
718 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000719 //
720 // C++ doesn't need this, and isTagName doesn't take SS.
721 if (SS == 0) {
722 const char *TagName = 0;
723 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000724
Chris Lattner20a0c612009-04-14 21:34:55 +0000725 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
726 default: break;
727 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
728 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
729 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
730 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
731 }
Mike Stump11289f42009-09-09 15:08:12 +0000732
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000733 if (TagName) {
734 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +0000735 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +0000736 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000737
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000738 // Parse this as a tag as if the missing tag were present.
739 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +0000740 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000741 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000742 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000743 return true;
744 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000745 }
Mike Stump11289f42009-09-09 15:08:12 +0000746
Douglas Gregor15e56022009-10-13 23:27:22 +0000747 // This is almost certainly an invalid type name. Let the action emit a
748 // diagnostic and attempt to recover.
749 Action::TypeTy *T = 0;
750 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
751 CurScope, SS, T)) {
752 // The action emitted a diagnostic, so we don't have to.
753 if (T) {
754 // The action has suggested that the type T could be used. Set that as
755 // the type in the declaration specifiers, consume the would-be type
756 // name token, and we're done.
757 const char *PrevSpec;
758 unsigned DiagID;
759 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
760 false);
761 DS.SetRangeEnd(Tok.getLocation());
762 ConsumeToken();
763
764 // There may be other declaration specifiers after this.
765 return true;
766 }
767
768 // Fall through; the action had no suggestion for us.
769 } else {
770 // The action did not emit a diagnostic, so emit one now.
771 SourceRange R;
772 if (SS) R = SS->getRange();
773 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
774 }
Mike Stump11289f42009-09-09 15:08:12 +0000775
Douglas Gregor15e56022009-10-13 23:27:22 +0000776 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000777 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000778 unsigned DiagID;
779 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000780 DS.SetRangeEnd(Tok.getLocation());
781 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattner20a0c612009-04-14 21:34:55 +0000783 // TODO: Could inject an invalid typedef decl in an enclosing scope to
784 // avoid rippling error messages on subsequent uses of the same type,
785 // could be useful if #include was forgotten.
786 return false;
787}
788
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000789/// \brief Determine the declaration specifier context from the declarator
790/// context.
791///
792/// \param Context the declarator context, which is one of the
793/// Declarator::TheContext enumerator values.
794Parser::DeclSpecContext
795Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
796 if (Context == Declarator::MemberContext)
797 return DSC_class;
798 if (Context == Declarator::FileContext)
799 return DSC_top_level;
800 return DSC_normal;
801}
802
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000803/// ParseDeclarationSpecifiers
804/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000805/// storage-class-specifier declaration-specifiers[opt]
806/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000807/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000808/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000809///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000810/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000811/// 'typedef'
812/// 'extern'
813/// 'static'
814/// 'auto'
815/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000816/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000817/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000818/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000819/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000820/// [C++] 'virtual'
821/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000822/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000823/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000824
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000825///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000826void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000827 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000828 AccessSpecifier AS,
829 DeclSpecContext DSContext) {
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000830 if (Tok.is(tok::code_completion)) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000831 Action::CodeCompletionContext CCC = Action::CCC_Namespace;
832 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
833 CCC = DSContext == DSC_class? Action::CCC_MemberTemplate
834 : Action::CCC_Template;
835 else if (DSContext == DSC_class)
836 CCC = Action::CCC_Class;
Douglas Gregorf1934162010-01-13 21:24:21 +0000837 else if (ObjCImpDecl)
838 CCC = Action::CCC_ObjCImplementation;
839
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000840 Actions.CodeCompleteOrdinaryName(CurScope, CCC);
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000841 ConsumeToken();
842 }
843
Chris Lattner2e232092008-03-13 06:29:04 +0000844 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000845 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000846 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000847 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000848 unsigned DiagID = 0;
849
Chris Lattner4d8f8732006-11-28 05:05:08 +0000850 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000851
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000852 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000853 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000854 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000855 // If this is not a declaration specifier token, we're done reading decl
856 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000857 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000858 return;
Mike Stump11289f42009-09-09 15:08:12 +0000859
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000860 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +0000861 // C++ scope specifier. Annotate and loop, or bail out on error.
862 if (TryAnnotateCXXScopeToken(true)) {
863 if (!DS.hasTypeSpecifier())
864 DS.SetTypeSpecError();
865 goto DoneWithDeclSpec;
866 }
John McCall8bc2a702010-03-01 18:20:46 +0000867 if (Tok.is(tok::coloncolon)) // ::new or ::delete
868 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +0000869 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000870
871 case tok::annot_cxxscope: {
872 if (DS.hasTypeSpecifier())
873 goto DoneWithDeclSpec;
874
John McCall9dab4e62009-12-12 11:40:51 +0000875 CXXScopeSpec SS;
876 SS.setScopeRep(Tok.getAnnotationValue());
877 SS.setRange(Tok.getAnnotationRange());
878
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000879 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000880 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000881 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000882 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000883 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000884 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000885
886 // C++ [class.qual]p2:
887 // In a lookup in which the constructor is an acceptable lookup
888 // result and the nested-name-specifier nominates a class C:
889 //
890 // - if the name specified after the
891 // nested-name-specifier, when looked up in C, is the
892 // injected-class-name of C (Clause 9), or
893 //
894 // - if the name specified after the nested-name-specifier
895 // is the same as the identifier or the
896 // simple-template-id's template-name in the last
897 // component of the nested-name-specifier,
898 //
899 // the name is instead considered to name the constructor of
900 // class C.
901 //
902 // Thus, if the template-name is actually the constructor
903 // name, then the code is ill-formed; this interpretation is
904 // reinforced by the NAD status of core issue 635.
905 TemplateIdAnnotation *TemplateId
906 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +0000907 if ((DSContext == DSC_top_level ||
908 (DSContext == DSC_class && DS.isFriendSpecified())) &&
909 TemplateId->Name &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000910 Actions.isCurrentClassName(*TemplateId->Name, CurScope, &SS)) {
911 if (isConstructorDeclarator()) {
912 // The user meant this to be an out-of-line constructor
913 // definition, but template arguments are not allowed
914 // there. Just allow this as a constructor; we'll
915 // complain about it later.
916 goto DoneWithDeclSpec;
917 }
918
919 // The user meant this to name a type, but it actually names
920 // a constructor with some extraneous template
921 // arguments. Complain, then parse it as a type as the user
922 // intended.
923 Diag(TemplateId->TemplateNameLoc,
924 diag::err_out_of_line_template_id_names_constructor)
925 << TemplateId->Name;
926 }
927
John McCall9dab4e62009-12-12 11:40:51 +0000928 DS.getTypeSpecScope() = SS;
929 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +0000930 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000931 "ParseOptionalCXXScopeSpecifier not working");
932 AnnotateTemplateIdTokenAsType(&SS);
933 continue;
934 }
935
Douglas Gregorc5790df2009-09-28 07:26:33 +0000936 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +0000937 DS.getTypeSpecScope() = SS;
938 ConsumeToken(); // The C++ scope.
Douglas Gregorc5790df2009-09-28 07:26:33 +0000939 if (Tok.getAnnotationValue())
940 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
941 PrevSpec, DiagID,
942 Tok.getAnnotationValue());
943 else
944 DS.SetTypeSpecError();
945 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
946 ConsumeToken(); // The typename
947 }
948
Douglas Gregor167fa622009-03-25 15:40:00 +0000949 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000950 goto DoneWithDeclSpec;
951
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000952 // If we're in a context where the identifier could be a class name,
953 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +0000954 if ((DSContext == DSC_top_level ||
955 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000956 Actions.isCurrentClassName(*Next.getIdentifierInfo(), CurScope,
957 &SS)) {
958 if (isConstructorDeclarator())
959 goto DoneWithDeclSpec;
960
961 // As noted in C++ [class.qual]p2 (cited above), when the name
962 // of the class is qualified in a context where it could name
963 // a constructor, its a constructor name. However, we've
964 // looked at the declarator, and the user probably meant this
965 // to be a type. Complain that it isn't supposed to be treated
966 // as a type, then proceed to parse it as a type.
967 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
968 << Next.getIdentifierInfo();
969 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000970
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000971 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
972 Next.getLocation(), CurScope, &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000973
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000974 // If the referenced identifier is not a type, then this declspec is
975 // erroneous: We already checked about that it has no type specifier, and
976 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +0000977 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000978 if (TypeRep == 0) {
979 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000980 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000981 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000982 }
Mike Stump11289f42009-09-09 15:08:12 +0000983
John McCall9dab4e62009-12-12 11:40:51 +0000984 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000985 ConsumeToken(); // The C++ scope.
986
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000987 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000988 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000989 if (isInvalid)
990 break;
Mike Stump11289f42009-09-09 15:08:12 +0000991
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000992 DS.SetRangeEnd(Tok.getLocation());
993 ConsumeToken(); // The typename.
994
995 continue;
996 }
Mike Stump11289f42009-09-09 15:08:12 +0000997
Chris Lattnere387d9e2009-01-21 19:48:37 +0000998 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000999 if (Tok.getAnnotationValue())
1000 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001001 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001002 else
1003 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001004
1005 if (isInvalid)
1006 break;
1007
Chris Lattnere387d9e2009-01-21 19:48:37 +00001008 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1009 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001010
Chris Lattnere387d9e2009-01-21 19:48:37 +00001011 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1012 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1013 // Objective-C interface. If we don't have Objective-C or a '<', this is
1014 // just a normal reference to a typedef name.
1015 if (!Tok.is(tok::less) || !getLang().ObjC1)
1016 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001017
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001018 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001019 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001020 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1021 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1022 LAngleLoc, EndProtoLoc);
1023 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1024 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001025
Chris Lattnere387d9e2009-01-21 19:48:37 +00001026 DS.SetRangeEnd(EndProtoLoc);
1027 continue;
1028 }
Mike Stump11289f42009-09-09 15:08:12 +00001029
Chris Lattner16fac4f2008-07-26 01:18:38 +00001030 // typedef-name
1031 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001032 // In C++, check to see if this is a scope specifier like foo::bar::, if
1033 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001034 if (getLang().CPlusPlus) {
1035 if (TryAnnotateCXXScopeToken(true)) {
1036 if (!DS.hasTypeSpecifier())
1037 DS.SetTypeSpecError();
1038 goto DoneWithDeclSpec;
1039 }
1040 if (!Tok.is(tok::identifier))
1041 continue;
1042 }
Mike Stump11289f42009-09-09 15:08:12 +00001043
Chris Lattner16fac4f2008-07-26 01:18:38 +00001044 // This identifier can only be a typedef name if we haven't already seen
1045 // a type-specifier. Without this check we misparse:
1046 // typedef int X; struct Y { short X; }; as 'short int'.
1047 if (DS.hasTypeSpecifier())
1048 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001049
John Thompson22334602010-02-05 00:12:22 +00001050 // Check for need to substitute AltiVec keyword tokens.
1051 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1052 break;
1053
Chris Lattner16fac4f2008-07-26 01:18:38 +00001054 // It has to be available as a typedef too!
Mike Stump11289f42009-09-09 15:08:12 +00001055 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00001056 Tok.getLocation(), CurScope);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001057
Chris Lattner6cc055a2009-04-12 20:42:31 +00001058 // If this is not a typedef name, don't parse it as part of the declspec,
1059 // it must be an implicit int or an error.
1060 if (TypeRep == 0) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001061 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001062 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001063 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001064
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001065 // If we're in a context where the identifier could be a class name,
1066 // check whether this is a constructor declaration.
1067 if (getLang().CPlusPlus && DSContext == DSC_class &&
Mike Stump11289f42009-09-09 15:08:12 +00001068 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001069 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001070 goto DoneWithDeclSpec;
1071
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001072 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001073 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001074 if (isInvalid)
1075 break;
Mike Stump11289f42009-09-09 15:08:12 +00001076
Chris Lattner16fac4f2008-07-26 01:18:38 +00001077 DS.SetRangeEnd(Tok.getLocation());
1078 ConsumeToken(); // The identifier
1079
1080 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1081 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1082 // Objective-C interface. If we don't have Objective-C or a '<', this is
1083 // just a normal reference to a typedef name.
1084 if (!Tok.is(tok::less) || !getLang().ObjC1)
1085 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001086
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001087 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001088 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001089 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1090 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1091 LAngleLoc, EndProtoLoc);
1092 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1093 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001094
Chris Lattner16fac4f2008-07-26 01:18:38 +00001095 DS.SetRangeEnd(EndProtoLoc);
1096
Steve Naroffcd5e7822008-09-22 10:28:57 +00001097 // Need to support trailing type qualifiers (e.g. "id<p> const").
1098 // If a type specifier follows, it will be diagnosed elsewhere.
1099 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001100 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001101
1102 // type-name
1103 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001104 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001105 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001106 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001107 // This template-id does not refer to a type name, so we're
1108 // done with the type-specifiers.
1109 goto DoneWithDeclSpec;
1110 }
1111
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001112 // If we're in a context where the template-id could be a
1113 // constructor name or specialization, check whether this is a
1114 // constructor declaration.
1115 if (getLang().CPlusPlus && DSContext == DSC_class &&
1116 Actions.isCurrentClassName(*TemplateId->Name, CurScope) &&
1117 isConstructorDeclarator())
1118 goto DoneWithDeclSpec;
1119
Douglas Gregor7f741122009-02-25 19:37:18 +00001120 // Turn the template-id annotation token into a type annotation
1121 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001122 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001123 continue;
1124 }
1125
Chris Lattnere37e2332006-08-15 04:50:22 +00001126 // GNU attributes support.
1127 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001128 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001129 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001130
1131 // Microsoft declspec support.
1132 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +00001133 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001134 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001135
Steve Naroff44ac7772008-12-25 14:16:32 +00001136 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001137 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001138 // FIXME: Add handling here!
1139 break;
1140
1141 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001142 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001143 case tok::kw___cdecl:
1144 case tok::kw___stdcall:
1145 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001146 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001147 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1148 continue;
1149
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001150 // storage-class-specifier
1151 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001152 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1153 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001154 break;
1155 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001156 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001157 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001158 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1159 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001160 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001161 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001162 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001163 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001164 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001165 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001166 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001167 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001168 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1169 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001170 break;
1171 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001172 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001173 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1174 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001175 else
John McCall49bfce42009-08-03 20:12:06 +00001176 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1177 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001178 break;
1179 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001180 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1181 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001182 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001183 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001184 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1185 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001186 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001187 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001188 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001189 break;
Mike Stump11289f42009-09-09 15:08:12 +00001190
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001191 // function-specifier
1192 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001193 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001194 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001195 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001196 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001197 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001198 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001199 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001200 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001201
Anders Carlssoncd8db412009-05-06 04:46:28 +00001202 // friend
1203 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001204 if (DSContext == DSC_class)
1205 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1206 else {
1207 PrevSpec = ""; // not actually used by the diagnostic
1208 DiagID = diag::err_friend_invalid_in_context;
1209 isInvalid = true;
1210 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001211 break;
Mike Stump11289f42009-09-09 15:08:12 +00001212
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001213 // constexpr
1214 case tok::kw_constexpr:
1215 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1216 break;
1217
Chris Lattnere387d9e2009-01-21 19:48:37 +00001218 // type-specifier
1219 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001220 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1221 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001222 break;
1223 case tok::kw_long:
1224 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001225 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1226 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001227 else
John McCall49bfce42009-08-03 20:12:06 +00001228 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1229 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001230 break;
1231 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001232 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1233 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001234 break;
1235 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001236 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1237 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001238 break;
1239 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001240 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1241 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001242 break;
1243 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001244 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1245 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001246 break;
1247 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001248 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1249 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001250 break;
1251 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001252 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1253 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001254 break;
1255 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001256 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1257 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001258 break;
1259 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001260 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1261 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001262 break;
1263 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001264 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1265 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001266 break;
1267 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001268 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1269 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001270 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001271 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001272 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1273 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001274 break;
1275 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001276 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1277 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001278 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001279 case tok::kw_bool:
1280 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001281 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1282 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001283 break;
1284 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001285 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1286 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001287 break;
1288 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001289 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1290 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001291 break;
1292 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001293 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1294 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001295 break;
John Thompson22334602010-02-05 00:12:22 +00001296 case tok::kw___vector:
1297 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1298 break;
1299 case tok::kw___pixel:
1300 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1301 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001302
1303 // class-specifier:
1304 case tok::kw_class:
1305 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001306 case tok::kw_union: {
1307 tok::TokenKind Kind = Tok.getKind();
1308 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001309 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001310 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001311 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001312
1313 // enum-specifier:
1314 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001315 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001316 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001317 continue;
1318
1319 // cv-qualifier:
1320 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001321 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1322 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001323 break;
1324 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001325 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1326 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001327 break;
1328 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001329 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1330 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001331 break;
1332
Douglas Gregor333489b2009-03-27 23:10:48 +00001333 // C++ typename-specifier:
1334 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001335 if (TryAnnotateTypeOrScopeToken()) {
1336 DS.SetTypeSpecError();
1337 goto DoneWithDeclSpec;
1338 }
1339 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001340 continue;
1341 break;
1342
Chris Lattnere387d9e2009-01-21 19:48:37 +00001343 // GNU typeof support.
1344 case tok::kw_typeof:
1345 ParseTypeofSpecifier(DS);
1346 continue;
1347
Anders Carlsson74948d02009-06-24 17:47:40 +00001348 case tok::kw_decltype:
1349 ParseDecltypeSpecifier(DS);
1350 continue;
1351
Steve Naroffcfdf6162008-06-05 00:02:44 +00001352 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001353 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001354 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1355 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001356 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001357 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001358
Chris Lattner0974b232008-07-26 00:20:22 +00001359 {
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001360 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001361 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001362 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1363 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1364 LAngleLoc, EndProtoLoc);
1365 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1366 ProtocolLocs.data(), LAngleLoc);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001367 DS.SetRangeEnd(EndProtoLoc);
1368
Chris Lattner6d29c102008-11-18 07:48:38 +00001369 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregora771f462010-03-31 17:46:05 +00001370 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001371 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001372 // Need to support trailing type qualifiers (e.g. "id<p> const").
1373 // If a type specifier follows, it will be diagnosed elsewhere.
1374 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001375 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001376 }
John McCall49bfce42009-08-03 20:12:06 +00001377 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001378 if (isInvalid) {
1379 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001380 assert(DiagID);
Chris Lattner6d29c102008-11-18 07:48:38 +00001381 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001382 }
Chris Lattner2e232092008-03-13 06:29:04 +00001383 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001384 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001385 }
1386}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001387
Chris Lattnera448d752009-01-06 06:59:53 +00001388/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001389/// primarily follow the C++ grammar with additions for C99 and GNU,
1390/// which together subsume the C grammar. Note that the C++
1391/// type-specifier also includes the C type-qualifier (for const,
1392/// volatile, and C99 restrict). Returns true if a type-specifier was
1393/// found (and parsed), false otherwise.
1394///
1395/// type-specifier: [C++ 7.1.5]
1396/// simple-type-specifier
1397/// class-specifier
1398/// enum-specifier
1399/// elaborated-type-specifier [TODO]
1400/// cv-qualifier
1401///
1402/// cv-qualifier: [C++ 7.1.5.1]
1403/// 'const'
1404/// 'volatile'
1405/// [C99] 'restrict'
1406///
1407/// simple-type-specifier: [ C++ 7.1.5.2]
1408/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1409/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1410/// 'char'
1411/// 'wchar_t'
1412/// 'bool'
1413/// 'short'
1414/// 'int'
1415/// 'long'
1416/// 'signed'
1417/// 'unsigned'
1418/// 'float'
1419/// 'double'
1420/// 'void'
1421/// [C99] '_Bool'
1422/// [C99] '_Complex'
1423/// [C99] '_Imaginary' // Removed in TC2?
1424/// [GNU] '_Decimal32'
1425/// [GNU] '_Decimal64'
1426/// [GNU] '_Decimal128'
1427/// [GNU] typeof-specifier
1428/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1429/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001430/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001431/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001432bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001433 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001434 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001435 const ParsedTemplateInfo &TemplateInfo,
1436 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001437 SourceLocation Loc = Tok.getLocation();
1438
1439 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001440 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001441 // If we already have a type specifier, this identifier is not a type.
1442 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1443 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1444 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1445 return false;
John Thompson22334602010-02-05 00:12:22 +00001446 // Check for need to substitute AltiVec keyword tokens.
1447 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1448 break;
1449 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001450 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001451 // Annotate typenames and C++ scope specifiers. If we get one, just
1452 // recurse to handle whatever we get.
1453 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001454 return true;
1455 if (Tok.is(tok::identifier))
1456 return false;
1457 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1458 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001459 case tok::coloncolon: // ::foo::bar
1460 if (NextToken().is(tok::kw_new) || // ::new
1461 NextToken().is(tok::kw_delete)) // ::delete
1462 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001463
Chris Lattner020bab92009-01-04 23:41:41 +00001464 // Annotate typenames and C++ scope specifiers. If we get one, just
1465 // recurse to handle whatever we get.
1466 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001467 return true;
1468 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1469 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001470
Douglas Gregor450c75a2008-11-07 15:42:26 +00001471 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001472 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001473 if (Tok.getAnnotationValue())
1474 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001475 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001476 else
1477 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001478 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1479 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001480
Douglas Gregor450c75a2008-11-07 15:42:26 +00001481 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1482 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1483 // Objective-C interface. If we don't have Objective-C or a '<', this is
1484 // just a normal reference to a typedef name.
1485 if (!Tok.is(tok::less) || !getLang().ObjC1)
1486 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001487
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001488 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001489 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001490 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1491 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1492 LAngleLoc, EndProtoLoc);
1493 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1494 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001495
Douglas Gregor450c75a2008-11-07 15:42:26 +00001496 DS.SetRangeEnd(EndProtoLoc);
1497 return true;
1498 }
1499
1500 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001501 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001502 break;
1503 case tok::kw_long:
1504 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001505 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1506 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001507 else
John McCall49bfce42009-08-03 20:12:06 +00001508 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1509 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001510 break;
1511 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001512 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001513 break;
1514 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001515 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1516 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001517 break;
1518 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001519 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1520 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001521 break;
1522 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001523 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1524 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001525 break;
1526 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001527 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001528 break;
1529 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001530 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001531 break;
1532 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001533 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001534 break;
1535 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001536 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001537 break;
1538 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001539 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001540 break;
1541 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001542 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001543 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001544 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001545 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001546 break;
1547 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001548 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001549 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001550 case tok::kw_bool:
1551 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001552 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001553 break;
1554 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001555 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1556 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001557 break;
1558 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001559 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1560 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001561 break;
1562 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001563 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1564 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001565 break;
John Thompson22334602010-02-05 00:12:22 +00001566 case tok::kw___vector:
1567 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1568 break;
1569 case tok::kw___pixel:
1570 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1571 break;
1572
Douglas Gregor450c75a2008-11-07 15:42:26 +00001573 // class-specifier:
1574 case tok::kw_class:
1575 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001576 case tok::kw_union: {
1577 tok::TokenKind Kind = Tok.getKind();
1578 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001579 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1580 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001581 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001582 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001583
1584 // enum-specifier:
1585 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001586 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001587 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001588 return true;
1589
1590 // cv-qualifier:
1591 case tok::kw_const:
1592 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001593 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001594 break;
1595 case tok::kw_volatile:
1596 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001597 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001598 break;
1599 case tok::kw_restrict:
1600 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001601 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001602 break;
1603
1604 // GNU typeof support.
1605 case tok::kw_typeof:
1606 ParseTypeofSpecifier(DS);
1607 return true;
1608
Anders Carlsson74948d02009-06-24 17:47:40 +00001609 // C++0x decltype support.
1610 case tok::kw_decltype:
1611 ParseDecltypeSpecifier(DS);
1612 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001613
Anders Carlssonbae27372009-06-26 23:44:14 +00001614 // C++0x auto support.
1615 case tok::kw_auto:
1616 if (!getLang().CPlusPlus0x)
1617 return false;
1618
John McCall49bfce42009-08-03 20:12:06 +00001619 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001620 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001621 case tok::kw___ptr64:
1622 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001623 case tok::kw___cdecl:
1624 case tok::kw___stdcall:
1625 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001626 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001627 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001628 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001629
Douglas Gregor450c75a2008-11-07 15:42:26 +00001630 default:
1631 // Not a type-specifier; do nothing.
1632 return false;
1633 }
1634
1635 // If the specifier combination wasn't legal, issue a diagnostic.
1636 if (isInvalid) {
1637 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001638 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001639 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001640 }
1641 DS.SetRangeEnd(Tok.getLocation());
1642 ConsumeToken(); // whatever we parsed above.
1643 return true;
1644}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001645
Chris Lattner70ae4912007-10-29 04:42:53 +00001646/// ParseStructDeclaration - Parse a struct declaration without the terminating
1647/// semicolon.
1648///
Chris Lattner90a26b02007-01-23 04:38:16 +00001649/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001650/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001651/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001652/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001653/// struct-declarator-list:
1654/// struct-declarator
1655/// struct-declarator-list ',' struct-declarator
1656/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1657/// struct-declarator:
1658/// declarator
1659/// [GNU] declarator attributes[opt]
1660/// declarator[opt] ':' constant-expression
1661/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1662///
Chris Lattnera12405b2008-04-10 06:46:29 +00001663void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001664ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001665 if (Tok.is(tok::kw___extension__)) {
1666 // __extension__ silences extension warnings in the subexpression.
1667 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001668 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001669 return ParseStructDeclaration(DS, Fields);
1670 }
Mike Stump11289f42009-09-09 15:08:12 +00001671
Steve Naroff97170802007-08-20 22:28:22 +00001672 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001673 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001674 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001675
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001676 // If there are no declarators, this is a free-standing declaration
1677 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001678 if (Tok.is(tok::semi)) {
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001679 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001680 return;
1681 }
1682
1683 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001684 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001685 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001686 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001687 FieldDeclarator DeclaratorInfo(DS);
1688
1689 // Attributes are only allowed here on successive declarators.
1690 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1691 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001692 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallcfefb6d2009-11-03 02:38:08 +00001693 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1694 }
Mike Stump11289f42009-09-09 15:08:12 +00001695
Steve Naroff97170802007-08-20 22:28:22 +00001696 /// struct-declarator: declarator
1697 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001698 if (Tok.isNot(tok::colon)) {
1699 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1700 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001701 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001702 }
Mike Stump11289f42009-09-09 15:08:12 +00001703
Chris Lattner76c72282007-10-09 17:33:22 +00001704 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001705 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001706 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001707 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001708 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001709 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001710 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001711 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001712
Steve Naroff97170802007-08-20 22:28:22 +00001713 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001714 if (Tok.is(tok::kw___attribute)) {
1715 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001716 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001717 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1718 }
1719
John McCallcfefb6d2009-11-03 02:38:08 +00001720 // We're done with this declarator; invoke the callback.
John McCall28a6aea2009-11-04 02:18:39 +00001721 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1722 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001723
Steve Naroff97170802007-08-20 22:28:22 +00001724 // If we don't have a comma, it is either the end of the list (a ';')
1725 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001726 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001727 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001728
Steve Naroff97170802007-08-20 22:28:22 +00001729 // Consume the comma.
1730 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001731
John McCallcfefb6d2009-11-03 02:38:08 +00001732 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001733 }
Steve Naroff97170802007-08-20 22:28:22 +00001734}
1735
1736/// ParseStructUnionBody
1737/// struct-contents:
1738/// struct-declaration-list
1739/// [EXT] empty
1740/// [GNU] "struct-declaration-list" without terminatoring ';'
1741/// struct-declaration-list:
1742/// struct-declaration
1743/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001744/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001745///
Chris Lattner1300fb92007-01-23 23:42:53 +00001746void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001747 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnereae6cb62009-03-05 08:00:35 +00001748 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1749 PP.getSourceManager(),
1750 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001751
Chris Lattner90a26b02007-01-23 04:38:16 +00001752 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001753
Douglas Gregor658b9552009-01-09 22:42:13 +00001754 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001755 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1756
Chris Lattner7b9ace62007-01-23 20:11:08 +00001757 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1758 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001759 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001760 Diag(Tok, diag::ext_empty_struct_union_enum)
1761 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001762
Chris Lattner83f095c2009-03-28 19:18:32 +00001763 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001764
Chris Lattner7b9ace62007-01-23 20:11:08 +00001765 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001766 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001767 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001768
Chris Lattner736ed5d2007-06-09 05:59:07 +00001769 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001770 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001771 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001772 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001773 ConsumeToken();
1774 continue;
1775 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001776
1777 // Parse all the comma separated declarators.
1778 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001779
John McCallcfefb6d2009-11-03 02:38:08 +00001780 if (!Tok.is(tok::at)) {
1781 struct CFieldCallback : FieldCallback {
1782 Parser &P;
1783 DeclPtrTy TagDecl;
1784 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1785
1786 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1787 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1788 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1789
1790 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001791 // Install the declarator into the current TagDecl.
John McCall5e6253b2009-11-03 21:13:47 +00001792 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1793 FD.D.getDeclSpec().getSourceRange().getBegin(),
1794 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001795 FieldDecls.push_back(Field);
1796 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001797 }
John McCallcfefb6d2009-11-03 02:38:08 +00001798 } Callback(*this, TagDecl, FieldDecls);
1799
1800 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001801 } else { // Handle @defs
1802 ConsumeToken();
1803 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1804 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001805 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001806 continue;
1807 }
1808 ConsumeToken();
1809 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1810 if (!Tok.is(tok::identifier)) {
1811 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001812 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001813 continue;
1814 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001815 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump11289f42009-09-09 15:08:12 +00001816 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001817 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001818 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1819 ConsumeToken();
1820 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001821 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001822
Chris Lattner76c72282007-10-09 17:33:22 +00001823 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001824 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001825 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001826 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001827 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001828 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001829 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1830 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001831 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001832 // If we stopped at a ';', eat it.
1833 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001834 }
1835 }
Mike Stump11289f42009-09-09 15:08:12 +00001836
Steve Naroff33a1e802007-10-29 21:38:07 +00001837 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001838
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001839 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner90a26b02007-01-23 04:38:16 +00001840 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001841 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001842 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar15619c72008-10-03 02:03:53 +00001843
1844 Actions.ActOnFields(CurScope,
Jay Foad7d0479f2009-05-21 09:52:38 +00001845 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001846 LBraceLoc, RBraceLoc,
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001847 AttrList.get());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001848 StructScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001849 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001850}
1851
1852
Chris Lattner3b561a32006-08-13 00:12:11 +00001853/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001854/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001855/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001856///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001857/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1858/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001859/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001860/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001861///
1862/// [C++] elaborated-type-specifier:
1863/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1864///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001865void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001866 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001867 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001868 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001869 if (Tok.is(tok::code_completion)) {
1870 // Code completion for an enum name.
1871 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1872 ConsumeToken();
1873 }
1874
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001875 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001876 // If attributes exist after tag, parse them.
1877 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001878 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001879
Abramo Bagnarad7548482010-05-19 21:37:53 +00001880 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00001881 if (getLang().CPlusPlus) {
1882 if (ParseOptionalCXXScopeSpecifier(SS, 0, false))
1883 return;
1884
1885 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001886 Diag(Tok, diag::err_expected_ident);
1887 if (Tok.isNot(tok::l_brace)) {
1888 // Has no name and is not a definition.
1889 // Skip the rest of this declarator, up until the comma or semicolon.
1890 SkipUntil(tok::comma, true);
1891 return;
1892 }
1893 }
1894 }
Mike Stump11289f42009-09-09 15:08:12 +00001895
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001896 // Must have either 'enum name' or 'enum {...}'.
1897 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1898 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001899
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001900 // Skip the rest of this declarator, up until the comma or semicolon.
1901 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001902 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001903 }
Mike Stump11289f42009-09-09 15:08:12 +00001904
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001905 // If an identifier is present, consume and remember it.
1906 IdentifierInfo *Name = 0;
1907 SourceLocation NameLoc;
1908 if (Tok.is(tok::identifier)) {
1909 Name = Tok.getIdentifierInfo();
1910 NameLoc = ConsumeToken();
1911 }
Mike Stump11289f42009-09-09 15:08:12 +00001912
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001913 // There are three options here. If we have 'enum foo;', then this is a
1914 // forward declaration. If we have 'enum foo {...' then this is a
1915 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1916 //
1917 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1918 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1919 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1920 //
John McCall9bb74a52009-07-31 02:45:11 +00001921 Action::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001922 if (Tok.is(tok::l_brace))
John McCall9bb74a52009-07-31 02:45:11 +00001923 TUK = Action::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001924 else if (Tok.is(tok::semi))
John McCall9bb74a52009-07-31 02:45:11 +00001925 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001926 else
John McCall9bb74a52009-07-31 02:45:11 +00001927 TUK = Action::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00001928
1929 // enums cannot be templates, although they can be referenced from a
1930 // template.
1931 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
1932 TUK != Action::TUK_Reference) {
1933 Diag(Tok, diag::err_enum_template);
1934
1935 // Skip the rest of this declarator, up until the comma or semicolon.
1936 SkipUntil(tok::comma, true);
1937 return;
1938 }
1939
Douglas Gregord6ab8742009-05-28 23:31:59 +00001940 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00001941 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00001942 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
1943 const char *PrevSpec = 0;
1944 unsigned DiagID;
John McCall9bb74a52009-07-31 02:45:11 +00001945 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001946 StartLoc, SS, Name, NameLoc, Attr.get(),
1947 AS,
Douglas Gregor27bdf00f2009-07-23 16:36:45 +00001948 Action::MultiTemplateParamsArg(Actions),
John McCall7f41d982009-09-11 04:59:25 +00001949 Owned, IsDependent);
Douglas Gregorba41d012010-04-24 16:38:41 +00001950 if (IsDependent) {
1951 // This enum has a dependent nested-name-specifier. Handle it as a
1952 // dependent tag.
1953 if (!Name) {
1954 DS.SetTypeSpecError();
1955 Diag(Tok, diag::err_expected_type_name_after_typename);
1956 return;
1957 }
1958
1959 TypeResult Type = Actions.ActOnDependentTag(CurScope, DeclSpec::TST_enum,
1960 TUK, SS, Name, StartLoc,
1961 NameLoc);
1962 if (Type.isInvalid()) {
1963 DS.SetTypeSpecError();
1964 return;
1965 }
1966
1967 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
1968 Type.get(), false))
1969 Diag(StartLoc, DiagID) << PrevSpec;
1970
1971 return;
1972 }
Mike Stump11289f42009-09-09 15:08:12 +00001973
Douglas Gregorba41d012010-04-24 16:38:41 +00001974 if (!TagDecl.get()) {
1975 // The action failed to produce an enumeration tag. If this is a
1976 // definition, consume the entire definition.
1977 if (Tok.is(tok::l_brace)) {
1978 ConsumeBrace();
1979 SkipUntil(tok::r_brace);
1980 }
1981
1982 DS.SetTypeSpecError();
1983 return;
1984 }
1985
Chris Lattner76c72282007-10-09 17:33:22 +00001986 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001987 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001988
Douglas Gregor72100632010-01-25 16:33:23 +00001989 // FIXME: The DeclSpec should keep the locations of both the keyword and the
1990 // name (if there is one).
Douglas Gregor72100632010-01-25 16:33:23 +00001991 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001992 TagDecl.getAs<void>(), Owned))
John McCall49bfce42009-08-03 20:12:06 +00001993 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00001994}
1995
Chris Lattnerc1915e22007-01-25 07:29:02 +00001996/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1997/// enumerator-list:
1998/// enumerator
1999/// enumerator-list ',' enumerator
2000/// enumerator:
2001/// enumeration-constant
2002/// enumeration-constant '=' constant-expression
2003/// enumeration-constant:
2004/// identifier
2005///
Chris Lattner83f095c2009-03-28 19:18:32 +00002006void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002007 // Enter the scope of the enum body and start the definition.
2008 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002009 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002010
Chris Lattnerc1915e22007-01-25 07:29:02 +00002011 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002012
Chris Lattner37256fb2007-08-27 17:24:30 +00002013 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002014 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00002015 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump11289f42009-09-09 15:08:12 +00002016
Chris Lattner83f095c2009-03-28 19:18:32 +00002017 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002018
Chris Lattner83f095c2009-03-28 19:18:32 +00002019 DeclPtrTy LastEnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002020
Chris Lattnerc1915e22007-01-25 07:29:02 +00002021 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002022 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002023 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2024 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002025
Chris Lattnerc1915e22007-01-25 07:29:02 +00002026 SourceLocation EqualLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +00002027 OwningExprResult AssignedVal(Actions);
Chris Lattner76c72282007-10-09 17:33:22 +00002028 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002029 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002030 AssignedVal = ParseConstantExpression();
2031 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002032 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002033 }
Mike Stump11289f42009-09-09 15:08:12 +00002034
Chris Lattnerc1915e22007-01-25 07:29:02 +00002035 // Install the enumerator constant into EnumDecl.
Chris Lattner83f095c2009-03-28 19:18:32 +00002036 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
2037 LastEnumConstDecl,
2038 IdentLoc, Ident,
2039 EqualLoc,
2040 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002041 EnumConstantDecls.push_back(EnumConstDecl);
2042 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002043
Chris Lattner76c72282007-10-09 17:33:22 +00002044 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002045 break;
2046 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002047
2048 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002049 !(getLang().C99 || getLang().CPlusPlus0x))
2050 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2051 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002052 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002053 }
Mike Stump11289f42009-09-09 15:08:12 +00002054
Chris Lattnerc1915e22007-01-25 07:29:02 +00002055 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002056 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002057
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002058 llvm::OwningPtr<AttributeList> Attr;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002059 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00002060 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002061 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002062
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002063 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2064 EnumConstantDecls.data(), EnumConstantDecls.size(),
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002065 CurScope, Attr.get());
Mike Stump11289f42009-09-09 15:08:12 +00002066
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002067 EnumScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00002068 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002069}
Chris Lattner3b561a32006-08-13 00:12:11 +00002070
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002071/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002072/// start of a type-qualifier-list.
2073bool Parser::isTypeQualifier() const {
2074 switch (Tok.getKind()) {
2075 default: return false;
2076 // type-qualifier
2077 case tok::kw_const:
2078 case tok::kw_volatile:
2079 case tok::kw_restrict:
2080 return true;
2081 }
2082}
2083
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002084/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2085/// is definitely a type-specifier. Return false if it isn't part of a type
2086/// specifier or if we're not sure.
2087bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2088 switch (Tok.getKind()) {
2089 default: return false;
2090 // type-specifiers
2091 case tok::kw_short:
2092 case tok::kw_long:
2093 case tok::kw_signed:
2094 case tok::kw_unsigned:
2095 case tok::kw__Complex:
2096 case tok::kw__Imaginary:
2097 case tok::kw_void:
2098 case tok::kw_char:
2099 case tok::kw_wchar_t:
2100 case tok::kw_char16_t:
2101 case tok::kw_char32_t:
2102 case tok::kw_int:
2103 case tok::kw_float:
2104 case tok::kw_double:
2105 case tok::kw_bool:
2106 case tok::kw__Bool:
2107 case tok::kw__Decimal32:
2108 case tok::kw__Decimal64:
2109 case tok::kw__Decimal128:
2110 case tok::kw___vector:
2111
2112 // struct-or-union-specifier (C99) or class-specifier (C++)
2113 case tok::kw_class:
2114 case tok::kw_struct:
2115 case tok::kw_union:
2116 // enum-specifier
2117 case tok::kw_enum:
2118
2119 // typedef-name
2120 case tok::annot_typename:
2121 return true;
2122 }
2123}
2124
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002125/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002126/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002127bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002128 switch (Tok.getKind()) {
2129 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002130
Chris Lattner020bab92009-01-04 23:41:41 +00002131 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002132 if (TryAltiVecVectorToken())
2133 return true;
2134 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002135 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002136 // Annotate typenames and C++ scope specifiers. If we get one, just
2137 // recurse to handle whatever we get.
2138 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002139 return true;
2140 if (Tok.is(tok::identifier))
2141 return false;
2142 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002143
Chris Lattner020bab92009-01-04 23:41:41 +00002144 case tok::coloncolon: // ::foo::bar
2145 if (NextToken().is(tok::kw_new) || // ::new
2146 NextToken().is(tok::kw_delete)) // ::delete
2147 return false;
2148
Chris Lattner020bab92009-01-04 23:41:41 +00002149 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002150 return true;
2151 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002152
Chris Lattnere37e2332006-08-15 04:50:22 +00002153 // GNU attributes support.
2154 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002155 // GNU typeof support.
2156 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002157
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002158 // type-specifiers
2159 case tok::kw_short:
2160 case tok::kw_long:
2161 case tok::kw_signed:
2162 case tok::kw_unsigned:
2163 case tok::kw__Complex:
2164 case tok::kw__Imaginary:
2165 case tok::kw_void:
2166 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002167 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002168 case tok::kw_char16_t:
2169 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002170 case tok::kw_int:
2171 case tok::kw_float:
2172 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002173 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002174 case tok::kw__Bool:
2175 case tok::kw__Decimal32:
2176 case tok::kw__Decimal64:
2177 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002178 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002179
Chris Lattner861a2262008-04-13 18:59:07 +00002180 // struct-or-union-specifier (C99) or class-specifier (C++)
2181 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002182 case tok::kw_struct:
2183 case tok::kw_union:
2184 // enum-specifier
2185 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002186
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002187 // type-qualifier
2188 case tok::kw_const:
2189 case tok::kw_volatile:
2190 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002191
2192 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002193 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002194 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002195
Chris Lattner409bf7d2008-10-20 00:25:30 +00002196 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2197 case tok::less:
2198 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002199
Steve Naroff44ac7772008-12-25 14:16:32 +00002200 case tok::kw___cdecl:
2201 case tok::kw___stdcall:
2202 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002203 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002204 case tok::kw___w64:
2205 case tok::kw___ptr64:
2206 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002207 }
2208}
2209
Chris Lattneracd58a32006-08-06 17:24:14 +00002210/// isDeclarationSpecifier() - Return true if the current token is part of a
2211/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002212bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00002213 switch (Tok.getKind()) {
2214 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002215
Chris Lattner020bab92009-01-04 23:41:41 +00002216 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002217 // Unfortunate hack to support "Class.factoryMethod" notation.
2218 if (getLang().ObjC1 && NextToken().is(tok::period))
2219 return false;
John Thompson22334602010-02-05 00:12:22 +00002220 if (TryAltiVecVectorToken())
2221 return true;
2222 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002223 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002224 // Annotate typenames and C++ scope specifiers. If we get one, just
2225 // recurse to handle whatever we get.
2226 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002227 return true;
2228 if (Tok.is(tok::identifier))
2229 return false;
2230 return isDeclarationSpecifier();
2231
Chris Lattner020bab92009-01-04 23:41:41 +00002232 case tok::coloncolon: // ::foo::bar
2233 if (NextToken().is(tok::kw_new) || // ::new
2234 NextToken().is(tok::kw_delete)) // ::delete
2235 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002236
Chris Lattner020bab92009-01-04 23:41:41 +00002237 // Annotate typenames and C++ scope specifiers. If we get one, just
2238 // recurse to handle whatever we get.
2239 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002240 return true;
2241 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002242
Chris Lattneracd58a32006-08-06 17:24:14 +00002243 // storage-class-specifier
2244 case tok::kw_typedef:
2245 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002246 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002247 case tok::kw_static:
2248 case tok::kw_auto:
2249 case tok::kw_register:
2250 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002251
Chris Lattneracd58a32006-08-06 17:24:14 +00002252 // type-specifiers
2253 case tok::kw_short:
2254 case tok::kw_long:
2255 case tok::kw_signed:
2256 case tok::kw_unsigned:
2257 case tok::kw__Complex:
2258 case tok::kw__Imaginary:
2259 case tok::kw_void:
2260 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002261 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002262 case tok::kw_char16_t:
2263 case tok::kw_char32_t:
2264
Chris Lattneracd58a32006-08-06 17:24:14 +00002265 case tok::kw_int:
2266 case tok::kw_float:
2267 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002268 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002269 case tok::kw__Bool:
2270 case tok::kw__Decimal32:
2271 case tok::kw__Decimal64:
2272 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002273 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002274
Chris Lattner861a2262008-04-13 18:59:07 +00002275 // struct-or-union-specifier (C99) or class-specifier (C++)
2276 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002277 case tok::kw_struct:
2278 case tok::kw_union:
2279 // enum-specifier
2280 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002281
Chris Lattneracd58a32006-08-06 17:24:14 +00002282 // type-qualifier
2283 case tok::kw_const:
2284 case tok::kw_volatile:
2285 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002286
Chris Lattneracd58a32006-08-06 17:24:14 +00002287 // function-specifier
2288 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002289 case tok::kw_virtual:
2290 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002291
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002292 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002293 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002294
Chris Lattner599e47e2007-08-09 17:01:07 +00002295 // GNU typeof support.
2296 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002297
Chris Lattner599e47e2007-08-09 17:01:07 +00002298 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002299 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002300 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002301
Chris Lattner8b2ec162008-07-26 03:38:44 +00002302 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2303 case tok::less:
2304 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002305
Steve Narofff192fab2009-01-06 19:34:12 +00002306 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002307 case tok::kw___cdecl:
2308 case tok::kw___stdcall:
2309 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002310 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002311 case tok::kw___w64:
2312 case tok::kw___ptr64:
2313 case tok::kw___forceinline:
2314 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002315 }
2316}
2317
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002318bool Parser::isConstructorDeclarator() {
2319 TentativeParsingAction TPA(*this);
2320
2321 // Parse the C++ scope specifier.
2322 CXXScopeSpec SS;
John McCall1f476a12010-02-26 08:45:28 +00002323 if (ParseOptionalCXXScopeSpecifier(SS, 0, true)) {
2324 TPA.Revert();
2325 return false;
2326 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002327
2328 // Parse the constructor name.
2329 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2330 // We already know that we have a constructor name; just consume
2331 // the token.
2332 ConsumeToken();
2333 } else {
2334 TPA.Revert();
2335 return false;
2336 }
2337
2338 // Current class name must be followed by a left parentheses.
2339 if (Tok.isNot(tok::l_paren)) {
2340 TPA.Revert();
2341 return false;
2342 }
2343 ConsumeParen();
2344
2345 // A right parentheses or ellipsis signals that we have a constructor.
2346 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2347 TPA.Revert();
2348 return true;
2349 }
2350
2351 // If we need to, enter the specified scope.
2352 DeclaratorScopeObj DeclScopeObj(*this, SS);
2353 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(CurScope, SS))
2354 DeclScopeObj.EnterDeclaratorScope();
2355
2356 // Check whether the next token(s) are part of a declaration
2357 // specifier, in which case we have the start of a parameter and,
2358 // therefore, we know that this is a constructor.
2359 bool IsConstructor = isDeclarationSpecifier();
2360 TPA.Revert();
2361 return IsConstructor;
2362}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002363
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002364/// ParseTypeQualifierListOpt
2365/// type-qualifier-list: [C99 6.7.5]
2366/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002367/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002368/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002369/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Alexis Hunt96d5c762009-11-21 08:43:09 +00002370/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2371/// if CXX0XAttributesAllowed = true
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002372///
Alexis Hunt96d5c762009-11-21 08:43:09 +00002373void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2374 bool CXX0XAttributesAllowed) {
2375 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2376 SourceLocation Loc = Tok.getLocation();
2377 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2378 if (CXX0XAttributesAllowed)
2379 DS.AddAttributes(Attr.AttrList);
2380 else
2381 Diag(Loc, diag::err_attributes_not_allowed);
2382 }
2383
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002384 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002385 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002386 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002387 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002388 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002389
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002390 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002391 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002392 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2393 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002394 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002395 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002396 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2397 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002398 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002399 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002400 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2401 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002402 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002403 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002404 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002405 case tok::kw___cdecl:
2406 case tok::kw___stdcall:
2407 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002408 case tok::kw___thiscall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002409 if (GNUAttributesAllowed) {
Eli Friedman53339e02009-06-08 23:27:34 +00002410 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2411 continue;
2412 }
2413 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002414 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002415 if (GNUAttributesAllowed) {
2416 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002417 continue; // do *not* consume the next token!
2418 }
2419 // otherwise, FALL THROUGH!
2420 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002421 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002422 // If this is not a type-qualifier token, we're done reading type
2423 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002424 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002425 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002426 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002427
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002428 // If the specifier combination wasn't legal, issue a diagnostic.
2429 if (isInvalid) {
2430 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002431 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002432 }
2433 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002434 }
2435}
2436
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002437
2438/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2439///
2440void Parser::ParseDeclarator(Declarator &D) {
2441 /// This implements the 'declarator' production in the C grammar, then checks
2442 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002443 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002444}
2445
Sebastian Redlbd150f42008-11-21 19:14:01 +00002446/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2447/// is parsed by the function passed to it. Pass null, and the direct-declarator
2448/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002449/// ptr-operator production.
2450///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002451/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2452/// [C] pointer[opt] direct-declarator
2453/// [C++] direct-declarator
2454/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002455///
2456/// pointer: [C99 6.7.5]
2457/// '*' type-qualifier-list[opt]
2458/// '*' type-qualifier-list[opt] pointer
2459///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002460/// ptr-operator:
2461/// '*' cv-qualifier-seq[opt]
2462/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002463/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002464/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002465/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002466/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002467void Parser::ParseDeclaratorInternal(Declarator &D,
2468 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002469 if (Diags.hasAllExtensionsSilenced())
2470 D.setExtension();
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002471 // C++ member pointers start with a '::' or a nested-name.
2472 // Member pointers get special handling, since there's no place for the
2473 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002474 if (getLang().CPlusPlus &&
2475 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2476 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002477 CXXScopeSpec SS;
John McCall1f476a12010-02-26 08:45:28 +00002478 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true); // ignore fail
2479
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002480 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002481 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002482 // The scope spec really belongs to the direct-declarator.
2483 D.getCXXScopeSpec() = SS;
2484 if (DirectDeclParser)
2485 (this->*DirectDeclParser)(D);
2486 return;
2487 }
2488
2489 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002490 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002491 DeclSpec DS;
2492 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002493 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002494
2495 // Recurse to parse whatever is left.
2496 ParseDeclaratorInternal(D, DirectDeclParser);
2497
2498 // Sema will have to catch (syntactically invalid) pointers into global
2499 // scope. It has to catch pointers into namespace scope anyway.
2500 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002501 Loc, DS.TakeAttributes()),
2502 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002503 return;
2504 }
2505 }
2506
2507 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002508 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002509 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002510 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002511 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002512 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002513 if (DirectDeclParser)
2514 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002515 return;
2516 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002517
Sebastian Redled0f3b02009-03-15 22:02:01 +00002518 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2519 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002520 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002521 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002522
Chris Lattner9eac9312009-03-27 04:18:06 +00002523 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002524 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002525 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002526
Bill Wendling3708c182007-05-27 10:15:43 +00002527 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002528 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002529
Bill Wendling3708c182007-05-27 10:15:43 +00002530 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002531 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002532 if (Kind == tok::star)
2533 // Remember that we parsed a pointer type, and remember the type-quals.
2534 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002535 DS.TakeAttributes()),
2536 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002537 else
2538 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002539 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002540 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002541 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002542 } else {
2543 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002544 DeclSpec DS;
2545
Sebastian Redl3b27be62009-03-23 00:00:23 +00002546 // Complain about rvalue references in C++03, but then go on and build
2547 // the declarator.
2548 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2549 Diag(Loc, diag::err_rvalue_reference);
2550
Bill Wendling93efb222007-06-02 23:28:54 +00002551 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2552 // cv-qualifiers are introduced through the use of a typedef or of a
2553 // template type argument, in which case the cv-qualifiers are ignored.
2554 //
2555 // [GNU] Retricted references are allowed.
2556 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002557 // [C++0x] Attributes on references are not allowed.
2558 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002559 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002560
2561 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2562 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2563 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002564 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002565 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2566 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002567 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002568 }
Bill Wendling3708c182007-05-27 10:15:43 +00002569
2570 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002571 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002572
Douglas Gregor66583c52008-11-03 15:51:28 +00002573 if (D.getNumTypeObjects() > 0) {
2574 // C++ [dcl.ref]p4: There shall be no references to references.
2575 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2576 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002577 if (const IdentifierInfo *II = D.getIdentifier())
2578 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2579 << II;
2580 else
2581 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2582 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002583
Sebastian Redlbd150f42008-11-21 19:14:01 +00002584 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002585 // can go ahead and build the (technically ill-formed)
2586 // declarator: reference collapsing will take care of it.
2587 }
2588 }
2589
Bill Wendling3708c182007-05-27 10:15:43 +00002590 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002591 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002592 DS.TakeAttributes(),
2593 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002594 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002595 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002596}
2597
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002598/// ParseDirectDeclarator
2599/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002600/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002601/// '(' declarator ')'
2602/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002603/// [C90] direct-declarator '[' constant-expression[opt] ']'
2604/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2605/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2606/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2607/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002608/// direct-declarator '(' parameter-type-list ')'
2609/// direct-declarator '(' identifier-list[opt] ')'
2610/// [GNU] direct-declarator '(' parameter-forward-declarations
2611/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002612/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2613/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002614/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002615///
2616/// declarator-id: [C++ 8]
2617/// id-expression
2618/// '::'[opt] nested-name-specifier[opt] type-name
2619///
2620/// id-expression: [C++ 5.1]
2621/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002622/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002623///
2624/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002625/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002626/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002627/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002628/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002629/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002630///
Chris Lattneracd58a32006-08-06 17:24:14 +00002631void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002632 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002633
Douglas Gregor7861a802009-11-03 01:35:08 +00002634 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2635 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002636 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002637 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2638 true);
John McCall1f476a12010-02-26 08:45:28 +00002639 }
2640
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002641 if (D.getCXXScopeSpec().isValid()) {
John McCall2b058ef2009-12-11 20:04:54 +00002642 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2643 // Change the declaration context for name lookup, until this function
2644 // is exited (and the declarator has been parsed).
2645 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002646 }
2647
Douglas Gregor7861a802009-11-03 01:35:08 +00002648 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2649 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2650 // We found something that indicates the start of an unqualified-id.
2651 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002652 bool AllowConstructorName;
2653 if (D.getDeclSpec().hasTypeSpecifier())
2654 AllowConstructorName = false;
2655 else if (D.getCXXScopeSpec().isSet())
2656 AllowConstructorName =
2657 (D.getContext() == Declarator::FileContext ||
2658 (D.getContext() == Declarator::MemberContext &&
2659 D.getDeclSpec().isFriendSpecified()));
2660 else
2661 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2662
Douglas Gregor7861a802009-11-03 01:35:08 +00002663 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2664 /*EnteringContext=*/true,
2665 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002666 AllowConstructorName,
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002667 /*ObjectType=*/0,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002668 D.getName()) ||
2669 // Once we're past the identifier, if the scope was bad, mark the
2670 // whole declarator bad.
2671 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002672 D.SetIdentifier(0, Tok.getLocation());
2673 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002674 } else {
2675 // Parsed the unqualified-id; update range information and move along.
2676 if (D.getSourceRange().getBegin().isInvalid())
2677 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2678 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002679 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002680 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002681 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002682 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002683 assert(!getLang().CPlusPlus &&
2684 "There's a C++-specific check for tok::identifier above");
2685 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2686 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2687 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002688 goto PastIdentifier;
2689 }
2690
2691 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002692 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002693 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002694 // Example: 'char (*X)' or 'int (*XX)(void)'
2695 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002696
2697 // If the declarator was parenthesized, we entered the declarator
2698 // scope when parsing the parenthesized declarator, then exited
2699 // the scope already. Re-enter the scope, if we need to.
2700 if (D.getCXXScopeSpec().isSet()) {
2701 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2702 // Change the declaration context for name lookup, until this function
2703 // is exited (and the declarator has been parsed).
2704 DeclScopeObj.EnterDeclaratorScope();
2705 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002706 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002707 // This could be something simple like "int" (in which case the declarator
2708 // portion is empty), if an abstract-declarator is allowed.
2709 D.SetIdentifier(0, Tok.getLocation());
2710 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002711 if (D.getContext() == Declarator::MemberContext)
2712 Diag(Tok, diag::err_expected_member_name_or_semi)
2713 << D.getDeclSpec().getSourceRange();
2714 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002715 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002716 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002717 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002718 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002719 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002720 }
Mike Stump11289f42009-09-09 15:08:12 +00002721
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002722 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002723 assert(D.isPastIdentifier() &&
2724 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002725
Alexis Hunt96d5c762009-11-21 08:43:09 +00002726 // Don't parse attributes unless we have an identifier.
Douglas Gregor0286b462010-02-19 16:47:56 +00002727 if (D.getIdentifier() && getLang().CPlusPlus0x
Alexis Hunt96d5c762009-11-21 08:43:09 +00002728 && isCXX0XAttributeSpecifier(true)) {
2729 SourceLocation AttrEndLoc;
2730 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2731 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2732 }
2733
Chris Lattneracd58a32006-08-06 17:24:14 +00002734 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002735 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002736 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2737 // In such a case, check if we actually have a function declarator; if it
2738 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002739 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2740 // When not in file scope, warn for ambiguous function declarators, just
2741 // in case the author intended it as a variable definition.
2742 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2743 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2744 break;
2745 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002746 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002747 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002748 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002749 } else {
2750 break;
2751 }
2752 }
2753}
2754
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002755/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2756/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002757/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002758/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2759///
2760/// direct-declarator:
2761/// '(' declarator ')'
2762/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002763/// direct-declarator '(' parameter-type-list ')'
2764/// direct-declarator '(' identifier-list[opt] ')'
2765/// [GNU] direct-declarator '(' parameter-forward-declarations
2766/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002767///
2768void Parser::ParseParenDeclarator(Declarator &D) {
2769 SourceLocation StartLoc = ConsumeParen();
2770 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002771
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002772 // Eat any attributes before we look at whether this is a grouping or function
2773 // declarator paren. If this is a grouping paren, the attribute applies to
2774 // the type being built up, for example:
2775 // int (__attribute__(()) *x)(long y)
2776 // If this ends up not being a grouping paren, the attribute applies to the
2777 // first argument, for example:
2778 // int (__attribute__(()) int x)
2779 // In either case, we need to eat any attributes to be able to determine what
2780 // sort of paren this is.
2781 //
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002782 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002783 bool RequiresArg = false;
2784 if (Tok.is(tok::kw___attribute)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002785 AttrList.reset(ParseGNUAttributes());
Mike Stump11289f42009-09-09 15:08:12 +00002786
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002787 // We require that the argument list (if this is a non-grouping paren) be
2788 // present even if the attribute list was empty.
2789 RequiresArg = true;
2790 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002791 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002792 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00002793 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2794 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002795 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman53339e02009-06-08 23:27:34 +00002796 }
Mike Stump11289f42009-09-09 15:08:12 +00002797
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002798 // If we haven't past the identifier yet (or where the identifier would be
2799 // stored, if this is an abstract declarator), then this is probably just
2800 // grouping parens. However, if this could be an abstract-declarator, then
2801 // this could also be the start of function arguments (consider 'void()').
2802 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002803
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002804 if (!D.mayOmitIdentifier()) {
2805 // If this can't be an abstract-declarator, this *must* be a grouping
2806 // paren, because we haven't seen the identifier yet.
2807 isGrouping = true;
2808 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002809 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002810 isDeclarationSpecifier()) { // 'int(int)' is a function.
2811 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2812 // considered to be a type, not a K&R identifier-list.
2813 isGrouping = false;
2814 } else {
2815 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2816 isGrouping = true;
2817 }
Mike Stump11289f42009-09-09 15:08:12 +00002818
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002819 // If this is a grouping paren, handle:
2820 // direct-declarator: '(' declarator ')'
2821 // direct-declarator: '(' attributes declarator ')'
2822 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002823 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002824 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002825 if (AttrList)
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002826 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002827
Sebastian Redlbd150f42008-11-21 19:14:01 +00002828 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002829 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002830 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002831
2832 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002833 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002834 return;
2835 }
Mike Stump11289f42009-09-09 15:08:12 +00002836
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002837 // Okay, if this wasn't a grouping paren, it must be the start of a function
2838 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002839 // identifier (and remember where it would have been), then call into
2840 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002841 D.SetIdentifier(0, Tok.getLocation());
2842
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002843 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002844}
2845
2846/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2847/// declarator D up to a paren, which indicates that we are parsing function
2848/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002849///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002850/// If AttrList is non-null, then the caller parsed those arguments immediately
2851/// after the open paren - they should be considered to be the first argument of
2852/// a parameter. If RequiresArg is true, then the first argument of the
2853/// function is required to be present and required to not be an identifier
2854/// list.
2855///
Chris Lattneracd58a32006-08-06 17:24:14 +00002856/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002857/// parameter-type-list: [C99 6.7.5]
2858/// parameter-list
2859/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002860/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002861///
2862/// parameter-list: [C99 6.7.5]
2863/// parameter-declaration
2864/// parameter-list ',' parameter-declaration
2865///
2866/// parameter-declaration: [C99 6.7.5]
2867/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002868/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002869/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002870/// declaration-specifiers abstract-declarator[opt]
2871/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002872/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002873/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002874///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002875/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002876/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002877///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002878void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2879 AttributeList *AttrList,
2880 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002881 // lparen is already consumed!
2882 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00002883
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002884 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002885 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002886 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002887 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002888 delete AttrList;
2889 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002890
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002891 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2892 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002893
2894 // cv-qualifier-seq[opt].
2895 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002896 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002897 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002898 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002899 llvm::SmallVector<TypeTy*, 2> Exceptions;
2900 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002901 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002902 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002903 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002904 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002905
2906 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002907 if (Tok.is(tok::kw_throw)) {
2908 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002909 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002910 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002911 hasAnyExceptionSpec);
2912 assert(Exceptions.size() == ExceptionRanges.size() &&
2913 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002914 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002915 }
2916
Chris Lattner371ed4e2008-04-06 06:57:35 +00002917 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002918 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002919 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002920 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002921 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002922 /*arglist*/ 0, 0,
2923 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002924 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002925 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002926 Exceptions.data(),
2927 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002928 Exceptions.size(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002929 LParenLoc, RParenLoc, D),
2930 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002931 return;
Sebastian Redld6434562009-05-29 18:02:33 +00002932 }
2933
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002934 // Alternatively, this parameter list may be an identifier list form for a
2935 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00002936 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
2937 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00002938 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002939 // K&R identifier lists can't have typedefs as identifiers, per
2940 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002941 if (RequiresArg) {
2942 Diag(Tok, diag::err_argument_required_after_attribute);
2943 delete AttrList;
2944 }
Chris Lattner9453ab82010-05-14 17:23:36 +00002945
Steve Naroffb0486722009-01-28 19:16:40 +00002946 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00002947 // normal declarators, not for abstract-declarators. Get the first
2948 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00002949 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00002950 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00002951
2952 // Identifier lists follow a really simple grammar: the identifiers can
2953 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
2954 // identifier lists are really rare in the brave new modern world, and it
2955 // is very common for someone to typo a type in a non-k&r style list. If
2956 // we are presented with something like: "void foo(intptr x, float y)",
2957 // we don't want to start parsing the function declarator as though it is
2958 // a K&R style declarator just because intptr is an invalid type.
2959 //
2960 // To handle this, we check to see if the token after the first identifier
2961 // is a "," or ")". Only if so, do we parse it as an identifier list.
2962 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
2963 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
2964 FirstTok.getIdentifierInfo(),
2965 FirstTok.getLocation(), D);
2966
2967 // If we get here, the code is invalid. Push the first identifier back
2968 // into the token stream and parse the first argument as an (invalid)
2969 // normal argument declarator.
2970 PP.EnterToken(Tok);
2971 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002972 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002973 }
Mike Stump11289f42009-09-09 15:08:12 +00002974
Chris Lattner371ed4e2008-04-06 06:57:35 +00002975 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00002976
Chris Lattner371ed4e2008-04-06 06:57:35 +00002977 // Build up an array of information about the parsed arguments.
2978 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002979
2980 // Enter function-declaration scope, limiting any declarators to the
2981 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00002982 ParseScope PrototypeScope(this,
2983 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00002984
Chris Lattner371ed4e2008-04-06 06:57:35 +00002985 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002986 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00002987 while (1) {
2988 if (Tok.is(tok::ellipsis)) {
2989 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002990 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002991 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00002992 }
Mike Stump11289f42009-09-09 15:08:12 +00002993
Chris Lattner371ed4e2008-04-06 06:57:35 +00002994 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002995
Chris Lattner371ed4e2008-04-06 06:57:35 +00002996 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00002997 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002998 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002999
3000 // If the caller parsed attributes for the first argument, add them now.
3001 if (AttrList) {
3002 DS.AddAttributes(AttrList);
3003 AttrList = 0; // Only apply the attributes to the first parameter.
3004 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003005 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003006
Chris Lattner371ed4e2008-04-06 06:57:35 +00003007 // Parse the declarator. This is "PrototypeContext", because we must
3008 // accept either 'declarator' or 'abstract-declarator' here.
3009 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3010 ParseDeclarator(ParmDecl);
3011
3012 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003013 if (Tok.is(tok::kw___attribute)) {
3014 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003015 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003016 ParmDecl.AddAttributes(AttrList, Loc);
3017 }
Mike Stump11289f42009-09-09 15:08:12 +00003018
Chris Lattner371ed4e2008-04-06 06:57:35 +00003019 // Remember this parsed parameter in ParamInfo.
3020 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003021
Douglas Gregor4d87df52008-12-16 21:30:33 +00003022 // DefArgToks is used when the parsing of default arguments needs
3023 // to be delayed.
3024 CachedTokens *DefArgToks = 0;
3025
Chris Lattner371ed4e2008-04-06 06:57:35 +00003026 // If no parameter was specified, verify that *something* was specified,
3027 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003028 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3029 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003030 // Completely missing, emit error.
3031 Diag(DSStart, diag::err_missing_param);
3032 } else {
3033 // Otherwise, we have something. Add it and let semantic analysis try
3034 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003035
Chris Lattner371ed4e2008-04-06 06:57:35 +00003036 // Inform the actions module about the parameter declarator, so it gets
3037 // added to the current scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00003038 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003039
3040 // Parse the default argument, if any. We parse the default
3041 // arguments in all dialects; the semantic analysis in
3042 // ActOnParamDefaultArgument will reject the default argument in
3043 // C.
3044 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003045 SourceLocation EqualLoc = Tok.getLocation();
3046
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003047 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003048 if (D.getContext() == Declarator::MemberContext) {
3049 // If we're inside a class definition, cache the tokens
3050 // corresponding to the default argument. We'll actually parse
3051 // them when we see the end of the class definition.
3052 // FIXME: Templates will require something similar.
3053 // FIXME: Can we use a smart pointer for Toks?
3054 DefArgToks = new CachedTokens;
3055
Mike Stump11289f42009-09-09 15:08:12 +00003056 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003057 /*StopAtSemi=*/true,
3058 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003059 delete DefArgToks;
3060 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003061 Actions.ActOnParamDefaultArgumentError(Param);
3062 } else
Mike Stump11289f42009-09-09 15:08:12 +00003063 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003064 (*DefArgToks)[1].getLocation());
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003065 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003066 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003067 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003068
Douglas Gregor4d87df52008-12-16 21:30:33 +00003069 OwningExprResult DefArgResult(ParseAssignmentExpression());
3070 if (DefArgResult.isInvalid()) {
3071 Actions.ActOnParamDefaultArgumentError(Param);
3072 SkipUntil(tok::comma, tok::r_paren, true, true);
3073 } else {
3074 // Inform the actions module about the default argument
3075 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00003076 move(DefArgResult));
Douglas Gregor4d87df52008-12-16 21:30:33 +00003077 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003078 }
3079 }
Mike Stump11289f42009-09-09 15:08:12 +00003080
3081 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3082 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003083 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003084 }
3085
3086 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003087 if (Tok.isNot(tok::comma)) {
3088 if (Tok.is(tok::ellipsis)) {
3089 IsVariadic = true;
3090 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3091
3092 if (!getLang().CPlusPlus) {
3093 // We have ellipsis without a preceding ',', which is ill-formed
3094 // in C. Complain and provide the fix.
3095 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003096 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003097 }
3098 }
3099
3100 break;
3101 }
Mike Stump11289f42009-09-09 15:08:12 +00003102
Chris Lattner371ed4e2008-04-06 06:57:35 +00003103 // Consume the comma.
3104 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003105 }
Mike Stump11289f42009-09-09 15:08:12 +00003106
Chris Lattner371ed4e2008-04-06 06:57:35 +00003107 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00003108 PrototypeScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00003109
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003110 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003111 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3112 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003113
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003114 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003115 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003116 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003117 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00003118 llvm::SmallVector<TypeTy*, 2> Exceptions;
3119 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003120
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003121 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003122 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003123 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003124 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003125 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003126
3127 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003128 if (Tok.is(tok::kw_throw)) {
3129 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003130 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003131 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003132 hasAnyExceptionSpec);
3133 assert(Exceptions.size() == ExceptionRanges.size() &&
3134 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003135 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003136 }
3137
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003138 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003139 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003140 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003141 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003142 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003143 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003144 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003145 Exceptions.data(),
3146 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003147 Exceptions.size(),
3148 LParenLoc, RParenLoc, D),
3149 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003150}
Chris Lattneracd58a32006-08-06 17:24:14 +00003151
Chris Lattner6c940e62008-04-06 06:34:08 +00003152/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3153/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003154/// first identifier has already been consumed, and the current token is the
3155/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003156///
3157/// identifier-list: [C99 6.7.5]
3158/// identifier
3159/// identifier-list ',' identifier
3160///
3161void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003162 IdentifierInfo *FirstIdent,
3163 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003164 Declarator &D) {
3165 // Build up an array of information about the parsed arguments.
3166 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3167 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003168
Chris Lattner6c940e62008-04-06 06:34:08 +00003169 // If there was no identifier specified for the declarator, either we are in
3170 // an abstract-declarator, or we are in a parameter declarator which was found
3171 // to be abstract. In abstract-declarators, identifier lists are not valid:
3172 // diagnose this.
3173 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003174 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003175
Chris Lattner9453ab82010-05-14 17:23:36 +00003176 // The first identifier was already read, and is known to be the first
3177 // identifier in the list. Remember this identifier in ParamInfo.
3178 ParamsSoFar.insert(FirstIdent);
3179 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00003180 DeclPtrTy()));
Mike Stump11289f42009-09-09 15:08:12 +00003181
Chris Lattner6c940e62008-04-06 06:34:08 +00003182 while (Tok.is(tok::comma)) {
3183 // Eat the comma.
3184 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003185
Chris Lattner9186f552008-04-06 06:39:19 +00003186 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003187 if (Tok.isNot(tok::identifier)) {
3188 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003189 SkipUntil(tok::r_paren);
3190 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003191 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003192
Chris Lattner6c940e62008-04-06 06:34:08 +00003193 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003194
3195 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00003196 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003197 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003198
Chris Lattner6c940e62008-04-06 06:34:08 +00003199 // Verify that the argument identifier has not already been mentioned.
3200 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003201 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003202 } else {
3203 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003204 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003205 Tok.getLocation(),
3206 DeclPtrTy()));
Chris Lattner9186f552008-04-06 06:39:19 +00003207 }
Mike Stump11289f42009-09-09 15:08:12 +00003208
Chris Lattner6c940e62008-04-06 06:34:08 +00003209 // Eat the identifier.
3210 ConsumeToken();
3211 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003212
3213 // If we have the closing ')', eat it and we're done.
3214 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3215
Chris Lattner9186f552008-04-06 06:39:19 +00003216 // Remember that we parsed a function type, and remember the attributes. This
3217 // function type is always a K&R style function type, which is not varargs and
3218 // has no prototype.
3219 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003220 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003221 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003222 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003223 /*exception*/false,
3224 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003225 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003226 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003227}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003228
Chris Lattnere8074e62006-08-06 18:30:15 +00003229/// [C90] direct-declarator '[' constant-expression[opt] ']'
3230/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3231/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3232/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3233/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3234void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003235 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003236
Chris Lattner84a11622008-12-18 07:27:21 +00003237 // C array syntax has many features, but by-far the most common is [] and [4].
3238 // This code does a fast path to handle some of the most obvious cases.
3239 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003240 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003241 //FIXME: Use these
3242 CXX0XAttributeList Attr;
3243 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3244 Attr = ParseCXX0XAttributes();
3245 }
3246
Chris Lattner84a11622008-12-18 07:27:21 +00003247 // Remember that we parsed the empty array type.
3248 OwningExprResult NumElements(Actions);
Douglas Gregor04318252009-07-06 15:59:29 +00003249 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3250 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003251 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003252 return;
3253 } else if (Tok.getKind() == tok::numeric_constant &&
3254 GetLookAheadToken(1).is(tok::r_square)) {
3255 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlffbcf962009-01-18 18:53:16 +00003256 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003257 ConsumeToken();
3258
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003259 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003260 //FIXME: Use these
3261 CXX0XAttributeList Attr;
3262 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3263 Attr = ParseCXX0XAttributes();
3264 }
Chris Lattner84a11622008-12-18 07:27:21 +00003265
3266 // If there was an error parsing the assignment-expression, recover.
3267 if (ExprRes.isInvalid())
3268 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00003269
Chris Lattner84a11622008-12-18 07:27:21 +00003270 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00003271 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3272 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003273 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003274 return;
3275 }
Mike Stump11289f42009-09-09 15:08:12 +00003276
Chris Lattnere8074e62006-08-06 18:30:15 +00003277 // If valid, this location is the position where we read the 'static' keyword.
3278 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003279 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003280 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003281
Chris Lattnere8074e62006-08-06 18:30:15 +00003282 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003283 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003284 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003285 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003286
Chris Lattnere8074e62006-08-06 18:30:15 +00003287 // If we haven't already read 'static', check to see if there is one after the
3288 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003289 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003290 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003291
Chris Lattnere8074e62006-08-06 18:30:15 +00003292 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003293 bool isStar = false;
Sebastian Redlc13f2682008-12-09 20:22:58 +00003294 OwningExprResult NumElements(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00003295
Chris Lattner521ff2b2008-04-06 05:26:30 +00003296 // Handle the case where we have '[*]' as the array size. However, a leading
3297 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3298 // the the token after the star is a ']'. Since stars in arrays are
3299 // infrequent, use of lookahead is not costly here.
3300 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003301 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003302
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003303 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003304 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003305 StaticLoc = SourceLocation(); // Drop the static.
3306 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003307 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003308 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003309 // Note, in C89, this production uses the constant-expr production instead
3310 // of assignment-expr. The only difference is that assignment-expr allows
3311 // things like '=' and '*='. Sema rejects these in C89 mode because they
3312 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003313
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003314 // Parse the constant-expression or assignment-expression now (depending
3315 // on dialect).
3316 if (getLang().CPlusPlus)
3317 NumElements = ParseConstantExpression();
3318 else
3319 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003320 }
Mike Stump11289f42009-09-09 15:08:12 +00003321
Chris Lattner62591722006-08-12 18:40:58 +00003322 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003323 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003324 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003325 // If the expression was invalid, skip it.
3326 SkipUntil(tok::r_square);
3327 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003328 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003329
3330 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3331
Alexis Hunt96d5c762009-11-21 08:43:09 +00003332 //FIXME: Use these
3333 CXX0XAttributeList Attr;
3334 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3335 Attr = ParseCXX0XAttributes();
3336 }
3337
Chris Lattner84a11622008-12-18 07:27:21 +00003338 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00003339 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3340 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003341 NumElements.release(),
3342 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003343 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003344}
3345
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003346/// [GNU] typeof-specifier:
3347/// typeof ( expressions )
3348/// typeof ( type-name )
3349/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003350///
3351void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003352 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003353 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003354 SourceLocation StartLoc = ConsumeToken();
3355
John McCalle8595032010-01-13 20:03:27 +00003356 const bool hasParens = Tok.is(tok::l_paren);
3357
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003358 bool isCastExpr;
3359 TypeTy *CastTy;
3360 SourceRange CastRange;
3361 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3362 isCastExpr,
3363 CastTy,
3364 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003365 if (hasParens)
3366 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003367
3368 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003369 // FIXME: Not accurate, the range gets one token more than it should.
3370 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003371 else
3372 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003373
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003374 if (isCastExpr) {
3375 if (!CastTy) {
3376 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003377 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003378 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003379
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003380 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003381 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003382 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3383 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003384 DiagID, CastTy))
3385 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003386 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003387 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003388
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003389 // If we get here, the operand to the typeof was an expresion.
3390 if (Operand.isInvalid()) {
3391 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003392 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003393 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003394
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003395 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003396 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003397 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3398 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003399 DiagID, Operand.release()))
3400 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003401}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003402
3403
3404/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3405/// from TryAltiVecVectorToken.
3406bool Parser::TryAltiVecVectorTokenOutOfLine() {
3407 Token Next = NextToken();
3408 switch (Next.getKind()) {
3409 default: return false;
3410 case tok::kw_short:
3411 case tok::kw_long:
3412 case tok::kw_signed:
3413 case tok::kw_unsigned:
3414 case tok::kw_void:
3415 case tok::kw_char:
3416 case tok::kw_int:
3417 case tok::kw_float:
3418 case tok::kw_double:
3419 case tok::kw_bool:
3420 case tok::kw___pixel:
3421 Tok.setKind(tok::kw___vector);
3422 return true;
3423 case tok::identifier:
3424 if (Next.getIdentifierInfo() == Ident_pixel) {
3425 Tok.setKind(tok::kw___vector);
3426 return true;
3427 }
3428 return false;
3429 }
3430}
3431
3432bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3433 const char *&PrevSpec, unsigned &DiagID,
3434 bool &isInvalid) {
3435 if (Tok.getIdentifierInfo() == Ident_vector) {
3436 Token Next = NextToken();
3437 switch (Next.getKind()) {
3438 case tok::kw_short:
3439 case tok::kw_long:
3440 case tok::kw_signed:
3441 case tok::kw_unsigned:
3442 case tok::kw_void:
3443 case tok::kw_char:
3444 case tok::kw_int:
3445 case tok::kw_float:
3446 case tok::kw_double:
3447 case tok::kw_bool:
3448 case tok::kw___pixel:
3449 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3450 return true;
3451 case tok::identifier:
3452 if (Next.getIdentifierInfo() == Ident_pixel) {
3453 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3454 return true;
3455 }
3456 break;
3457 default:
3458 break;
3459 }
3460 } else if (Tok.getIdentifierInfo() == Ident_pixel &&
3461 DS.isTypeAltiVecVector()) {
3462 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3463 return true;
3464 }
3465 return false;
3466}
3467