blob: e7650eae84625aeefb9d9452db8f234a1d146517 [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();
John McCallb54367d2010-05-21 20:45:30 +0000367 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, AS_none,
368 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000369 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000370 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000371 }
Mike Stump11289f42009-09-09 15:08:12 +0000372
Chris Lattner005fc1b2010-04-05 18:18:31 +0000373 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld5a36322009-11-03 19:26:08 +0000374}
Mike Stump11289f42009-09-09 15:08:12 +0000375
John McCalld5a36322009-11-03 19:26:08 +0000376/// ParseDeclGroup - Having concluded that this is either a function
377/// definition or a group of object declarations, actually parse the
378/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000379Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
380 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000381 bool AllowFunctionDefinitions,
382 SourceLocation *DeclEnd) {
383 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000384 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000385 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000386
John McCalld5a36322009-11-03 19:26:08 +0000387 // Bail out if the first declarator didn't seem well-formed.
388 if (!D.hasName() && !D.mayOmitIdentifier()) {
389 // Skip until ; or }.
390 SkipUntil(tok::r_brace, true, true);
391 if (Tok.is(tok::semi))
392 ConsumeToken();
393 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000394 }
Mike Stump11289f42009-09-09 15:08:12 +0000395
John McCalld5a36322009-11-03 19:26:08 +0000396 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
397 if (isDeclarationAfterDeclarator()) {
398 // Fall though. We have to check this first, though, because
399 // __attribute__ might be the start of a function definition in
400 // (extended) K&R C.
401 } else if (isStartOfFunctionDefinition()) {
402 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
403 Diag(Tok, diag::err_function_declared_typedef);
404
405 // Recover by treating the 'typedef' as spurious.
406 DS.ClearStorageClassSpecs();
407 }
408
409 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
410 return Actions.ConvertDeclToDeclGroup(TheDecl);
411 } else {
412 Diag(Tok, diag::err_expected_fn_body);
413 SkipUntil(tok::semi);
414 return DeclGroupPtrTy();
415 }
416 }
417
418 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
419 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000420 D.complete(FirstDecl);
John McCalld5a36322009-11-03 19:26:08 +0000421 if (FirstDecl.get())
422 DeclsInGroup.push_back(FirstDecl);
423
424 // If we don't have a comma, it is either the end of the list (a ';') or an
425 // error, bail out.
426 while (Tok.is(tok::comma)) {
427 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000428 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000429
430 // Parse the next declarator.
431 D.clear();
432
433 // Accept attributes in an init-declarator. In the first declarator in a
434 // declaration, these would be part of the declspec. In subsequent
435 // declarators, they become part of the declarator itself, so that they
436 // don't apply to declarators after *this* one. Examples:
437 // short __attribute__((common)) var; -> declspec
438 // short var __attribute__((common)); -> declarator
439 // short x, __attribute__((common)) var; -> declarator
440 if (Tok.is(tok::kw___attribute)) {
441 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000442 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld5a36322009-11-03 19:26:08 +0000443 D.AddAttributes(AttrList, Loc);
444 }
445
446 ParseDeclarator(D);
447
448 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000449 D.complete(ThisDecl);
John McCalld5a36322009-11-03 19:26:08 +0000450 if (ThisDecl.get())
451 DeclsInGroup.push_back(ThisDecl);
452 }
453
454 if (DeclEnd)
455 *DeclEnd = Tok.getLocation();
456
457 if (Context != Declarator::ForContext &&
458 ExpectAndConsume(tok::semi,
459 Context == Declarator::FileContext
460 ? diag::err_invalid_token_after_toplevel_declarator
461 : diag::err_expected_semi_declaration)) {
462 SkipUntil(tok::r_brace, true, true);
463 if (Tok.is(tok::semi))
464 ConsumeToken();
465 }
466
467 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
468 DeclsInGroup.data(),
469 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000470}
471
Douglas Gregor23996282009-05-12 21:31:51 +0000472/// \brief Parse 'declaration' after parsing 'declaration-specifiers
473/// declarator'. This method parses the remainder of the declaration
474/// (including any attributes or initializer, among other things) and
475/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000476///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000477/// init-declarator: [C99 6.7]
478/// declarator
479/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000480/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
481/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000482/// [C++] declarator initializer[opt]
483///
484/// [C++] initializer:
485/// [C++] '=' initializer-clause
486/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000487/// [C++0x] '=' 'default' [TODO]
488/// [C++0x] '=' 'delete'
489///
490/// According to the standard grammar, =default and =delete are function
491/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000492///
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000493Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
494 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000495 // If a simple-asm-expr is present, parse it.
496 if (Tok.is(tok::kw_asm)) {
497 SourceLocation Loc;
498 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
499 if (AsmLabel.isInvalid()) {
500 SkipUntil(tok::semi, true, true);
501 return DeclPtrTy();
502 }
Mike Stump11289f42009-09-09 15:08:12 +0000503
Douglas Gregor23996282009-05-12 21:31:51 +0000504 D.setAsmLabel(AsmLabel.release());
505 D.SetRangeEnd(Loc);
506 }
Mike Stump11289f42009-09-09 15:08:12 +0000507
Douglas Gregor23996282009-05-12 21:31:51 +0000508 // If attributes are present, parse them.
509 if (Tok.is(tok::kw___attribute)) {
510 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000511 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor23996282009-05-12 21:31:51 +0000512 D.AddAttributes(AttrList, Loc);
513 }
Mike Stump11289f42009-09-09 15:08:12 +0000514
Douglas Gregor23996282009-05-12 21:31:51 +0000515 // Inform the current actions module that we just parsed this declarator.
Douglas Gregor450f00842009-09-25 18:43:00 +0000516 DeclPtrTy ThisDecl;
517 switch (TemplateInfo.Kind) {
518 case ParsedTemplateInfo::NonTemplate:
519 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
520 break;
521
522 case ParsedTemplateInfo::Template:
523 case ParsedTemplateInfo::ExplicitSpecialization:
524 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000525 Action::MultiTemplateParamsArg(Actions,
526 TemplateInfo.TemplateParams->data(),
527 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000528 D);
529 break;
530
531 case ParsedTemplateInfo::ExplicitInstantiation: {
532 Action::DeclResult ThisRes
533 = Actions.ActOnExplicitInstantiation(CurScope,
534 TemplateInfo.ExternLoc,
535 TemplateInfo.TemplateLoc,
536 D);
537 if (ThisRes.isInvalid()) {
538 SkipUntil(tok::semi, true, true);
539 return DeclPtrTy();
540 }
541
542 ThisDecl = ThisRes.get();
543 break;
544 }
545 }
Mike Stump11289f42009-09-09 15:08:12 +0000546
Douglas Gregor23996282009-05-12 21:31:51 +0000547 // Parse declarator '=' initializer.
548 if (Tok.is(tok::equal)) {
549 ConsumeToken();
550 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
551 SourceLocation DelLoc = ConsumeToken();
552 Actions.SetDeclDeleted(ThisDecl, DelLoc);
553 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000554 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
555 EnterScope(0);
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000556 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000557 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000558
Douglas Gregor23996282009-05-12 21:31:51 +0000559 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000560
John McCall1f4ee7b2009-12-19 09:28:58 +0000561 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000562 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000563 ExitScope();
564 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000565
Douglas Gregor23996282009-05-12 21:31:51 +0000566 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000567 SkipUntil(tok::comma, true, true);
568 Actions.ActOnInitializerError(ThisDecl);
569 } else
570 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor23996282009-05-12 21:31:51 +0000571 }
572 } else if (Tok.is(tok::l_paren)) {
573 // Parse C++ direct initializer: '(' expression-list ')'
574 SourceLocation LParenLoc = ConsumeParen();
575 ExprVector Exprs(Actions);
576 CommaLocsTy CommaLocs;
577
Douglas Gregor613bf102009-12-22 17:47:17 +0000578 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
579 EnterScope(0);
580 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
581 }
582
Douglas Gregor23996282009-05-12 21:31:51 +0000583 if (ParseExpressionList(Exprs, CommaLocs)) {
584 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000585
586 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
587 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
588 ExitScope();
589 }
Douglas Gregor23996282009-05-12 21:31:51 +0000590 } else {
591 // Match the ')'.
592 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
593
594 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
595 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000596
597 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
598 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
599 ExitScope();
600 }
601
Douglas Gregor23996282009-05-12 21:31:51 +0000602 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
603 move_arg(Exprs),
Jay Foad7d0479f2009-05-21 09:52:38 +0000604 CommaLocs.data(), RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000605 }
606 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000607 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000608 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
609 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000610 }
611
612 return ThisDecl;
613}
614
Chris Lattner1890ac82006-08-13 01:16:23 +0000615/// ParseSpecifierQualifierList
616/// specifier-qualifier-list:
617/// type-specifier specifier-qualifier-list[opt]
618/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000619/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000620///
621void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
622 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
623 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000624 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000625
Chris Lattner1890ac82006-08-13 01:16:23 +0000626 // Validate declspec for type-name.
627 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000628 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
629 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000630 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000631
Chris Lattner1b22eed2006-11-28 05:12:07 +0000632 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000633 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000634 if (DS.getStorageClassSpecLoc().isValid())
635 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
636 else
637 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000638 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000639 }
Mike Stump11289f42009-09-09 15:08:12 +0000640
Chris Lattner1b22eed2006-11-28 05:12:07 +0000641 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000642 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000643 if (DS.isInlineSpecified())
644 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
645 if (DS.isVirtualSpecified())
646 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
647 if (DS.isExplicitSpecified())
648 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000649 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000650 }
651}
Chris Lattner53361ac2006-08-10 05:19:57 +0000652
Chris Lattner6cc055a2009-04-12 20:42:31 +0000653/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
654/// specified token is valid after the identifier in a declarator which
655/// immediately follows the declspec. For example, these things are valid:
656///
657/// int x [ 4]; // direct-declarator
658/// int x ( int y); // direct-declarator
659/// int(int x ) // direct-declarator
660/// int x ; // simple-declaration
661/// int x = 17; // init-declarator-list
662/// int x , y; // init-declarator-list
663/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000664/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000665/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000666///
667/// This is not, because 'x' does not immediately follow the declspec (though
668/// ')' happens to be valid anyway).
669/// int (x)
670///
671static bool isValidAfterIdentifierInDeclarator(const Token &T) {
672 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
673 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000674 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000675}
676
Chris Lattner20a0c612009-04-14 21:34:55 +0000677
678/// ParseImplicitInt - This method is called when we have an non-typename
679/// identifier in a declspec (which normally terminates the decl spec) when
680/// the declspec has no type specifier. In this case, the declspec is either
681/// malformed or is "implicit int" (in K&R and C89).
682///
683/// This method handles diagnosing this prettily and returns false if the
684/// declspec is done being processed. If it recovers and thinks there may be
685/// other pieces of declspec after it, it returns true.
686///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000687bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000688 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000689 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000690 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000691
Chris Lattner20a0c612009-04-14 21:34:55 +0000692 SourceLocation Loc = Tok.getLocation();
693 // If we see an identifier that is not a type name, we normally would
694 // parse it as the identifer being declared. However, when a typename
695 // is typo'd or the definition is not included, this will incorrectly
696 // parse the typename as the identifier name and fall over misparsing
697 // later parts of the diagnostic.
698 //
699 // As such, we try to do some look-ahead in cases where this would
700 // otherwise be an "implicit-int" case to see if this is invalid. For
701 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
702 // an identifier with implicit int, we'd get a parse error because the
703 // next token is obviously invalid for a type. Parse these as a case
704 // with an invalid type specifier.
705 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000706
Chris Lattner20a0c612009-04-14 21:34:55 +0000707 // Since we know that this either implicit int (which is rare) or an
708 // error, we'd do lookahead to try to do better recovery.
709 if (isValidAfterIdentifierInDeclarator(NextToken())) {
710 // If this token is valid for implicit int, e.g. "static x = 4", then
711 // we just avoid eating the identifier, so it will be parsed as the
712 // identifier in the declarator.
713 return false;
714 }
Mike Stump11289f42009-09-09 15:08:12 +0000715
Chris Lattner20a0c612009-04-14 21:34:55 +0000716 // Otherwise, if we don't consume this token, we are going to emit an
717 // error anyway. Try to recover from various common problems. Check
718 // to see if this was a reference to a tag name without a tag specified.
719 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000720 //
721 // C++ doesn't need this, and isTagName doesn't take SS.
722 if (SS == 0) {
723 const char *TagName = 0;
724 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000725
Chris Lattner20a0c612009-04-14 21:34:55 +0000726 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
727 default: break;
728 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
729 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
730 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
731 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
732 }
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000734 if (TagName) {
735 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +0000736 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +0000737 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000738
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000739 // Parse this as a tag as if the missing tag were present.
740 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +0000741 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000742 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000743 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000744 return true;
745 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000746 }
Mike Stump11289f42009-09-09 15:08:12 +0000747
Douglas Gregor15e56022009-10-13 23:27:22 +0000748 // This is almost certainly an invalid type name. Let the action emit a
749 // diagnostic and attempt to recover.
750 Action::TypeTy *T = 0;
751 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
752 CurScope, SS, T)) {
753 // The action emitted a diagnostic, so we don't have to.
754 if (T) {
755 // The action has suggested that the type T could be used. Set that as
756 // the type in the declaration specifiers, consume the would-be type
757 // name token, and we're done.
758 const char *PrevSpec;
759 unsigned DiagID;
760 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
761 false);
762 DS.SetRangeEnd(Tok.getLocation());
763 ConsumeToken();
764
765 // There may be other declaration specifiers after this.
766 return true;
767 }
768
769 // Fall through; the action had no suggestion for us.
770 } else {
771 // The action did not emit a diagnostic, so emit one now.
772 SourceRange R;
773 if (SS) R = SS->getRange();
774 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
Douglas Gregor15e56022009-10-13 23:27:22 +0000777 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000778 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000779 unsigned DiagID;
780 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000781 DS.SetRangeEnd(Tok.getLocation());
782 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000783
Chris Lattner20a0c612009-04-14 21:34:55 +0000784 // TODO: Could inject an invalid typedef decl in an enclosing scope to
785 // avoid rippling error messages on subsequent uses of the same type,
786 // could be useful if #include was forgotten.
787 return false;
788}
789
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000790/// \brief Determine the declaration specifier context from the declarator
791/// context.
792///
793/// \param Context the declarator context, which is one of the
794/// Declarator::TheContext enumerator values.
795Parser::DeclSpecContext
796Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
797 if (Context == Declarator::MemberContext)
798 return DSC_class;
799 if (Context == Declarator::FileContext)
800 return DSC_top_level;
801 return DSC_normal;
802}
803
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000804/// ParseDeclarationSpecifiers
805/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000806/// storage-class-specifier declaration-specifiers[opt]
807/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000808/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000809/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000810///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000811/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000812/// 'typedef'
813/// 'extern'
814/// 'static'
815/// 'auto'
816/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000817/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000818/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000819/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000820/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000821/// [C++] 'virtual'
822/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000823/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000824/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000825
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000826///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000827void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000828 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000829 AccessSpecifier AS,
830 DeclSpecContext DSContext) {
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000831 if (Tok.is(tok::code_completion)) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000832 Action::CodeCompletionContext CCC = Action::CCC_Namespace;
833 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
834 CCC = DSContext == DSC_class? Action::CCC_MemberTemplate
835 : Action::CCC_Template;
836 else if (DSContext == DSC_class)
837 CCC = Action::CCC_Class;
Douglas Gregorf1934162010-01-13 21:24:21 +0000838 else if (ObjCImpDecl)
839 CCC = Action::CCC_ObjCImplementation;
840
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000841 Actions.CodeCompleteOrdinaryName(CurScope, CCC);
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000842 ConsumeToken();
843 }
844
Chris Lattner2e232092008-03-13 06:29:04 +0000845 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000846 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000847 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000848 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000849 unsigned DiagID = 0;
850
Chris Lattner4d8f8732006-11-28 05:05:08 +0000851 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000852
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000853 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000854 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000855 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000856 // If this is not a declaration specifier token, we're done reading decl
857 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000858 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000859 return;
Mike Stump11289f42009-09-09 15:08:12 +0000860
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000861 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +0000862 // C++ scope specifier. Annotate and loop, or bail out on error.
863 if (TryAnnotateCXXScopeToken(true)) {
864 if (!DS.hasTypeSpecifier())
865 DS.SetTypeSpecError();
866 goto DoneWithDeclSpec;
867 }
John McCall8bc2a702010-03-01 18:20:46 +0000868 if (Tok.is(tok::coloncolon)) // ::new or ::delete
869 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +0000870 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000871
872 case tok::annot_cxxscope: {
873 if (DS.hasTypeSpecifier())
874 goto DoneWithDeclSpec;
875
John McCall9dab4e62009-12-12 11:40:51 +0000876 CXXScopeSpec SS;
877 SS.setScopeRep(Tok.getAnnotationValue());
878 SS.setRange(Tok.getAnnotationRange());
879
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000880 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000881 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000882 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000883 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000884 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000885 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000886
887 // C++ [class.qual]p2:
888 // In a lookup in which the constructor is an acceptable lookup
889 // result and the nested-name-specifier nominates a class C:
890 //
891 // - if the name specified after the
892 // nested-name-specifier, when looked up in C, is the
893 // injected-class-name of C (Clause 9), or
894 //
895 // - if the name specified after the nested-name-specifier
896 // is the same as the identifier or the
897 // simple-template-id's template-name in the last
898 // component of the nested-name-specifier,
899 //
900 // the name is instead considered to name the constructor of
901 // class C.
902 //
903 // Thus, if the template-name is actually the constructor
904 // name, then the code is ill-formed; this interpretation is
905 // reinforced by the NAD status of core issue 635.
906 TemplateIdAnnotation *TemplateId
907 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +0000908 if ((DSContext == DSC_top_level ||
909 (DSContext == DSC_class && DS.isFriendSpecified())) &&
910 TemplateId->Name &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000911 Actions.isCurrentClassName(*TemplateId->Name, CurScope, &SS)) {
912 if (isConstructorDeclarator()) {
913 // The user meant this to be an out-of-line constructor
914 // definition, but template arguments are not allowed
915 // there. Just allow this as a constructor; we'll
916 // complain about it later.
917 goto DoneWithDeclSpec;
918 }
919
920 // The user meant this to name a type, but it actually names
921 // a constructor with some extraneous template
922 // arguments. Complain, then parse it as a type as the user
923 // intended.
924 Diag(TemplateId->TemplateNameLoc,
925 diag::err_out_of_line_template_id_names_constructor)
926 << TemplateId->Name;
927 }
928
John McCall9dab4e62009-12-12 11:40:51 +0000929 DS.getTypeSpecScope() = SS;
930 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +0000931 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000932 "ParseOptionalCXXScopeSpecifier not working");
933 AnnotateTemplateIdTokenAsType(&SS);
934 continue;
935 }
936
Douglas Gregorc5790df2009-09-28 07:26:33 +0000937 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +0000938 DS.getTypeSpecScope() = SS;
939 ConsumeToken(); // The C++ scope.
Douglas Gregorc5790df2009-09-28 07:26:33 +0000940 if (Tok.getAnnotationValue())
941 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
942 PrevSpec, DiagID,
943 Tok.getAnnotationValue());
944 else
945 DS.SetTypeSpecError();
946 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
947 ConsumeToken(); // The typename
948 }
949
Douglas Gregor167fa622009-03-25 15:40:00 +0000950 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000951 goto DoneWithDeclSpec;
952
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000953 // If we're in a context where the identifier could be a class name,
954 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +0000955 if ((DSContext == DSC_top_level ||
956 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000957 Actions.isCurrentClassName(*Next.getIdentifierInfo(), CurScope,
958 &SS)) {
959 if (isConstructorDeclarator())
960 goto DoneWithDeclSpec;
961
962 // As noted in C++ [class.qual]p2 (cited above), when the name
963 // of the class is qualified in a context where it could name
964 // a constructor, its a constructor name. However, we've
965 // looked at the declarator, and the user probably meant this
966 // to be a type. Complain that it isn't supposed to be treated
967 // as a type, then proceed to parse it as a type.
968 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
969 << Next.getIdentifierInfo();
970 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000971
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000972 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
973 Next.getLocation(), CurScope, &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000974
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000975 // If the referenced identifier is not a type, then this declspec is
976 // erroneous: We already checked about that it has no type specifier, and
977 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +0000978 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000979 if (TypeRep == 0) {
980 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000981 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000982 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000983 }
Mike Stump11289f42009-09-09 15:08:12 +0000984
John McCall9dab4e62009-12-12 11:40:51 +0000985 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000986 ConsumeToken(); // The C++ scope.
987
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000988 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000989 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000990 if (isInvalid)
991 break;
Mike Stump11289f42009-09-09 15:08:12 +0000992
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000993 DS.SetRangeEnd(Tok.getLocation());
994 ConsumeToken(); // The typename.
995
996 continue;
997 }
Mike Stump11289f42009-09-09 15:08:12 +0000998
Chris Lattnere387d9e2009-01-21 19:48:37 +0000999 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001000 if (Tok.getAnnotationValue())
1001 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001002 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001003 else
1004 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001005
1006 if (isInvalid)
1007 break;
1008
Chris Lattnere387d9e2009-01-21 19:48:37 +00001009 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1010 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001011
Chris Lattnere387d9e2009-01-21 19:48:37 +00001012 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1013 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1014 // Objective-C interface. If we don't have Objective-C or a '<', this is
1015 // just a normal reference to a typedef name.
1016 if (!Tok.is(tok::less) || !getLang().ObjC1)
1017 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001018
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001019 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001020 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001021 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1022 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1023 LAngleLoc, EndProtoLoc);
1024 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1025 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001026
Chris Lattnere387d9e2009-01-21 19:48:37 +00001027 DS.SetRangeEnd(EndProtoLoc);
1028 continue;
1029 }
Mike Stump11289f42009-09-09 15:08:12 +00001030
Chris Lattner16fac4f2008-07-26 01:18:38 +00001031 // typedef-name
1032 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001033 // In C++, check to see if this is a scope specifier like foo::bar::, if
1034 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001035 if (getLang().CPlusPlus) {
1036 if (TryAnnotateCXXScopeToken(true)) {
1037 if (!DS.hasTypeSpecifier())
1038 DS.SetTypeSpecError();
1039 goto DoneWithDeclSpec;
1040 }
1041 if (!Tok.is(tok::identifier))
1042 continue;
1043 }
Mike Stump11289f42009-09-09 15:08:12 +00001044
Chris Lattner16fac4f2008-07-26 01:18:38 +00001045 // This identifier can only be a typedef name if we haven't already seen
1046 // a type-specifier. Without this check we misparse:
1047 // typedef int X; struct Y { short X; }; as 'short int'.
1048 if (DS.hasTypeSpecifier())
1049 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001050
John Thompson22334602010-02-05 00:12:22 +00001051 // Check for need to substitute AltiVec keyword tokens.
1052 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1053 break;
1054
Chris Lattner16fac4f2008-07-26 01:18:38 +00001055 // It has to be available as a typedef too!
Mike Stump11289f42009-09-09 15:08:12 +00001056 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00001057 Tok.getLocation(), CurScope);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001058
Chris Lattner6cc055a2009-04-12 20:42:31 +00001059 // If this is not a typedef name, don't parse it as part of the declspec,
1060 // it must be an implicit int or an error.
1061 if (TypeRep == 0) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001062 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001063 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001064 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001065
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001066 // If we're in a context where the identifier could be a class name,
1067 // check whether this is a constructor declaration.
1068 if (getLang().CPlusPlus && DSContext == DSC_class &&
Mike Stump11289f42009-09-09 15:08:12 +00001069 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001070 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001071 goto DoneWithDeclSpec;
1072
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001073 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001074 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001075 if (isInvalid)
1076 break;
Mike Stump11289f42009-09-09 15:08:12 +00001077
Chris Lattner16fac4f2008-07-26 01:18:38 +00001078 DS.SetRangeEnd(Tok.getLocation());
1079 ConsumeToken(); // The identifier
1080
1081 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1082 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1083 // Objective-C interface. If we don't have Objective-C or a '<', this is
1084 // just a normal reference to a typedef name.
1085 if (!Tok.is(tok::less) || !getLang().ObjC1)
1086 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001087
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001088 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001089 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001090 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1091 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1092 LAngleLoc, EndProtoLoc);
1093 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1094 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001095
Chris Lattner16fac4f2008-07-26 01:18:38 +00001096 DS.SetRangeEnd(EndProtoLoc);
1097
Steve Naroffcd5e7822008-09-22 10:28:57 +00001098 // Need to support trailing type qualifiers (e.g. "id<p> const").
1099 // If a type specifier follows, it will be diagnosed elsewhere.
1100 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001101 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001102
1103 // type-name
1104 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001105 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001106 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001107 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001108 // This template-id does not refer to a type name, so we're
1109 // done with the type-specifiers.
1110 goto DoneWithDeclSpec;
1111 }
1112
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001113 // If we're in a context where the template-id could be a
1114 // constructor name or specialization, check whether this is a
1115 // constructor declaration.
1116 if (getLang().CPlusPlus && DSContext == DSC_class &&
1117 Actions.isCurrentClassName(*TemplateId->Name, CurScope) &&
1118 isConstructorDeclarator())
1119 goto DoneWithDeclSpec;
1120
Douglas Gregor7f741122009-02-25 19:37:18 +00001121 // Turn the template-id annotation token into a type annotation
1122 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001123 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001124 continue;
1125 }
1126
Chris Lattnere37e2332006-08-15 04:50:22 +00001127 // GNU attributes support.
1128 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001129 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001130 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001131
1132 // Microsoft declspec support.
1133 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +00001134 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001135 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001136
Steve Naroff44ac7772008-12-25 14:16:32 +00001137 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001138 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001139 // FIXME: Add handling here!
1140 break;
1141
1142 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001143 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001144 case tok::kw___cdecl:
1145 case tok::kw___stdcall:
1146 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001147 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001148 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1149 continue;
1150
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001151 // storage-class-specifier
1152 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001153 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1154 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001155 break;
1156 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001157 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001158 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001159 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1160 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001161 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001162 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001163 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001164 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001165 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001166 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001167 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001168 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001169 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1170 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001171 break;
1172 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001173 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001174 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1175 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001176 else
John McCall49bfce42009-08-03 20:12:06 +00001177 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1178 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001179 break;
1180 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001181 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1182 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001183 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001184 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001185 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1186 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001187 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001188 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001189 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001190 break;
Mike Stump11289f42009-09-09 15:08:12 +00001191
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001192 // function-specifier
1193 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001194 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001195 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001196 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001197 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001198 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001199 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001200 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001201 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001202
Anders Carlssoncd8db412009-05-06 04:46:28 +00001203 // friend
1204 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001205 if (DSContext == DSC_class)
1206 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1207 else {
1208 PrevSpec = ""; // not actually used by the diagnostic
1209 DiagID = diag::err_friend_invalid_in_context;
1210 isInvalid = true;
1211 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001212 break;
Mike Stump11289f42009-09-09 15:08:12 +00001213
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001214 // constexpr
1215 case tok::kw_constexpr:
1216 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1217 break;
1218
Chris Lattnere387d9e2009-01-21 19:48:37 +00001219 // type-specifier
1220 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001221 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1222 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001223 break;
1224 case tok::kw_long:
1225 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001226 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1227 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001228 else
John McCall49bfce42009-08-03 20:12:06 +00001229 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1230 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001231 break;
1232 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001233 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1234 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001235 break;
1236 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001237 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1238 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001239 break;
1240 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001241 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1242 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001243 break;
1244 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001245 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1246 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001247 break;
1248 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001249 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1250 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001251 break;
1252 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001253 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1254 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001255 break;
1256 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001257 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1258 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001259 break;
1260 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001261 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1262 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001263 break;
1264 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001265 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1266 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001267 break;
1268 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001269 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1270 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001271 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001272 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001273 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1274 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001275 break;
1276 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001277 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1278 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001279 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001280 case tok::kw_bool:
1281 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001282 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1283 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001284 break;
1285 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001286 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1287 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001288 break;
1289 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001290 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1291 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001292 break;
1293 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001294 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1295 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001296 break;
John Thompson22334602010-02-05 00:12:22 +00001297 case tok::kw___vector:
1298 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1299 break;
1300 case tok::kw___pixel:
1301 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1302 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001303
1304 // class-specifier:
1305 case tok::kw_class:
1306 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001307 case tok::kw_union: {
1308 tok::TokenKind Kind = Tok.getKind();
1309 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001310 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001311 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001312 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001313
1314 // enum-specifier:
1315 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001316 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001317 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001318 continue;
1319
1320 // cv-qualifier:
1321 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001322 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1323 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001324 break;
1325 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001326 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1327 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001328 break;
1329 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001330 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1331 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001332 break;
1333
Douglas Gregor333489b2009-03-27 23:10:48 +00001334 // C++ typename-specifier:
1335 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001336 if (TryAnnotateTypeOrScopeToken()) {
1337 DS.SetTypeSpecError();
1338 goto DoneWithDeclSpec;
1339 }
1340 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001341 continue;
1342 break;
1343
Chris Lattnere387d9e2009-01-21 19:48:37 +00001344 // GNU typeof support.
1345 case tok::kw_typeof:
1346 ParseTypeofSpecifier(DS);
1347 continue;
1348
Anders Carlsson74948d02009-06-24 17:47:40 +00001349 case tok::kw_decltype:
1350 ParseDecltypeSpecifier(DS);
1351 continue;
1352
Steve Naroffcfdf6162008-06-05 00:02:44 +00001353 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001354 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001355 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1356 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001357 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001358 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001359
Chris Lattner0974b232008-07-26 00:20:22 +00001360 {
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001361 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001362 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001363 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1364 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1365 LAngleLoc, EndProtoLoc);
1366 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1367 ProtocolLocs.data(), LAngleLoc);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001368 DS.SetRangeEnd(EndProtoLoc);
1369
Chris Lattner6d29c102008-11-18 07:48:38 +00001370 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregora771f462010-03-31 17:46:05 +00001371 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001372 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001373 // Need to support trailing type qualifiers (e.g. "id<p> const").
1374 // If a type specifier follows, it will be diagnosed elsewhere.
1375 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001376 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001377 }
John McCall49bfce42009-08-03 20:12:06 +00001378 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001379 if (isInvalid) {
1380 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001381 assert(DiagID);
Chris Lattner6d29c102008-11-18 07:48:38 +00001382 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001383 }
Chris Lattner2e232092008-03-13 06:29:04 +00001384 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001385 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001386 }
1387}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001388
Chris Lattnera448d752009-01-06 06:59:53 +00001389/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001390/// primarily follow the C++ grammar with additions for C99 and GNU,
1391/// which together subsume the C grammar. Note that the C++
1392/// type-specifier also includes the C type-qualifier (for const,
1393/// volatile, and C99 restrict). Returns true if a type-specifier was
1394/// found (and parsed), false otherwise.
1395///
1396/// type-specifier: [C++ 7.1.5]
1397/// simple-type-specifier
1398/// class-specifier
1399/// enum-specifier
1400/// elaborated-type-specifier [TODO]
1401/// cv-qualifier
1402///
1403/// cv-qualifier: [C++ 7.1.5.1]
1404/// 'const'
1405/// 'volatile'
1406/// [C99] 'restrict'
1407///
1408/// simple-type-specifier: [ C++ 7.1.5.2]
1409/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1410/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1411/// 'char'
1412/// 'wchar_t'
1413/// 'bool'
1414/// 'short'
1415/// 'int'
1416/// 'long'
1417/// 'signed'
1418/// 'unsigned'
1419/// 'float'
1420/// 'double'
1421/// 'void'
1422/// [C99] '_Bool'
1423/// [C99] '_Complex'
1424/// [C99] '_Imaginary' // Removed in TC2?
1425/// [GNU] '_Decimal32'
1426/// [GNU] '_Decimal64'
1427/// [GNU] '_Decimal128'
1428/// [GNU] typeof-specifier
1429/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1430/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001431/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001432/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001433bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001434 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001435 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001436 const ParsedTemplateInfo &TemplateInfo,
1437 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001438 SourceLocation Loc = Tok.getLocation();
1439
1440 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001441 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001442 // If we already have a type specifier, this identifier is not a type.
1443 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1444 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1445 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1446 return false;
John Thompson22334602010-02-05 00:12:22 +00001447 // Check for need to substitute AltiVec keyword tokens.
1448 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1449 break;
1450 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001451 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001452 // Annotate typenames and C++ scope specifiers. If we get one, just
1453 // recurse to handle whatever we get.
1454 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001455 return true;
1456 if (Tok.is(tok::identifier))
1457 return false;
1458 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1459 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001460 case tok::coloncolon: // ::foo::bar
1461 if (NextToken().is(tok::kw_new) || // ::new
1462 NextToken().is(tok::kw_delete)) // ::delete
1463 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001464
Chris Lattner020bab92009-01-04 23:41:41 +00001465 // Annotate typenames and C++ scope specifiers. If we get one, just
1466 // recurse to handle whatever we get.
1467 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001468 return true;
1469 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1470 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001471
Douglas Gregor450c75a2008-11-07 15:42:26 +00001472 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001473 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001474 if (Tok.getAnnotationValue())
1475 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001476 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001477 else
1478 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001479 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1480 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001481
Douglas Gregor450c75a2008-11-07 15:42:26 +00001482 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1483 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1484 // Objective-C interface. If we don't have Objective-C or a '<', this is
1485 // just a normal reference to a typedef name.
1486 if (!Tok.is(tok::less) || !getLang().ObjC1)
1487 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001488
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001489 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001490 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001491 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1492 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1493 LAngleLoc, EndProtoLoc);
1494 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1495 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001496
Douglas Gregor450c75a2008-11-07 15:42:26 +00001497 DS.SetRangeEnd(EndProtoLoc);
1498 return true;
1499 }
1500
1501 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001502 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001503 break;
1504 case tok::kw_long:
1505 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001506 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1507 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001508 else
John McCall49bfce42009-08-03 20:12:06 +00001509 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1510 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001511 break;
1512 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001513 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001514 break;
1515 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001516 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1517 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001518 break;
1519 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001520 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1521 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001522 break;
1523 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001524 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1525 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001526 break;
1527 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001528 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001529 break;
1530 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001531 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001532 break;
1533 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001534 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001535 break;
1536 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001537 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001538 break;
1539 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001540 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001541 break;
1542 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001543 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001544 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001545 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001546 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001547 break;
1548 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001549 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001550 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001551 case tok::kw_bool:
1552 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001553 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001554 break;
1555 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001556 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1557 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001558 break;
1559 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001560 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1561 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001562 break;
1563 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001564 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1565 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001566 break;
John Thompson22334602010-02-05 00:12:22 +00001567 case tok::kw___vector:
1568 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1569 break;
1570 case tok::kw___pixel:
1571 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1572 break;
1573
Douglas Gregor450c75a2008-11-07 15:42:26 +00001574 // class-specifier:
1575 case tok::kw_class:
1576 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001577 case tok::kw_union: {
1578 tok::TokenKind Kind = Tok.getKind();
1579 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001580 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1581 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001582 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001583 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001584
1585 // enum-specifier:
1586 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001587 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001588 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001589 return true;
1590
1591 // cv-qualifier:
1592 case tok::kw_const:
1593 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001594 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001595 break;
1596 case tok::kw_volatile:
1597 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001598 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001599 break;
1600 case tok::kw_restrict:
1601 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001602 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001603 break;
1604
1605 // GNU typeof support.
1606 case tok::kw_typeof:
1607 ParseTypeofSpecifier(DS);
1608 return true;
1609
Anders Carlsson74948d02009-06-24 17:47:40 +00001610 // C++0x decltype support.
1611 case tok::kw_decltype:
1612 ParseDecltypeSpecifier(DS);
1613 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001614
Anders Carlssonbae27372009-06-26 23:44:14 +00001615 // C++0x auto support.
1616 case tok::kw_auto:
1617 if (!getLang().CPlusPlus0x)
1618 return false;
1619
John McCall49bfce42009-08-03 20:12:06 +00001620 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001621 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001622 case tok::kw___ptr64:
1623 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001624 case tok::kw___cdecl:
1625 case tok::kw___stdcall:
1626 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001627 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001628 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001629 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001630
Douglas Gregor450c75a2008-11-07 15:42:26 +00001631 default:
1632 // Not a type-specifier; do nothing.
1633 return false;
1634 }
1635
1636 // If the specifier combination wasn't legal, issue a diagnostic.
1637 if (isInvalid) {
1638 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001639 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001640 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001641 }
1642 DS.SetRangeEnd(Tok.getLocation());
1643 ConsumeToken(); // whatever we parsed above.
1644 return true;
1645}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001646
Chris Lattner70ae4912007-10-29 04:42:53 +00001647/// ParseStructDeclaration - Parse a struct declaration without the terminating
1648/// semicolon.
1649///
Chris Lattner90a26b02007-01-23 04:38:16 +00001650/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001651/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001652/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001653/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001654/// struct-declarator-list:
1655/// struct-declarator
1656/// struct-declarator-list ',' struct-declarator
1657/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1658/// struct-declarator:
1659/// declarator
1660/// [GNU] declarator attributes[opt]
1661/// declarator[opt] ':' constant-expression
1662/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1663///
Chris Lattnera12405b2008-04-10 06:46:29 +00001664void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001665ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001666 if (Tok.is(tok::kw___extension__)) {
1667 // __extension__ silences extension warnings in the subexpression.
1668 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001669 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001670 return ParseStructDeclaration(DS, Fields);
1671 }
Mike Stump11289f42009-09-09 15:08:12 +00001672
Steve Naroff97170802007-08-20 22:28:22 +00001673 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001674 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001675 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001676
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001677 // If there are no declarators, this is a free-standing declaration
1678 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001679 if (Tok.is(tok::semi)) {
John McCallb54367d2010-05-21 20:45:30 +00001680 Actions.ParsedFreeStandingDeclSpec(CurScope, AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001681 return;
1682 }
1683
1684 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001685 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001686 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001687 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001688 FieldDeclarator DeclaratorInfo(DS);
1689
1690 // Attributes are only allowed here on successive declarators.
1691 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1692 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001693 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallcfefb6d2009-11-03 02:38:08 +00001694 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1695 }
Mike Stump11289f42009-09-09 15:08:12 +00001696
Steve Naroff97170802007-08-20 22:28:22 +00001697 /// struct-declarator: declarator
1698 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001699 if (Tok.isNot(tok::colon)) {
1700 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1701 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001702 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001703 }
Mike Stump11289f42009-09-09 15:08:12 +00001704
Chris Lattner76c72282007-10-09 17:33:22 +00001705 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001706 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001707 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001708 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001709 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001710 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001711 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001712 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001713
Steve Naroff97170802007-08-20 22:28:22 +00001714 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001715 if (Tok.is(tok::kw___attribute)) {
1716 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001717 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001718 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1719 }
1720
John McCallcfefb6d2009-11-03 02:38:08 +00001721 // We're done with this declarator; invoke the callback.
John McCall28a6aea2009-11-04 02:18:39 +00001722 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1723 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001724
Steve Naroff97170802007-08-20 22:28:22 +00001725 // If we don't have a comma, it is either the end of the list (a ';')
1726 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001727 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001728 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001729
Steve Naroff97170802007-08-20 22:28:22 +00001730 // Consume the comma.
1731 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001732
John McCallcfefb6d2009-11-03 02:38:08 +00001733 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001734 }
Steve Naroff97170802007-08-20 22:28:22 +00001735}
1736
1737/// ParseStructUnionBody
1738/// struct-contents:
1739/// struct-declaration-list
1740/// [EXT] empty
1741/// [GNU] "struct-declaration-list" without terminatoring ';'
1742/// struct-declaration-list:
1743/// struct-declaration
1744/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001745/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001746///
Chris Lattner1300fb92007-01-23 23:42:53 +00001747void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001748 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnereae6cb62009-03-05 08:00:35 +00001749 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1750 PP.getSourceManager(),
1751 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001752
Chris Lattner90a26b02007-01-23 04:38:16 +00001753 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001754
Douglas Gregor658b9552009-01-09 22:42:13 +00001755 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001756 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1757
Chris Lattner7b9ace62007-01-23 20:11:08 +00001758 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1759 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001760 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001761 Diag(Tok, diag::ext_empty_struct_union_enum)
1762 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001763
Chris Lattner83f095c2009-03-28 19:18:32 +00001764 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001765
Chris Lattner7b9ace62007-01-23 20:11:08 +00001766 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001767 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001768 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001769
Chris Lattner736ed5d2007-06-09 05:59:07 +00001770 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001771 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001772 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001773 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001774 ConsumeToken();
1775 continue;
1776 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001777
1778 // Parse all the comma separated declarators.
1779 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001780
John McCallcfefb6d2009-11-03 02:38:08 +00001781 if (!Tok.is(tok::at)) {
1782 struct CFieldCallback : FieldCallback {
1783 Parser &P;
1784 DeclPtrTy TagDecl;
1785 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1786
1787 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1788 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1789 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1790
1791 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001792 // Install the declarator into the current TagDecl.
John McCall5e6253b2009-11-03 21:13:47 +00001793 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1794 FD.D.getDeclSpec().getSourceRange().getBegin(),
1795 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001796 FieldDecls.push_back(Field);
1797 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001798 }
John McCallcfefb6d2009-11-03 02:38:08 +00001799 } Callback(*this, TagDecl, FieldDecls);
1800
1801 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001802 } else { // Handle @defs
1803 ConsumeToken();
1804 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1805 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001806 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001807 continue;
1808 }
1809 ConsumeToken();
1810 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1811 if (!Tok.is(tok::identifier)) {
1812 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001813 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001814 continue;
1815 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001816 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump11289f42009-09-09 15:08:12 +00001817 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001818 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001819 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1820 ConsumeToken();
1821 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001822 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001823
Chris Lattner76c72282007-10-09 17:33:22 +00001824 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001825 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001826 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001827 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001828 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001829 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001830 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1831 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001832 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001833 // If we stopped at a ';', eat it.
1834 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001835 }
1836 }
Mike Stump11289f42009-09-09 15:08:12 +00001837
Steve Naroff33a1e802007-10-29 21:38:07 +00001838 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001839
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001840 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner90a26b02007-01-23 04:38:16 +00001841 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001842 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001843 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar15619c72008-10-03 02:03:53 +00001844
1845 Actions.ActOnFields(CurScope,
Jay Foad7d0479f2009-05-21 09:52:38 +00001846 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001847 LBraceLoc, RBraceLoc,
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001848 AttrList.get());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001849 StructScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001850 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001851}
1852
1853
Chris Lattner3b561a32006-08-13 00:12:11 +00001854/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001855/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001856/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001857///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001858/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1859/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001860/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001861/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001862///
1863/// [C++] elaborated-type-specifier:
1864/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1865///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001866void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001867 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001868 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001869 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001870 if (Tok.is(tok::code_completion)) {
1871 // Code completion for an enum name.
1872 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1873 ConsumeToken();
1874 }
1875
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001876 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001877 // If attributes exist after tag, parse them.
1878 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001879 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001880
Abramo Bagnarad7548482010-05-19 21:37:53 +00001881 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00001882 if (getLang().CPlusPlus) {
1883 if (ParseOptionalCXXScopeSpecifier(SS, 0, false))
1884 return;
1885
1886 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001887 Diag(Tok, diag::err_expected_ident);
1888 if (Tok.isNot(tok::l_brace)) {
1889 // Has no name and is not a definition.
1890 // Skip the rest of this declarator, up until the comma or semicolon.
1891 SkipUntil(tok::comma, true);
1892 return;
1893 }
1894 }
1895 }
Mike Stump11289f42009-09-09 15:08:12 +00001896
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001897 // Must have either 'enum name' or 'enum {...}'.
1898 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1899 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001900
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001901 // Skip the rest of this declarator, up until the comma or semicolon.
1902 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001903 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001904 }
Mike Stump11289f42009-09-09 15:08:12 +00001905
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001906 // If an identifier is present, consume and remember it.
1907 IdentifierInfo *Name = 0;
1908 SourceLocation NameLoc;
1909 if (Tok.is(tok::identifier)) {
1910 Name = Tok.getIdentifierInfo();
1911 NameLoc = ConsumeToken();
1912 }
Mike Stump11289f42009-09-09 15:08:12 +00001913
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001914 // There are three options here. If we have 'enum foo;', then this is a
1915 // forward declaration. If we have 'enum foo {...' then this is a
1916 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1917 //
1918 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1919 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1920 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1921 //
John McCall9bb74a52009-07-31 02:45:11 +00001922 Action::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001923 if (Tok.is(tok::l_brace))
John McCall9bb74a52009-07-31 02:45:11 +00001924 TUK = Action::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001925 else if (Tok.is(tok::semi))
John McCall9bb74a52009-07-31 02:45:11 +00001926 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001927 else
John McCall9bb74a52009-07-31 02:45:11 +00001928 TUK = Action::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00001929
1930 // enums cannot be templates, although they can be referenced from a
1931 // template.
1932 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
1933 TUK != Action::TUK_Reference) {
1934 Diag(Tok, diag::err_enum_template);
1935
1936 // Skip the rest of this declarator, up until the comma or semicolon.
1937 SkipUntil(tok::comma, true);
1938 return;
1939 }
1940
Douglas Gregord6ab8742009-05-28 23:31:59 +00001941 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00001942 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00001943 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
1944 const char *PrevSpec = 0;
1945 unsigned DiagID;
John McCall9bb74a52009-07-31 02:45:11 +00001946 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001947 StartLoc, SS, Name, NameLoc, Attr.get(),
1948 AS,
Douglas Gregor27bdf00f2009-07-23 16:36:45 +00001949 Action::MultiTemplateParamsArg(Actions),
John McCall7f41d982009-09-11 04:59:25 +00001950 Owned, IsDependent);
Douglas Gregorba41d012010-04-24 16:38:41 +00001951 if (IsDependent) {
1952 // This enum has a dependent nested-name-specifier. Handle it as a
1953 // dependent tag.
1954 if (!Name) {
1955 DS.SetTypeSpecError();
1956 Diag(Tok, diag::err_expected_type_name_after_typename);
1957 return;
1958 }
1959
1960 TypeResult Type = Actions.ActOnDependentTag(CurScope, DeclSpec::TST_enum,
1961 TUK, SS, Name, StartLoc,
1962 NameLoc);
1963 if (Type.isInvalid()) {
1964 DS.SetTypeSpecError();
1965 return;
1966 }
1967
1968 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
1969 Type.get(), false))
1970 Diag(StartLoc, DiagID) << PrevSpec;
1971
1972 return;
1973 }
Mike Stump11289f42009-09-09 15:08:12 +00001974
Douglas Gregorba41d012010-04-24 16:38:41 +00001975 if (!TagDecl.get()) {
1976 // The action failed to produce an enumeration tag. If this is a
1977 // definition, consume the entire definition.
1978 if (Tok.is(tok::l_brace)) {
1979 ConsumeBrace();
1980 SkipUntil(tok::r_brace);
1981 }
1982
1983 DS.SetTypeSpecError();
1984 return;
1985 }
1986
Chris Lattner76c72282007-10-09 17:33:22 +00001987 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001988 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001989
Douglas Gregor72100632010-01-25 16:33:23 +00001990 // FIXME: The DeclSpec should keep the locations of both the keyword and the
1991 // name (if there is one).
Douglas Gregor72100632010-01-25 16:33:23 +00001992 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001993 TagDecl.getAs<void>(), Owned))
John McCall49bfce42009-08-03 20:12:06 +00001994 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00001995}
1996
Chris Lattnerc1915e22007-01-25 07:29:02 +00001997/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1998/// enumerator-list:
1999/// enumerator
2000/// enumerator-list ',' enumerator
2001/// enumerator:
2002/// enumeration-constant
2003/// enumeration-constant '=' constant-expression
2004/// enumeration-constant:
2005/// identifier
2006///
Chris Lattner83f095c2009-03-28 19:18:32 +00002007void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002008 // Enter the scope of the enum body and start the definition.
2009 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002010 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002011
Chris Lattnerc1915e22007-01-25 07:29:02 +00002012 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002013
Chris Lattner37256fb2007-08-27 17:24:30 +00002014 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002015 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00002016 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump11289f42009-09-09 15:08:12 +00002017
Chris Lattner83f095c2009-03-28 19:18:32 +00002018 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002019
Chris Lattner83f095c2009-03-28 19:18:32 +00002020 DeclPtrTy LastEnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002021
Chris Lattnerc1915e22007-01-25 07:29:02 +00002022 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002023 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002024 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2025 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002026
Chris Lattnerc1915e22007-01-25 07:29:02 +00002027 SourceLocation EqualLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +00002028 OwningExprResult AssignedVal(Actions);
Chris Lattner76c72282007-10-09 17:33:22 +00002029 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002030 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002031 AssignedVal = ParseConstantExpression();
2032 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002033 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002034 }
Mike Stump11289f42009-09-09 15:08:12 +00002035
Chris Lattnerc1915e22007-01-25 07:29:02 +00002036 // Install the enumerator constant into EnumDecl.
Chris Lattner83f095c2009-03-28 19:18:32 +00002037 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
2038 LastEnumConstDecl,
2039 IdentLoc, Ident,
2040 EqualLoc,
2041 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002042 EnumConstantDecls.push_back(EnumConstDecl);
2043 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002044
Chris Lattner76c72282007-10-09 17:33:22 +00002045 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002046 break;
2047 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002048
2049 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002050 !(getLang().C99 || getLang().CPlusPlus0x))
2051 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2052 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002053 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002054 }
Mike Stump11289f42009-09-09 15:08:12 +00002055
Chris Lattnerc1915e22007-01-25 07:29:02 +00002056 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002057 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002058
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002059 llvm::OwningPtr<AttributeList> Attr;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002060 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00002061 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002062 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002063
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002064 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2065 EnumConstantDecls.data(), EnumConstantDecls.size(),
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002066 CurScope, Attr.get());
Mike Stump11289f42009-09-09 15:08:12 +00002067
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002068 EnumScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00002069 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002070}
Chris Lattner3b561a32006-08-13 00:12:11 +00002071
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002072/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002073/// start of a type-qualifier-list.
2074bool Parser::isTypeQualifier() const {
2075 switch (Tok.getKind()) {
2076 default: return false;
2077 // type-qualifier
2078 case tok::kw_const:
2079 case tok::kw_volatile:
2080 case tok::kw_restrict:
2081 return true;
2082 }
2083}
2084
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002085/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2086/// is definitely a type-specifier. Return false if it isn't part of a type
2087/// specifier or if we're not sure.
2088bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2089 switch (Tok.getKind()) {
2090 default: return false;
2091 // type-specifiers
2092 case tok::kw_short:
2093 case tok::kw_long:
2094 case tok::kw_signed:
2095 case tok::kw_unsigned:
2096 case tok::kw__Complex:
2097 case tok::kw__Imaginary:
2098 case tok::kw_void:
2099 case tok::kw_char:
2100 case tok::kw_wchar_t:
2101 case tok::kw_char16_t:
2102 case tok::kw_char32_t:
2103 case tok::kw_int:
2104 case tok::kw_float:
2105 case tok::kw_double:
2106 case tok::kw_bool:
2107 case tok::kw__Bool:
2108 case tok::kw__Decimal32:
2109 case tok::kw__Decimal64:
2110 case tok::kw__Decimal128:
2111 case tok::kw___vector:
2112
2113 // struct-or-union-specifier (C99) or class-specifier (C++)
2114 case tok::kw_class:
2115 case tok::kw_struct:
2116 case tok::kw_union:
2117 // enum-specifier
2118 case tok::kw_enum:
2119
2120 // typedef-name
2121 case tok::annot_typename:
2122 return true;
2123 }
2124}
2125
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002126/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002127/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002128bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002129 switch (Tok.getKind()) {
2130 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002131
Chris Lattner020bab92009-01-04 23:41:41 +00002132 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002133 if (TryAltiVecVectorToken())
2134 return true;
2135 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002136 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002137 // Annotate typenames and C++ scope specifiers. If we get one, just
2138 // recurse to handle whatever we get.
2139 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002140 return true;
2141 if (Tok.is(tok::identifier))
2142 return false;
2143 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002144
Chris Lattner020bab92009-01-04 23:41:41 +00002145 case tok::coloncolon: // ::foo::bar
2146 if (NextToken().is(tok::kw_new) || // ::new
2147 NextToken().is(tok::kw_delete)) // ::delete
2148 return false;
2149
Chris Lattner020bab92009-01-04 23:41:41 +00002150 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002151 return true;
2152 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002153
Chris Lattnere37e2332006-08-15 04:50:22 +00002154 // GNU attributes support.
2155 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002156 // GNU typeof support.
2157 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002158
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002159 // type-specifiers
2160 case tok::kw_short:
2161 case tok::kw_long:
2162 case tok::kw_signed:
2163 case tok::kw_unsigned:
2164 case tok::kw__Complex:
2165 case tok::kw__Imaginary:
2166 case tok::kw_void:
2167 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002168 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002169 case tok::kw_char16_t:
2170 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002171 case tok::kw_int:
2172 case tok::kw_float:
2173 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002174 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002175 case tok::kw__Bool:
2176 case tok::kw__Decimal32:
2177 case tok::kw__Decimal64:
2178 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002179 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002180
Chris Lattner861a2262008-04-13 18:59:07 +00002181 // struct-or-union-specifier (C99) or class-specifier (C++)
2182 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002183 case tok::kw_struct:
2184 case tok::kw_union:
2185 // enum-specifier
2186 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002187
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002188 // type-qualifier
2189 case tok::kw_const:
2190 case tok::kw_volatile:
2191 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002192
2193 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002194 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002195 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002196
Chris Lattner409bf7d2008-10-20 00:25:30 +00002197 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2198 case tok::less:
2199 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002200
Steve Naroff44ac7772008-12-25 14:16:32 +00002201 case tok::kw___cdecl:
2202 case tok::kw___stdcall:
2203 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002204 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002205 case tok::kw___w64:
2206 case tok::kw___ptr64:
2207 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002208 }
2209}
2210
Chris Lattneracd58a32006-08-06 17:24:14 +00002211/// isDeclarationSpecifier() - Return true if the current token is part of a
2212/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002213bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00002214 switch (Tok.getKind()) {
2215 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002216
Chris Lattner020bab92009-01-04 23:41:41 +00002217 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002218 // Unfortunate hack to support "Class.factoryMethod" notation.
2219 if (getLang().ObjC1 && NextToken().is(tok::period))
2220 return false;
John Thompson22334602010-02-05 00:12:22 +00002221 if (TryAltiVecVectorToken())
2222 return true;
2223 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002224 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002225 // Annotate typenames and C++ scope specifiers. If we get one, just
2226 // recurse to handle whatever we get.
2227 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002228 return true;
2229 if (Tok.is(tok::identifier))
2230 return false;
2231 return isDeclarationSpecifier();
2232
Chris Lattner020bab92009-01-04 23:41:41 +00002233 case tok::coloncolon: // ::foo::bar
2234 if (NextToken().is(tok::kw_new) || // ::new
2235 NextToken().is(tok::kw_delete)) // ::delete
2236 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002237
Chris Lattner020bab92009-01-04 23:41:41 +00002238 // Annotate typenames and C++ scope specifiers. If we get one, just
2239 // recurse to handle whatever we get.
2240 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002241 return true;
2242 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002243
Chris Lattneracd58a32006-08-06 17:24:14 +00002244 // storage-class-specifier
2245 case tok::kw_typedef:
2246 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002247 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002248 case tok::kw_static:
2249 case tok::kw_auto:
2250 case tok::kw_register:
2251 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002252
Chris Lattneracd58a32006-08-06 17:24:14 +00002253 // type-specifiers
2254 case tok::kw_short:
2255 case tok::kw_long:
2256 case tok::kw_signed:
2257 case tok::kw_unsigned:
2258 case tok::kw__Complex:
2259 case tok::kw__Imaginary:
2260 case tok::kw_void:
2261 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002262 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002263 case tok::kw_char16_t:
2264 case tok::kw_char32_t:
2265
Chris Lattneracd58a32006-08-06 17:24:14 +00002266 case tok::kw_int:
2267 case tok::kw_float:
2268 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002269 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002270 case tok::kw__Bool:
2271 case tok::kw__Decimal32:
2272 case tok::kw__Decimal64:
2273 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002274 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002275
Chris Lattner861a2262008-04-13 18:59:07 +00002276 // struct-or-union-specifier (C99) or class-specifier (C++)
2277 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002278 case tok::kw_struct:
2279 case tok::kw_union:
2280 // enum-specifier
2281 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002282
Chris Lattneracd58a32006-08-06 17:24:14 +00002283 // type-qualifier
2284 case tok::kw_const:
2285 case tok::kw_volatile:
2286 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002287
Chris Lattneracd58a32006-08-06 17:24:14 +00002288 // function-specifier
2289 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002290 case tok::kw_virtual:
2291 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002292
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002293 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002294 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002295
Chris Lattner599e47e2007-08-09 17:01:07 +00002296 // GNU typeof support.
2297 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002298
Chris Lattner599e47e2007-08-09 17:01:07 +00002299 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002300 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002301 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002302
Chris Lattner8b2ec162008-07-26 03:38:44 +00002303 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2304 case tok::less:
2305 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002306
Steve Narofff192fab2009-01-06 19:34:12 +00002307 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002308 case tok::kw___cdecl:
2309 case tok::kw___stdcall:
2310 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002311 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002312 case tok::kw___w64:
2313 case tok::kw___ptr64:
2314 case tok::kw___forceinline:
2315 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002316 }
2317}
2318
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002319bool Parser::isConstructorDeclarator() {
2320 TentativeParsingAction TPA(*this);
2321
2322 // Parse the C++ scope specifier.
2323 CXXScopeSpec SS;
John McCall1f476a12010-02-26 08:45:28 +00002324 if (ParseOptionalCXXScopeSpecifier(SS, 0, true)) {
2325 TPA.Revert();
2326 return false;
2327 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002328
2329 // Parse the constructor name.
2330 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2331 // We already know that we have a constructor name; just consume
2332 // the token.
2333 ConsumeToken();
2334 } else {
2335 TPA.Revert();
2336 return false;
2337 }
2338
2339 // Current class name must be followed by a left parentheses.
2340 if (Tok.isNot(tok::l_paren)) {
2341 TPA.Revert();
2342 return false;
2343 }
2344 ConsumeParen();
2345
2346 // A right parentheses or ellipsis signals that we have a constructor.
2347 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2348 TPA.Revert();
2349 return true;
2350 }
2351
2352 // If we need to, enter the specified scope.
2353 DeclaratorScopeObj DeclScopeObj(*this, SS);
2354 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(CurScope, SS))
2355 DeclScopeObj.EnterDeclaratorScope();
2356
2357 // Check whether the next token(s) are part of a declaration
2358 // specifier, in which case we have the start of a parameter and,
2359 // therefore, we know that this is a constructor.
2360 bool IsConstructor = isDeclarationSpecifier();
2361 TPA.Revert();
2362 return IsConstructor;
2363}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002364
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002365/// ParseTypeQualifierListOpt
2366/// type-qualifier-list: [C99 6.7.5]
2367/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002368/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002369/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002370/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Alexis Hunt96d5c762009-11-21 08:43:09 +00002371/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2372/// if CXX0XAttributesAllowed = true
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002373///
Alexis Hunt96d5c762009-11-21 08:43:09 +00002374void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2375 bool CXX0XAttributesAllowed) {
2376 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2377 SourceLocation Loc = Tok.getLocation();
2378 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2379 if (CXX0XAttributesAllowed)
2380 DS.AddAttributes(Attr.AttrList);
2381 else
2382 Diag(Loc, diag::err_attributes_not_allowed);
2383 }
2384
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002385 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002386 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002387 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002388 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002389 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002390
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002391 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002392 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002393 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2394 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002395 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002396 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002397 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2398 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002399 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002400 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002401 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2402 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002403 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002404 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002405 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002406 case tok::kw___cdecl:
2407 case tok::kw___stdcall:
2408 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002409 case tok::kw___thiscall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002410 if (GNUAttributesAllowed) {
Eli Friedman53339e02009-06-08 23:27:34 +00002411 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2412 continue;
2413 }
2414 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002415 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002416 if (GNUAttributesAllowed) {
2417 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002418 continue; // do *not* consume the next token!
2419 }
2420 // otherwise, FALL THROUGH!
2421 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002422 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002423 // If this is not a type-qualifier token, we're done reading type
2424 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002425 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002426 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002427 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002428
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002429 // If the specifier combination wasn't legal, issue a diagnostic.
2430 if (isInvalid) {
2431 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002432 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002433 }
2434 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002435 }
2436}
2437
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002438
2439/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2440///
2441void Parser::ParseDeclarator(Declarator &D) {
2442 /// This implements the 'declarator' production in the C grammar, then checks
2443 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002444 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002445}
2446
Sebastian Redlbd150f42008-11-21 19:14:01 +00002447/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2448/// is parsed by the function passed to it. Pass null, and the direct-declarator
2449/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002450/// ptr-operator production.
2451///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002452/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2453/// [C] pointer[opt] direct-declarator
2454/// [C++] direct-declarator
2455/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002456///
2457/// pointer: [C99 6.7.5]
2458/// '*' type-qualifier-list[opt]
2459/// '*' type-qualifier-list[opt] pointer
2460///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002461/// ptr-operator:
2462/// '*' cv-qualifier-seq[opt]
2463/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002464/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002465/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002466/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002467/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002468void Parser::ParseDeclaratorInternal(Declarator &D,
2469 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002470 if (Diags.hasAllExtensionsSilenced())
2471 D.setExtension();
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002472 // C++ member pointers start with a '::' or a nested-name.
2473 // Member pointers get special handling, since there's no place for the
2474 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002475 if (getLang().CPlusPlus &&
2476 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2477 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002478 CXXScopeSpec SS;
John McCall1f476a12010-02-26 08:45:28 +00002479 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true); // ignore fail
2480
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002481 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002482 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002483 // The scope spec really belongs to the direct-declarator.
2484 D.getCXXScopeSpec() = SS;
2485 if (DirectDeclParser)
2486 (this->*DirectDeclParser)(D);
2487 return;
2488 }
2489
2490 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002491 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002492 DeclSpec DS;
2493 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002494 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002495
2496 // Recurse to parse whatever is left.
2497 ParseDeclaratorInternal(D, DirectDeclParser);
2498
2499 // Sema will have to catch (syntactically invalid) pointers into global
2500 // scope. It has to catch pointers into namespace scope anyway.
2501 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002502 Loc, DS.TakeAttributes()),
2503 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002504 return;
2505 }
2506 }
2507
2508 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002509 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002510 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002511 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002512 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002513 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002514 if (DirectDeclParser)
2515 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002516 return;
2517 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002518
Sebastian Redled0f3b02009-03-15 22:02:01 +00002519 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2520 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002521 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002522 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002523
Chris Lattner9eac9312009-03-27 04:18:06 +00002524 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002525 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002526 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002527
Bill Wendling3708c182007-05-27 10:15:43 +00002528 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002529 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002530
Bill Wendling3708c182007-05-27 10:15:43 +00002531 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002532 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002533 if (Kind == tok::star)
2534 // Remember that we parsed a pointer type, and remember the type-quals.
2535 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002536 DS.TakeAttributes()),
2537 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002538 else
2539 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002540 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002541 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002542 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002543 } else {
2544 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002545 DeclSpec DS;
2546
Sebastian Redl3b27be62009-03-23 00:00:23 +00002547 // Complain about rvalue references in C++03, but then go on and build
2548 // the declarator.
2549 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2550 Diag(Loc, diag::err_rvalue_reference);
2551
Bill Wendling93efb222007-06-02 23:28:54 +00002552 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2553 // cv-qualifiers are introduced through the use of a typedef or of a
2554 // template type argument, in which case the cv-qualifiers are ignored.
2555 //
2556 // [GNU] Retricted references are allowed.
2557 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002558 // [C++0x] Attributes on references are not allowed.
2559 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002560 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002561
2562 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2563 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2564 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002565 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002566 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2567 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002568 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002569 }
Bill Wendling3708c182007-05-27 10:15:43 +00002570
2571 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002572 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002573
Douglas Gregor66583c52008-11-03 15:51:28 +00002574 if (D.getNumTypeObjects() > 0) {
2575 // C++ [dcl.ref]p4: There shall be no references to references.
2576 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2577 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002578 if (const IdentifierInfo *II = D.getIdentifier())
2579 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2580 << II;
2581 else
2582 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2583 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002584
Sebastian Redlbd150f42008-11-21 19:14:01 +00002585 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002586 // can go ahead and build the (technically ill-formed)
2587 // declarator: reference collapsing will take care of it.
2588 }
2589 }
2590
Bill Wendling3708c182007-05-27 10:15:43 +00002591 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002592 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002593 DS.TakeAttributes(),
2594 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002595 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002596 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002597}
2598
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002599/// ParseDirectDeclarator
2600/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002601/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002602/// '(' declarator ')'
2603/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002604/// [C90] direct-declarator '[' constant-expression[opt] ']'
2605/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2606/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2607/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2608/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002609/// direct-declarator '(' parameter-type-list ')'
2610/// direct-declarator '(' identifier-list[opt] ')'
2611/// [GNU] direct-declarator '(' parameter-forward-declarations
2612/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002613/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2614/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002615/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002616///
2617/// declarator-id: [C++ 8]
2618/// id-expression
2619/// '::'[opt] nested-name-specifier[opt] type-name
2620///
2621/// id-expression: [C++ 5.1]
2622/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002623/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002624///
2625/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002626/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002627/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002628/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002629/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002630/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002631///
Chris Lattneracd58a32006-08-06 17:24:14 +00002632void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002633 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002634
Douglas Gregor7861a802009-11-03 01:35:08 +00002635 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2636 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002637 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002638 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2639 true);
John McCall1f476a12010-02-26 08:45:28 +00002640 }
2641
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002642 if (D.getCXXScopeSpec().isValid()) {
John McCall2b058ef2009-12-11 20:04:54 +00002643 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2644 // Change the declaration context for name lookup, until this function
2645 // is exited (and the declarator has been parsed).
2646 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002647 }
2648
Douglas Gregor7861a802009-11-03 01:35:08 +00002649 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2650 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2651 // We found something that indicates the start of an unqualified-id.
2652 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002653 bool AllowConstructorName;
2654 if (D.getDeclSpec().hasTypeSpecifier())
2655 AllowConstructorName = false;
2656 else if (D.getCXXScopeSpec().isSet())
2657 AllowConstructorName =
2658 (D.getContext() == Declarator::FileContext ||
2659 (D.getContext() == Declarator::MemberContext &&
2660 D.getDeclSpec().isFriendSpecified()));
2661 else
2662 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2663
Douglas Gregor7861a802009-11-03 01:35:08 +00002664 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2665 /*EnteringContext=*/true,
2666 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002667 AllowConstructorName,
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002668 /*ObjectType=*/0,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002669 D.getName()) ||
2670 // Once we're past the identifier, if the scope was bad, mark the
2671 // whole declarator bad.
2672 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002673 D.SetIdentifier(0, Tok.getLocation());
2674 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002675 } else {
2676 // Parsed the unqualified-id; update range information and move along.
2677 if (D.getSourceRange().getBegin().isInvalid())
2678 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2679 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002680 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002681 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002682 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002683 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002684 assert(!getLang().CPlusPlus &&
2685 "There's a C++-specific check for tok::identifier above");
2686 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2687 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2688 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002689 goto PastIdentifier;
2690 }
2691
2692 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002693 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002694 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002695 // Example: 'char (*X)' or 'int (*XX)(void)'
2696 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002697
2698 // If the declarator was parenthesized, we entered the declarator
2699 // scope when parsing the parenthesized declarator, then exited
2700 // the scope already. Re-enter the scope, if we need to.
2701 if (D.getCXXScopeSpec().isSet()) {
2702 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2703 // Change the declaration context for name lookup, until this function
2704 // is exited (and the declarator has been parsed).
2705 DeclScopeObj.EnterDeclaratorScope();
2706 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002707 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002708 // This could be something simple like "int" (in which case the declarator
2709 // portion is empty), if an abstract-declarator is allowed.
2710 D.SetIdentifier(0, Tok.getLocation());
2711 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002712 if (D.getContext() == Declarator::MemberContext)
2713 Diag(Tok, diag::err_expected_member_name_or_semi)
2714 << D.getDeclSpec().getSourceRange();
2715 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002716 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002717 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002718 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002719 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002720 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002721 }
Mike Stump11289f42009-09-09 15:08:12 +00002722
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002723 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002724 assert(D.isPastIdentifier() &&
2725 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002726
Alexis Hunt96d5c762009-11-21 08:43:09 +00002727 // Don't parse attributes unless we have an identifier.
Douglas Gregor0286b462010-02-19 16:47:56 +00002728 if (D.getIdentifier() && getLang().CPlusPlus0x
Alexis Hunt96d5c762009-11-21 08:43:09 +00002729 && isCXX0XAttributeSpecifier(true)) {
2730 SourceLocation AttrEndLoc;
2731 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2732 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2733 }
2734
Chris Lattneracd58a32006-08-06 17:24:14 +00002735 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002736 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002737 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2738 // In such a case, check if we actually have a function declarator; if it
2739 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002740 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2741 // When not in file scope, warn for ambiguous function declarators, just
2742 // in case the author intended it as a variable definition.
2743 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2744 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2745 break;
2746 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002747 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002748 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002749 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002750 } else {
2751 break;
2752 }
2753 }
2754}
2755
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002756/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2757/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002758/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002759/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2760///
2761/// direct-declarator:
2762/// '(' declarator ')'
2763/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002764/// direct-declarator '(' parameter-type-list ')'
2765/// direct-declarator '(' identifier-list[opt] ')'
2766/// [GNU] direct-declarator '(' parameter-forward-declarations
2767/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002768///
2769void Parser::ParseParenDeclarator(Declarator &D) {
2770 SourceLocation StartLoc = ConsumeParen();
2771 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002772
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002773 // Eat any attributes before we look at whether this is a grouping or function
2774 // declarator paren. If this is a grouping paren, the attribute applies to
2775 // the type being built up, for example:
2776 // int (__attribute__(()) *x)(long y)
2777 // If this ends up not being a grouping paren, the attribute applies to the
2778 // first argument, for example:
2779 // int (__attribute__(()) int x)
2780 // In either case, we need to eat any attributes to be able to determine what
2781 // sort of paren this is.
2782 //
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002783 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002784 bool RequiresArg = false;
2785 if (Tok.is(tok::kw___attribute)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002786 AttrList.reset(ParseGNUAttributes());
Mike Stump11289f42009-09-09 15:08:12 +00002787
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002788 // We require that the argument list (if this is a non-grouping paren) be
2789 // present even if the attribute list was empty.
2790 RequiresArg = true;
2791 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002792 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002793 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00002794 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2795 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002796 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman53339e02009-06-08 23:27:34 +00002797 }
Mike Stump11289f42009-09-09 15:08:12 +00002798
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002799 // If we haven't past the identifier yet (or where the identifier would be
2800 // stored, if this is an abstract declarator), then this is probably just
2801 // grouping parens. However, if this could be an abstract-declarator, then
2802 // this could also be the start of function arguments (consider 'void()').
2803 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002804
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002805 if (!D.mayOmitIdentifier()) {
2806 // If this can't be an abstract-declarator, this *must* be a grouping
2807 // paren, because we haven't seen the identifier yet.
2808 isGrouping = true;
2809 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002810 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002811 isDeclarationSpecifier()) { // 'int(int)' is a function.
2812 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2813 // considered to be a type, not a K&R identifier-list.
2814 isGrouping = false;
2815 } else {
2816 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2817 isGrouping = true;
2818 }
Mike Stump11289f42009-09-09 15:08:12 +00002819
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002820 // If this is a grouping paren, handle:
2821 // direct-declarator: '(' declarator ')'
2822 // direct-declarator: '(' attributes declarator ')'
2823 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002824 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002825 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002826 if (AttrList)
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002827 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002828
Sebastian Redlbd150f42008-11-21 19:14:01 +00002829 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002830 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002831 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002832
2833 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002834 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002835 return;
2836 }
Mike Stump11289f42009-09-09 15:08:12 +00002837
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002838 // Okay, if this wasn't a grouping paren, it must be the start of a function
2839 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002840 // identifier (and remember where it would have been), then call into
2841 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002842 D.SetIdentifier(0, Tok.getLocation());
2843
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002844 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002845}
2846
2847/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2848/// declarator D up to a paren, which indicates that we are parsing function
2849/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002850///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002851/// If AttrList is non-null, then the caller parsed those arguments immediately
2852/// after the open paren - they should be considered to be the first argument of
2853/// a parameter. If RequiresArg is true, then the first argument of the
2854/// function is required to be present and required to not be an identifier
2855/// list.
2856///
Chris Lattneracd58a32006-08-06 17:24:14 +00002857/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002858/// parameter-type-list: [C99 6.7.5]
2859/// parameter-list
2860/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002861/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002862///
2863/// parameter-list: [C99 6.7.5]
2864/// parameter-declaration
2865/// parameter-list ',' parameter-declaration
2866///
2867/// parameter-declaration: [C99 6.7.5]
2868/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002869/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002870/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002871/// declaration-specifiers abstract-declarator[opt]
2872/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002873/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002874/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002875///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002876/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002877/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002878///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002879void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2880 AttributeList *AttrList,
2881 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002882 // lparen is already consumed!
2883 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00002884
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002885 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002886 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002887 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002888 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002889 delete AttrList;
2890 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002891
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002892 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2893 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002894
2895 // cv-qualifier-seq[opt].
2896 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002897 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002898 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002899 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002900 llvm::SmallVector<TypeTy*, 2> Exceptions;
2901 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002902 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002903 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002904 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002905 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002906
2907 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002908 if (Tok.is(tok::kw_throw)) {
2909 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002910 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002911 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002912 hasAnyExceptionSpec);
2913 assert(Exceptions.size() == ExceptionRanges.size() &&
2914 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002915 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002916 }
2917
Chris Lattner371ed4e2008-04-06 06:57:35 +00002918 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002919 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002920 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002921 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002922 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002923 /*arglist*/ 0, 0,
2924 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002925 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002926 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002927 Exceptions.data(),
2928 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002929 Exceptions.size(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002930 LParenLoc, RParenLoc, D),
2931 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002932 return;
Sebastian Redld6434562009-05-29 18:02:33 +00002933 }
2934
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002935 // Alternatively, this parameter list may be an identifier list form for a
2936 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00002937 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
2938 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00002939 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002940 // K&R identifier lists can't have typedefs as identifiers, per
2941 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002942 if (RequiresArg) {
2943 Diag(Tok, diag::err_argument_required_after_attribute);
2944 delete AttrList;
2945 }
Chris Lattner9453ab82010-05-14 17:23:36 +00002946
Steve Naroffb0486722009-01-28 19:16:40 +00002947 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00002948 // normal declarators, not for abstract-declarators. Get the first
2949 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00002950 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00002951 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00002952
2953 // Identifier lists follow a really simple grammar: the identifiers can
2954 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
2955 // identifier lists are really rare in the brave new modern world, and it
2956 // is very common for someone to typo a type in a non-k&r style list. If
2957 // we are presented with something like: "void foo(intptr x, float y)",
2958 // we don't want to start parsing the function declarator as though it is
2959 // a K&R style declarator just because intptr is an invalid type.
2960 //
2961 // To handle this, we check to see if the token after the first identifier
2962 // is a "," or ")". Only if so, do we parse it as an identifier list.
2963 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
2964 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
2965 FirstTok.getIdentifierInfo(),
2966 FirstTok.getLocation(), D);
2967
2968 // If we get here, the code is invalid. Push the first identifier back
2969 // into the token stream and parse the first argument as an (invalid)
2970 // normal argument declarator.
2971 PP.EnterToken(Tok);
2972 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002973 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002974 }
Mike Stump11289f42009-09-09 15:08:12 +00002975
Chris Lattner371ed4e2008-04-06 06:57:35 +00002976 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00002977
Chris Lattner371ed4e2008-04-06 06:57:35 +00002978 // Build up an array of information about the parsed arguments.
2979 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002980
2981 // Enter function-declaration scope, limiting any declarators to the
2982 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00002983 ParseScope PrototypeScope(this,
2984 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00002985
Chris Lattner371ed4e2008-04-06 06:57:35 +00002986 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002987 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00002988 while (1) {
2989 if (Tok.is(tok::ellipsis)) {
2990 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002991 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002992 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00002993 }
Mike Stump11289f42009-09-09 15:08:12 +00002994
Chris Lattner371ed4e2008-04-06 06:57:35 +00002995 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002996
Chris Lattner371ed4e2008-04-06 06:57:35 +00002997 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00002998 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002999 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003000
3001 // If the caller parsed attributes for the first argument, add them now.
3002 if (AttrList) {
3003 DS.AddAttributes(AttrList);
3004 AttrList = 0; // Only apply the attributes to the first parameter.
3005 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003006 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003007
Chris Lattner371ed4e2008-04-06 06:57:35 +00003008 // Parse the declarator. This is "PrototypeContext", because we must
3009 // accept either 'declarator' or 'abstract-declarator' here.
3010 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3011 ParseDeclarator(ParmDecl);
3012
3013 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003014 if (Tok.is(tok::kw___attribute)) {
3015 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003016 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003017 ParmDecl.AddAttributes(AttrList, Loc);
3018 }
Mike Stump11289f42009-09-09 15:08:12 +00003019
Chris Lattner371ed4e2008-04-06 06:57:35 +00003020 // Remember this parsed parameter in ParamInfo.
3021 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003022
Douglas Gregor4d87df52008-12-16 21:30:33 +00003023 // DefArgToks is used when the parsing of default arguments needs
3024 // to be delayed.
3025 CachedTokens *DefArgToks = 0;
3026
Chris Lattner371ed4e2008-04-06 06:57:35 +00003027 // If no parameter was specified, verify that *something* was specified,
3028 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003029 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3030 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003031 // Completely missing, emit error.
3032 Diag(DSStart, diag::err_missing_param);
3033 } else {
3034 // Otherwise, we have something. Add it and let semantic analysis try
3035 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003036
Chris Lattner371ed4e2008-04-06 06:57:35 +00003037 // Inform the actions module about the parameter declarator, so it gets
3038 // added to the current scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00003039 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003040
3041 // Parse the default argument, if any. We parse the default
3042 // arguments in all dialects; the semantic analysis in
3043 // ActOnParamDefaultArgument will reject the default argument in
3044 // C.
3045 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003046 SourceLocation EqualLoc = Tok.getLocation();
3047
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003048 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003049 if (D.getContext() == Declarator::MemberContext) {
3050 // If we're inside a class definition, cache the tokens
3051 // corresponding to the default argument. We'll actually parse
3052 // them when we see the end of the class definition.
3053 // FIXME: Templates will require something similar.
3054 // FIXME: Can we use a smart pointer for Toks?
3055 DefArgToks = new CachedTokens;
3056
Mike Stump11289f42009-09-09 15:08:12 +00003057 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003058 /*StopAtSemi=*/true,
3059 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003060 delete DefArgToks;
3061 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003062 Actions.ActOnParamDefaultArgumentError(Param);
3063 } else
Mike Stump11289f42009-09-09 15:08:12 +00003064 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003065 (*DefArgToks)[1].getLocation());
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003066 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003067 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003068 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003069
Douglas Gregor4d87df52008-12-16 21:30:33 +00003070 OwningExprResult DefArgResult(ParseAssignmentExpression());
3071 if (DefArgResult.isInvalid()) {
3072 Actions.ActOnParamDefaultArgumentError(Param);
3073 SkipUntil(tok::comma, tok::r_paren, true, true);
3074 } else {
3075 // Inform the actions module about the default argument
3076 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00003077 move(DefArgResult));
Douglas Gregor4d87df52008-12-16 21:30:33 +00003078 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003079 }
3080 }
Mike Stump11289f42009-09-09 15:08:12 +00003081
3082 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3083 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003084 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003085 }
3086
3087 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003088 if (Tok.isNot(tok::comma)) {
3089 if (Tok.is(tok::ellipsis)) {
3090 IsVariadic = true;
3091 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3092
3093 if (!getLang().CPlusPlus) {
3094 // We have ellipsis without a preceding ',', which is ill-formed
3095 // in C. Complain and provide the fix.
3096 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003097 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003098 }
3099 }
3100
3101 break;
3102 }
Mike Stump11289f42009-09-09 15:08:12 +00003103
Chris Lattner371ed4e2008-04-06 06:57:35 +00003104 // Consume the comma.
3105 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003106 }
Mike Stump11289f42009-09-09 15:08:12 +00003107
Chris Lattner371ed4e2008-04-06 06:57:35 +00003108 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00003109 PrototypeScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00003110
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003111 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003112 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3113 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003114
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003115 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003116 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003117 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003118 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00003119 llvm::SmallVector<TypeTy*, 2> Exceptions;
3120 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003121
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003122 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003123 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003124 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003125 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003126 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003127
3128 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003129 if (Tok.is(tok::kw_throw)) {
3130 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003131 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003132 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003133 hasAnyExceptionSpec);
3134 assert(Exceptions.size() == ExceptionRanges.size() &&
3135 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003136 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003137 }
3138
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003139 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003140 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003141 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003142 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003143 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003144 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003145 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003146 Exceptions.data(),
3147 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003148 Exceptions.size(),
3149 LParenLoc, RParenLoc, D),
3150 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003151}
Chris Lattneracd58a32006-08-06 17:24:14 +00003152
Chris Lattner6c940e62008-04-06 06:34:08 +00003153/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3154/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003155/// first identifier has already been consumed, and the current token is the
3156/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003157///
3158/// identifier-list: [C99 6.7.5]
3159/// identifier
3160/// identifier-list ',' identifier
3161///
3162void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003163 IdentifierInfo *FirstIdent,
3164 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003165 Declarator &D) {
3166 // Build up an array of information about the parsed arguments.
3167 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3168 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003169
Chris Lattner6c940e62008-04-06 06:34:08 +00003170 // If there was no identifier specified for the declarator, either we are in
3171 // an abstract-declarator, or we are in a parameter declarator which was found
3172 // to be abstract. In abstract-declarators, identifier lists are not valid:
3173 // diagnose this.
3174 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003175 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003176
Chris Lattner9453ab82010-05-14 17:23:36 +00003177 // The first identifier was already read, and is known to be the first
3178 // identifier in the list. Remember this identifier in ParamInfo.
3179 ParamsSoFar.insert(FirstIdent);
3180 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00003181 DeclPtrTy()));
Mike Stump11289f42009-09-09 15:08:12 +00003182
Chris Lattner6c940e62008-04-06 06:34:08 +00003183 while (Tok.is(tok::comma)) {
3184 // Eat the comma.
3185 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003186
Chris Lattner9186f552008-04-06 06:39:19 +00003187 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003188 if (Tok.isNot(tok::identifier)) {
3189 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003190 SkipUntil(tok::r_paren);
3191 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003192 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003193
Chris Lattner6c940e62008-04-06 06:34:08 +00003194 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003195
3196 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00003197 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003198 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003199
Chris Lattner6c940e62008-04-06 06:34:08 +00003200 // Verify that the argument identifier has not already been mentioned.
3201 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003202 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003203 } else {
3204 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003205 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003206 Tok.getLocation(),
3207 DeclPtrTy()));
Chris Lattner9186f552008-04-06 06:39:19 +00003208 }
Mike Stump11289f42009-09-09 15:08:12 +00003209
Chris Lattner6c940e62008-04-06 06:34:08 +00003210 // Eat the identifier.
3211 ConsumeToken();
3212 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003213
3214 // If we have the closing ')', eat it and we're done.
3215 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3216
Chris Lattner9186f552008-04-06 06:39:19 +00003217 // Remember that we parsed a function type, and remember the attributes. This
3218 // function type is always a K&R style function type, which is not varargs and
3219 // has no prototype.
3220 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003221 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003222 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003223 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003224 /*exception*/false,
3225 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003226 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003227 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003228}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003229
Chris Lattnere8074e62006-08-06 18:30:15 +00003230/// [C90] direct-declarator '[' constant-expression[opt] ']'
3231/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3232/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3233/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3234/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3235void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003236 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003237
Chris Lattner84a11622008-12-18 07:27:21 +00003238 // C array syntax has many features, but by-far the most common is [] and [4].
3239 // This code does a fast path to handle some of the most obvious cases.
3240 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003241 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003242 //FIXME: Use these
3243 CXX0XAttributeList Attr;
3244 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3245 Attr = ParseCXX0XAttributes();
3246 }
3247
Chris Lattner84a11622008-12-18 07:27:21 +00003248 // Remember that we parsed the empty array type.
3249 OwningExprResult NumElements(Actions);
Douglas Gregor04318252009-07-06 15:59:29 +00003250 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3251 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003252 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003253 return;
3254 } else if (Tok.getKind() == tok::numeric_constant &&
3255 GetLookAheadToken(1).is(tok::r_square)) {
3256 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlffbcf962009-01-18 18:53:16 +00003257 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003258 ConsumeToken();
3259
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003260 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003261 //FIXME: Use these
3262 CXX0XAttributeList Attr;
3263 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3264 Attr = ParseCXX0XAttributes();
3265 }
Chris Lattner84a11622008-12-18 07:27:21 +00003266
3267 // If there was an error parsing the assignment-expression, recover.
3268 if (ExprRes.isInvalid())
3269 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00003270
Chris Lattner84a11622008-12-18 07:27:21 +00003271 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00003272 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3273 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003274 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003275 return;
3276 }
Mike Stump11289f42009-09-09 15:08:12 +00003277
Chris Lattnere8074e62006-08-06 18:30:15 +00003278 // If valid, this location is the position where we read the 'static' keyword.
3279 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003280 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003281 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003282
Chris Lattnere8074e62006-08-06 18:30:15 +00003283 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003284 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003285 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003286 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003287
Chris Lattnere8074e62006-08-06 18:30:15 +00003288 // If we haven't already read 'static', check to see if there is one after the
3289 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003290 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003291 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003292
Chris Lattnere8074e62006-08-06 18:30:15 +00003293 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003294 bool isStar = false;
Sebastian Redlc13f2682008-12-09 20:22:58 +00003295 OwningExprResult NumElements(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00003296
Chris Lattner521ff2b2008-04-06 05:26:30 +00003297 // Handle the case where we have '[*]' as the array size. However, a leading
3298 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3299 // the the token after the star is a ']'. Since stars in arrays are
3300 // infrequent, use of lookahead is not costly here.
3301 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003302 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003303
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003304 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003305 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003306 StaticLoc = SourceLocation(); // Drop the static.
3307 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003308 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003309 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003310 // Note, in C89, this production uses the constant-expr production instead
3311 // of assignment-expr. The only difference is that assignment-expr allows
3312 // things like '=' and '*='. Sema rejects these in C89 mode because they
3313 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003314
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003315 // Parse the constant-expression or assignment-expression now (depending
3316 // on dialect).
3317 if (getLang().CPlusPlus)
3318 NumElements = ParseConstantExpression();
3319 else
3320 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003321 }
Mike Stump11289f42009-09-09 15:08:12 +00003322
Chris Lattner62591722006-08-12 18:40:58 +00003323 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003324 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003325 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003326 // If the expression was invalid, skip it.
3327 SkipUntil(tok::r_square);
3328 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003329 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003330
3331 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3332
Alexis Hunt96d5c762009-11-21 08:43:09 +00003333 //FIXME: Use these
3334 CXX0XAttributeList Attr;
3335 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3336 Attr = ParseCXX0XAttributes();
3337 }
3338
Chris Lattner84a11622008-12-18 07:27:21 +00003339 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00003340 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3341 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003342 NumElements.release(),
3343 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003344 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003345}
3346
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003347/// [GNU] typeof-specifier:
3348/// typeof ( expressions )
3349/// typeof ( type-name )
3350/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003351///
3352void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003353 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003354 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003355 SourceLocation StartLoc = ConsumeToken();
3356
John McCalle8595032010-01-13 20:03:27 +00003357 const bool hasParens = Tok.is(tok::l_paren);
3358
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003359 bool isCastExpr;
3360 TypeTy *CastTy;
3361 SourceRange CastRange;
3362 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3363 isCastExpr,
3364 CastTy,
3365 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003366 if (hasParens)
3367 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003368
3369 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003370 // FIXME: Not accurate, the range gets one token more than it should.
3371 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003372 else
3373 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003374
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003375 if (isCastExpr) {
3376 if (!CastTy) {
3377 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003378 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003379 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003380
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003381 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003382 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003383 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3384 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003385 DiagID, CastTy))
3386 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003387 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003388 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003389
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003390 // If we get here, the operand to the typeof was an expresion.
3391 if (Operand.isInvalid()) {
3392 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003393 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003394 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003395
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003396 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003397 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003398 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3399 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003400 DiagID, Operand.release()))
3401 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003402}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003403
3404
3405/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3406/// from TryAltiVecVectorToken.
3407bool Parser::TryAltiVecVectorTokenOutOfLine() {
3408 Token Next = NextToken();
3409 switch (Next.getKind()) {
3410 default: return false;
3411 case tok::kw_short:
3412 case tok::kw_long:
3413 case tok::kw_signed:
3414 case tok::kw_unsigned:
3415 case tok::kw_void:
3416 case tok::kw_char:
3417 case tok::kw_int:
3418 case tok::kw_float:
3419 case tok::kw_double:
3420 case tok::kw_bool:
3421 case tok::kw___pixel:
3422 Tok.setKind(tok::kw___vector);
3423 return true;
3424 case tok::identifier:
3425 if (Next.getIdentifierInfo() == Ident_pixel) {
3426 Tok.setKind(tok::kw___vector);
3427 return true;
3428 }
3429 return false;
3430 }
3431}
3432
3433bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3434 const char *&PrevSpec, unsigned &DiagID,
3435 bool &isInvalid) {
3436 if (Tok.getIdentifierInfo() == Ident_vector) {
3437 Token Next = NextToken();
3438 switch (Next.getKind()) {
3439 case tok::kw_short:
3440 case tok::kw_long:
3441 case tok::kw_signed:
3442 case tok::kw_unsigned:
3443 case tok::kw_void:
3444 case tok::kw_char:
3445 case tok::kw_int:
3446 case tok::kw_float:
3447 case tok::kw_double:
3448 case tok::kw_bool:
3449 case tok::kw___pixel:
3450 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3451 return true;
3452 case tok::identifier:
3453 if (Next.getIdentifierInfo() == Ident_pixel) {
3454 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3455 return true;
3456 }
3457 break;
3458 default:
3459 break;
3460 }
3461 } else if (Tok.getIdentifierInfo() == Ident_pixel &&
3462 DS.isTypeAltiVecVector()) {
3463 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3464 return true;
3465 }
3466 return false;
3467}
3468