blob: 814f2031581b6a94ba8a050bf5327e0e230eff1f [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
John McCallb268a282010-08-23 23:25:46 +0000596 Actions.AddInitializerToDecl(ThisDecl, Init.take());
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,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000856 DeclSpecContext DSContext) {
Chris Lattner2e232092008-03-13 06:29:04 +0000857 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000858 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000859 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000860 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000861 unsigned DiagID = 0;
862
Chris Lattner4d8f8732006-11-28 05:05:08 +0000863 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000864
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000865 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000866 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000867 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000868 // If this is not a declaration specifier token, we're done reading decl
869 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000870 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000871 return;
Mike Stump11289f42009-09-09 15:08:12 +0000872
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000873 case tok::code_completion: {
874 Action::ParserCompletionContext CCC = Action::PCC_Namespace;
875 if (DS.hasTypeSpecifier()) {
876 bool AllowNonIdentifiers
877 = (getCurScope()->getFlags() & (Scope::ControlScope |
878 Scope::BlockScope |
879 Scope::TemplateParamScope |
880 Scope::FunctionPrototypeScope |
881 Scope::AtCatchScope)) == 0;
882 bool AllowNestedNameSpecifiers
883 = DSContext == DSC_top_level ||
884 (DSContext == DSC_class && DS.isFriendSpecified());
885
886 Actions.CodeCompleteDeclarator(getCurScope(), AllowNonIdentifiers,
887 AllowNestedNameSpecifiers);
888 ConsumeCodeCompletionToken();
889 return;
890 }
891
892 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
893 CCC = DSContext == DSC_class? Action::PCC_MemberTemplate
894 : Action::PCC_Template;
895 else if (DSContext == DSC_class)
896 CCC = Action::PCC_Class;
897 else if (ObjCImpDecl)
898 CCC = Action::PCC_ObjCImplementation;
899
900 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
901 ConsumeCodeCompletionToken();
902 return;
903 }
904
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000905 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +0000906 // C++ scope specifier. Annotate and loop, or bail out on error.
907 if (TryAnnotateCXXScopeToken(true)) {
908 if (!DS.hasTypeSpecifier())
909 DS.SetTypeSpecError();
910 goto DoneWithDeclSpec;
911 }
John McCall8bc2a702010-03-01 18:20:46 +0000912 if (Tok.is(tok::coloncolon)) // ::new or ::delete
913 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +0000914 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000915
916 case tok::annot_cxxscope: {
917 if (DS.hasTypeSpecifier())
918 goto DoneWithDeclSpec;
919
John McCall9dab4e62009-12-12 11:40:51 +0000920 CXXScopeSpec SS;
John McCall37ad5512010-08-23 06:44:23 +0000921 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCall9dab4e62009-12-12 11:40:51 +0000922 SS.setRange(Tok.getAnnotationRange());
923
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000924 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000925 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000926 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000927 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000928 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000929 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000930
931 // C++ [class.qual]p2:
932 // In a lookup in which the constructor is an acceptable lookup
933 // result and the nested-name-specifier nominates a class C:
934 //
935 // - if the name specified after the
936 // nested-name-specifier, when looked up in C, is the
937 // injected-class-name of C (Clause 9), or
938 //
939 // - if the name specified after the nested-name-specifier
940 // is the same as the identifier or the
941 // simple-template-id's template-name in the last
942 // component of the nested-name-specifier,
943 //
944 // the name is instead considered to name the constructor of
945 // class C.
946 //
947 // Thus, if the template-name is actually the constructor
948 // name, then the code is ill-formed; this interpretation is
949 // reinforced by the NAD status of core issue 635.
950 TemplateIdAnnotation *TemplateId
951 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +0000952 if ((DSContext == DSC_top_level ||
953 (DSContext == DSC_class && DS.isFriendSpecified())) &&
954 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000955 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000956 if (isConstructorDeclarator()) {
957 // The user meant this to be an out-of-line constructor
958 // definition, but template arguments are not allowed
959 // there. Just allow this as a constructor; we'll
960 // complain about it later.
961 goto DoneWithDeclSpec;
962 }
963
964 // The user meant this to name a type, but it actually names
965 // a constructor with some extraneous template
966 // arguments. Complain, then parse it as a type as the user
967 // intended.
968 Diag(TemplateId->TemplateNameLoc,
969 diag::err_out_of_line_template_id_names_constructor)
970 << TemplateId->Name;
971 }
972
John McCall9dab4e62009-12-12 11:40:51 +0000973 DS.getTypeSpecScope() = SS;
974 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +0000975 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000976 "ParseOptionalCXXScopeSpecifier not working");
977 AnnotateTemplateIdTokenAsType(&SS);
978 continue;
979 }
980
Douglas Gregorc5790df2009-09-28 07:26:33 +0000981 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +0000982 DS.getTypeSpecScope() = SS;
983 ConsumeToken(); // The C++ scope.
Douglas Gregorc5790df2009-09-28 07:26:33 +0000984 if (Tok.getAnnotationValue())
985 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
986 PrevSpec, DiagID,
987 Tok.getAnnotationValue());
988 else
989 DS.SetTypeSpecError();
990 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
991 ConsumeToken(); // The typename
992 }
993
Douglas Gregor167fa622009-03-25 15:40:00 +0000994 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000995 goto DoneWithDeclSpec;
996
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000997 // If we're in a context where the identifier could be a class name,
998 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +0000999 if ((DSContext == DSC_top_level ||
1000 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001001 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001002 &SS)) {
1003 if (isConstructorDeclarator())
1004 goto DoneWithDeclSpec;
1005
1006 // As noted in C++ [class.qual]p2 (cited above), when the name
1007 // of the class is qualified in a context where it could name
1008 // a constructor, its a constructor name. However, we've
1009 // looked at the declarator, and the user probably meant this
1010 // to be a type. Complain that it isn't supposed to be treated
1011 // as a type, then proceed to parse it as a type.
1012 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1013 << Next.getIdentifierInfo();
1014 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001015
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00001016 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
Douglas Gregor0be31a22010-07-02 17:43:08 +00001017 Next.getLocation(), getCurScope(), &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001018
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001019 // If the referenced identifier is not a type, then this declspec is
1020 // erroneous: We already checked about that it has no type specifier, and
1021 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001022 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001023 if (TypeRep == 0) {
1024 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001025 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001026 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001027 }
Mike Stump11289f42009-09-09 15:08:12 +00001028
John McCall9dab4e62009-12-12 11:40:51 +00001029 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001030 ConsumeToken(); // The C++ scope.
1031
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001032 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001033 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001034 if (isInvalid)
1035 break;
Mike Stump11289f42009-09-09 15:08:12 +00001036
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001037 DS.SetRangeEnd(Tok.getLocation());
1038 ConsumeToken(); // The typename.
1039
1040 continue;
1041 }
Mike Stump11289f42009-09-09 15:08:12 +00001042
Chris Lattnere387d9e2009-01-21 19:48:37 +00001043 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001044 if (Tok.getAnnotationValue())
1045 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001046 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001047 else
1048 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001049
1050 if (isInvalid)
1051 break;
1052
Chris Lattnere387d9e2009-01-21 19:48:37 +00001053 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1054 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001055
Chris Lattnere387d9e2009-01-21 19:48:37 +00001056 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1057 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1058 // Objective-C interface. If we don't have Objective-C or a '<', this is
1059 // just a normal reference to a typedef name.
1060 if (!Tok.is(tok::less) || !getLang().ObjC1)
1061 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001062
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001063 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001064 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001065 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1066 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1067 LAngleLoc, EndProtoLoc);
1068 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1069 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001070
Chris Lattnere387d9e2009-01-21 19:48:37 +00001071 DS.SetRangeEnd(EndProtoLoc);
1072 continue;
1073 }
Mike Stump11289f42009-09-09 15:08:12 +00001074
Chris Lattner16fac4f2008-07-26 01:18:38 +00001075 // typedef-name
1076 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001077 // In C++, check to see if this is a scope specifier like foo::bar::, if
1078 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001079 if (getLang().CPlusPlus) {
1080 if (TryAnnotateCXXScopeToken(true)) {
1081 if (!DS.hasTypeSpecifier())
1082 DS.SetTypeSpecError();
1083 goto DoneWithDeclSpec;
1084 }
1085 if (!Tok.is(tok::identifier))
1086 continue;
1087 }
Mike Stump11289f42009-09-09 15:08:12 +00001088
Chris Lattner16fac4f2008-07-26 01:18:38 +00001089 // This identifier can only be a typedef name if we haven't already seen
1090 // a type-specifier. Without this check we misparse:
1091 // typedef int X; struct Y { short X; }; as 'short int'.
1092 if (DS.hasTypeSpecifier())
1093 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001094
John Thompson22334602010-02-05 00:12:22 +00001095 // Check for need to substitute AltiVec keyword tokens.
1096 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1097 break;
1098
Chris Lattner16fac4f2008-07-26 01:18:38 +00001099 // It has to be available as a typedef too!
Mike Stump11289f42009-09-09 15:08:12 +00001100 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor0be31a22010-07-02 17:43:08 +00001101 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001102
Chris Lattner6cc055a2009-04-12 20:42:31 +00001103 // If this is not a typedef name, don't parse it as part of the declspec,
1104 // it must be an implicit int or an error.
1105 if (TypeRep == 0) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001106 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001107 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001108 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001109
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001110 // If we're in a context where the identifier could be a class name,
1111 // check whether this is a constructor declaration.
1112 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001113 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001114 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001115 goto DoneWithDeclSpec;
1116
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001117 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001118 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001119 if (isInvalid)
1120 break;
Mike Stump11289f42009-09-09 15:08:12 +00001121
Chris Lattner16fac4f2008-07-26 01:18:38 +00001122 DS.SetRangeEnd(Tok.getLocation());
1123 ConsumeToken(); // The identifier
1124
1125 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1126 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1127 // Objective-C interface. If we don't have Objective-C or a '<', this is
1128 // just a normal reference to a typedef name.
1129 if (!Tok.is(tok::less) || !getLang().ObjC1)
1130 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001131
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001132 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001133 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001134 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1135 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1136 LAngleLoc, EndProtoLoc);
1137 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1138 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001139
Chris Lattner16fac4f2008-07-26 01:18:38 +00001140 DS.SetRangeEnd(EndProtoLoc);
1141
Steve Naroffcd5e7822008-09-22 10:28:57 +00001142 // Need to support trailing type qualifiers (e.g. "id<p> const").
1143 // If a type specifier follows, it will be diagnosed elsewhere.
1144 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001145 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001146
1147 // type-name
1148 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001149 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001150 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001151 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001152 // This template-id does not refer to a type name, so we're
1153 // done with the type-specifiers.
1154 goto DoneWithDeclSpec;
1155 }
1156
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001157 // If we're in a context where the template-id could be a
1158 // constructor name or specialization, check whether this is a
1159 // constructor declaration.
1160 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001161 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001162 isConstructorDeclarator())
1163 goto DoneWithDeclSpec;
1164
Douglas Gregor7f741122009-02-25 19:37:18 +00001165 // Turn the template-id annotation token into a type annotation
1166 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001167 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001168 continue;
1169 }
1170
Chris Lattnere37e2332006-08-15 04:50:22 +00001171 // GNU attributes support.
1172 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001173 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001174 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001175
1176 // Microsoft declspec support.
1177 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +00001178 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001179 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001180
Steve Naroff44ac7772008-12-25 14:16:32 +00001181 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001182 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001183 // FIXME: Add handling here!
1184 break;
1185
1186 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001187 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001188 case tok::kw___cdecl:
1189 case tok::kw___stdcall:
1190 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001191 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001192 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1193 continue;
1194
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001195 // storage-class-specifier
1196 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001197 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1198 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001199 break;
1200 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001201 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001202 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001203 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1204 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001205 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001206 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001207 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001208 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001209 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001210 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001211 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001212 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001213 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1214 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001215 break;
1216 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001217 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001218 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1219 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001220 else
John McCall49bfce42009-08-03 20:12:06 +00001221 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1222 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001223 break;
1224 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001225 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1226 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001227 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001228 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001229 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1230 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001231 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001232 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001233 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001234 break;
Mike Stump11289f42009-09-09 15:08:12 +00001235
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001236 // function-specifier
1237 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001238 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001239 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001240 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001241 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001242 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001243 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001244 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001245 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001246
Anders Carlssoncd8db412009-05-06 04:46:28 +00001247 // friend
1248 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001249 if (DSContext == DSC_class)
1250 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1251 else {
1252 PrevSpec = ""; // not actually used by the diagnostic
1253 DiagID = diag::err_friend_invalid_in_context;
1254 isInvalid = true;
1255 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001256 break;
Mike Stump11289f42009-09-09 15:08:12 +00001257
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001258 // constexpr
1259 case tok::kw_constexpr:
1260 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1261 break;
1262
Chris Lattnere387d9e2009-01-21 19:48:37 +00001263 // type-specifier
1264 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001265 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1266 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001267 break;
1268 case tok::kw_long:
1269 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001270 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1271 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001272 else
John McCall49bfce42009-08-03 20:12:06 +00001273 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1274 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001275 break;
1276 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001277 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1278 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001279 break;
1280 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001281 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1282 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001283 break;
1284 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001285 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1286 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001287 break;
1288 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001289 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1290 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001291 break;
1292 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001293 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1294 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001295 break;
1296 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001297 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1298 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001299 break;
1300 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001301 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1302 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001303 break;
1304 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001305 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1306 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001307 break;
1308 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001309 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1310 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001311 break;
1312 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001313 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1314 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001315 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001316 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001317 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1318 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001319 break;
1320 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001321 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1322 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001323 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001324 case tok::kw_bool:
1325 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001326 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1327 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001328 break;
1329 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001330 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1331 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001332 break;
1333 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001334 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1335 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001336 break;
1337 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001338 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1339 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001340 break;
John Thompson22334602010-02-05 00:12:22 +00001341 case tok::kw___vector:
1342 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1343 break;
1344 case tok::kw___pixel:
1345 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1346 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001347
1348 // class-specifier:
1349 case tok::kw_class:
1350 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001351 case tok::kw_union: {
1352 tok::TokenKind Kind = Tok.getKind();
1353 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001354 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001355 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001356 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001357
1358 // enum-specifier:
1359 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001360 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001361 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001362 continue;
1363
1364 // cv-qualifier:
1365 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001366 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1367 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001368 break;
1369 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001370 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1371 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001372 break;
1373 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001374 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1375 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001376 break;
1377
Douglas Gregor333489b2009-03-27 23:10:48 +00001378 // C++ typename-specifier:
1379 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001380 if (TryAnnotateTypeOrScopeToken()) {
1381 DS.SetTypeSpecError();
1382 goto DoneWithDeclSpec;
1383 }
1384 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001385 continue;
1386 break;
1387
Chris Lattnere387d9e2009-01-21 19:48:37 +00001388 // GNU typeof support.
1389 case tok::kw_typeof:
1390 ParseTypeofSpecifier(DS);
1391 continue;
1392
Anders Carlsson74948d02009-06-24 17:47:40 +00001393 case tok::kw_decltype:
1394 ParseDecltypeSpecifier(DS);
1395 continue;
1396
Steve Naroffcfdf6162008-06-05 00:02:44 +00001397 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001398 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001399 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1400 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001401 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001402 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001403
Chris Lattner0974b232008-07-26 00:20:22 +00001404 {
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001405 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001406 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001407 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1408 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1409 LAngleLoc, EndProtoLoc);
1410 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1411 ProtocolLocs.data(), LAngleLoc);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001412 DS.SetRangeEnd(EndProtoLoc);
1413
Chris Lattner6d29c102008-11-18 07:48:38 +00001414 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregora771f462010-03-31 17:46:05 +00001415 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001416 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001417 // Need to support trailing type qualifiers (e.g. "id<p> const").
1418 // If a type specifier follows, it will be diagnosed elsewhere.
1419 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001420 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001421 }
John McCall49bfce42009-08-03 20:12:06 +00001422 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001423 if (isInvalid) {
1424 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001425 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001426
1427 if (DiagID == diag::ext_duplicate_declspec)
1428 Diag(Tok, DiagID)
1429 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1430 else
1431 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001432 }
Chris Lattner2e232092008-03-13 06:29:04 +00001433 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001434 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001435 }
1436}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001437
Chris Lattnera448d752009-01-06 06:59:53 +00001438/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001439/// primarily follow the C++ grammar with additions for C99 and GNU,
1440/// which together subsume the C grammar. Note that the C++
1441/// type-specifier also includes the C type-qualifier (for const,
1442/// volatile, and C99 restrict). Returns true if a type-specifier was
1443/// found (and parsed), false otherwise.
1444///
1445/// type-specifier: [C++ 7.1.5]
1446/// simple-type-specifier
1447/// class-specifier
1448/// enum-specifier
1449/// elaborated-type-specifier [TODO]
1450/// cv-qualifier
1451///
1452/// cv-qualifier: [C++ 7.1.5.1]
1453/// 'const'
1454/// 'volatile'
1455/// [C99] 'restrict'
1456///
1457/// simple-type-specifier: [ C++ 7.1.5.2]
1458/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1459/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1460/// 'char'
1461/// 'wchar_t'
1462/// 'bool'
1463/// 'short'
1464/// 'int'
1465/// 'long'
1466/// 'signed'
1467/// 'unsigned'
1468/// 'float'
1469/// 'double'
1470/// 'void'
1471/// [C99] '_Bool'
1472/// [C99] '_Complex'
1473/// [C99] '_Imaginary' // Removed in TC2?
1474/// [GNU] '_Decimal32'
1475/// [GNU] '_Decimal64'
1476/// [GNU] '_Decimal128'
1477/// [GNU] typeof-specifier
1478/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1479/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001480/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001481/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001482bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001483 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001484 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001485 const ParsedTemplateInfo &TemplateInfo,
1486 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001487 SourceLocation Loc = Tok.getLocation();
1488
1489 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001490 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001491 // If we already have a type specifier, this identifier is not a type.
1492 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1493 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1494 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1495 return false;
John Thompson22334602010-02-05 00:12:22 +00001496 // Check for need to substitute AltiVec keyword tokens.
1497 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1498 break;
1499 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001500 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001501 // Annotate typenames and C++ scope specifiers. If we get one, just
1502 // recurse to handle whatever we get.
1503 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001504 return true;
1505 if (Tok.is(tok::identifier))
1506 return false;
1507 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1508 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001509 case tok::coloncolon: // ::foo::bar
1510 if (NextToken().is(tok::kw_new) || // ::new
1511 NextToken().is(tok::kw_delete)) // ::delete
1512 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001513
Chris Lattner020bab92009-01-04 23:41:41 +00001514 // Annotate typenames and C++ scope specifiers. If we get one, just
1515 // recurse to handle whatever we get.
1516 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001517 return true;
1518 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1519 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001520
Douglas Gregor450c75a2008-11-07 15:42:26 +00001521 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001522 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001523 if (Tok.getAnnotationValue())
1524 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001525 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001526 else
1527 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001528 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1529 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001530
Douglas Gregor450c75a2008-11-07 15:42:26 +00001531 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1532 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1533 // Objective-C interface. If we don't have Objective-C or a '<', this is
1534 // just a normal reference to a typedef name.
1535 if (!Tok.is(tok::less) || !getLang().ObjC1)
1536 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001537
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001538 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001539 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001540 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1541 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1542 LAngleLoc, EndProtoLoc);
1543 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1544 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001545
Douglas Gregor450c75a2008-11-07 15:42:26 +00001546 DS.SetRangeEnd(EndProtoLoc);
1547 return true;
1548 }
1549
1550 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001551 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001552 break;
1553 case tok::kw_long:
1554 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001555 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1556 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001557 else
John McCall49bfce42009-08-03 20:12:06 +00001558 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1559 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001560 break;
1561 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001562 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001563 break;
1564 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001565 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1566 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001567 break;
1568 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001569 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1570 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001571 break;
1572 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001573 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1574 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001575 break;
1576 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001577 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001578 break;
1579 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001580 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001581 break;
1582 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001583 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001584 break;
1585 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001586 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001587 break;
1588 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001589 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001590 break;
1591 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001592 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001593 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001594 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001595 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001596 break;
1597 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001598 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001599 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001600 case tok::kw_bool:
1601 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001602 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001603 break;
1604 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001605 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1606 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001607 break;
1608 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001609 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1610 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001611 break;
1612 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001613 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1614 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001615 break;
John Thompson22334602010-02-05 00:12:22 +00001616 case tok::kw___vector:
1617 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1618 break;
1619 case tok::kw___pixel:
1620 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1621 break;
1622
Douglas Gregor450c75a2008-11-07 15:42:26 +00001623 // class-specifier:
1624 case tok::kw_class:
1625 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001626 case tok::kw_union: {
1627 tok::TokenKind Kind = Tok.getKind();
1628 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001629 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1630 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001631 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001632 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001633
1634 // enum-specifier:
1635 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001636 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001637 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001638 return true;
1639
1640 // cv-qualifier:
1641 case tok::kw_const:
1642 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001643 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001644 break;
1645 case tok::kw_volatile:
1646 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001647 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001648 break;
1649 case tok::kw_restrict:
1650 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001651 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001652 break;
1653
1654 // GNU typeof support.
1655 case tok::kw_typeof:
1656 ParseTypeofSpecifier(DS);
1657 return true;
1658
Anders Carlsson74948d02009-06-24 17:47:40 +00001659 // C++0x decltype support.
1660 case tok::kw_decltype:
1661 ParseDecltypeSpecifier(DS);
1662 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001663
Anders Carlssonbae27372009-06-26 23:44:14 +00001664 // C++0x auto support.
1665 case tok::kw_auto:
1666 if (!getLang().CPlusPlus0x)
1667 return false;
1668
John McCall49bfce42009-08-03 20:12:06 +00001669 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001670 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001671 case tok::kw___ptr64:
1672 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001673 case tok::kw___cdecl:
1674 case tok::kw___stdcall:
1675 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001676 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001677 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001678 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001679
Douglas Gregor450c75a2008-11-07 15:42:26 +00001680 default:
1681 // Not a type-specifier; do nothing.
1682 return false;
1683 }
1684
1685 // If the specifier combination wasn't legal, issue a diagnostic.
1686 if (isInvalid) {
1687 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001688 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001689 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001690 }
1691 DS.SetRangeEnd(Tok.getLocation());
1692 ConsumeToken(); // whatever we parsed above.
1693 return true;
1694}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001695
Chris Lattner70ae4912007-10-29 04:42:53 +00001696/// ParseStructDeclaration - Parse a struct declaration without the terminating
1697/// semicolon.
1698///
Chris Lattner90a26b02007-01-23 04:38:16 +00001699/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001700/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001701/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001702/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001703/// struct-declarator-list:
1704/// struct-declarator
1705/// struct-declarator-list ',' struct-declarator
1706/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1707/// struct-declarator:
1708/// declarator
1709/// [GNU] declarator attributes[opt]
1710/// declarator[opt] ':' constant-expression
1711/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1712///
Chris Lattnera12405b2008-04-10 06:46:29 +00001713void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001714ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001715 if (Tok.is(tok::kw___extension__)) {
1716 // __extension__ silences extension warnings in the subexpression.
1717 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001718 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001719 return ParseStructDeclaration(DS, Fields);
1720 }
Mike Stump11289f42009-09-09 15:08:12 +00001721
Steve Naroff97170802007-08-20 22:28:22 +00001722 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001723 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001724 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001725
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001726 // If there are no declarators, this is a free-standing declaration
1727 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001728 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001729 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001730 return;
1731 }
1732
1733 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001734 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001735 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001736 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001737 FieldDeclarator DeclaratorInfo(DS);
1738
1739 // Attributes are only allowed here on successive declarators.
1740 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1741 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001742 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallcfefb6d2009-11-03 02:38:08 +00001743 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1744 }
Mike Stump11289f42009-09-09 15:08:12 +00001745
Steve Naroff97170802007-08-20 22:28:22 +00001746 /// struct-declarator: declarator
1747 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001748 if (Tok.isNot(tok::colon)) {
1749 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1750 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001751 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001752 }
Mike Stump11289f42009-09-09 15:08:12 +00001753
Chris Lattner76c72282007-10-09 17:33:22 +00001754 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001755 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001756 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001757 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001758 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001759 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001760 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001761 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001762
Steve Naroff97170802007-08-20 22:28:22 +00001763 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001764 if (Tok.is(tok::kw___attribute)) {
1765 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001766 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001767 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1768 }
1769
John McCallcfefb6d2009-11-03 02:38:08 +00001770 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00001771 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00001772 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001773
Steve Naroff97170802007-08-20 22:28:22 +00001774 // If we don't have a comma, it is either the end of the list (a ';')
1775 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001776 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001777 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001778
Steve Naroff97170802007-08-20 22:28:22 +00001779 // Consume the comma.
1780 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001781
John McCallcfefb6d2009-11-03 02:38:08 +00001782 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001783 }
Steve Naroff97170802007-08-20 22:28:22 +00001784}
1785
1786/// ParseStructUnionBody
1787/// struct-contents:
1788/// struct-declaration-list
1789/// [EXT] empty
1790/// [GNU] "struct-declaration-list" without terminatoring ';'
1791/// struct-declaration-list:
1792/// struct-declaration
1793/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001794/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001795///
Chris Lattner1300fb92007-01-23 23:42:53 +00001796void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001797 unsigned TagType, Decl *TagDecl) {
Chris Lattnereae6cb62009-03-05 08:00:35 +00001798 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1799 PP.getSourceManager(),
1800 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001801
Chris Lattner90a26b02007-01-23 04:38:16 +00001802 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001803
Douglas Gregor658b9552009-01-09 22:42:13 +00001804 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001805 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001806
Chris Lattner7b9ace62007-01-23 20:11:08 +00001807 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1808 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001809 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00001810 Diag(Tok, diag::ext_empty_struct_union)
1811 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001812
John McCall48871652010-08-21 09:40:31 +00001813 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001814
Chris Lattner7b9ace62007-01-23 20:11:08 +00001815 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001816 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001817 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001818
Chris Lattner736ed5d2007-06-09 05:59:07 +00001819 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001820 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001821 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00001822 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00001823 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001824 ConsumeToken();
1825 continue;
1826 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001827
1828 // Parse all the comma separated declarators.
1829 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001830
John McCallcfefb6d2009-11-03 02:38:08 +00001831 if (!Tok.is(tok::at)) {
1832 struct CFieldCallback : FieldCallback {
1833 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001834 Decl *TagDecl;
1835 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001836
John McCall48871652010-08-21 09:40:31 +00001837 CFieldCallback(Parser &P, Decl *TagDecl,
1838 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001839 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1840
John McCall48871652010-08-21 09:40:31 +00001841 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001842 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00001843 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00001844 FD.D.getDeclSpec().getSourceRange().getBegin(),
1845 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001846 FieldDecls.push_back(Field);
1847 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001848 }
John McCallcfefb6d2009-11-03 02:38:08 +00001849 } Callback(*this, TagDecl, FieldDecls);
1850
1851 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001852 } else { // Handle @defs
1853 ConsumeToken();
1854 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1855 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001856 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001857 continue;
1858 }
1859 ConsumeToken();
1860 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1861 if (!Tok.is(tok::identifier)) {
1862 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001863 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001864 continue;
1865 }
John McCall48871652010-08-21 09:40:31 +00001866 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001867 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001868 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001869 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1870 ConsumeToken();
1871 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001872 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001873
Chris Lattner76c72282007-10-09 17:33:22 +00001874 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001875 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001876 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001877 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001878 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001879 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001880 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1881 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001882 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001883 // If we stopped at a ';', eat it.
1884 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001885 }
1886 }
Mike Stump11289f42009-09-09 15:08:12 +00001887
Steve Naroff33a1e802007-10-29 21:38:07 +00001888 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001889
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001890 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner90a26b02007-01-23 04:38:16 +00001891 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001892 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001893 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar15619c72008-10-03 02:03:53 +00001894
Douglas Gregor0be31a22010-07-02 17:43:08 +00001895 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001896 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001897 LBraceLoc, RBraceLoc,
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001898 AttrList.get());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001899 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001900 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001901}
1902
1903
Chris Lattner3b561a32006-08-13 00:12:11 +00001904/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001905/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001906/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001907///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001908/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1909/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001910/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001911/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001912///
1913/// [C++] elaborated-type-specifier:
1914/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1915///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001916void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001917 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001918 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001919 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001920 if (Tok.is(tok::code_completion)) {
1921 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001922 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001923 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001924 }
1925
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001926 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001927 // If attributes exist after tag, parse them.
1928 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001929 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001930
Abramo Bagnarad7548482010-05-19 21:37:53 +00001931 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00001932 if (getLang().CPlusPlus) {
1933 if (ParseOptionalCXXScopeSpecifier(SS, 0, false))
1934 return;
1935
1936 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001937 Diag(Tok, diag::err_expected_ident);
1938 if (Tok.isNot(tok::l_brace)) {
1939 // Has no name and is not a definition.
1940 // Skip the rest of this declarator, up until the comma or semicolon.
1941 SkipUntil(tok::comma, true);
1942 return;
1943 }
1944 }
1945 }
Mike Stump11289f42009-09-09 15:08:12 +00001946
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001947 // Must have either 'enum name' or 'enum {...}'.
1948 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1949 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001950
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001951 // Skip the rest of this declarator, up until the comma or semicolon.
1952 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001953 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001954 }
Mike Stump11289f42009-09-09 15:08:12 +00001955
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001956 // If an identifier is present, consume and remember it.
1957 IdentifierInfo *Name = 0;
1958 SourceLocation NameLoc;
1959 if (Tok.is(tok::identifier)) {
1960 Name = Tok.getIdentifierInfo();
1961 NameLoc = ConsumeToken();
1962 }
Mike Stump11289f42009-09-09 15:08:12 +00001963
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001964 // There are three options here. If we have 'enum foo;', then this is a
1965 // forward declaration. If we have 'enum foo {...' then this is a
1966 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1967 //
1968 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1969 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1970 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1971 //
John McCall9bb74a52009-07-31 02:45:11 +00001972 Action::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001973 if (Tok.is(tok::l_brace))
John McCall9bb74a52009-07-31 02:45:11 +00001974 TUK = Action::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001975 else if (Tok.is(tok::semi))
John McCall9bb74a52009-07-31 02:45:11 +00001976 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001977 else
John McCall9bb74a52009-07-31 02:45:11 +00001978 TUK = Action::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00001979
1980 // enums cannot be templates, although they can be referenced from a
1981 // template.
1982 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
1983 TUK != Action::TUK_Reference) {
1984 Diag(Tok, diag::err_enum_template);
1985
1986 // Skip the rest of this declarator, up until the comma or semicolon.
1987 SkipUntil(tok::comma, true);
1988 return;
1989 }
1990
Douglas Gregord6ab8742009-05-28 23:31:59 +00001991 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00001992 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00001993 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
1994 const char *PrevSpec = 0;
1995 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00001996 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
1997 StartLoc, SS, Name, NameLoc, Attr.get(),
1998 AS,
1999 Action::MultiTemplateParamsArg(Actions),
2000 Owned, IsDependent);
Douglas Gregorba41d012010-04-24 16:38:41 +00002001 if (IsDependent) {
2002 // This enum has a dependent nested-name-specifier. Handle it as a
2003 // dependent tag.
2004 if (!Name) {
2005 DS.SetTypeSpecError();
2006 Diag(Tok, diag::err_expected_type_name_after_typename);
2007 return;
2008 }
2009
Douglas Gregor0be31a22010-07-02 17:43:08 +00002010 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002011 TUK, SS, Name, StartLoc,
2012 NameLoc);
2013 if (Type.isInvalid()) {
2014 DS.SetTypeSpecError();
2015 return;
2016 }
2017
2018 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
2019 Type.get(), false))
2020 Diag(StartLoc, DiagID) << PrevSpec;
2021
2022 return;
2023 }
Mike Stump11289f42009-09-09 15:08:12 +00002024
John McCall48871652010-08-21 09:40:31 +00002025 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002026 // The action failed to produce an enumeration tag. If this is a
2027 // definition, consume the entire definition.
2028 if (Tok.is(tok::l_brace)) {
2029 ConsumeBrace();
2030 SkipUntil(tok::r_brace);
2031 }
2032
2033 DS.SetTypeSpecError();
2034 return;
2035 }
2036
Chris Lattner76c72282007-10-09 17:33:22 +00002037 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002038 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002039
Douglas Gregor72100632010-01-25 16:33:23 +00002040 // FIXME: The DeclSpec should keep the locations of both the keyword and the
2041 // name (if there is one).
Douglas Gregor72100632010-01-25 16:33:23 +00002042 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCall48871652010-08-21 09:40:31 +00002043 TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002044 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002045}
2046
Chris Lattnerc1915e22007-01-25 07:29:02 +00002047/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2048/// enumerator-list:
2049/// enumerator
2050/// enumerator-list ',' enumerator
2051/// enumerator:
2052/// enumeration-constant
2053/// enumeration-constant '=' constant-expression
2054/// enumeration-constant:
2055/// identifier
2056///
John McCall48871652010-08-21 09:40:31 +00002057void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002058 // Enter the scope of the enum body and start the definition.
2059 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002060 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002061
Chris Lattnerc1915e22007-01-25 07:29:02 +00002062 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002063
Chris Lattner37256fb2007-08-27 17:24:30 +00002064 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002065 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002066 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002067
John McCall48871652010-08-21 09:40:31 +00002068 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002069
John McCall48871652010-08-21 09:40:31 +00002070 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002071
Chris Lattnerc1915e22007-01-25 07:29:02 +00002072 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002073 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002074 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2075 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002076
Chris Lattnerc1915e22007-01-25 07:29:02 +00002077 SourceLocation EqualLoc;
John McCall37ad5512010-08-23 06:44:23 +00002078 OwningExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002079 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002080 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002081 AssignedVal = ParseConstantExpression();
2082 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002083 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002084 }
Mike Stump11289f42009-09-09 15:08:12 +00002085
Chris Lattnerc1915e22007-01-25 07:29:02 +00002086 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002087 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2088 LastEnumConstDecl,
2089 IdentLoc, Ident,
2090 EqualLoc,
2091 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002092 EnumConstantDecls.push_back(EnumConstDecl);
2093 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002094
Chris Lattner76c72282007-10-09 17:33:22 +00002095 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002096 break;
2097 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002098
2099 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002100 !(getLang().C99 || getLang().CPlusPlus0x))
2101 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2102 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002103 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002104 }
Mike Stump11289f42009-09-09 15:08:12 +00002105
Chris Lattnerc1915e22007-01-25 07:29:02 +00002106 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002107 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002108
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002109 llvm::OwningPtr<AttributeList> Attr;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002110 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00002111 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002112 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002113
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002114 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2115 EnumConstantDecls.data(), EnumConstantDecls.size(),
Douglas Gregor0be31a22010-07-02 17:43:08 +00002116 getCurScope(), Attr.get());
Mike Stump11289f42009-09-09 15:08:12 +00002117
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002118 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002119 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002120}
Chris Lattner3b561a32006-08-13 00:12:11 +00002121
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002122/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002123/// start of a type-qualifier-list.
2124bool Parser::isTypeQualifier() const {
2125 switch (Tok.getKind()) {
2126 default: return false;
2127 // type-qualifier
2128 case tok::kw_const:
2129 case tok::kw_volatile:
2130 case tok::kw_restrict:
2131 return true;
2132 }
2133}
2134
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002135/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2136/// is definitely a type-specifier. Return false if it isn't part of a type
2137/// specifier or if we're not sure.
2138bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2139 switch (Tok.getKind()) {
2140 default: return false;
2141 // type-specifiers
2142 case tok::kw_short:
2143 case tok::kw_long:
2144 case tok::kw_signed:
2145 case tok::kw_unsigned:
2146 case tok::kw__Complex:
2147 case tok::kw__Imaginary:
2148 case tok::kw_void:
2149 case tok::kw_char:
2150 case tok::kw_wchar_t:
2151 case tok::kw_char16_t:
2152 case tok::kw_char32_t:
2153 case tok::kw_int:
2154 case tok::kw_float:
2155 case tok::kw_double:
2156 case tok::kw_bool:
2157 case tok::kw__Bool:
2158 case tok::kw__Decimal32:
2159 case tok::kw__Decimal64:
2160 case tok::kw__Decimal128:
2161 case tok::kw___vector:
2162
2163 // struct-or-union-specifier (C99) or class-specifier (C++)
2164 case tok::kw_class:
2165 case tok::kw_struct:
2166 case tok::kw_union:
2167 // enum-specifier
2168 case tok::kw_enum:
2169
2170 // typedef-name
2171 case tok::annot_typename:
2172 return true;
2173 }
2174}
2175
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002176/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002177/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002178bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002179 switch (Tok.getKind()) {
2180 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002181
Chris Lattner020bab92009-01-04 23:41:41 +00002182 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002183 if (TryAltiVecVectorToken())
2184 return true;
2185 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002186 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002187 // Annotate typenames and C++ scope specifiers. If we get one, just
2188 // recurse to handle whatever we get.
2189 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002190 return true;
2191 if (Tok.is(tok::identifier))
2192 return false;
2193 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002194
Chris Lattner020bab92009-01-04 23:41:41 +00002195 case tok::coloncolon: // ::foo::bar
2196 if (NextToken().is(tok::kw_new) || // ::new
2197 NextToken().is(tok::kw_delete)) // ::delete
2198 return false;
2199
Chris Lattner020bab92009-01-04 23:41:41 +00002200 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002201 return true;
2202 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002203
Chris Lattnere37e2332006-08-15 04:50:22 +00002204 // GNU attributes support.
2205 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002206 // GNU typeof support.
2207 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002208
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002209 // type-specifiers
2210 case tok::kw_short:
2211 case tok::kw_long:
2212 case tok::kw_signed:
2213 case tok::kw_unsigned:
2214 case tok::kw__Complex:
2215 case tok::kw__Imaginary:
2216 case tok::kw_void:
2217 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002218 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002219 case tok::kw_char16_t:
2220 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002221 case tok::kw_int:
2222 case tok::kw_float:
2223 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002224 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002225 case tok::kw__Bool:
2226 case tok::kw__Decimal32:
2227 case tok::kw__Decimal64:
2228 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002229 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002230
Chris Lattner861a2262008-04-13 18:59:07 +00002231 // struct-or-union-specifier (C99) or class-specifier (C++)
2232 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002233 case tok::kw_struct:
2234 case tok::kw_union:
2235 // enum-specifier
2236 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002237
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002238 // type-qualifier
2239 case tok::kw_const:
2240 case tok::kw_volatile:
2241 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002242
2243 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002244 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002245 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002246
Chris Lattner409bf7d2008-10-20 00:25:30 +00002247 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2248 case tok::less:
2249 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002250
Steve Naroff44ac7772008-12-25 14:16:32 +00002251 case tok::kw___cdecl:
2252 case tok::kw___stdcall:
2253 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002254 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002255 case tok::kw___w64:
2256 case tok::kw___ptr64:
2257 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002258 }
2259}
2260
Chris Lattneracd58a32006-08-06 17:24:14 +00002261/// isDeclarationSpecifier() - Return true if the current token is part of a
2262/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002263bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00002264 switch (Tok.getKind()) {
2265 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002266
Chris Lattner020bab92009-01-04 23:41:41 +00002267 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002268 // Unfortunate hack to support "Class.factoryMethod" notation.
2269 if (getLang().ObjC1 && NextToken().is(tok::period))
2270 return false;
John Thompson22334602010-02-05 00:12:22 +00002271 if (TryAltiVecVectorToken())
2272 return true;
2273 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002274 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002275 // Annotate typenames and C++ scope specifiers. If we get one, just
2276 // recurse to handle whatever we get.
2277 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002278 return true;
2279 if (Tok.is(tok::identifier))
2280 return false;
2281 return isDeclarationSpecifier();
2282
Chris Lattner020bab92009-01-04 23:41:41 +00002283 case tok::coloncolon: // ::foo::bar
2284 if (NextToken().is(tok::kw_new) || // ::new
2285 NextToken().is(tok::kw_delete)) // ::delete
2286 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002287
Chris Lattner020bab92009-01-04 23:41:41 +00002288 // Annotate typenames and C++ scope specifiers. If we get one, just
2289 // recurse to handle whatever we get.
2290 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002291 return true;
2292 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002293
Chris Lattneracd58a32006-08-06 17:24:14 +00002294 // storage-class-specifier
2295 case tok::kw_typedef:
2296 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002297 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002298 case tok::kw_static:
2299 case tok::kw_auto:
2300 case tok::kw_register:
2301 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002302
Chris Lattneracd58a32006-08-06 17:24:14 +00002303 // type-specifiers
2304 case tok::kw_short:
2305 case tok::kw_long:
2306 case tok::kw_signed:
2307 case tok::kw_unsigned:
2308 case tok::kw__Complex:
2309 case tok::kw__Imaginary:
2310 case tok::kw_void:
2311 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002312 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002313 case tok::kw_char16_t:
2314 case tok::kw_char32_t:
2315
Chris Lattneracd58a32006-08-06 17:24:14 +00002316 case tok::kw_int:
2317 case tok::kw_float:
2318 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002319 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002320 case tok::kw__Bool:
2321 case tok::kw__Decimal32:
2322 case tok::kw__Decimal64:
2323 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002324 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002325
Chris Lattner861a2262008-04-13 18:59:07 +00002326 // struct-or-union-specifier (C99) or class-specifier (C++)
2327 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002328 case tok::kw_struct:
2329 case tok::kw_union:
2330 // enum-specifier
2331 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002332
Chris Lattneracd58a32006-08-06 17:24:14 +00002333 // type-qualifier
2334 case tok::kw_const:
2335 case tok::kw_volatile:
2336 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002337
Chris Lattneracd58a32006-08-06 17:24:14 +00002338 // function-specifier
2339 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002340 case tok::kw_virtual:
2341 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002342
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002343 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002344 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002345
Chris Lattner599e47e2007-08-09 17:01:07 +00002346 // GNU typeof support.
2347 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002348
Chris Lattner599e47e2007-08-09 17:01:07 +00002349 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002350 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002351 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002352
Chris Lattner8b2ec162008-07-26 03:38:44 +00002353 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2354 case tok::less:
2355 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002356
Steve Narofff192fab2009-01-06 19:34:12 +00002357 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002358 case tok::kw___cdecl:
2359 case tok::kw___stdcall:
2360 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002361 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002362 case tok::kw___w64:
2363 case tok::kw___ptr64:
2364 case tok::kw___forceinline:
2365 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002366 }
2367}
2368
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002369bool Parser::isConstructorDeclarator() {
2370 TentativeParsingAction TPA(*this);
2371
2372 // Parse the C++ scope specifier.
2373 CXXScopeSpec SS;
John McCall1f476a12010-02-26 08:45:28 +00002374 if (ParseOptionalCXXScopeSpecifier(SS, 0, true)) {
2375 TPA.Revert();
2376 return false;
2377 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002378
2379 // Parse the constructor name.
2380 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2381 // We already know that we have a constructor name; just consume
2382 // the token.
2383 ConsumeToken();
2384 } else {
2385 TPA.Revert();
2386 return false;
2387 }
2388
2389 // Current class name must be followed by a left parentheses.
2390 if (Tok.isNot(tok::l_paren)) {
2391 TPA.Revert();
2392 return false;
2393 }
2394 ConsumeParen();
2395
2396 // A right parentheses or ellipsis signals that we have a constructor.
2397 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2398 TPA.Revert();
2399 return true;
2400 }
2401
2402 // If we need to, enter the specified scope.
2403 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002404 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002405 DeclScopeObj.EnterDeclaratorScope();
2406
2407 // Check whether the next token(s) are part of a declaration
2408 // specifier, in which case we have the start of a parameter and,
2409 // therefore, we know that this is a constructor.
2410 bool IsConstructor = isDeclarationSpecifier();
2411 TPA.Revert();
2412 return IsConstructor;
2413}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002414
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002415/// ParseTypeQualifierListOpt
2416/// type-qualifier-list: [C99 6.7.5]
2417/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002418/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002419/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002420/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Alexis Hunt96d5c762009-11-21 08:43:09 +00002421/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2422/// if CXX0XAttributesAllowed = true
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002423///
Alexis Hunt96d5c762009-11-21 08:43:09 +00002424void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2425 bool CXX0XAttributesAllowed) {
2426 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2427 SourceLocation Loc = Tok.getLocation();
2428 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2429 if (CXX0XAttributesAllowed)
2430 DS.AddAttributes(Attr.AttrList);
2431 else
2432 Diag(Loc, diag::err_attributes_not_allowed);
2433 }
2434
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002435 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002436 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002437 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002438 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002439 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002440
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002441 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002442 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002443 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2444 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002445 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002446 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002447 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2448 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002449 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002450 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002451 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2452 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002453 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002454 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002455 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002456 case tok::kw___cdecl:
2457 case tok::kw___stdcall:
2458 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002459 case tok::kw___thiscall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002460 if (GNUAttributesAllowed) {
Eli Friedman53339e02009-06-08 23:27:34 +00002461 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2462 continue;
2463 }
2464 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002465 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002466 if (GNUAttributesAllowed) {
2467 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002468 continue; // do *not* consume the next token!
2469 }
2470 // otherwise, FALL THROUGH!
2471 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002472 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002473 // If this is not a type-qualifier token, we're done reading type
2474 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002475 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002476 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002477 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002478
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002479 // If the specifier combination wasn't legal, issue a diagnostic.
2480 if (isInvalid) {
2481 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002482 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002483 }
2484 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002485 }
2486}
2487
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002488
2489/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2490///
2491void Parser::ParseDeclarator(Declarator &D) {
2492 /// This implements the 'declarator' production in the C grammar, then checks
2493 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002494 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002495}
2496
Sebastian Redlbd150f42008-11-21 19:14:01 +00002497/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2498/// is parsed by the function passed to it. Pass null, and the direct-declarator
2499/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002500/// ptr-operator production.
2501///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002502/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2503/// [C] pointer[opt] direct-declarator
2504/// [C++] direct-declarator
2505/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002506///
2507/// pointer: [C99 6.7.5]
2508/// '*' type-qualifier-list[opt]
2509/// '*' type-qualifier-list[opt] pointer
2510///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002511/// ptr-operator:
2512/// '*' cv-qualifier-seq[opt]
2513/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002514/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002515/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002516/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002517/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002518void Parser::ParseDeclaratorInternal(Declarator &D,
2519 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002520 if (Diags.hasAllExtensionsSilenced())
2521 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002522
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002523 // C++ member pointers start with a '::' or a nested-name.
2524 // Member pointers get special handling, since there's no place for the
2525 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002526 if (getLang().CPlusPlus &&
2527 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2528 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002529 CXXScopeSpec SS;
John McCall1f476a12010-02-26 08:45:28 +00002530 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true); // ignore fail
2531
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002532 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002533 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002534 // The scope spec really belongs to the direct-declarator.
2535 D.getCXXScopeSpec() = SS;
2536 if (DirectDeclParser)
2537 (this->*DirectDeclParser)(D);
2538 return;
2539 }
2540
2541 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002542 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002543 DeclSpec DS;
2544 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002545 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002546
2547 // Recurse to parse whatever is left.
2548 ParseDeclaratorInternal(D, DirectDeclParser);
2549
2550 // Sema will have to catch (syntactically invalid) pointers into global
2551 // scope. It has to catch pointers into namespace scope anyway.
2552 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002553 Loc, DS.TakeAttributes()),
2554 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002555 return;
2556 }
2557 }
2558
2559 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002560 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002561 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002562 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002563 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002564 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002565 if (DirectDeclParser)
2566 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002567 return;
2568 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002569
Sebastian Redled0f3b02009-03-15 22:02:01 +00002570 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2571 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002572 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002573 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002574
Chris Lattner9eac9312009-03-27 04:18:06 +00002575 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002576 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002577 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002578
Bill Wendling3708c182007-05-27 10:15:43 +00002579 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002580 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002581
Bill Wendling3708c182007-05-27 10:15:43 +00002582 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002583 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002584 if (Kind == tok::star)
2585 // Remember that we parsed a pointer type, and remember the type-quals.
2586 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002587 DS.TakeAttributes()),
2588 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002589 else
2590 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002591 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002592 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002593 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002594 } else {
2595 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002596 DeclSpec DS;
2597
Sebastian Redl3b27be62009-03-23 00:00:23 +00002598 // Complain about rvalue references in C++03, but then go on and build
2599 // the declarator.
2600 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2601 Diag(Loc, diag::err_rvalue_reference);
2602
Bill Wendling93efb222007-06-02 23:28:54 +00002603 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2604 // cv-qualifiers are introduced through the use of a typedef or of a
2605 // template type argument, in which case the cv-qualifiers are ignored.
2606 //
2607 // [GNU] Retricted references are allowed.
2608 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002609 // [C++0x] Attributes on references are not allowed.
2610 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002611 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002612
2613 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2614 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2615 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002616 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002617 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2618 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002619 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002620 }
Bill Wendling3708c182007-05-27 10:15:43 +00002621
2622 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002623 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002624
Douglas Gregor66583c52008-11-03 15:51:28 +00002625 if (D.getNumTypeObjects() > 0) {
2626 // C++ [dcl.ref]p4: There shall be no references to references.
2627 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2628 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002629 if (const IdentifierInfo *II = D.getIdentifier())
2630 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2631 << II;
2632 else
2633 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2634 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002635
Sebastian Redlbd150f42008-11-21 19:14:01 +00002636 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002637 // can go ahead and build the (technically ill-formed)
2638 // declarator: reference collapsing will take care of it.
2639 }
2640 }
2641
Bill Wendling3708c182007-05-27 10:15:43 +00002642 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002643 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002644 DS.TakeAttributes(),
2645 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002646 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002647 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002648}
2649
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002650/// ParseDirectDeclarator
2651/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002652/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002653/// '(' declarator ')'
2654/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002655/// [C90] direct-declarator '[' constant-expression[opt] ']'
2656/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2657/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2658/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2659/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002660/// direct-declarator '(' parameter-type-list ')'
2661/// direct-declarator '(' identifier-list[opt] ')'
2662/// [GNU] direct-declarator '(' parameter-forward-declarations
2663/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002664/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2665/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002666/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002667///
2668/// declarator-id: [C++ 8]
2669/// id-expression
2670/// '::'[opt] nested-name-specifier[opt] type-name
2671///
2672/// id-expression: [C++ 5.1]
2673/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002674/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002675///
2676/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002677/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002678/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002679/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002680/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002681/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002682///
Chris Lattneracd58a32006-08-06 17:24:14 +00002683void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002684 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002685
Douglas Gregor7861a802009-11-03 01:35:08 +00002686 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2687 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002688 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00002689 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2690 true);
John McCall1f476a12010-02-26 08:45:28 +00002691 }
2692
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002693 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002694 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00002695 // Change the declaration context for name lookup, until this function
2696 // is exited (and the declarator has been parsed).
2697 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002698 }
2699
Douglas Gregor7861a802009-11-03 01:35:08 +00002700 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2701 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2702 // We found something that indicates the start of an unqualified-id.
2703 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002704 bool AllowConstructorName;
2705 if (D.getDeclSpec().hasTypeSpecifier())
2706 AllowConstructorName = false;
2707 else if (D.getCXXScopeSpec().isSet())
2708 AllowConstructorName =
2709 (D.getContext() == Declarator::FileContext ||
2710 (D.getContext() == Declarator::MemberContext &&
2711 D.getDeclSpec().isFriendSpecified()));
2712 else
2713 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2714
Douglas Gregor7861a802009-11-03 01:35:08 +00002715 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2716 /*EnteringContext=*/true,
2717 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002718 AllowConstructorName,
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002719 /*ObjectType=*/0,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002720 D.getName()) ||
2721 // Once we're past the identifier, if the scope was bad, mark the
2722 // whole declarator bad.
2723 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002724 D.SetIdentifier(0, Tok.getLocation());
2725 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002726 } else {
2727 // Parsed the unqualified-id; update range information and move along.
2728 if (D.getSourceRange().getBegin().isInvalid())
2729 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2730 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002731 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002732 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002733 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002734 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002735 assert(!getLang().CPlusPlus &&
2736 "There's a C++-specific check for tok::identifier above");
2737 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2738 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2739 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002740 goto PastIdentifier;
2741 }
2742
2743 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002744 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002745 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002746 // Example: 'char (*X)' or 'int (*XX)(void)'
2747 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002748
2749 // If the declarator was parenthesized, we entered the declarator
2750 // scope when parsing the parenthesized declarator, then exited
2751 // the scope already. Re-enter the scope, if we need to.
2752 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002753 // If there was an error parsing parenthesized declarator, declarator
2754 // scope may have been enterred before. Don't do it again.
2755 if (!D.isInvalidType() &&
2756 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002757 // Change the declaration context for name lookup, until this function
2758 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002759 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002760 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002761 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002762 // This could be something simple like "int" (in which case the declarator
2763 // portion is empty), if an abstract-declarator is allowed.
2764 D.SetIdentifier(0, Tok.getLocation());
2765 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002766 if (D.getContext() == Declarator::MemberContext)
2767 Diag(Tok, diag::err_expected_member_name_or_semi)
2768 << D.getDeclSpec().getSourceRange();
2769 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002770 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002771 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002772 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002773 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002774 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002775 }
Mike Stump11289f42009-09-09 15:08:12 +00002776
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002777 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002778 assert(D.isPastIdentifier() &&
2779 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002780
Alexis Hunt96d5c762009-11-21 08:43:09 +00002781 // Don't parse attributes unless we have an identifier.
Douglas Gregor0286b462010-02-19 16:47:56 +00002782 if (D.getIdentifier() && getLang().CPlusPlus0x
Alexis Hunt96d5c762009-11-21 08:43:09 +00002783 && isCXX0XAttributeSpecifier(true)) {
2784 SourceLocation AttrEndLoc;
2785 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2786 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2787 }
2788
Chris Lattneracd58a32006-08-06 17:24:14 +00002789 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002790 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002791 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2792 // In such a case, check if we actually have a function declarator; if it
2793 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002794 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2795 // When not in file scope, warn for ambiguous function declarators, just
2796 // in case the author intended it as a variable definition.
2797 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2798 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2799 break;
2800 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002801 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002802 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002803 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002804 } else {
2805 break;
2806 }
2807 }
2808}
2809
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002810/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2811/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002812/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002813/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2814///
2815/// direct-declarator:
2816/// '(' declarator ')'
2817/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002818/// direct-declarator '(' parameter-type-list ')'
2819/// direct-declarator '(' identifier-list[opt] ')'
2820/// [GNU] direct-declarator '(' parameter-forward-declarations
2821/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002822///
2823void Parser::ParseParenDeclarator(Declarator &D) {
2824 SourceLocation StartLoc = ConsumeParen();
2825 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002826
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002827 // Eat any attributes before we look at whether this is a grouping or function
2828 // declarator paren. If this is a grouping paren, the attribute applies to
2829 // the type being built up, for example:
2830 // int (__attribute__(()) *x)(long y)
2831 // If this ends up not being a grouping paren, the attribute applies to the
2832 // first argument, for example:
2833 // int (__attribute__(()) int x)
2834 // In either case, we need to eat any attributes to be able to determine what
2835 // sort of paren this is.
2836 //
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002837 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002838 bool RequiresArg = false;
2839 if (Tok.is(tok::kw___attribute)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002840 AttrList.reset(ParseGNUAttributes());
Mike Stump11289f42009-09-09 15:08:12 +00002841
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002842 // We require that the argument list (if this is a non-grouping paren) be
2843 // present even if the attribute list was empty.
2844 RequiresArg = true;
2845 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002846 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002847 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00002848 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2849 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002850 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman53339e02009-06-08 23:27:34 +00002851 }
Mike Stump11289f42009-09-09 15:08:12 +00002852
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002853 // If we haven't past the identifier yet (or where the identifier would be
2854 // stored, if this is an abstract declarator), then this is probably just
2855 // grouping parens. However, if this could be an abstract-declarator, then
2856 // this could also be the start of function arguments (consider 'void()').
2857 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002858
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002859 if (!D.mayOmitIdentifier()) {
2860 // If this can't be an abstract-declarator, this *must* be a grouping
2861 // paren, because we haven't seen the identifier yet.
2862 isGrouping = true;
2863 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002864 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002865 isDeclarationSpecifier()) { // 'int(int)' is a function.
2866 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2867 // considered to be a type, not a K&R identifier-list.
2868 isGrouping = false;
2869 } else {
2870 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2871 isGrouping = true;
2872 }
Mike Stump11289f42009-09-09 15:08:12 +00002873
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002874 // If this is a grouping paren, handle:
2875 // direct-declarator: '(' declarator ')'
2876 // direct-declarator: '(' attributes declarator ')'
2877 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002878 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002879 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002880 if (AttrList)
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002881 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002882
Sebastian Redlbd150f42008-11-21 19:14:01 +00002883 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002884 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002885 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002886
2887 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002888 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002889 return;
2890 }
Mike Stump11289f42009-09-09 15:08:12 +00002891
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002892 // Okay, if this wasn't a grouping paren, it must be the start of a function
2893 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002894 // identifier (and remember where it would have been), then call into
2895 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002896 D.SetIdentifier(0, Tok.getLocation());
2897
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002898 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002899}
2900
2901/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2902/// declarator D up to a paren, which indicates that we are parsing function
2903/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002904///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002905/// If AttrList is non-null, then the caller parsed those arguments immediately
2906/// after the open paren - they should be considered to be the first argument of
2907/// a parameter. If RequiresArg is true, then the first argument of the
2908/// function is required to be present and required to not be an identifier
2909/// list.
2910///
Chris Lattneracd58a32006-08-06 17:24:14 +00002911/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002912/// parameter-type-list: [C99 6.7.5]
2913/// parameter-list
2914/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002915/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002916///
2917/// parameter-list: [C99 6.7.5]
2918/// parameter-declaration
2919/// parameter-list ',' parameter-declaration
2920///
2921/// parameter-declaration: [C99 6.7.5]
2922/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002923/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002924/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002925/// declaration-specifiers abstract-declarator[opt]
2926/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002927/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002928/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002929///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002930/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002931/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002932///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002933void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2934 AttributeList *AttrList,
2935 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002936 // lparen is already consumed!
2937 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00002938
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002939 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002940 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002941 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002942 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002943 delete AttrList;
2944 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002945
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002946 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2947 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002948
2949 // cv-qualifier-seq[opt].
2950 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002951 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002952 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002953 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002954 llvm::SmallVector<TypeTy*, 2> Exceptions;
2955 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002956 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002957 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002958 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002959 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002960
2961 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002962 if (Tok.is(tok::kw_throw)) {
2963 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002964 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002965 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002966 hasAnyExceptionSpec);
2967 assert(Exceptions.size() == ExceptionRanges.size() &&
2968 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002969 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002970 }
2971
Chris Lattner371ed4e2008-04-06 06:57:35 +00002972 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002973 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002974 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002975 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002976 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002977 /*arglist*/ 0, 0,
2978 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002979 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002980 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002981 Exceptions.data(),
2982 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002983 Exceptions.size(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002984 LParenLoc, RParenLoc, D),
2985 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002986 return;
Sebastian Redld6434562009-05-29 18:02:33 +00002987 }
2988
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002989 // Alternatively, this parameter list may be an identifier list form for a
2990 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00002991 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
2992 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00002993 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002994 // K&R identifier lists can't have typedefs as identifiers, per
2995 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002996 if (RequiresArg) {
2997 Diag(Tok, diag::err_argument_required_after_attribute);
2998 delete AttrList;
2999 }
Chris Lattner9453ab82010-05-14 17:23:36 +00003000
Steve Naroffb0486722009-01-28 19:16:40 +00003001 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003002 // normal declarators, not for abstract-declarators. Get the first
3003 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003004 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003005 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003006
3007 // Identifier lists follow a really simple grammar: the identifiers can
3008 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3009 // identifier lists are really rare in the brave new modern world, and it
3010 // is very common for someone to typo a type in a non-k&r style list. If
3011 // we are presented with something like: "void foo(intptr x, float y)",
3012 // we don't want to start parsing the function declarator as though it is
3013 // a K&R style declarator just because intptr is an invalid type.
3014 //
3015 // To handle this, we check to see if the token after the first identifier
3016 // is a "," or ")". Only if so, do we parse it as an identifier list.
3017 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3018 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3019 FirstTok.getIdentifierInfo(),
3020 FirstTok.getLocation(), D);
3021
3022 // If we get here, the code is invalid. Push the first identifier back
3023 // into the token stream and parse the first argument as an (invalid)
3024 // normal argument declarator.
3025 PP.EnterToken(Tok);
3026 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003027 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003028 }
Mike Stump11289f42009-09-09 15:08:12 +00003029
Chris Lattner371ed4e2008-04-06 06:57:35 +00003030 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003031
Chris Lattner371ed4e2008-04-06 06:57:35 +00003032 // Build up an array of information about the parsed arguments.
3033 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003034
3035 // Enter function-declaration scope, limiting any declarators to the
3036 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003037 ParseScope PrototypeScope(this,
3038 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003039
Chris Lattner371ed4e2008-04-06 06:57:35 +00003040 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003041 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003042 while (1) {
3043 if (Tok.is(tok::ellipsis)) {
3044 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003045 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003046 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003047 }
Mike Stump11289f42009-09-09 15:08:12 +00003048
Chris Lattner371ed4e2008-04-06 06:57:35 +00003049 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00003050
Chris Lattner371ed4e2008-04-06 06:57:35 +00003051 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003052 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003053 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003054
3055 // If the caller parsed attributes for the first argument, add them now.
3056 if (AttrList) {
3057 DS.AddAttributes(AttrList);
3058 AttrList = 0; // Only apply the attributes to the first parameter.
3059 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003060 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003061
Chris Lattner371ed4e2008-04-06 06:57:35 +00003062 // Parse the declarator. This is "PrototypeContext", because we must
3063 // accept either 'declarator' or 'abstract-declarator' here.
3064 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3065 ParseDeclarator(ParmDecl);
3066
3067 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003068 if (Tok.is(tok::kw___attribute)) {
3069 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003070 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003071 ParmDecl.AddAttributes(AttrList, Loc);
3072 }
Mike Stump11289f42009-09-09 15:08:12 +00003073
Chris Lattner371ed4e2008-04-06 06:57:35 +00003074 // Remember this parsed parameter in ParamInfo.
3075 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003076
Douglas Gregor4d87df52008-12-16 21:30:33 +00003077 // DefArgToks is used when the parsing of default arguments needs
3078 // to be delayed.
3079 CachedTokens *DefArgToks = 0;
3080
Chris Lattner371ed4e2008-04-06 06:57:35 +00003081 // If no parameter was specified, verify that *something* was specified,
3082 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003083 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3084 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003085 // Completely missing, emit error.
3086 Diag(DSStart, diag::err_missing_param);
3087 } else {
3088 // Otherwise, we have something. Add it and let semantic analysis try
3089 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003090
Chris Lattner371ed4e2008-04-06 06:57:35 +00003091 // Inform the actions module about the parameter declarator, so it gets
3092 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003093 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003094
3095 // Parse the default argument, if any. We parse the default
3096 // arguments in all dialects; the semantic analysis in
3097 // ActOnParamDefaultArgument will reject the default argument in
3098 // C.
3099 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003100 SourceLocation EqualLoc = Tok.getLocation();
3101
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003102 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003103 if (D.getContext() == Declarator::MemberContext) {
3104 // If we're inside a class definition, cache the tokens
3105 // corresponding to the default argument. We'll actually parse
3106 // them when we see the end of the class definition.
3107 // FIXME: Templates will require something similar.
3108 // FIXME: Can we use a smart pointer for Toks?
3109 DefArgToks = new CachedTokens;
3110
Mike Stump11289f42009-09-09 15:08:12 +00003111 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003112 /*StopAtSemi=*/true,
3113 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003114 delete DefArgToks;
3115 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003116 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003117 } else {
3118 // Mark the end of the default argument so that we know when to
3119 // stop when we parse it later on.
3120 Token DefArgEnd;
3121 DefArgEnd.startToken();
3122 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3123 DefArgEnd.setLocation(Tok.getLocation());
3124 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003125 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003126 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003127 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003128 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003129 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003130 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003131
Douglas Gregor4d87df52008-12-16 21:30:33 +00003132 OwningExprResult DefArgResult(ParseAssignmentExpression());
3133 if (DefArgResult.isInvalid()) {
3134 Actions.ActOnParamDefaultArgumentError(Param);
3135 SkipUntil(tok::comma, tok::r_paren, true, true);
3136 } else {
3137 // Inform the actions module about the default argument
3138 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003139 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003140 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003141 }
3142 }
Mike Stump11289f42009-09-09 15:08:12 +00003143
3144 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3145 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003146 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003147 }
3148
3149 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003150 if (Tok.isNot(tok::comma)) {
3151 if (Tok.is(tok::ellipsis)) {
3152 IsVariadic = true;
3153 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3154
3155 if (!getLang().CPlusPlus) {
3156 // We have ellipsis without a preceding ',', which is ill-formed
3157 // in C. Complain and provide the fix.
3158 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003159 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003160 }
3161 }
3162
3163 break;
3164 }
Mike Stump11289f42009-09-09 15:08:12 +00003165
Chris Lattner371ed4e2008-04-06 06:57:35 +00003166 // Consume the comma.
3167 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003168 }
Mike Stump11289f42009-09-09 15:08:12 +00003169
Chris Lattner371ed4e2008-04-06 06:57:35 +00003170 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00003171 PrototypeScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00003172
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003173 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003174 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3175 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003176
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003177 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003178 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003179 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003180 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00003181 llvm::SmallVector<TypeTy*, 2> Exceptions;
3182 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003183
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003184 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003185 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003186 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003187 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003188 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003189
3190 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003191 if (Tok.is(tok::kw_throw)) {
3192 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003193 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003194 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003195 hasAnyExceptionSpec);
3196 assert(Exceptions.size() == ExceptionRanges.size() &&
3197 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003198 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003199 }
3200
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003201 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003202 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003203 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003204 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003205 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003206 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003207 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003208 Exceptions.data(),
3209 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003210 Exceptions.size(),
3211 LParenLoc, RParenLoc, D),
3212 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003213}
Chris Lattneracd58a32006-08-06 17:24:14 +00003214
Chris Lattner6c940e62008-04-06 06:34:08 +00003215/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3216/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003217/// first identifier has already been consumed, and the current token is the
3218/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003219///
3220/// identifier-list: [C99 6.7.5]
3221/// identifier
3222/// identifier-list ',' identifier
3223///
3224void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003225 IdentifierInfo *FirstIdent,
3226 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003227 Declarator &D) {
3228 // Build up an array of information about the parsed arguments.
3229 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3230 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003231
Chris Lattner6c940e62008-04-06 06:34:08 +00003232 // If there was no identifier specified for the declarator, either we are in
3233 // an abstract-declarator, or we are in a parameter declarator which was found
3234 // to be abstract. In abstract-declarators, identifier lists are not valid:
3235 // diagnose this.
3236 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003237 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003238
Chris Lattner9453ab82010-05-14 17:23:36 +00003239 // The first identifier was already read, and is known to be the first
3240 // identifier in the list. Remember this identifier in ParamInfo.
3241 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003242 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003243
Chris Lattner6c940e62008-04-06 06:34:08 +00003244 while (Tok.is(tok::comma)) {
3245 // Eat the comma.
3246 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003247
Chris Lattner9186f552008-04-06 06:39:19 +00003248 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003249 if (Tok.isNot(tok::identifier)) {
3250 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003251 SkipUntil(tok::r_paren);
3252 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003253 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003254
Chris Lattner6c940e62008-04-06 06:34:08 +00003255 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003256
3257 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003258 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003259 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003260
Chris Lattner6c940e62008-04-06 06:34:08 +00003261 // Verify that the argument identifier has not already been mentioned.
3262 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003263 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003264 } else {
3265 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003266 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003267 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003268 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003269 }
Mike Stump11289f42009-09-09 15:08:12 +00003270
Chris Lattner6c940e62008-04-06 06:34:08 +00003271 // Eat the identifier.
3272 ConsumeToken();
3273 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003274
3275 // If we have the closing ')', eat it and we're done.
3276 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3277
Chris Lattner9186f552008-04-06 06:39:19 +00003278 // Remember that we parsed a function type, and remember the attributes. This
3279 // function type is always a K&R style function type, which is not varargs and
3280 // has no prototype.
3281 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003282 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003283 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003284 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003285 /*exception*/false,
3286 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003287 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003288 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003289}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003290
Chris Lattnere8074e62006-08-06 18:30:15 +00003291/// [C90] direct-declarator '[' constant-expression[opt] ']'
3292/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3293/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3294/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3295/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3296void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003297 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003298
Chris Lattner84a11622008-12-18 07:27:21 +00003299 // C array syntax has many features, but by-far the most common is [] and [4].
3300 // This code does a fast path to handle some of the most obvious cases.
3301 if (Tok.getKind() == tok::r_square) {
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(true)) {
3306 Attr = ParseCXX0XAttributes();
3307 }
3308
Chris Lattner84a11622008-12-18 07:27:21 +00003309 // Remember that we parsed the empty array type.
John McCall37ad5512010-08-23 06:44:23 +00003310 OwningExprResult NumElements;
Douglas Gregor04318252009-07-06 15:59:29 +00003311 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3312 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003313 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003314 return;
3315 } else if (Tok.getKind() == tok::numeric_constant &&
3316 GetLookAheadToken(1).is(tok::r_square)) {
3317 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlffbcf962009-01-18 18:53:16 +00003318 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003319 ConsumeToken();
3320
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003321 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003322 //FIXME: Use these
3323 CXX0XAttributeList Attr;
3324 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3325 Attr = ParseCXX0XAttributes();
3326 }
Chris Lattner84a11622008-12-18 07:27:21 +00003327
3328 // If there was an error parsing the assignment-expression, recover.
3329 if (ExprRes.isInvalid())
3330 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00003331
Chris Lattner84a11622008-12-18 07:27:21 +00003332 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00003333 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3334 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003335 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003336 return;
3337 }
Mike Stump11289f42009-09-09 15:08:12 +00003338
Chris Lattnere8074e62006-08-06 18:30:15 +00003339 // If valid, this location is the position where we read the 'static' keyword.
3340 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003341 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003342 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003343
Chris Lattnere8074e62006-08-06 18:30:15 +00003344 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003345 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003346 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003347 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003348
Chris Lattnere8074e62006-08-06 18:30:15 +00003349 // If we haven't already read 'static', check to see if there is one after the
3350 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003351 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003352 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003353
Chris Lattnere8074e62006-08-06 18:30:15 +00003354 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003355 bool isStar = false;
John McCall37ad5512010-08-23 06:44:23 +00003356 OwningExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00003357
Chris Lattner521ff2b2008-04-06 05:26:30 +00003358 // Handle the case where we have '[*]' as the array size. However, a leading
3359 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3360 // the the token after the star is a ']'. Since stars in arrays are
3361 // infrequent, use of lookahead is not costly here.
3362 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003363 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003364
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003365 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003366 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003367 StaticLoc = SourceLocation(); // Drop the static.
3368 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003369 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003370 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003371 // Note, in C89, this production uses the constant-expr production instead
3372 // of assignment-expr. The only difference is that assignment-expr allows
3373 // things like '=' and '*='. Sema rejects these in C89 mode because they
3374 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003375
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003376 // Parse the constant-expression or assignment-expression now (depending
3377 // on dialect).
3378 if (getLang().CPlusPlus)
3379 NumElements = ParseConstantExpression();
3380 else
3381 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003382 }
Mike Stump11289f42009-09-09 15:08:12 +00003383
Chris Lattner62591722006-08-12 18:40:58 +00003384 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003385 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003386 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003387 // If the expression was invalid, skip it.
3388 SkipUntil(tok::r_square);
3389 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003390 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003391
3392 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3393
Alexis Hunt96d5c762009-11-21 08:43:09 +00003394 //FIXME: Use these
3395 CXX0XAttributeList Attr;
3396 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3397 Attr = ParseCXX0XAttributes();
3398 }
3399
Chris Lattner84a11622008-12-18 07:27:21 +00003400 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00003401 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3402 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003403 NumElements.release(),
3404 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003405 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003406}
3407
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003408/// [GNU] typeof-specifier:
3409/// typeof ( expressions )
3410/// typeof ( type-name )
3411/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003412///
3413void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003414 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003415 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003416 SourceLocation StartLoc = ConsumeToken();
3417
John McCalle8595032010-01-13 20:03:27 +00003418 const bool hasParens = Tok.is(tok::l_paren);
3419
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003420 bool isCastExpr;
3421 TypeTy *CastTy;
3422 SourceRange CastRange;
3423 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3424 isCastExpr,
3425 CastTy,
3426 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003427 if (hasParens)
3428 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003429
3430 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003431 // FIXME: Not accurate, the range gets one token more than it should.
3432 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003433 else
3434 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003435
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003436 if (isCastExpr) {
3437 if (!CastTy) {
3438 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003439 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003440 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003441
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003442 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003443 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003444 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3445 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003446 DiagID, CastTy))
3447 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003448 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003449 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003450
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003451 // If we get here, the operand to the typeof was an expresion.
3452 if (Operand.isInvalid()) {
3453 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003454 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003455 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003456
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003457 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003458 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003459 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3460 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003461 DiagID, Operand.release()))
3462 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003463}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003464
3465
3466/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3467/// from TryAltiVecVectorToken.
3468bool Parser::TryAltiVecVectorTokenOutOfLine() {
3469 Token Next = NextToken();
3470 switch (Next.getKind()) {
3471 default: return false;
3472 case tok::kw_short:
3473 case tok::kw_long:
3474 case tok::kw_signed:
3475 case tok::kw_unsigned:
3476 case tok::kw_void:
3477 case tok::kw_char:
3478 case tok::kw_int:
3479 case tok::kw_float:
3480 case tok::kw_double:
3481 case tok::kw_bool:
3482 case tok::kw___pixel:
3483 Tok.setKind(tok::kw___vector);
3484 return true;
3485 case tok::identifier:
3486 if (Next.getIdentifierInfo() == Ident_pixel) {
3487 Tok.setKind(tok::kw___vector);
3488 return true;
3489 }
3490 return false;
3491 }
3492}
3493
3494bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3495 const char *&PrevSpec, unsigned &DiagID,
3496 bool &isInvalid) {
3497 if (Tok.getIdentifierInfo() == Ident_vector) {
3498 Token Next = NextToken();
3499 switch (Next.getKind()) {
3500 case tok::kw_short:
3501 case tok::kw_long:
3502 case tok::kw_signed:
3503 case tok::kw_unsigned:
3504 case tok::kw_void:
3505 case tok::kw_char:
3506 case tok::kw_int:
3507 case tok::kw_float:
3508 case tok::kw_double:
3509 case tok::kw_bool:
3510 case tok::kw___pixel:
3511 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3512 return true;
3513 case tok::identifier:
3514 if (Next.getIdentifierInfo() == Ident_pixel) {
3515 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3516 return true;
3517 }
3518 break;
3519 default:
3520 break;
3521 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00003522 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003523 DS.isTypeAltiVecVector()) {
3524 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3525 return true;
3526 }
3527 return false;
3528}
3529