blob: 371d8ad02e8648e0a2d892146a8b4476ceb0469a [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"
John McCall8b0666c2010-08-20 18:27:03 +000016#include "clang/Sema/Scope.h"
17#include "clang/Sema/ParsedTemplate.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
Douglas Gregor0be31a22010-07-02 17:43:08 +000045 return Actions.ActOnTypeName(getCurScope(), 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:
Fariborz Jahanian9d7d3d82010-08-17 23:19:16 +0000177 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
178 0, SourceLocation(), 0, 0, CurrAttr);
179 if (CurrAttr->getKind() == AttributeList::AT_IBOutletCollection)
180 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begemanf2758702009-06-26 06:32:41 +0000181 // If it's a builtin type name, eat it and expect a rparen
182 // __attribute__(( vec_type_hint(char) ))
183 ConsumeToken();
Nate Begemanf2758702009-06-26 06:32:41 +0000184 if (Tok.is(tok::r_paren))
185 ConsumeParen();
186 break;
187 default:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000188 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000189 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000190 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000191
Steve Naroff0f2fe172007-06-01 17:11:19 +0000192 // now parse the list of expressions
193 while (1) {
Sebastian Redl59b5e512008-12-11 21:36:32 +0000194 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000195 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000196 ArgExprsOk = false;
197 SkipUntil(tok::r_paren);
198 break;
199 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000200 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000201 }
Chris Lattner76c72282007-10-09 17:33:22 +0000202 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000203 break;
204 ConsumeToken(); // Eat the comma, move to the next argument
205 }
206 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000207 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000208 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl511ed552008-11-25 22:21:31 +0000209 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000210 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
211 ArgExprs.size(),
Steve Naroffb8371e12007-06-09 03:39:29 +0000212 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000213 }
Nate Begemanf2758702009-06-26 06:32:41 +0000214 break;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000215 }
216 }
217 } else {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000218 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000219 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000220 }
221 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000222 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000223 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000224 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000225 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
226 SkipUntil(tok::r_paren, false);
227 }
228 if (EndLoc)
229 *EndLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000230 }
231 return CurrAttr;
232}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000233
Eli Friedman06de2b52009-06-08 07:21:15 +0000234/// ParseMicrosoftDeclSpec - Parse an __declspec construct
235///
236/// [MS] decl-specifier:
237/// __declspec ( extended-decl-modifier-seq )
238///
239/// [MS] extended-decl-modifier-seq:
240/// extended-decl-modifier[opt]
241/// extended-decl-modifier extended-decl-modifier-seq
242
Eli Friedman53339e02009-06-08 23:27:34 +0000243AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000244 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000245
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000246 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000247 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
248 "declspec")) {
249 SkipUntil(tok::r_paren, true); // skip until ) or ;
250 return CurrAttr;
251 }
Eli Friedman53339e02009-06-08 23:27:34 +0000252 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000253 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
254 SourceLocation AttrNameLoc = ConsumeToken();
255 if (Tok.is(tok::l_paren)) {
256 ConsumeParen();
257 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
258 // correctly.
259 OwningExprResult ArgExpr(ParseAssignmentExpression());
260 if (!ArgExpr.isInvalid()) {
John McCall37ad5512010-08-23 06:44:23 +0000261 Expr *ExprList = ArgExpr.take();
Alexis Hunt96d5c762009-11-21 08:43:09 +0000262 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman06de2b52009-06-08 07:21:15 +0000263 SourceLocation(), &ExprList, 1,
264 CurrAttr, true);
265 }
266 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
267 SkipUntil(tok::r_paren, false);
268 } else {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000269 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
270 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000271 }
272 }
273 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
274 SkipUntil(tok::r_paren, false);
Eli Friedman53339e02009-06-08 23:27:34 +0000275 return CurrAttr;
276}
277
278AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
279 // Treat these like attributes
280 // FIXME: Allow Sema to distinguish between these and real attributes!
281 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000282 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
283 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000284 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
285 SourceLocation AttrNameLoc = ConsumeToken();
286 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
287 // FIXME: Support these properly!
288 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000289 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman53339e02009-06-08 23:27:34 +0000290 SourceLocation(), 0, 0, CurrAttr, true);
291 }
292 return CurrAttr;
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000293}
294
Chris Lattner53361ac2006-08-10 05:19:57 +0000295/// ParseDeclaration - Parse a full 'declaration', which consists of
296/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000297/// 'Context' should be a Declarator::TheContext value. This returns the
298/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000299///
300/// declaration: [C99 6.7]
301/// block-declaration ->
302/// simple-declaration
303/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000304/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000305/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000306/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000307/// [C++] using-declaration
Sebastian Redlf769df52009-03-24 22:27:57 +0000308/// [C++0x] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000309/// others... [FIXME]
310///
Chris Lattner49836b42009-04-02 04:16:50 +0000311Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000312 SourceLocation &DeclEnd,
313 CXX0XAttributeList Attr) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000314 ParenBraceBracketBalancer BalancerRAIIObj(*this);
315
John McCall48871652010-08-21 09:40:31 +0000316 Decl *SingleDecl = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000317 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000318 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000319 case tok::kw_export:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000320 if (Attr.HasAttr)
321 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
322 << Attr.Range;
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000323 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000324 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000325 case tok::kw_namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000326 if (Attr.HasAttr)
327 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
328 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000329 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000330 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000331 case tok::kw_using:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000332 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000333 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000334 case tok::kw_static_assert:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000335 if (Attr.HasAttr)
336 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
337 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000338 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000339 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000340 default:
Chris Lattner005fc1b2010-04-05 18:18:31 +0000341 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000342 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000343
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000344 // This routine returns a DeclGroup, if the thing we parsed only contains a
345 // single decl, convert it now.
346 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000347}
348
349/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
350/// declaration-specifiers init-declarator-list[opt] ';'
351///[C90/C++]init-declarator-list ';' [TODO]
352/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000353///
354/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000355/// declaration. If it is true, it checks for and eats it.
Chris Lattner32dc41c2009-03-29 17:27:48 +0000356Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000357 SourceLocation &DeclEnd,
Chris Lattner005fc1b2010-04-05 18:18:31 +0000358 AttributeList *Attr,
359 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000360 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000361 ParsingDeclSpec DS(*this);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000362 if (Attr)
363 DS.AddAttributes(Attr);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000364 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
365 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump11289f42009-09-09 15:08:12 +0000366
Chris Lattner0e894622006-08-13 19:58:17 +0000367 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
368 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000369 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000370 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000371 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallb54367d2010-05-21 20:45:30 +0000372 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000373 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000374 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000375 }
Mike Stump11289f42009-09-09 15:08:12 +0000376
Chris Lattner005fc1b2010-04-05 18:18:31 +0000377 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld5a36322009-11-03 19:26:08 +0000378}
Mike Stump11289f42009-09-09 15:08:12 +0000379
John McCalld5a36322009-11-03 19:26:08 +0000380/// ParseDeclGroup - Having concluded that this is either a function
381/// definition or a group of object declarations, actually parse the
382/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000383Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
384 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000385 bool AllowFunctionDefinitions,
386 SourceLocation *DeclEnd) {
387 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000388 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000389 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000390
John McCalld5a36322009-11-03 19:26:08 +0000391 // Bail out if the first declarator didn't seem well-formed.
392 if (!D.hasName() && !D.mayOmitIdentifier()) {
393 // Skip until ; or }.
394 SkipUntil(tok::r_brace, true, true);
395 if (Tok.is(tok::semi))
396 ConsumeToken();
397 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000398 }
Mike Stump11289f42009-09-09 15:08:12 +0000399
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000400 // Check to see if we have a function *definition* which must have a body.
401 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
402 // Look at the next token to make sure that this isn't a function
403 // declaration. We have to check this because __attribute__ might be the
404 // start of a function definition in GCC-extended K&R C.
405 !isDeclarationAfterDeclarator()) {
406
Chris Lattner13901342010-07-11 22:42:07 +0000407 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000408 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
409 Diag(Tok, diag::err_function_declared_typedef);
410
411 // Recover by treating the 'typedef' as spurious.
412 DS.ClearStorageClassSpecs();
413 }
414
John McCall48871652010-08-21 09:40:31 +0000415 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000416 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000417 }
418
419 if (isDeclarationSpecifier()) {
420 // If there is an invalid declaration specifier right after the function
421 // prototype, then we must be in a missing semicolon case where this isn't
422 // actually a body. Just fall through into the code that handles it as a
423 // prototype, and let the top-level code handle the erroneous declspec
424 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000425 } else {
426 Diag(Tok, diag::err_expected_fn_body);
427 SkipUntil(tok::semi);
428 return DeclGroupPtrTy();
429 }
430 }
431
John McCall48871652010-08-21 09:40:31 +0000432 llvm::SmallVector<Decl *, 8> DeclsInGroup;
433 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000434 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000435 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000436 DeclsInGroup.push_back(FirstDecl);
437
438 // If we don't have a comma, it is either the end of the list (a ';') or an
439 // error, bail out.
440 while (Tok.is(tok::comma)) {
441 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000442 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000443
444 // Parse the next declarator.
445 D.clear();
446
447 // Accept attributes in an init-declarator. In the first declarator in a
448 // declaration, these would be part of the declspec. In subsequent
449 // declarators, they become part of the declarator itself, so that they
450 // don't apply to declarators after *this* one. Examples:
451 // short __attribute__((common)) var; -> declspec
452 // short var __attribute__((common)); -> declarator
453 // short x, __attribute__((common)) var; -> declarator
454 if (Tok.is(tok::kw___attribute)) {
455 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000456 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld5a36322009-11-03 19:26:08 +0000457 D.AddAttributes(AttrList, Loc);
458 }
459
460 ParseDeclarator(D);
461
John McCall48871652010-08-21 09:40:31 +0000462 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000463 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000464 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000465 DeclsInGroup.push_back(ThisDecl);
466 }
467
468 if (DeclEnd)
469 *DeclEnd = Tok.getLocation();
470
471 if (Context != Declarator::ForContext &&
472 ExpectAndConsume(tok::semi,
473 Context == Declarator::FileContext
474 ? diag::err_invalid_token_after_toplevel_declarator
475 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000476 // Okay, there was no semicolon and one was expected. If we see a
477 // declaration specifier, just assume it was missing and continue parsing.
478 // Otherwise things are very confused and we skip to recover.
479 if (!isDeclarationSpecifier()) {
480 SkipUntil(tok::r_brace, true, true);
481 if (Tok.is(tok::semi))
482 ConsumeToken();
483 }
John McCalld5a36322009-11-03 19:26:08 +0000484 }
485
Douglas Gregor0be31a22010-07-02 17:43:08 +0000486 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000487 DeclsInGroup.data(),
488 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000489}
490
Douglas Gregor23996282009-05-12 21:31:51 +0000491/// \brief Parse 'declaration' after parsing 'declaration-specifiers
492/// declarator'. This method parses the remainder of the declaration
493/// (including any attributes or initializer, among other things) and
494/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000495///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000496/// init-declarator: [C99 6.7]
497/// declarator
498/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000499/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
500/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000501/// [C++] declarator initializer[opt]
502///
503/// [C++] initializer:
504/// [C++] '=' initializer-clause
505/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000506/// [C++0x] '=' 'default' [TODO]
507/// [C++0x] '=' 'delete'
508///
509/// According to the standard grammar, =default and =delete are function
510/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000511///
John McCall48871652010-08-21 09:40:31 +0000512Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000513 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000514 // If a simple-asm-expr is present, parse it.
515 if (Tok.is(tok::kw_asm)) {
516 SourceLocation Loc;
517 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
518 if (AsmLabel.isInvalid()) {
519 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000520 return 0;
Douglas Gregor23996282009-05-12 21:31:51 +0000521 }
Mike Stump11289f42009-09-09 15:08:12 +0000522
Douglas Gregor23996282009-05-12 21:31:51 +0000523 D.setAsmLabel(AsmLabel.release());
524 D.SetRangeEnd(Loc);
525 }
Mike Stump11289f42009-09-09 15:08:12 +0000526
Douglas Gregor23996282009-05-12 21:31:51 +0000527 // If attributes are present, parse them.
528 if (Tok.is(tok::kw___attribute)) {
529 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000530 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor23996282009-05-12 21:31:51 +0000531 D.AddAttributes(AttrList, Loc);
532 }
Mike Stump11289f42009-09-09 15:08:12 +0000533
Douglas Gregor23996282009-05-12 21:31:51 +0000534 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +0000535 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000536 switch (TemplateInfo.Kind) {
537 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000538 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000539 break;
540
541 case ParsedTemplateInfo::Template:
542 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000543 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000544 Action::MultiTemplateParamsArg(Actions,
545 TemplateInfo.TemplateParams->data(),
546 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000547 D);
548 break;
549
550 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +0000551 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000552 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000553 TemplateInfo.ExternLoc,
554 TemplateInfo.TemplateLoc,
555 D);
556 if (ThisRes.isInvalid()) {
557 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000558 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000559 }
560
561 ThisDecl = ThisRes.get();
562 break;
563 }
564 }
Mike Stump11289f42009-09-09 15:08:12 +0000565
Douglas Gregor23996282009-05-12 21:31:51 +0000566 // Parse declarator '=' initializer.
567 if (Tok.is(tok::equal)) {
568 ConsumeToken();
569 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
570 SourceLocation DelLoc = ConsumeToken();
571 Actions.SetDeclDeleted(ThisDecl, DelLoc);
572 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000573 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
574 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000575 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000576 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000577
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000578 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000579 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000580 ConsumeCodeCompletionToken();
581 SkipUntil(tok::comma, true, true);
582 return ThisDecl;
583 }
584
Douglas Gregor23996282009-05-12 21:31:51 +0000585 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000586
John McCall1f4ee7b2009-12-19 09:28:58 +0000587 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000588 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000589 ExitScope();
590 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000591
Douglas Gregor23996282009-05-12 21:31:51 +0000592 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000593 SkipUntil(tok::comma, true, true);
594 Actions.ActOnInitializerError(ThisDecl);
595 } else
596 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor23996282009-05-12 21:31:51 +0000597 }
598 } else if (Tok.is(tok::l_paren)) {
599 // Parse C++ direct initializer: '(' expression-list ')'
600 SourceLocation LParenLoc = ConsumeParen();
601 ExprVector Exprs(Actions);
602 CommaLocsTy CommaLocs;
603
Douglas Gregor613bf102009-12-22 17:47:17 +0000604 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
605 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000606 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000607 }
608
Douglas Gregor23996282009-05-12 21:31:51 +0000609 if (ParseExpressionList(Exprs, CommaLocs)) {
610 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000611
612 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000613 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000614 ExitScope();
615 }
Douglas Gregor23996282009-05-12 21:31:51 +0000616 } else {
617 // Match the ')'.
618 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
619
620 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
621 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000622
623 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000624 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000625 ExitScope();
626 }
627
Douglas Gregor23996282009-05-12 21:31:51 +0000628 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
629 move_arg(Exprs),
Jay Foad7d0479f2009-05-21 09:52:38 +0000630 CommaLocs.data(), RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000631 }
632 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000633 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000634 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
635 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000636 }
637
638 return ThisDecl;
639}
640
Chris Lattner1890ac82006-08-13 01:16:23 +0000641/// ParseSpecifierQualifierList
642/// specifier-qualifier-list:
643/// type-specifier specifier-qualifier-list[opt]
644/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000645/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000646///
647void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
648 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
649 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000650 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000651
Chris Lattner1890ac82006-08-13 01:16:23 +0000652 // Validate declspec for type-name.
653 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000654 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
655 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000656 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000657
Chris Lattner1b22eed2006-11-28 05:12:07 +0000658 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000659 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000660 if (DS.getStorageClassSpecLoc().isValid())
661 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
662 else
663 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000664 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000665 }
Mike Stump11289f42009-09-09 15:08:12 +0000666
Chris Lattner1b22eed2006-11-28 05:12:07 +0000667 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000668 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000669 if (DS.isInlineSpecified())
670 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
671 if (DS.isVirtualSpecified())
672 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
673 if (DS.isExplicitSpecified())
674 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000675 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000676 }
677}
Chris Lattner53361ac2006-08-10 05:19:57 +0000678
Chris Lattner6cc055a2009-04-12 20:42:31 +0000679/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
680/// specified token is valid after the identifier in a declarator which
681/// immediately follows the declspec. For example, these things are valid:
682///
683/// int x [ 4]; // direct-declarator
684/// int x ( int y); // direct-declarator
685/// int(int x ) // direct-declarator
686/// int x ; // simple-declaration
687/// int x = 17; // init-declarator-list
688/// int x , y; // init-declarator-list
689/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000690/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000691/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000692///
693/// This is not, because 'x' does not immediately follow the declspec (though
694/// ')' happens to be valid anyway).
695/// int (x)
696///
697static bool isValidAfterIdentifierInDeclarator(const Token &T) {
698 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
699 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000700 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000701}
702
Chris Lattner20a0c612009-04-14 21:34:55 +0000703
704/// ParseImplicitInt - This method is called when we have an non-typename
705/// identifier in a declspec (which normally terminates the decl spec) when
706/// the declspec has no type specifier. In this case, the declspec is either
707/// malformed or is "implicit int" (in K&R and C89).
708///
709/// This method handles diagnosing this prettily and returns false if the
710/// declspec is done being processed. If it recovers and thinks there may be
711/// other pieces of declspec after it, it returns true.
712///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000713bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000714 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000715 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000716 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000717
Chris Lattner20a0c612009-04-14 21:34:55 +0000718 SourceLocation Loc = Tok.getLocation();
719 // If we see an identifier that is not a type name, we normally would
720 // parse it as the identifer being declared. However, when a typename
721 // is typo'd or the definition is not included, this will incorrectly
722 // parse the typename as the identifier name and fall over misparsing
723 // later parts of the diagnostic.
724 //
725 // As such, we try to do some look-ahead in cases where this would
726 // otherwise be an "implicit-int" case to see if this is invalid. For
727 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
728 // an identifier with implicit int, we'd get a parse error because the
729 // next token is obviously invalid for a type. Parse these as a case
730 // with an invalid type specifier.
731 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000732
Chris Lattner20a0c612009-04-14 21:34:55 +0000733 // Since we know that this either implicit int (which is rare) or an
734 // error, we'd do lookahead to try to do better recovery.
735 if (isValidAfterIdentifierInDeclarator(NextToken())) {
736 // If this token is valid for implicit int, e.g. "static x = 4", then
737 // we just avoid eating the identifier, so it will be parsed as the
738 // identifier in the declarator.
739 return false;
740 }
Mike Stump11289f42009-09-09 15:08:12 +0000741
Chris Lattner20a0c612009-04-14 21:34:55 +0000742 // Otherwise, if we don't consume this token, we are going to emit an
743 // error anyway. Try to recover from various common problems. Check
744 // to see if this was a reference to a tag name without a tag specified.
745 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000746 //
747 // C++ doesn't need this, and isTagName doesn't take SS.
748 if (SS == 0) {
749 const char *TagName = 0;
750 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000751
Douglas Gregor0be31a22010-07-02 17:43:08 +0000752 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +0000753 default: break;
754 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
755 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
756 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
757 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
758 }
Mike Stump11289f42009-09-09 15:08:12 +0000759
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000760 if (TagName) {
761 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +0000762 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +0000763 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000764
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000765 // Parse this as a tag as if the missing tag were present.
766 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +0000767 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000768 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000769 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000770 return true;
771 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000772 }
Mike Stump11289f42009-09-09 15:08:12 +0000773
Douglas Gregor15e56022009-10-13 23:27:22 +0000774 // This is almost certainly an invalid type name. Let the action emit a
775 // diagnostic and attempt to recover.
776 Action::TypeTy *T = 0;
777 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +0000778 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +0000779 // The action emitted a diagnostic, so we don't have to.
780 if (T) {
781 // The action has suggested that the type T could be used. Set that as
782 // the type in the declaration specifiers, consume the would-be type
783 // name token, and we're done.
784 const char *PrevSpec;
785 unsigned DiagID;
786 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
787 false);
788 DS.SetRangeEnd(Tok.getLocation());
789 ConsumeToken();
790
791 // There may be other declaration specifiers after this.
792 return true;
793 }
794
795 // Fall through; the action had no suggestion for us.
796 } else {
797 // The action did not emit a diagnostic, so emit one now.
798 SourceRange R;
799 if (SS) R = SS->getRange();
800 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
801 }
Mike Stump11289f42009-09-09 15:08:12 +0000802
Douglas Gregor15e56022009-10-13 23:27:22 +0000803 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000804 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000805 unsigned DiagID;
806 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000807 DS.SetRangeEnd(Tok.getLocation());
808 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000809
Chris Lattner20a0c612009-04-14 21:34:55 +0000810 // TODO: Could inject an invalid typedef decl in an enclosing scope to
811 // avoid rippling error messages on subsequent uses of the same type,
812 // could be useful if #include was forgotten.
813 return false;
814}
815
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000816/// \brief Determine the declaration specifier context from the declarator
817/// context.
818///
819/// \param Context the declarator context, which is one of the
820/// Declarator::TheContext enumerator values.
821Parser::DeclSpecContext
822Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
823 if (Context == Declarator::MemberContext)
824 return DSC_class;
825 if (Context == Declarator::FileContext)
826 return DSC_top_level;
827 return DSC_normal;
828}
829
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000830/// ParseDeclarationSpecifiers
831/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000832/// storage-class-specifier declaration-specifiers[opt]
833/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000834/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000835/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000836///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000837/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000838/// 'typedef'
839/// 'extern'
840/// 'static'
841/// 'auto'
842/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000843/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000844/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000845/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000846/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000847/// [C++] 'virtual'
848/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000849/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000850/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000851
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000852///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000853void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000854 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000855 AccessSpecifier AS,
856 DeclSpecContext DSContext) {
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000857 if (Tok.is(tok::code_completion)) {
Douglas Gregor00c37ef2010-08-11 21:23:17 +0000858 Action::ParserCompletionContext CCC = Action::PCC_Namespace;
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000859 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Douglas Gregor00c37ef2010-08-11 21:23:17 +0000860 CCC = DSContext == DSC_class? Action::PCC_MemberTemplate
861 : Action::PCC_Template;
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000862 else if (DSContext == DSC_class)
Douglas Gregor00c37ef2010-08-11 21:23:17 +0000863 CCC = Action::PCC_Class;
Douglas Gregorf1934162010-01-13 21:24:21 +0000864 else if (ObjCImpDecl)
Douglas Gregor00c37ef2010-08-11 21:23:17 +0000865 CCC = Action::PCC_ObjCImplementation;
Douglas Gregorf1934162010-01-13 21:24:21 +0000866
Douglas Gregor0be31a22010-07-02 17:43:08 +0000867 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000868 ConsumeCodeCompletionToken();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000869 }
870
Chris Lattner2e232092008-03-13 06:29:04 +0000871 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000872 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000873 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000874 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000875 unsigned DiagID = 0;
876
Chris Lattner4d8f8732006-11-28 05:05:08 +0000877 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000878
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000879 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000880 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000881 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000882 // If this is not a declaration specifier token, we're done reading decl
883 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000884 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000885 return;
Mike Stump11289f42009-09-09 15:08:12 +0000886
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000887 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +0000888 // C++ scope specifier. Annotate and loop, or bail out on error.
889 if (TryAnnotateCXXScopeToken(true)) {
890 if (!DS.hasTypeSpecifier())
891 DS.SetTypeSpecError();
892 goto DoneWithDeclSpec;
893 }
John McCall8bc2a702010-03-01 18:20:46 +0000894 if (Tok.is(tok::coloncolon)) // ::new or ::delete
895 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +0000896 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000897
898 case tok::annot_cxxscope: {
899 if (DS.hasTypeSpecifier())
900 goto DoneWithDeclSpec;
901
John McCall9dab4e62009-12-12 11:40:51 +0000902 CXXScopeSpec SS;
John McCall37ad5512010-08-23 06:44:23 +0000903 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCall9dab4e62009-12-12 11:40:51 +0000904 SS.setRange(Tok.getAnnotationRange());
905
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000906 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000907 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000908 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000909 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000910 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000911 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000912
913 // C++ [class.qual]p2:
914 // In a lookup in which the constructor is an acceptable lookup
915 // result and the nested-name-specifier nominates a class C:
916 //
917 // - if the name specified after the
918 // nested-name-specifier, when looked up in C, is the
919 // injected-class-name of C (Clause 9), or
920 //
921 // - if the name specified after the nested-name-specifier
922 // is the same as the identifier or the
923 // simple-template-id's template-name in the last
924 // component of the nested-name-specifier,
925 //
926 // the name is instead considered to name the constructor of
927 // class C.
928 //
929 // Thus, if the template-name is actually the constructor
930 // name, then the code is ill-formed; this interpretation is
931 // reinforced by the NAD status of core issue 635.
932 TemplateIdAnnotation *TemplateId
933 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +0000934 if ((DSContext == DSC_top_level ||
935 (DSContext == DSC_class && DS.isFriendSpecified())) &&
936 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000937 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000938 if (isConstructorDeclarator()) {
939 // The user meant this to be an out-of-line constructor
940 // definition, but template arguments are not allowed
941 // there. Just allow this as a constructor; we'll
942 // complain about it later.
943 goto DoneWithDeclSpec;
944 }
945
946 // The user meant this to name a type, but it actually names
947 // a constructor with some extraneous template
948 // arguments. Complain, then parse it as a type as the user
949 // intended.
950 Diag(TemplateId->TemplateNameLoc,
951 diag::err_out_of_line_template_id_names_constructor)
952 << TemplateId->Name;
953 }
954
John McCall9dab4e62009-12-12 11:40:51 +0000955 DS.getTypeSpecScope() = SS;
956 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +0000957 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000958 "ParseOptionalCXXScopeSpecifier not working");
959 AnnotateTemplateIdTokenAsType(&SS);
960 continue;
961 }
962
Douglas Gregorc5790df2009-09-28 07:26:33 +0000963 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +0000964 DS.getTypeSpecScope() = SS;
965 ConsumeToken(); // The C++ scope.
Douglas Gregorc5790df2009-09-28 07:26:33 +0000966 if (Tok.getAnnotationValue())
967 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
968 PrevSpec, DiagID,
969 Tok.getAnnotationValue());
970 else
971 DS.SetTypeSpecError();
972 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
973 ConsumeToken(); // The typename
974 }
975
Douglas Gregor167fa622009-03-25 15:40:00 +0000976 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000977 goto DoneWithDeclSpec;
978
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000979 // If we're in a context where the identifier could be a class name,
980 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +0000981 if ((DSContext == DSC_top_level ||
982 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000983 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000984 &SS)) {
985 if (isConstructorDeclarator())
986 goto DoneWithDeclSpec;
987
988 // As noted in C++ [class.qual]p2 (cited above), when the name
989 // of the class is qualified in a context where it could name
990 // a constructor, its a constructor name. However, we've
991 // looked at the declarator, and the user probably meant this
992 // to be a type. Complain that it isn't supposed to be treated
993 // as a type, then proceed to parse it as a type.
994 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
995 << Next.getIdentifierInfo();
996 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000997
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000998 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
Douglas Gregor0be31a22010-07-02 17:43:08 +0000999 Next.getLocation(), getCurScope(), &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001000
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001001 // If the referenced identifier is not a type, then this declspec is
1002 // erroneous: We already checked about that it has no type specifier, and
1003 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001004 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001005 if (TypeRep == 0) {
1006 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001007 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001008 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001009 }
Mike Stump11289f42009-09-09 15:08:12 +00001010
John McCall9dab4e62009-12-12 11:40:51 +00001011 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001012 ConsumeToken(); // The C++ scope.
1013
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001014 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001015 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001016 if (isInvalid)
1017 break;
Mike Stump11289f42009-09-09 15:08:12 +00001018
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001019 DS.SetRangeEnd(Tok.getLocation());
1020 ConsumeToken(); // The typename.
1021
1022 continue;
1023 }
Mike Stump11289f42009-09-09 15:08:12 +00001024
Chris Lattnere387d9e2009-01-21 19:48:37 +00001025 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001026 if (Tok.getAnnotationValue())
1027 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001028 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001029 else
1030 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001031
1032 if (isInvalid)
1033 break;
1034
Chris Lattnere387d9e2009-01-21 19:48:37 +00001035 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1036 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001037
Chris Lattnere387d9e2009-01-21 19:48:37 +00001038 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1039 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1040 // Objective-C interface. If we don't have Objective-C or a '<', this is
1041 // just a normal reference to a typedef name.
1042 if (!Tok.is(tok::less) || !getLang().ObjC1)
1043 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001044
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001045 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001046 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001047 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1048 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1049 LAngleLoc, EndProtoLoc);
1050 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1051 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001052
Chris Lattnere387d9e2009-01-21 19:48:37 +00001053 DS.SetRangeEnd(EndProtoLoc);
1054 continue;
1055 }
Mike Stump11289f42009-09-09 15:08:12 +00001056
Chris Lattner16fac4f2008-07-26 01:18:38 +00001057 // typedef-name
1058 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001059 // In C++, check to see if this is a scope specifier like foo::bar::, if
1060 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001061 if (getLang().CPlusPlus) {
1062 if (TryAnnotateCXXScopeToken(true)) {
1063 if (!DS.hasTypeSpecifier())
1064 DS.SetTypeSpecError();
1065 goto DoneWithDeclSpec;
1066 }
1067 if (!Tok.is(tok::identifier))
1068 continue;
1069 }
Mike Stump11289f42009-09-09 15:08:12 +00001070
Chris Lattner16fac4f2008-07-26 01:18:38 +00001071 // This identifier can only be a typedef name if we haven't already seen
1072 // a type-specifier. Without this check we misparse:
1073 // typedef int X; struct Y { short X; }; as 'short int'.
1074 if (DS.hasTypeSpecifier())
1075 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001076
John Thompson22334602010-02-05 00:12:22 +00001077 // Check for need to substitute AltiVec keyword tokens.
1078 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1079 break;
1080
Chris Lattner16fac4f2008-07-26 01:18:38 +00001081 // It has to be available as a typedef too!
Mike Stump11289f42009-09-09 15:08:12 +00001082 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor0be31a22010-07-02 17:43:08 +00001083 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001084
Chris Lattner6cc055a2009-04-12 20:42:31 +00001085 // If this is not a typedef name, don't parse it as part of the declspec,
1086 // it must be an implicit int or an error.
1087 if (TypeRep == 0) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001088 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001089 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001090 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001091
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001092 // If we're in a context where the identifier could be a class name,
1093 // check whether this is a constructor declaration.
1094 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001095 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001096 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001097 goto DoneWithDeclSpec;
1098
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001099 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001100 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001101 if (isInvalid)
1102 break;
Mike Stump11289f42009-09-09 15:08:12 +00001103
Chris Lattner16fac4f2008-07-26 01:18:38 +00001104 DS.SetRangeEnd(Tok.getLocation());
1105 ConsumeToken(); // The identifier
1106
1107 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1108 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1109 // Objective-C interface. If we don't have Objective-C or a '<', this is
1110 // just a normal reference to a typedef name.
1111 if (!Tok.is(tok::less) || !getLang().ObjC1)
1112 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001113
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001114 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001115 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001116 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1117 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1118 LAngleLoc, EndProtoLoc);
1119 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1120 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001121
Chris Lattner16fac4f2008-07-26 01:18:38 +00001122 DS.SetRangeEnd(EndProtoLoc);
1123
Steve Naroffcd5e7822008-09-22 10:28:57 +00001124 // Need to support trailing type qualifiers (e.g. "id<p> const").
1125 // If a type specifier follows, it will be diagnosed elsewhere.
1126 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001127 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001128
1129 // type-name
1130 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001131 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001132 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001133 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001134 // This template-id does not refer to a type name, so we're
1135 // done with the type-specifiers.
1136 goto DoneWithDeclSpec;
1137 }
1138
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001139 // If we're in a context where the template-id could be a
1140 // constructor name or specialization, check whether this is a
1141 // constructor declaration.
1142 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001143 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001144 isConstructorDeclarator())
1145 goto DoneWithDeclSpec;
1146
Douglas Gregor7f741122009-02-25 19:37:18 +00001147 // Turn the template-id annotation token into a type annotation
1148 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001149 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001150 continue;
1151 }
1152
Chris Lattnere37e2332006-08-15 04:50:22 +00001153 // GNU attributes support.
1154 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001155 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001156 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001157
1158 // Microsoft declspec support.
1159 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +00001160 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001161 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001162
Steve Naroff44ac7772008-12-25 14:16:32 +00001163 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001164 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001165 // FIXME: Add handling here!
1166 break;
1167
1168 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001169 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001170 case tok::kw___cdecl:
1171 case tok::kw___stdcall:
1172 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001173 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001174 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1175 continue;
1176
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001177 // storage-class-specifier
1178 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001179 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1180 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001181 break;
1182 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001183 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001184 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001185 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1186 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001187 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001188 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001189 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001190 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001191 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001192 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001193 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001194 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001195 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1196 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001197 break;
1198 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001199 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001200 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1201 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001202 else
John McCall49bfce42009-08-03 20:12:06 +00001203 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1204 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001205 break;
1206 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001207 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1208 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001209 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001210 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001211 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1212 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001213 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001214 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001215 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001216 break;
Mike Stump11289f42009-09-09 15:08:12 +00001217
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001218 // function-specifier
1219 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001220 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001221 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001222 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001223 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001224 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001225 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001226 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001227 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001228
Anders Carlssoncd8db412009-05-06 04:46:28 +00001229 // friend
1230 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001231 if (DSContext == DSC_class)
1232 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1233 else {
1234 PrevSpec = ""; // not actually used by the diagnostic
1235 DiagID = diag::err_friend_invalid_in_context;
1236 isInvalid = true;
1237 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001238 break;
Mike Stump11289f42009-09-09 15:08:12 +00001239
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001240 // constexpr
1241 case tok::kw_constexpr:
1242 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1243 break;
1244
Chris Lattnere387d9e2009-01-21 19:48:37 +00001245 // type-specifier
1246 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001247 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1248 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001249 break;
1250 case tok::kw_long:
1251 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001252 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1253 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001254 else
John McCall49bfce42009-08-03 20:12:06 +00001255 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1256 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001257 break;
1258 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001259 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1260 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001261 break;
1262 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001263 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1264 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001265 break;
1266 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001267 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1268 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001269 break;
1270 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001271 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1272 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001273 break;
1274 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001275 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1276 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001277 break;
1278 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001279 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1280 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001281 break;
1282 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001283 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1284 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001285 break;
1286 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001287 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1288 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001289 break;
1290 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001291 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1292 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001293 break;
1294 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001295 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1296 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001297 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001298 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001299 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1300 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001301 break;
1302 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001303 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1304 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001305 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001306 case tok::kw_bool:
1307 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001308 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1309 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001310 break;
1311 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001312 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1313 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001314 break;
1315 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001316 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1317 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001318 break;
1319 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001320 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1321 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001322 break;
John Thompson22334602010-02-05 00:12:22 +00001323 case tok::kw___vector:
1324 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1325 break;
1326 case tok::kw___pixel:
1327 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1328 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001329
1330 // class-specifier:
1331 case tok::kw_class:
1332 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001333 case tok::kw_union: {
1334 tok::TokenKind Kind = Tok.getKind();
1335 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001336 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001337 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001338 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001339
1340 // enum-specifier:
1341 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001342 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001343 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001344 continue;
1345
1346 // cv-qualifier:
1347 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001348 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1349 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001350 break;
1351 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001352 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1353 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001354 break;
1355 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001356 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1357 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001358 break;
1359
Douglas Gregor333489b2009-03-27 23:10:48 +00001360 // C++ typename-specifier:
1361 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001362 if (TryAnnotateTypeOrScopeToken()) {
1363 DS.SetTypeSpecError();
1364 goto DoneWithDeclSpec;
1365 }
1366 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001367 continue;
1368 break;
1369
Chris Lattnere387d9e2009-01-21 19:48:37 +00001370 // GNU typeof support.
1371 case tok::kw_typeof:
1372 ParseTypeofSpecifier(DS);
1373 continue;
1374
Anders Carlsson74948d02009-06-24 17:47:40 +00001375 case tok::kw_decltype:
1376 ParseDecltypeSpecifier(DS);
1377 continue;
1378
Steve Naroffcfdf6162008-06-05 00:02:44 +00001379 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001380 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001381 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1382 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001383 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001384 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001385
Chris Lattner0974b232008-07-26 00:20:22 +00001386 {
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001387 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001388 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001389 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1390 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1391 LAngleLoc, EndProtoLoc);
1392 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1393 ProtocolLocs.data(), LAngleLoc);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001394 DS.SetRangeEnd(EndProtoLoc);
1395
Chris Lattner6d29c102008-11-18 07:48:38 +00001396 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregora771f462010-03-31 17:46:05 +00001397 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001398 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001399 // Need to support trailing type qualifiers (e.g. "id<p> const").
1400 // If a type specifier follows, it will be diagnosed elsewhere.
1401 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001402 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001403 }
John McCall49bfce42009-08-03 20:12:06 +00001404 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001405 if (isInvalid) {
1406 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001407 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001408
1409 if (DiagID == diag::ext_duplicate_declspec)
1410 Diag(Tok, DiagID)
1411 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1412 else
1413 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001414 }
Chris Lattner2e232092008-03-13 06:29:04 +00001415 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001416 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001417 }
1418}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001419
Chris Lattnera448d752009-01-06 06:59:53 +00001420/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001421/// primarily follow the C++ grammar with additions for C99 and GNU,
1422/// which together subsume the C grammar. Note that the C++
1423/// type-specifier also includes the C type-qualifier (for const,
1424/// volatile, and C99 restrict). Returns true if a type-specifier was
1425/// found (and parsed), false otherwise.
1426///
1427/// type-specifier: [C++ 7.1.5]
1428/// simple-type-specifier
1429/// class-specifier
1430/// enum-specifier
1431/// elaborated-type-specifier [TODO]
1432/// cv-qualifier
1433///
1434/// cv-qualifier: [C++ 7.1.5.1]
1435/// 'const'
1436/// 'volatile'
1437/// [C99] 'restrict'
1438///
1439/// simple-type-specifier: [ C++ 7.1.5.2]
1440/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1441/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1442/// 'char'
1443/// 'wchar_t'
1444/// 'bool'
1445/// 'short'
1446/// 'int'
1447/// 'long'
1448/// 'signed'
1449/// 'unsigned'
1450/// 'float'
1451/// 'double'
1452/// 'void'
1453/// [C99] '_Bool'
1454/// [C99] '_Complex'
1455/// [C99] '_Imaginary' // Removed in TC2?
1456/// [GNU] '_Decimal32'
1457/// [GNU] '_Decimal64'
1458/// [GNU] '_Decimal128'
1459/// [GNU] typeof-specifier
1460/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1461/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001462/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001463/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001464bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001465 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001466 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001467 const ParsedTemplateInfo &TemplateInfo,
1468 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001469 SourceLocation Loc = Tok.getLocation();
1470
1471 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001472 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001473 // If we already have a type specifier, this identifier is not a type.
1474 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1475 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1476 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1477 return false;
John Thompson22334602010-02-05 00:12:22 +00001478 // Check for need to substitute AltiVec keyword tokens.
1479 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1480 break;
1481 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001482 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001483 // Annotate typenames and C++ scope specifiers. If we get one, just
1484 // recurse to handle whatever we get.
1485 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001486 return true;
1487 if (Tok.is(tok::identifier))
1488 return false;
1489 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1490 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001491 case tok::coloncolon: // ::foo::bar
1492 if (NextToken().is(tok::kw_new) || // ::new
1493 NextToken().is(tok::kw_delete)) // ::delete
1494 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001495
Chris Lattner020bab92009-01-04 23:41:41 +00001496 // Annotate typenames and C++ scope specifiers. If we get one, just
1497 // recurse to handle whatever we get.
1498 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001499 return true;
1500 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1501 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001502
Douglas Gregor450c75a2008-11-07 15:42:26 +00001503 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001504 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001505 if (Tok.getAnnotationValue())
1506 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001507 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001508 else
1509 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001510 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1511 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001512
Douglas Gregor450c75a2008-11-07 15:42:26 +00001513 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1514 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1515 // Objective-C interface. If we don't have Objective-C or a '<', this is
1516 // just a normal reference to a typedef name.
1517 if (!Tok.is(tok::less) || !getLang().ObjC1)
1518 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001519
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001520 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001521 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001522 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1523 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1524 LAngleLoc, EndProtoLoc);
1525 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1526 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001527
Douglas Gregor450c75a2008-11-07 15:42:26 +00001528 DS.SetRangeEnd(EndProtoLoc);
1529 return true;
1530 }
1531
1532 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001533 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001534 break;
1535 case tok::kw_long:
1536 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001537 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1538 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001539 else
John McCall49bfce42009-08-03 20:12:06 +00001540 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1541 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001542 break;
1543 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001544 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001545 break;
1546 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001547 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1548 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001549 break;
1550 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001551 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1552 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001553 break;
1554 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001555 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1556 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001557 break;
1558 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001559 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001560 break;
1561 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001562 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001563 break;
1564 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001565 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001566 break;
1567 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001568 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001569 break;
1570 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001571 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001572 break;
1573 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001574 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001575 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001576 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001577 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001578 break;
1579 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001580 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001581 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001582 case tok::kw_bool:
1583 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001584 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001585 break;
1586 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001587 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1588 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001589 break;
1590 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001591 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1592 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001593 break;
1594 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001595 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1596 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001597 break;
John Thompson22334602010-02-05 00:12:22 +00001598 case tok::kw___vector:
1599 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1600 break;
1601 case tok::kw___pixel:
1602 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1603 break;
1604
Douglas Gregor450c75a2008-11-07 15:42:26 +00001605 // class-specifier:
1606 case tok::kw_class:
1607 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001608 case tok::kw_union: {
1609 tok::TokenKind Kind = Tok.getKind();
1610 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001611 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1612 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001613 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001614 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001615
1616 // enum-specifier:
1617 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001618 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001619 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001620 return true;
1621
1622 // cv-qualifier:
1623 case tok::kw_const:
1624 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001625 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001626 break;
1627 case tok::kw_volatile:
1628 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001629 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001630 break;
1631 case tok::kw_restrict:
1632 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001633 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001634 break;
1635
1636 // GNU typeof support.
1637 case tok::kw_typeof:
1638 ParseTypeofSpecifier(DS);
1639 return true;
1640
Anders Carlsson74948d02009-06-24 17:47:40 +00001641 // C++0x decltype support.
1642 case tok::kw_decltype:
1643 ParseDecltypeSpecifier(DS);
1644 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001645
Anders Carlssonbae27372009-06-26 23:44:14 +00001646 // C++0x auto support.
1647 case tok::kw_auto:
1648 if (!getLang().CPlusPlus0x)
1649 return false;
1650
John McCall49bfce42009-08-03 20:12:06 +00001651 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001652 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001653 case tok::kw___ptr64:
1654 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001655 case tok::kw___cdecl:
1656 case tok::kw___stdcall:
1657 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001658 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001659 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001660 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001661
Douglas Gregor450c75a2008-11-07 15:42:26 +00001662 default:
1663 // Not a type-specifier; do nothing.
1664 return false;
1665 }
1666
1667 // If the specifier combination wasn't legal, issue a diagnostic.
1668 if (isInvalid) {
1669 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001670 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001671 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001672 }
1673 DS.SetRangeEnd(Tok.getLocation());
1674 ConsumeToken(); // whatever we parsed above.
1675 return true;
1676}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001677
Chris Lattner70ae4912007-10-29 04:42:53 +00001678/// ParseStructDeclaration - Parse a struct declaration without the terminating
1679/// semicolon.
1680///
Chris Lattner90a26b02007-01-23 04:38:16 +00001681/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001682/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001683/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001684/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001685/// struct-declarator-list:
1686/// struct-declarator
1687/// struct-declarator-list ',' struct-declarator
1688/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1689/// struct-declarator:
1690/// declarator
1691/// [GNU] declarator attributes[opt]
1692/// declarator[opt] ':' constant-expression
1693/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1694///
Chris Lattnera12405b2008-04-10 06:46:29 +00001695void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001696ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001697 if (Tok.is(tok::kw___extension__)) {
1698 // __extension__ silences extension warnings in the subexpression.
1699 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001700 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001701 return ParseStructDeclaration(DS, Fields);
1702 }
Mike Stump11289f42009-09-09 15:08:12 +00001703
Steve Naroff97170802007-08-20 22:28:22 +00001704 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001705 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001706 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001707
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001708 // If there are no declarators, this is a free-standing declaration
1709 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001710 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001711 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001712 return;
1713 }
1714
1715 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001716 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001717 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001718 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001719 FieldDeclarator DeclaratorInfo(DS);
1720
1721 // Attributes are only allowed here on successive declarators.
1722 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1723 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001724 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallcfefb6d2009-11-03 02:38:08 +00001725 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1726 }
Mike Stump11289f42009-09-09 15:08:12 +00001727
Steve Naroff97170802007-08-20 22:28:22 +00001728 /// struct-declarator: declarator
1729 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001730 if (Tok.isNot(tok::colon)) {
1731 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1732 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001733 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001734 }
Mike Stump11289f42009-09-09 15:08:12 +00001735
Chris Lattner76c72282007-10-09 17:33:22 +00001736 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001737 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001738 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001739 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001740 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001741 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001742 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001743 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001744
Steve Naroff97170802007-08-20 22:28:22 +00001745 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001746 if (Tok.is(tok::kw___attribute)) {
1747 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001748 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001749 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1750 }
1751
John McCallcfefb6d2009-11-03 02:38:08 +00001752 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00001753 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00001754 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001755
Steve Naroff97170802007-08-20 22:28:22 +00001756 // If we don't have a comma, it is either the end of the list (a ';')
1757 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001758 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001759 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001760
Steve Naroff97170802007-08-20 22:28:22 +00001761 // Consume the comma.
1762 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001763
John McCallcfefb6d2009-11-03 02:38:08 +00001764 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001765 }
Steve Naroff97170802007-08-20 22:28:22 +00001766}
1767
1768/// ParseStructUnionBody
1769/// struct-contents:
1770/// struct-declaration-list
1771/// [EXT] empty
1772/// [GNU] "struct-declaration-list" without terminatoring ';'
1773/// struct-declaration-list:
1774/// struct-declaration
1775/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001776/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001777///
Chris Lattner1300fb92007-01-23 23:42:53 +00001778void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001779 unsigned TagType, Decl *TagDecl) {
Chris Lattnereae6cb62009-03-05 08:00:35 +00001780 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1781 PP.getSourceManager(),
1782 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001783
Chris Lattner90a26b02007-01-23 04:38:16 +00001784 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001785
Douglas Gregor658b9552009-01-09 22:42:13 +00001786 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001787 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001788
Chris Lattner7b9ace62007-01-23 20:11:08 +00001789 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1790 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001791 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00001792 Diag(Tok, diag::ext_empty_struct_union)
1793 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001794
John McCall48871652010-08-21 09:40:31 +00001795 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001796
Chris Lattner7b9ace62007-01-23 20:11:08 +00001797 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001798 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001799 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001800
Chris Lattner736ed5d2007-06-09 05:59:07 +00001801 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001802 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001803 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00001804 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00001805 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001806 ConsumeToken();
1807 continue;
1808 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001809
1810 // Parse all the comma separated declarators.
1811 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001812
John McCallcfefb6d2009-11-03 02:38:08 +00001813 if (!Tok.is(tok::at)) {
1814 struct CFieldCallback : FieldCallback {
1815 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001816 Decl *TagDecl;
1817 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001818
John McCall48871652010-08-21 09:40:31 +00001819 CFieldCallback(Parser &P, Decl *TagDecl,
1820 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001821 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1822
John McCall48871652010-08-21 09:40:31 +00001823 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001824 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00001825 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00001826 FD.D.getDeclSpec().getSourceRange().getBegin(),
1827 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001828 FieldDecls.push_back(Field);
1829 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001830 }
John McCallcfefb6d2009-11-03 02:38:08 +00001831 } Callback(*this, TagDecl, FieldDecls);
1832
1833 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001834 } else { // Handle @defs
1835 ConsumeToken();
1836 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1837 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001838 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001839 continue;
1840 }
1841 ConsumeToken();
1842 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1843 if (!Tok.is(tok::identifier)) {
1844 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001845 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001846 continue;
1847 }
John McCall48871652010-08-21 09:40:31 +00001848 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001849 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001850 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001851 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1852 ConsumeToken();
1853 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001854 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001855
Chris Lattner76c72282007-10-09 17:33:22 +00001856 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001857 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001858 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001859 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001860 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001861 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001862 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1863 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001864 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001865 // If we stopped at a ';', eat it.
1866 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001867 }
1868 }
Mike Stump11289f42009-09-09 15:08:12 +00001869
Steve Naroff33a1e802007-10-29 21:38:07 +00001870 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001871
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001872 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner90a26b02007-01-23 04:38:16 +00001873 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001874 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001875 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar15619c72008-10-03 02:03:53 +00001876
Douglas Gregor0be31a22010-07-02 17:43:08 +00001877 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001878 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001879 LBraceLoc, RBraceLoc,
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001880 AttrList.get());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001881 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001882 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001883}
1884
1885
Chris Lattner3b561a32006-08-13 00:12:11 +00001886/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001887/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001888/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001889///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001890/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1891/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001892/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001893/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001894///
1895/// [C++] elaborated-type-specifier:
1896/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1897///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001898void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001899 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001900 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001901 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001902 if (Tok.is(tok::code_completion)) {
1903 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001904 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001905 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001906 }
1907
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001908 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001909 // If attributes exist after tag, parse them.
1910 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001911 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001912
Abramo Bagnarad7548482010-05-19 21:37:53 +00001913 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00001914 if (getLang().CPlusPlus) {
1915 if (ParseOptionalCXXScopeSpecifier(SS, 0, false))
1916 return;
1917
1918 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001919 Diag(Tok, diag::err_expected_ident);
1920 if (Tok.isNot(tok::l_brace)) {
1921 // Has no name and is not a definition.
1922 // Skip the rest of this declarator, up until the comma or semicolon.
1923 SkipUntil(tok::comma, true);
1924 return;
1925 }
1926 }
1927 }
Mike Stump11289f42009-09-09 15:08:12 +00001928
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001929 // Must have either 'enum name' or 'enum {...}'.
1930 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1931 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001932
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001933 // Skip the rest of this declarator, up until the comma or semicolon.
1934 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001935 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001936 }
Mike Stump11289f42009-09-09 15:08:12 +00001937
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001938 // If an identifier is present, consume and remember it.
1939 IdentifierInfo *Name = 0;
1940 SourceLocation NameLoc;
1941 if (Tok.is(tok::identifier)) {
1942 Name = Tok.getIdentifierInfo();
1943 NameLoc = ConsumeToken();
1944 }
Mike Stump11289f42009-09-09 15:08:12 +00001945
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001946 // There are three options here. If we have 'enum foo;', then this is a
1947 // forward declaration. If we have 'enum foo {...' then this is a
1948 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1949 //
1950 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1951 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1952 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1953 //
John McCall9bb74a52009-07-31 02:45:11 +00001954 Action::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001955 if (Tok.is(tok::l_brace))
John McCall9bb74a52009-07-31 02:45:11 +00001956 TUK = Action::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001957 else if (Tok.is(tok::semi))
John McCall9bb74a52009-07-31 02:45:11 +00001958 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001959 else
John McCall9bb74a52009-07-31 02:45:11 +00001960 TUK = Action::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00001961
1962 // enums cannot be templates, although they can be referenced from a
1963 // template.
1964 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
1965 TUK != Action::TUK_Reference) {
1966 Diag(Tok, diag::err_enum_template);
1967
1968 // Skip the rest of this declarator, up until the comma or semicolon.
1969 SkipUntil(tok::comma, true);
1970 return;
1971 }
1972
Douglas Gregord6ab8742009-05-28 23:31:59 +00001973 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00001974 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00001975 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
1976 const char *PrevSpec = 0;
1977 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00001978 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
1979 StartLoc, SS, Name, NameLoc, Attr.get(),
1980 AS,
1981 Action::MultiTemplateParamsArg(Actions),
1982 Owned, IsDependent);
Douglas Gregorba41d012010-04-24 16:38:41 +00001983 if (IsDependent) {
1984 // This enum has a dependent nested-name-specifier. Handle it as a
1985 // dependent tag.
1986 if (!Name) {
1987 DS.SetTypeSpecError();
1988 Diag(Tok, diag::err_expected_type_name_after_typename);
1989 return;
1990 }
1991
Douglas Gregor0be31a22010-07-02 17:43:08 +00001992 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00001993 TUK, SS, Name, StartLoc,
1994 NameLoc);
1995 if (Type.isInvalid()) {
1996 DS.SetTypeSpecError();
1997 return;
1998 }
1999
2000 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
2001 Type.get(), false))
2002 Diag(StartLoc, DiagID) << PrevSpec;
2003
2004 return;
2005 }
Mike Stump11289f42009-09-09 15:08:12 +00002006
John McCall48871652010-08-21 09:40:31 +00002007 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002008 // The action failed to produce an enumeration tag. If this is a
2009 // definition, consume the entire definition.
2010 if (Tok.is(tok::l_brace)) {
2011 ConsumeBrace();
2012 SkipUntil(tok::r_brace);
2013 }
2014
2015 DS.SetTypeSpecError();
2016 return;
2017 }
2018
Chris Lattner76c72282007-10-09 17:33:22 +00002019 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002020 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002021
Douglas Gregor72100632010-01-25 16:33:23 +00002022 // FIXME: The DeclSpec should keep the locations of both the keyword and the
2023 // name (if there is one).
Douglas Gregor72100632010-01-25 16:33:23 +00002024 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCall48871652010-08-21 09:40:31 +00002025 TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002026 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002027}
2028
Chris Lattnerc1915e22007-01-25 07:29:02 +00002029/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2030/// enumerator-list:
2031/// enumerator
2032/// enumerator-list ',' enumerator
2033/// enumerator:
2034/// enumeration-constant
2035/// enumeration-constant '=' constant-expression
2036/// enumeration-constant:
2037/// identifier
2038///
John McCall48871652010-08-21 09:40:31 +00002039void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002040 // Enter the scope of the enum body and start the definition.
2041 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002042 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002043
Chris Lattnerc1915e22007-01-25 07:29:02 +00002044 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002045
Chris Lattner37256fb2007-08-27 17:24:30 +00002046 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002047 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002048 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002049
John McCall48871652010-08-21 09:40:31 +00002050 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002051
John McCall48871652010-08-21 09:40:31 +00002052 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002053
Chris Lattnerc1915e22007-01-25 07:29:02 +00002054 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002055 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002056 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2057 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002058
Chris Lattnerc1915e22007-01-25 07:29:02 +00002059 SourceLocation EqualLoc;
John McCall37ad5512010-08-23 06:44:23 +00002060 OwningExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002061 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002062 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002063 AssignedVal = ParseConstantExpression();
2064 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002065 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002066 }
Mike Stump11289f42009-09-09 15:08:12 +00002067
Chris Lattnerc1915e22007-01-25 07:29:02 +00002068 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002069 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2070 LastEnumConstDecl,
2071 IdentLoc, Ident,
2072 EqualLoc,
2073 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002074 EnumConstantDecls.push_back(EnumConstDecl);
2075 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002076
Chris Lattner76c72282007-10-09 17:33:22 +00002077 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002078 break;
2079 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002080
2081 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002082 !(getLang().C99 || getLang().CPlusPlus0x))
2083 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2084 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002085 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002086 }
Mike Stump11289f42009-09-09 15:08:12 +00002087
Chris Lattnerc1915e22007-01-25 07:29:02 +00002088 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002089 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002090
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002091 llvm::OwningPtr<AttributeList> Attr;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002092 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00002093 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002094 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002095
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002096 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2097 EnumConstantDecls.data(), EnumConstantDecls.size(),
Douglas Gregor0be31a22010-07-02 17:43:08 +00002098 getCurScope(), Attr.get());
Mike Stump11289f42009-09-09 15:08:12 +00002099
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002100 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002101 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002102}
Chris Lattner3b561a32006-08-13 00:12:11 +00002103
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002104/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002105/// start of a type-qualifier-list.
2106bool Parser::isTypeQualifier() const {
2107 switch (Tok.getKind()) {
2108 default: return false;
2109 // type-qualifier
2110 case tok::kw_const:
2111 case tok::kw_volatile:
2112 case tok::kw_restrict:
2113 return true;
2114 }
2115}
2116
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002117/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2118/// is definitely a type-specifier. Return false if it isn't part of a type
2119/// specifier or if we're not sure.
2120bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2121 switch (Tok.getKind()) {
2122 default: return false;
2123 // type-specifiers
2124 case tok::kw_short:
2125 case tok::kw_long:
2126 case tok::kw_signed:
2127 case tok::kw_unsigned:
2128 case tok::kw__Complex:
2129 case tok::kw__Imaginary:
2130 case tok::kw_void:
2131 case tok::kw_char:
2132 case tok::kw_wchar_t:
2133 case tok::kw_char16_t:
2134 case tok::kw_char32_t:
2135 case tok::kw_int:
2136 case tok::kw_float:
2137 case tok::kw_double:
2138 case tok::kw_bool:
2139 case tok::kw__Bool:
2140 case tok::kw__Decimal32:
2141 case tok::kw__Decimal64:
2142 case tok::kw__Decimal128:
2143 case tok::kw___vector:
2144
2145 // struct-or-union-specifier (C99) or class-specifier (C++)
2146 case tok::kw_class:
2147 case tok::kw_struct:
2148 case tok::kw_union:
2149 // enum-specifier
2150 case tok::kw_enum:
2151
2152 // typedef-name
2153 case tok::annot_typename:
2154 return true;
2155 }
2156}
2157
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002158/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002159/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002160bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002161 switch (Tok.getKind()) {
2162 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002163
Chris Lattner020bab92009-01-04 23:41:41 +00002164 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002165 if (TryAltiVecVectorToken())
2166 return true;
2167 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002168 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002169 // Annotate typenames and C++ scope specifiers. If we get one, just
2170 // recurse to handle whatever we get.
2171 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002172 return true;
2173 if (Tok.is(tok::identifier))
2174 return false;
2175 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002176
Chris Lattner020bab92009-01-04 23:41:41 +00002177 case tok::coloncolon: // ::foo::bar
2178 if (NextToken().is(tok::kw_new) || // ::new
2179 NextToken().is(tok::kw_delete)) // ::delete
2180 return false;
2181
Chris Lattner020bab92009-01-04 23:41:41 +00002182 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002183 return true;
2184 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002185
Chris Lattnere37e2332006-08-15 04:50:22 +00002186 // GNU attributes support.
2187 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002188 // GNU typeof support.
2189 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002190
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002191 // type-specifiers
2192 case tok::kw_short:
2193 case tok::kw_long:
2194 case tok::kw_signed:
2195 case tok::kw_unsigned:
2196 case tok::kw__Complex:
2197 case tok::kw__Imaginary:
2198 case tok::kw_void:
2199 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002200 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002201 case tok::kw_char16_t:
2202 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002203 case tok::kw_int:
2204 case tok::kw_float:
2205 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002206 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002207 case tok::kw__Bool:
2208 case tok::kw__Decimal32:
2209 case tok::kw__Decimal64:
2210 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002211 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002212
Chris Lattner861a2262008-04-13 18:59:07 +00002213 // struct-or-union-specifier (C99) or class-specifier (C++)
2214 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002215 case tok::kw_struct:
2216 case tok::kw_union:
2217 // enum-specifier
2218 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002219
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002220 // type-qualifier
2221 case tok::kw_const:
2222 case tok::kw_volatile:
2223 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002224
2225 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002226 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002227 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002228
Chris Lattner409bf7d2008-10-20 00:25:30 +00002229 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2230 case tok::less:
2231 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002232
Steve Naroff44ac7772008-12-25 14:16:32 +00002233 case tok::kw___cdecl:
2234 case tok::kw___stdcall:
2235 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002236 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002237 case tok::kw___w64:
2238 case tok::kw___ptr64:
2239 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002240 }
2241}
2242
Chris Lattneracd58a32006-08-06 17:24:14 +00002243/// isDeclarationSpecifier() - Return true if the current token is part of a
2244/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002245bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00002246 switch (Tok.getKind()) {
2247 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002248
Chris Lattner020bab92009-01-04 23:41:41 +00002249 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002250 // Unfortunate hack to support "Class.factoryMethod" notation.
2251 if (getLang().ObjC1 && NextToken().is(tok::period))
2252 return false;
John Thompson22334602010-02-05 00:12:22 +00002253 if (TryAltiVecVectorToken())
2254 return true;
2255 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002256 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002257 // Annotate typenames and C++ scope specifiers. If we get one, just
2258 // recurse to handle whatever we get.
2259 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002260 return true;
2261 if (Tok.is(tok::identifier))
2262 return false;
2263 return isDeclarationSpecifier();
2264
Chris Lattner020bab92009-01-04 23:41:41 +00002265 case tok::coloncolon: // ::foo::bar
2266 if (NextToken().is(tok::kw_new) || // ::new
2267 NextToken().is(tok::kw_delete)) // ::delete
2268 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002269
Chris Lattner020bab92009-01-04 23:41:41 +00002270 // Annotate typenames and C++ scope specifiers. If we get one, just
2271 // recurse to handle whatever we get.
2272 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002273 return true;
2274 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002275
Chris Lattneracd58a32006-08-06 17:24:14 +00002276 // storage-class-specifier
2277 case tok::kw_typedef:
2278 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002279 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002280 case tok::kw_static:
2281 case tok::kw_auto:
2282 case tok::kw_register:
2283 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002284
Chris Lattneracd58a32006-08-06 17:24:14 +00002285 // type-specifiers
2286 case tok::kw_short:
2287 case tok::kw_long:
2288 case tok::kw_signed:
2289 case tok::kw_unsigned:
2290 case tok::kw__Complex:
2291 case tok::kw__Imaginary:
2292 case tok::kw_void:
2293 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002294 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002295 case tok::kw_char16_t:
2296 case tok::kw_char32_t:
2297
Chris Lattneracd58a32006-08-06 17:24:14 +00002298 case tok::kw_int:
2299 case tok::kw_float:
2300 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002301 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002302 case tok::kw__Bool:
2303 case tok::kw__Decimal32:
2304 case tok::kw__Decimal64:
2305 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002306 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002307
Chris Lattner861a2262008-04-13 18:59:07 +00002308 // struct-or-union-specifier (C99) or class-specifier (C++)
2309 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002310 case tok::kw_struct:
2311 case tok::kw_union:
2312 // enum-specifier
2313 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002314
Chris Lattneracd58a32006-08-06 17:24:14 +00002315 // type-qualifier
2316 case tok::kw_const:
2317 case tok::kw_volatile:
2318 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002319
Chris Lattneracd58a32006-08-06 17:24:14 +00002320 // function-specifier
2321 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002322 case tok::kw_virtual:
2323 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002324
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002325 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002326 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002327
Chris Lattner599e47e2007-08-09 17:01:07 +00002328 // GNU typeof support.
2329 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002330
Chris Lattner599e47e2007-08-09 17:01:07 +00002331 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002332 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002333 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002334
Chris Lattner8b2ec162008-07-26 03:38:44 +00002335 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2336 case tok::less:
2337 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002338
Steve Narofff192fab2009-01-06 19:34:12 +00002339 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002340 case tok::kw___cdecl:
2341 case tok::kw___stdcall:
2342 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002343 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002344 case tok::kw___w64:
2345 case tok::kw___ptr64:
2346 case tok::kw___forceinline:
2347 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002348 }
2349}
2350
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002351bool Parser::isConstructorDeclarator() {
2352 TentativeParsingAction TPA(*this);
2353
2354 // Parse the C++ scope specifier.
2355 CXXScopeSpec SS;
John McCall1f476a12010-02-26 08:45:28 +00002356 if (ParseOptionalCXXScopeSpecifier(SS, 0, true)) {
2357 TPA.Revert();
2358 return false;
2359 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002360
2361 // Parse the constructor name.
2362 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2363 // We already know that we have a constructor name; just consume
2364 // the token.
2365 ConsumeToken();
2366 } else {
2367 TPA.Revert();
2368 return false;
2369 }
2370
2371 // Current class name must be followed by a left parentheses.
2372 if (Tok.isNot(tok::l_paren)) {
2373 TPA.Revert();
2374 return false;
2375 }
2376 ConsumeParen();
2377
2378 // A right parentheses or ellipsis signals that we have a constructor.
2379 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2380 TPA.Revert();
2381 return true;
2382 }
2383
2384 // If we need to, enter the specified scope.
2385 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002386 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002387 DeclScopeObj.EnterDeclaratorScope();
2388
2389 // Check whether the next token(s) are part of a declaration
2390 // specifier, in which case we have the start of a parameter and,
2391 // therefore, we know that this is a constructor.
2392 bool IsConstructor = isDeclarationSpecifier();
2393 TPA.Revert();
2394 return IsConstructor;
2395}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002396
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002397/// ParseTypeQualifierListOpt
2398/// type-qualifier-list: [C99 6.7.5]
2399/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002400/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002401/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002402/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Alexis Hunt96d5c762009-11-21 08:43:09 +00002403/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2404/// if CXX0XAttributesAllowed = true
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002405///
Alexis Hunt96d5c762009-11-21 08:43:09 +00002406void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2407 bool CXX0XAttributesAllowed) {
2408 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2409 SourceLocation Loc = Tok.getLocation();
2410 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2411 if (CXX0XAttributesAllowed)
2412 DS.AddAttributes(Attr.AttrList);
2413 else
2414 Diag(Loc, diag::err_attributes_not_allowed);
2415 }
2416
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002417 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002418 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002419 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002420 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002421 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002422
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002423 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002424 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002425 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2426 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002427 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002428 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002429 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2430 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002431 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002432 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002433 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2434 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002435 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002436 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002437 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002438 case tok::kw___cdecl:
2439 case tok::kw___stdcall:
2440 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002441 case tok::kw___thiscall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002442 if (GNUAttributesAllowed) {
Eli Friedman53339e02009-06-08 23:27:34 +00002443 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2444 continue;
2445 }
2446 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002447 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002448 if (GNUAttributesAllowed) {
2449 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002450 continue; // do *not* consume the next token!
2451 }
2452 // otherwise, FALL THROUGH!
2453 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002454 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002455 // If this is not a type-qualifier token, we're done reading type
2456 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002457 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002458 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002459 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002460
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002461 // If the specifier combination wasn't legal, issue a diagnostic.
2462 if (isInvalid) {
2463 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002464 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002465 }
2466 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002467 }
2468}
2469
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002470
2471/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2472///
2473void Parser::ParseDeclarator(Declarator &D) {
2474 /// This implements the 'declarator' production in the C grammar, then checks
2475 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002476 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002477}
2478
Sebastian Redlbd150f42008-11-21 19:14:01 +00002479/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2480/// is parsed by the function passed to it. Pass null, and the direct-declarator
2481/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002482/// ptr-operator production.
2483///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002484/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2485/// [C] pointer[opt] direct-declarator
2486/// [C++] direct-declarator
2487/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002488///
2489/// pointer: [C99 6.7.5]
2490/// '*' type-qualifier-list[opt]
2491/// '*' type-qualifier-list[opt] pointer
2492///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002493/// ptr-operator:
2494/// '*' cv-qualifier-seq[opt]
2495/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002496/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002497/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002498/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002499/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002500void Parser::ParseDeclaratorInternal(Declarator &D,
2501 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002502 if (Diags.hasAllExtensionsSilenced())
2503 D.setExtension();
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002504 // C++ member pointers start with a '::' or a nested-name.
2505 // Member pointers get special handling, since there's no place for the
2506 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002507 if (getLang().CPlusPlus &&
2508 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2509 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002510 CXXScopeSpec SS;
John McCall1f476a12010-02-26 08:45:28 +00002511 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true); // ignore fail
2512
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002513 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002514 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002515 // The scope spec really belongs to the direct-declarator.
2516 D.getCXXScopeSpec() = SS;
2517 if (DirectDeclParser)
2518 (this->*DirectDeclParser)(D);
2519 return;
2520 }
2521
2522 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002523 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002524 DeclSpec DS;
2525 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002526 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002527
2528 // Recurse to parse whatever is left.
2529 ParseDeclaratorInternal(D, DirectDeclParser);
2530
2531 // Sema will have to catch (syntactically invalid) pointers into global
2532 // scope. It has to catch pointers into namespace scope anyway.
2533 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002534 Loc, DS.TakeAttributes()),
2535 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002536 return;
2537 }
2538 }
2539
2540 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002541 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002542 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002543 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002544 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002545 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002546 if (DirectDeclParser)
2547 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002548 return;
2549 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002550
Sebastian Redled0f3b02009-03-15 22:02:01 +00002551 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2552 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002553 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002554 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002555
Chris Lattner9eac9312009-03-27 04:18:06 +00002556 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002557 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002558 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002559
Bill Wendling3708c182007-05-27 10:15:43 +00002560 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002561 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002562
Bill Wendling3708c182007-05-27 10:15:43 +00002563 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002564 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002565 if (Kind == tok::star)
2566 // Remember that we parsed a pointer type, and remember the type-quals.
2567 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002568 DS.TakeAttributes()),
2569 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002570 else
2571 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002572 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002573 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002574 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002575 } else {
2576 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002577 DeclSpec DS;
2578
Sebastian Redl3b27be62009-03-23 00:00:23 +00002579 // Complain about rvalue references in C++03, but then go on and build
2580 // the declarator.
2581 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2582 Diag(Loc, diag::err_rvalue_reference);
2583
Bill Wendling93efb222007-06-02 23:28:54 +00002584 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2585 // cv-qualifiers are introduced through the use of a typedef or of a
2586 // template type argument, in which case the cv-qualifiers are ignored.
2587 //
2588 // [GNU] Retricted references are allowed.
2589 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002590 // [C++0x] Attributes on references are not allowed.
2591 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002592 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002593
2594 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2595 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2596 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002597 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002598 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2599 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002600 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002601 }
Bill Wendling3708c182007-05-27 10:15:43 +00002602
2603 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002604 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002605
Douglas Gregor66583c52008-11-03 15:51:28 +00002606 if (D.getNumTypeObjects() > 0) {
2607 // C++ [dcl.ref]p4: There shall be no references to references.
2608 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2609 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002610 if (const IdentifierInfo *II = D.getIdentifier())
2611 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2612 << II;
2613 else
2614 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2615 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002616
Sebastian Redlbd150f42008-11-21 19:14:01 +00002617 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002618 // can go ahead and build the (technically ill-formed)
2619 // declarator: reference collapsing will take care of it.
2620 }
2621 }
2622
Bill Wendling3708c182007-05-27 10:15:43 +00002623 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002624 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002625 DS.TakeAttributes(),
2626 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002627 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002628 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002629}
2630
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002631/// ParseDirectDeclarator
2632/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002633/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002634/// '(' declarator ')'
2635/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002636/// [C90] direct-declarator '[' constant-expression[opt] ']'
2637/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2638/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2639/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2640/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002641/// direct-declarator '(' parameter-type-list ')'
2642/// direct-declarator '(' identifier-list[opt] ')'
2643/// [GNU] direct-declarator '(' parameter-forward-declarations
2644/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002645/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2646/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002647/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002648///
2649/// declarator-id: [C++ 8]
2650/// id-expression
2651/// '::'[opt] nested-name-specifier[opt] type-name
2652///
2653/// id-expression: [C++ 5.1]
2654/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002655/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002656///
2657/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002658/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002659/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002660/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002661/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002662/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002663///
Chris Lattneracd58a32006-08-06 17:24:14 +00002664void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002665 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002666
Douglas Gregor7861a802009-11-03 01:35:08 +00002667 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2668 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002669 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002670 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2671 true);
John McCall1f476a12010-02-26 08:45:28 +00002672 }
2673
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002674 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002675 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00002676 // Change the declaration context for name lookup, until this function
2677 // is exited (and the declarator has been parsed).
2678 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002679 }
2680
Douglas Gregor7861a802009-11-03 01:35:08 +00002681 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2682 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2683 // We found something that indicates the start of an unqualified-id.
2684 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002685 bool AllowConstructorName;
2686 if (D.getDeclSpec().hasTypeSpecifier())
2687 AllowConstructorName = false;
2688 else if (D.getCXXScopeSpec().isSet())
2689 AllowConstructorName =
2690 (D.getContext() == Declarator::FileContext ||
2691 (D.getContext() == Declarator::MemberContext &&
2692 D.getDeclSpec().isFriendSpecified()));
2693 else
2694 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2695
Douglas Gregor7861a802009-11-03 01:35:08 +00002696 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2697 /*EnteringContext=*/true,
2698 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002699 AllowConstructorName,
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002700 /*ObjectType=*/0,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002701 D.getName()) ||
2702 // Once we're past the identifier, if the scope was bad, mark the
2703 // whole declarator bad.
2704 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002705 D.SetIdentifier(0, Tok.getLocation());
2706 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002707 } else {
2708 // Parsed the unqualified-id; update range information and move along.
2709 if (D.getSourceRange().getBegin().isInvalid())
2710 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2711 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002712 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002713 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002714 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002715 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002716 assert(!getLang().CPlusPlus &&
2717 "There's a C++-specific check for tok::identifier above");
2718 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2719 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2720 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002721 goto PastIdentifier;
2722 }
2723
2724 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002725 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002726 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002727 // Example: 'char (*X)' or 'int (*XX)(void)'
2728 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002729
2730 // If the declarator was parenthesized, we entered the declarator
2731 // scope when parsing the parenthesized declarator, then exited
2732 // the scope already. Re-enter the scope, if we need to.
2733 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002734 // If there was an error parsing parenthesized declarator, declarator
2735 // scope may have been enterred before. Don't do it again.
2736 if (!D.isInvalidType() &&
2737 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002738 // Change the declaration context for name lookup, until this function
2739 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002740 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002741 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002742 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002743 // This could be something simple like "int" (in which case the declarator
2744 // portion is empty), if an abstract-declarator is allowed.
2745 D.SetIdentifier(0, Tok.getLocation());
2746 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002747 if (D.getContext() == Declarator::MemberContext)
2748 Diag(Tok, diag::err_expected_member_name_or_semi)
2749 << D.getDeclSpec().getSourceRange();
2750 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002751 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002752 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002753 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002754 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002755 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002756 }
Mike Stump11289f42009-09-09 15:08:12 +00002757
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002758 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002759 assert(D.isPastIdentifier() &&
2760 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002761
Alexis Hunt96d5c762009-11-21 08:43:09 +00002762 // Don't parse attributes unless we have an identifier.
Douglas Gregor0286b462010-02-19 16:47:56 +00002763 if (D.getIdentifier() && getLang().CPlusPlus0x
Alexis Hunt96d5c762009-11-21 08:43:09 +00002764 && isCXX0XAttributeSpecifier(true)) {
2765 SourceLocation AttrEndLoc;
2766 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2767 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2768 }
2769
Chris Lattneracd58a32006-08-06 17:24:14 +00002770 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002771 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002772 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2773 // In such a case, check if we actually have a function declarator; if it
2774 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002775 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2776 // When not in file scope, warn for ambiguous function declarators, just
2777 // in case the author intended it as a variable definition.
2778 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2779 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2780 break;
2781 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002782 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002783 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002784 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002785 } else {
2786 break;
2787 }
2788 }
2789}
2790
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002791/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2792/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002793/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002794/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2795///
2796/// direct-declarator:
2797/// '(' declarator ')'
2798/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002799/// direct-declarator '(' parameter-type-list ')'
2800/// direct-declarator '(' identifier-list[opt] ')'
2801/// [GNU] direct-declarator '(' parameter-forward-declarations
2802/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002803///
2804void Parser::ParseParenDeclarator(Declarator &D) {
2805 SourceLocation StartLoc = ConsumeParen();
2806 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002807
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002808 // Eat any attributes before we look at whether this is a grouping or function
2809 // declarator paren. If this is a grouping paren, the attribute applies to
2810 // the type being built up, for example:
2811 // int (__attribute__(()) *x)(long y)
2812 // If this ends up not being a grouping paren, the attribute applies to the
2813 // first argument, for example:
2814 // int (__attribute__(()) int x)
2815 // In either case, we need to eat any attributes to be able to determine what
2816 // sort of paren this is.
2817 //
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002818 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002819 bool RequiresArg = false;
2820 if (Tok.is(tok::kw___attribute)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002821 AttrList.reset(ParseGNUAttributes());
Mike Stump11289f42009-09-09 15:08:12 +00002822
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002823 // We require that the argument list (if this is a non-grouping paren) be
2824 // present even if the attribute list was empty.
2825 RequiresArg = true;
2826 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002827 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002828 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00002829 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2830 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002831 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman53339e02009-06-08 23:27:34 +00002832 }
Mike Stump11289f42009-09-09 15:08:12 +00002833
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002834 // If we haven't past the identifier yet (or where the identifier would be
2835 // stored, if this is an abstract declarator), then this is probably just
2836 // grouping parens. However, if this could be an abstract-declarator, then
2837 // this could also be the start of function arguments (consider 'void()').
2838 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002839
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002840 if (!D.mayOmitIdentifier()) {
2841 // If this can't be an abstract-declarator, this *must* be a grouping
2842 // paren, because we haven't seen the identifier yet.
2843 isGrouping = true;
2844 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002845 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002846 isDeclarationSpecifier()) { // 'int(int)' is a function.
2847 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2848 // considered to be a type, not a K&R identifier-list.
2849 isGrouping = false;
2850 } else {
2851 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2852 isGrouping = true;
2853 }
Mike Stump11289f42009-09-09 15:08:12 +00002854
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002855 // If this is a grouping paren, handle:
2856 // direct-declarator: '(' declarator ')'
2857 // direct-declarator: '(' attributes declarator ')'
2858 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002859 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002860 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002861 if (AttrList)
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002862 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002863
Sebastian Redlbd150f42008-11-21 19:14:01 +00002864 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002865 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002866 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002867
2868 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002869 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002870 return;
2871 }
Mike Stump11289f42009-09-09 15:08:12 +00002872
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002873 // Okay, if this wasn't a grouping paren, it must be the start of a function
2874 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002875 // identifier (and remember where it would have been), then call into
2876 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002877 D.SetIdentifier(0, Tok.getLocation());
2878
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002879 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002880}
2881
2882/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2883/// declarator D up to a paren, which indicates that we are parsing function
2884/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002885///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002886/// If AttrList is non-null, then the caller parsed those arguments immediately
2887/// after the open paren - they should be considered to be the first argument of
2888/// a parameter. If RequiresArg is true, then the first argument of the
2889/// function is required to be present and required to not be an identifier
2890/// list.
2891///
Chris Lattneracd58a32006-08-06 17:24:14 +00002892/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002893/// parameter-type-list: [C99 6.7.5]
2894/// parameter-list
2895/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002896/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002897///
2898/// parameter-list: [C99 6.7.5]
2899/// parameter-declaration
2900/// parameter-list ',' parameter-declaration
2901///
2902/// parameter-declaration: [C99 6.7.5]
2903/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002904/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002905/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002906/// declaration-specifiers abstract-declarator[opt]
2907/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002908/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002909/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002910///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002911/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002912/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002913///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002914void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2915 AttributeList *AttrList,
2916 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002917 // lparen is already consumed!
2918 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00002919
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002920 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002921 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002922 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002923 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002924 delete AttrList;
2925 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002926
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002927 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2928 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002929
2930 // cv-qualifier-seq[opt].
2931 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002932 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002933 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002934 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002935 llvm::SmallVector<TypeTy*, 2> Exceptions;
2936 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002937 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002938 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002939 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002940 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002941
2942 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002943 if (Tok.is(tok::kw_throw)) {
2944 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002945 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002946 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002947 hasAnyExceptionSpec);
2948 assert(Exceptions.size() == ExceptionRanges.size() &&
2949 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002950 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002951 }
2952
Chris Lattner371ed4e2008-04-06 06:57:35 +00002953 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002954 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002955 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002956 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002957 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002958 /*arglist*/ 0, 0,
2959 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002960 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002961 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002962 Exceptions.data(),
2963 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002964 Exceptions.size(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002965 LParenLoc, RParenLoc, D),
2966 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002967 return;
Sebastian Redld6434562009-05-29 18:02:33 +00002968 }
2969
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002970 // Alternatively, this parameter list may be an identifier list form for a
2971 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00002972 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
2973 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00002974 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002975 // K&R identifier lists can't have typedefs as identifiers, per
2976 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002977 if (RequiresArg) {
2978 Diag(Tok, diag::err_argument_required_after_attribute);
2979 delete AttrList;
2980 }
Chris Lattner9453ab82010-05-14 17:23:36 +00002981
Steve Naroffb0486722009-01-28 19:16:40 +00002982 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00002983 // normal declarators, not for abstract-declarators. Get the first
2984 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00002985 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00002986 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00002987
2988 // Identifier lists follow a really simple grammar: the identifiers can
2989 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
2990 // identifier lists are really rare in the brave new modern world, and it
2991 // is very common for someone to typo a type in a non-k&r style list. If
2992 // we are presented with something like: "void foo(intptr x, float y)",
2993 // we don't want to start parsing the function declarator as though it is
2994 // a K&R style declarator just because intptr is an invalid type.
2995 //
2996 // To handle this, we check to see if the token after the first identifier
2997 // is a "," or ")". Only if so, do we parse it as an identifier list.
2998 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
2999 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3000 FirstTok.getIdentifierInfo(),
3001 FirstTok.getLocation(), D);
3002
3003 // If we get here, the code is invalid. Push the first identifier back
3004 // into the token stream and parse the first argument as an (invalid)
3005 // normal argument declarator.
3006 PP.EnterToken(Tok);
3007 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003008 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003009 }
Mike Stump11289f42009-09-09 15:08:12 +00003010
Chris Lattner371ed4e2008-04-06 06:57:35 +00003011 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003012
Chris Lattner371ed4e2008-04-06 06:57:35 +00003013 // Build up an array of information about the parsed arguments.
3014 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003015
3016 // Enter function-declaration scope, limiting any declarators to the
3017 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003018 ParseScope PrototypeScope(this,
3019 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003020
Chris Lattner371ed4e2008-04-06 06:57:35 +00003021 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003022 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003023 while (1) {
3024 if (Tok.is(tok::ellipsis)) {
3025 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003026 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003027 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003028 }
Mike Stump11289f42009-09-09 15:08:12 +00003029
Chris Lattner371ed4e2008-04-06 06:57:35 +00003030 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00003031
Chris Lattner371ed4e2008-04-06 06:57:35 +00003032 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003033 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003034 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003035
3036 // If the caller parsed attributes for the first argument, add them now.
3037 if (AttrList) {
3038 DS.AddAttributes(AttrList);
3039 AttrList = 0; // Only apply the attributes to the first parameter.
3040 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003041 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003042
Chris Lattner371ed4e2008-04-06 06:57:35 +00003043 // Parse the declarator. This is "PrototypeContext", because we must
3044 // accept either 'declarator' or 'abstract-declarator' here.
3045 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3046 ParseDeclarator(ParmDecl);
3047
3048 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003049 if (Tok.is(tok::kw___attribute)) {
3050 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003051 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003052 ParmDecl.AddAttributes(AttrList, Loc);
3053 }
Mike Stump11289f42009-09-09 15:08:12 +00003054
Chris Lattner371ed4e2008-04-06 06:57:35 +00003055 // Remember this parsed parameter in ParamInfo.
3056 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003057
Douglas Gregor4d87df52008-12-16 21:30:33 +00003058 // DefArgToks is used when the parsing of default arguments needs
3059 // to be delayed.
3060 CachedTokens *DefArgToks = 0;
3061
Chris Lattner371ed4e2008-04-06 06:57:35 +00003062 // If no parameter was specified, verify that *something* was specified,
3063 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003064 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3065 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003066 // Completely missing, emit error.
3067 Diag(DSStart, diag::err_missing_param);
3068 } else {
3069 // Otherwise, we have something. Add it and let semantic analysis try
3070 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003071
Chris Lattner371ed4e2008-04-06 06:57:35 +00003072 // Inform the actions module about the parameter declarator, so it gets
3073 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003074 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003075
3076 // Parse the default argument, if any. We parse the default
3077 // arguments in all dialects; the semantic analysis in
3078 // ActOnParamDefaultArgument will reject the default argument in
3079 // C.
3080 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003081 SourceLocation EqualLoc = Tok.getLocation();
3082
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003083 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003084 if (D.getContext() == Declarator::MemberContext) {
3085 // If we're inside a class definition, cache the tokens
3086 // corresponding to the default argument. We'll actually parse
3087 // them when we see the end of the class definition.
3088 // FIXME: Templates will require something similar.
3089 // FIXME: Can we use a smart pointer for Toks?
3090 DefArgToks = new CachedTokens;
3091
Mike Stump11289f42009-09-09 15:08:12 +00003092 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003093 /*StopAtSemi=*/true,
3094 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003095 delete DefArgToks;
3096 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003097 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003098 } else {
3099 // Mark the end of the default argument so that we know when to
3100 // stop when we parse it later on.
3101 Token DefArgEnd;
3102 DefArgEnd.startToken();
3103 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3104 DefArgEnd.setLocation(Tok.getLocation());
3105 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003106 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003107 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003108 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003109 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003110 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003111 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003112
Douglas Gregor4d87df52008-12-16 21:30:33 +00003113 OwningExprResult DefArgResult(ParseAssignmentExpression());
3114 if (DefArgResult.isInvalid()) {
3115 Actions.ActOnParamDefaultArgumentError(Param);
3116 SkipUntil(tok::comma, tok::r_paren, true, true);
3117 } else {
3118 // Inform the actions module about the default argument
3119 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00003120 move(DefArgResult));
Douglas Gregor4d87df52008-12-16 21:30:33 +00003121 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003122 }
3123 }
Mike Stump11289f42009-09-09 15:08:12 +00003124
3125 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3126 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003127 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003128 }
3129
3130 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003131 if (Tok.isNot(tok::comma)) {
3132 if (Tok.is(tok::ellipsis)) {
3133 IsVariadic = true;
3134 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3135
3136 if (!getLang().CPlusPlus) {
3137 // We have ellipsis without a preceding ',', which is ill-formed
3138 // in C. Complain and provide the fix.
3139 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003140 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003141 }
3142 }
3143
3144 break;
3145 }
Mike Stump11289f42009-09-09 15:08:12 +00003146
Chris Lattner371ed4e2008-04-06 06:57:35 +00003147 // Consume the comma.
3148 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003149 }
Mike Stump11289f42009-09-09 15:08:12 +00003150
Chris Lattner371ed4e2008-04-06 06:57:35 +00003151 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00003152 PrototypeScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00003153
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003154 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003155 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3156 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003157
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003158 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003159 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003160 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003161 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00003162 llvm::SmallVector<TypeTy*, 2> Exceptions;
3163 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003164
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003165 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003166 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003167 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003168 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003169 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003170
3171 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003172 if (Tok.is(tok::kw_throw)) {
3173 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003174 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003175 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003176 hasAnyExceptionSpec);
3177 assert(Exceptions.size() == ExceptionRanges.size() &&
3178 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003179 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003180 }
3181
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003182 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003183 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003184 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003185 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003186 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003187 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003188 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003189 Exceptions.data(),
3190 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003191 Exceptions.size(),
3192 LParenLoc, RParenLoc, D),
3193 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003194}
Chris Lattneracd58a32006-08-06 17:24:14 +00003195
Chris Lattner6c940e62008-04-06 06:34:08 +00003196/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3197/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003198/// first identifier has already been consumed, and the current token is the
3199/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003200///
3201/// identifier-list: [C99 6.7.5]
3202/// identifier
3203/// identifier-list ',' identifier
3204///
3205void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003206 IdentifierInfo *FirstIdent,
3207 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003208 Declarator &D) {
3209 // Build up an array of information about the parsed arguments.
3210 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3211 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003212
Chris Lattner6c940e62008-04-06 06:34:08 +00003213 // If there was no identifier specified for the declarator, either we are in
3214 // an abstract-declarator, or we are in a parameter declarator which was found
3215 // to be abstract. In abstract-declarators, identifier lists are not valid:
3216 // diagnose this.
3217 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003218 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003219
Chris Lattner9453ab82010-05-14 17:23:36 +00003220 // The first identifier was already read, and is known to be the first
3221 // identifier in the list. Remember this identifier in ParamInfo.
3222 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003223 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003224
Chris Lattner6c940e62008-04-06 06:34:08 +00003225 while (Tok.is(tok::comma)) {
3226 // Eat the comma.
3227 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003228
Chris Lattner9186f552008-04-06 06:39:19 +00003229 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003230 if (Tok.isNot(tok::identifier)) {
3231 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003232 SkipUntil(tok::r_paren);
3233 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003234 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003235
Chris Lattner6c940e62008-04-06 06:34:08 +00003236 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003237
3238 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003239 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003240 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003241
Chris Lattner6c940e62008-04-06 06:34:08 +00003242 // Verify that the argument identifier has not already been mentioned.
3243 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003244 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003245 } else {
3246 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003247 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003248 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003249 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003250 }
Mike Stump11289f42009-09-09 15:08:12 +00003251
Chris Lattner6c940e62008-04-06 06:34:08 +00003252 // Eat the identifier.
3253 ConsumeToken();
3254 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003255
3256 // If we have the closing ')', eat it and we're done.
3257 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3258
Chris Lattner9186f552008-04-06 06:39:19 +00003259 // Remember that we parsed a function type, and remember the attributes. This
3260 // function type is always a K&R style function type, which is not varargs and
3261 // has no prototype.
3262 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003263 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003264 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003265 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003266 /*exception*/false,
3267 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003268 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003269 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003270}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003271
Chris Lattnere8074e62006-08-06 18:30:15 +00003272/// [C90] direct-declarator '[' constant-expression[opt] ']'
3273/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3274/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3275/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3276/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3277void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003278 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003279
Chris Lattner84a11622008-12-18 07:27:21 +00003280 // C array syntax has many features, but by-far the most common is [] and [4].
3281 // This code does a fast path to handle some of the most obvious cases.
3282 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003283 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003284 //FIXME: Use these
3285 CXX0XAttributeList Attr;
3286 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3287 Attr = ParseCXX0XAttributes();
3288 }
3289
Chris Lattner84a11622008-12-18 07:27:21 +00003290 // Remember that we parsed the empty array type.
John McCall37ad5512010-08-23 06:44:23 +00003291 OwningExprResult NumElements;
Douglas Gregor04318252009-07-06 15:59:29 +00003292 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3293 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003294 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003295 return;
3296 } else if (Tok.getKind() == tok::numeric_constant &&
3297 GetLookAheadToken(1).is(tok::r_square)) {
3298 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlffbcf962009-01-18 18:53:16 +00003299 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003300 ConsumeToken();
3301
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003302 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003303 //FIXME: Use these
3304 CXX0XAttributeList Attr;
3305 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3306 Attr = ParseCXX0XAttributes();
3307 }
Chris Lattner84a11622008-12-18 07:27:21 +00003308
3309 // If there was an error parsing the assignment-expression, recover.
3310 if (ExprRes.isInvalid())
3311 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00003312
Chris Lattner84a11622008-12-18 07:27:21 +00003313 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00003314 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3315 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003316 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003317 return;
3318 }
Mike Stump11289f42009-09-09 15:08:12 +00003319
Chris Lattnere8074e62006-08-06 18:30:15 +00003320 // If valid, this location is the position where we read the 'static' keyword.
3321 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003322 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003323 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003324
Chris Lattnere8074e62006-08-06 18:30:15 +00003325 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003326 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003327 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003328 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003329
Chris Lattnere8074e62006-08-06 18:30:15 +00003330 // If we haven't already read 'static', check to see if there is one after the
3331 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003332 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003333 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003334
Chris Lattnere8074e62006-08-06 18:30:15 +00003335 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003336 bool isStar = false;
John McCall37ad5512010-08-23 06:44:23 +00003337 OwningExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00003338
Chris Lattner521ff2b2008-04-06 05:26:30 +00003339 // Handle the case where we have '[*]' as the array size. However, a leading
3340 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3341 // the the token after the star is a ']'. Since stars in arrays are
3342 // infrequent, use of lookahead is not costly here.
3343 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003344 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003345
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003346 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003347 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003348 StaticLoc = SourceLocation(); // Drop the static.
3349 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003350 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003351 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003352 // Note, in C89, this production uses the constant-expr production instead
3353 // of assignment-expr. The only difference is that assignment-expr allows
3354 // things like '=' and '*='. Sema rejects these in C89 mode because they
3355 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003356
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003357 // Parse the constant-expression or assignment-expression now (depending
3358 // on dialect).
3359 if (getLang().CPlusPlus)
3360 NumElements = ParseConstantExpression();
3361 else
3362 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003363 }
Mike Stump11289f42009-09-09 15:08:12 +00003364
Chris Lattner62591722006-08-12 18:40:58 +00003365 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003366 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003367 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003368 // If the expression was invalid, skip it.
3369 SkipUntil(tok::r_square);
3370 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003371 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003372
3373 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3374
Alexis Hunt96d5c762009-11-21 08:43:09 +00003375 //FIXME: Use these
3376 CXX0XAttributeList Attr;
3377 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3378 Attr = ParseCXX0XAttributes();
3379 }
3380
Chris Lattner84a11622008-12-18 07:27:21 +00003381 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00003382 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3383 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003384 NumElements.release(),
3385 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003386 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003387}
3388
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003389/// [GNU] typeof-specifier:
3390/// typeof ( expressions )
3391/// typeof ( type-name )
3392/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003393///
3394void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003395 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003396 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003397 SourceLocation StartLoc = ConsumeToken();
3398
John McCalle8595032010-01-13 20:03:27 +00003399 const bool hasParens = Tok.is(tok::l_paren);
3400
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003401 bool isCastExpr;
3402 TypeTy *CastTy;
3403 SourceRange CastRange;
3404 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3405 isCastExpr,
3406 CastTy,
3407 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003408 if (hasParens)
3409 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003410
3411 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003412 // FIXME: Not accurate, the range gets one token more than it should.
3413 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003414 else
3415 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003416
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003417 if (isCastExpr) {
3418 if (!CastTy) {
3419 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003420 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003421 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003422
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003423 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003424 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003425 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3426 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003427 DiagID, CastTy))
3428 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003429 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003430 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003431
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003432 // If we get here, the operand to the typeof was an expresion.
3433 if (Operand.isInvalid()) {
3434 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003435 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003436 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003437
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003438 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003439 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003440 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3441 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003442 DiagID, Operand.release()))
3443 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003444}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003445
3446
3447/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3448/// from TryAltiVecVectorToken.
3449bool Parser::TryAltiVecVectorTokenOutOfLine() {
3450 Token Next = NextToken();
3451 switch (Next.getKind()) {
3452 default: return false;
3453 case tok::kw_short:
3454 case tok::kw_long:
3455 case tok::kw_signed:
3456 case tok::kw_unsigned:
3457 case tok::kw_void:
3458 case tok::kw_char:
3459 case tok::kw_int:
3460 case tok::kw_float:
3461 case tok::kw_double:
3462 case tok::kw_bool:
3463 case tok::kw___pixel:
3464 Tok.setKind(tok::kw___vector);
3465 return true;
3466 case tok::identifier:
3467 if (Next.getIdentifierInfo() == Ident_pixel) {
3468 Tok.setKind(tok::kw___vector);
3469 return true;
3470 }
3471 return false;
3472 }
3473}
3474
3475bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3476 const char *&PrevSpec, unsigned &DiagID,
3477 bool &isInvalid) {
3478 if (Tok.getIdentifierInfo() == Ident_vector) {
3479 Token Next = NextToken();
3480 switch (Next.getKind()) {
3481 case tok::kw_short:
3482 case tok::kw_long:
3483 case tok::kw_signed:
3484 case tok::kw_unsigned:
3485 case tok::kw_void:
3486 case tok::kw_char:
3487 case tok::kw_int:
3488 case tok::kw_float:
3489 case tok::kw_double:
3490 case tok::kw_bool:
3491 case tok::kw___pixel:
3492 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3493 return true;
3494 case tok::identifier:
3495 if (Next.getIdentifierInfo() == Ident_pixel) {
3496 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3497 return true;
3498 }
3499 break;
3500 default:
3501 break;
3502 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00003503 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003504 DS.isTypeAltiVecVector()) {
3505 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3506 return true;
3507 }
3508 return false;
3509}
3510