blob: bef9f9585da25d1857f8201affa0c253e4e90bb3 [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"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000019#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000020#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// C99 6.7: Declarations.
25//===----------------------------------------------------------------------===//
26
Chris Lattnerf5fbd792006-08-10 23:56:11 +000027/// ParseTypeName
28/// type-name: [C99 6.7.6]
29/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000030///
31/// Called type-id in C++.
John McCallfaf5fb42010-08-26 23:41:50 +000032TypeResult Parser::ParseTypeName(SourceRange *Range) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000033 // Parse the common declaration-specifiers piece.
34 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000035 ParseSpecifierQualifierList(DS);
Sebastian Redld6434562009-05-29 18:02:33 +000036
Chris Lattnerf5fbd792006-08-10 23:56:11 +000037 // Parse the abstract-declarator, if present.
38 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
39 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000040 if (Range)
41 *Range = DeclaratorInfo.getSourceRange();
42
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000043 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000044 return true;
45
Douglas Gregor0be31a22010-07-02 17:43:08 +000046 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000047}
48
Alexis Hunt96d5c762009-11-21 08:43:09 +000049/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000050///
51/// [GNU] attributes:
52/// attribute
53/// attributes attribute
54///
55/// [GNU] attribute:
56/// '__attribute__' '(' '(' attribute-list ')' ')'
57///
58/// [GNU] attribute-list:
59/// attrib
60/// attribute_list ',' attrib
61///
62/// [GNU] attrib:
63/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000064/// attrib-name
65/// attrib-name '(' identifier ')'
66/// attrib-name '(' identifier ',' nonempty-expr-list ')'
67/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000068///
Steve Naroff0f2fe172007-06-01 17:11:19 +000069/// [GNU] attrib-name:
70/// identifier
71/// typespec
72/// typequal
73/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000074///
Steve Naroff0f2fe172007-06-01 17:11:19 +000075/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000076/// token lookahead. Comment from gcc: "If they start with an identifier
77/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +000078/// start with that identifier; otherwise they are an expression list."
79///
80/// At the moment, I am not doing 2 token lookahead. I am also unaware of
81/// any attributes that don't work (based on my limited testing). Most
82/// attributes are very simple in practice. Until we find a bug, I don't see
83/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000084
Alexis Hunt96d5c762009-11-21 08:43:09 +000085AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) {
86 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +000087
Steve Naroffb8371e12007-06-09 03:39:29 +000088 AttributeList *CurrAttr = 0;
Mike Stump11289f42009-09-09 15:08:12 +000089
Chris Lattner76c72282007-10-09 17:33:22 +000090 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000091 ConsumeToken();
92 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
93 "attribute")) {
94 SkipUntil(tok::r_paren, true); // skip until ) or ;
95 return CurrAttr;
96 }
97 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
98 SkipUntil(tok::r_paren, true); // skip until ) or ;
99 return CurrAttr;
100 }
101 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000102 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
103 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000104
105 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000106 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
107 ConsumeToken();
108 continue;
109 }
110 // we have an identifier or declaration specifier (const, int, etc.)
111 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
112 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000113
Douglas Gregora2f49452010-03-16 19:09:18 +0000114 // check if we have a "parameterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000115 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000116 ConsumeParen(); // ignore the left paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000117
Chris Lattner76c72282007-10-09 17:33:22 +0000118 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000119 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
120 SourceLocation ParmLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000121
122 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000123 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000124 ConsumeParen(); // ignore the right paren loc for now
Alexis Hunt96d5c762009-11-21 08:43:09 +0000125 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000126 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner76c72282007-10-09 17:33:22 +0000127 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000128 ConsumeToken();
129 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000130 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000131 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000132
Steve Naroff0f2fe172007-06-01 17:11:19 +0000133 // now parse the non-empty comma separated list of expressions
134 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000135 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000136 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000137 ArgExprsOk = false;
138 SkipUntil(tok::r_paren);
139 break;
140 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000141 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000142 }
Chris Lattner76c72282007-10-09 17:33:22 +0000143 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000144 break;
145 ConsumeToken(); // Eat the comma, move to the next argument
146 }
Chris Lattner76c72282007-10-09 17:33:22 +0000147 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000148 ConsumeParen(); // ignore the right paren loc for now
Alexis Hunt96d5c762009-11-21 08:43:09 +0000149 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
150 AttrNameLoc, ParmName, ParmLoc,
151 ArgExprs.take(), ArgExprs.size(),
152 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000153 }
154 }
155 } else { // not an identifier
Nate Begemanf2758702009-06-26 06:32:41 +0000156 switch (Tok.getKind()) {
157 case tok::r_paren:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000158 // parse a possibly empty comma separated list of expressions
Steve Naroff0f2fe172007-06-01 17:11:19 +0000159 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000160 ConsumeParen(); // ignore the right paren loc for now
Alexis Hunt96d5c762009-11-21 08:43:09 +0000161 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000162 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begemanf2758702009-06-26 06:32:41 +0000163 break;
164 case tok::kw_char:
165 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000166 case tok::kw_char16_t:
167 case tok::kw_char32_t:
Nate Begemanf2758702009-06-26 06:32:41 +0000168 case tok::kw_bool:
169 case tok::kw_short:
170 case tok::kw_int:
171 case tok::kw_long:
172 case tok::kw_signed:
173 case tok::kw_unsigned:
174 case tok::kw_float:
175 case tok::kw_double:
176 case tok::kw_void:
177 case tok::kw_typeof:
Fariborz Jahanian9d7d3d82010-08-17 23:19:16 +0000178 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
179 0, SourceLocation(), 0, 0, CurrAttr);
180 if (CurrAttr->getKind() == AttributeList::AT_IBOutletCollection)
181 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begemanf2758702009-06-26 06:32:41 +0000182 // If it's a builtin type name, eat it and expect a rparen
183 // __attribute__(( vec_type_hint(char) ))
184 ConsumeToken();
Nate Begemanf2758702009-06-26 06:32:41 +0000185 if (Tok.is(tok::r_paren))
186 ConsumeParen();
187 break;
188 default:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000189 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000190 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000191 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000192
Steve Naroff0f2fe172007-06-01 17:11:19 +0000193 // now parse the list of expressions
194 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000195 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000196 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000197 ArgExprsOk = false;
198 SkipUntil(tok::r_paren);
199 break;
200 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000201 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000202 }
Chris Lattner76c72282007-10-09 17:33:22 +0000203 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000204 break;
205 ConsumeToken(); // Eat the comma, move to the next argument
206 }
207 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000208 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000209 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl511ed552008-11-25 22:21:31 +0000210 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000211 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
212 ArgExprs.size(),
Steve Naroffb8371e12007-06-09 03:39:29 +0000213 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000214 }
Nate Begemanf2758702009-06-26 06:32:41 +0000215 break;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000216 }
217 }
218 } else {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000219 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000220 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000221 }
222 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000223 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000224 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000225 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000226 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
227 SkipUntil(tok::r_paren, false);
228 }
229 if (EndLoc)
230 *EndLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000231 }
232 return CurrAttr;
233}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000234
Eli Friedman06de2b52009-06-08 07:21:15 +0000235/// ParseMicrosoftDeclSpec - Parse an __declspec construct
236///
237/// [MS] decl-specifier:
238/// __declspec ( extended-decl-modifier-seq )
239///
240/// [MS] extended-decl-modifier-seq:
241/// extended-decl-modifier[opt]
242/// extended-decl-modifier extended-decl-modifier-seq
243
Eli Friedman53339e02009-06-08 23:27:34 +0000244AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000245 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000246
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000247 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000248 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
249 "declspec")) {
250 SkipUntil(tok::r_paren, true); // skip until ) or ;
251 return CurrAttr;
252 }
Eli Friedman53339e02009-06-08 23:27:34 +0000253 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000254 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
255 SourceLocation AttrNameLoc = ConsumeToken();
256 if (Tok.is(tok::l_paren)) {
257 ConsumeParen();
258 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
259 // correctly.
John McCalldadc5752010-08-24 06:29:42 +0000260 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedman06de2b52009-06-08 07:21:15 +0000261 if (!ArgExpr.isInvalid()) {
John McCall37ad5512010-08-23 06:44:23 +0000262 Expr *ExprList = ArgExpr.take();
Alexis Hunt96d5c762009-11-21 08:43:09 +0000263 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman06de2b52009-06-08 07:21:15 +0000264 SourceLocation(), &ExprList, 1,
265 CurrAttr, true);
266 }
267 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
268 SkipUntil(tok::r_paren, false);
269 } else {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000270 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
271 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000272 }
273 }
274 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
275 SkipUntil(tok::r_paren, false);
Eli Friedman53339e02009-06-08 23:27:34 +0000276 return CurrAttr;
277}
278
279AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
280 // Treat these like attributes
281 // FIXME: Allow Sema to distinguish between these and real attributes!
282 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000283 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
284 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000285 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
286 SourceLocation AttrNameLoc = ConsumeToken();
287 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
288 // FIXME: Support these properly!
289 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000290 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman53339e02009-06-08 23:27:34 +0000291 SourceLocation(), 0, 0, CurrAttr, true);
292 }
293 return CurrAttr;
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000294}
295
Dawn Perchik335e16b2010-09-03 01:29:35 +0000296AttributeList* Parser::ParseBorlandTypeAttributes(AttributeList *CurrAttr) {
297 // Treat these like attributes
298 while (Tok.is(tok::kw___pascal)) {
299 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
300 SourceLocation AttrNameLoc = ConsumeToken();
301 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
302 SourceLocation(), 0, 0, CurrAttr, true);
303 }
304 return CurrAttr;
305}
306
Chris Lattner53361ac2006-08-10 05:19:57 +0000307/// ParseDeclaration - Parse a full 'declaration', which consists of
308/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000309/// 'Context' should be a Declarator::TheContext value. This returns the
310/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000311///
312/// declaration: [C99 6.7]
313/// block-declaration ->
314/// simple-declaration
315/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000316/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000317/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000318/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000319/// [C++] using-declaration
Sebastian Redlf769df52009-03-24 22:27:57 +0000320/// [C++0x] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000321/// others... [FIXME]
322///
Chris Lattner49836b42009-04-02 04:16:50 +0000323Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000324 SourceLocation &DeclEnd,
325 CXX0XAttributeList Attr) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000326 ParenBraceBracketBalancer BalancerRAIIObj(*this);
327
John McCall48871652010-08-21 09:40:31 +0000328 Decl *SingleDecl = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000329 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000330 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000331 case tok::kw_export:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000332 if (Attr.HasAttr)
333 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
334 << Attr.Range;
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000335 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000336 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000337 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000338 // Could be the start of an inline namespace. Allowed as an ext in C++03.
339 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
Sebastian Redl67667942010-08-27 23:12:46 +0000340 if (Attr.HasAttr)
341 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
342 << Attr.Range;
343 SourceLocation InlineLoc = ConsumeToken();
344 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
345 break;
346 }
347 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000348 case tok::kw_namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000349 if (Attr.HasAttr)
350 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
351 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000352 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000353 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000354 case tok::kw_using:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000355 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000356 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000357 case tok::kw_static_assert:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000358 if (Attr.HasAttr)
359 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
360 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000361 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000362 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000363 default:
Chris Lattner005fc1b2010-04-05 18:18:31 +0000364 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000365 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000366
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000367 // This routine returns a DeclGroup, if the thing we parsed only contains a
368 // single decl, convert it now.
369 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000370}
371
372/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
373/// declaration-specifiers init-declarator-list[opt] ';'
374///[C90/C++]init-declarator-list ';' [TODO]
375/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000376///
377/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000378/// declaration. If it is true, it checks for and eats it.
Chris Lattner32dc41c2009-03-29 17:27:48 +0000379Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000380 SourceLocation &DeclEnd,
Chris Lattner005fc1b2010-04-05 18:18:31 +0000381 AttributeList *Attr,
382 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000383 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000384 ParsingDeclSpec DS(*this);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000385 if (Attr)
386 DS.AddAttributes(Attr);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000387 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
388 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump11289f42009-09-09 15:08:12 +0000389
Chris Lattner0e894622006-08-13 19:58:17 +0000390 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
391 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000392 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000393 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000394 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallb54367d2010-05-21 20:45:30 +0000395 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000396 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000397 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000398 }
Mike Stump11289f42009-09-09 15:08:12 +0000399
Chris Lattner005fc1b2010-04-05 18:18:31 +0000400 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld5a36322009-11-03 19:26:08 +0000401}
Mike Stump11289f42009-09-09 15:08:12 +0000402
John McCalld5a36322009-11-03 19:26:08 +0000403/// ParseDeclGroup - Having concluded that this is either a function
404/// definition or a group of object declarations, actually parse the
405/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000406Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
407 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000408 bool AllowFunctionDefinitions,
409 SourceLocation *DeclEnd) {
410 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000411 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000412 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000413
John McCalld5a36322009-11-03 19:26:08 +0000414 // Bail out if the first declarator didn't seem well-formed.
415 if (!D.hasName() && !D.mayOmitIdentifier()) {
416 // Skip until ; or }.
417 SkipUntil(tok::r_brace, true, true);
418 if (Tok.is(tok::semi))
419 ConsumeToken();
420 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000421 }
Mike Stump11289f42009-09-09 15:08:12 +0000422
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000423 // Check to see if we have a function *definition* which must have a body.
424 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
425 // Look at the next token to make sure that this isn't a function
426 // declaration. We have to check this because __attribute__ might be the
427 // start of a function definition in GCC-extended K&R C.
428 !isDeclarationAfterDeclarator()) {
429
Chris Lattner13901342010-07-11 22:42:07 +0000430 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000431 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
432 Diag(Tok, diag::err_function_declared_typedef);
433
434 // Recover by treating the 'typedef' as spurious.
435 DS.ClearStorageClassSpecs();
436 }
437
John McCall48871652010-08-21 09:40:31 +0000438 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000439 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000440 }
441
442 if (isDeclarationSpecifier()) {
443 // If there is an invalid declaration specifier right after the function
444 // prototype, then we must be in a missing semicolon case where this isn't
445 // actually a body. Just fall through into the code that handles it as a
446 // prototype, and let the top-level code handle the erroneous declspec
447 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000448 } else {
449 Diag(Tok, diag::err_expected_fn_body);
450 SkipUntil(tok::semi);
451 return DeclGroupPtrTy();
452 }
453 }
454
John McCall48871652010-08-21 09:40:31 +0000455 llvm::SmallVector<Decl *, 8> DeclsInGroup;
456 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000457 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000458 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000459 DeclsInGroup.push_back(FirstDecl);
460
461 // If we don't have a comma, it is either the end of the list (a ';') or an
462 // error, bail out.
463 while (Tok.is(tok::comma)) {
464 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000465 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000466
467 // Parse the next declarator.
468 D.clear();
469
470 // Accept attributes in an init-declarator. In the first declarator in a
471 // declaration, these would be part of the declspec. In subsequent
472 // declarators, they become part of the declarator itself, so that they
473 // don't apply to declarators after *this* one. Examples:
474 // short __attribute__((common)) var; -> declspec
475 // short var __attribute__((common)); -> declarator
476 // short x, __attribute__((common)) var; -> declarator
477 if (Tok.is(tok::kw___attribute)) {
478 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000479 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld5a36322009-11-03 19:26:08 +0000480 D.AddAttributes(AttrList, Loc);
481 }
482
483 ParseDeclarator(D);
484
John McCall48871652010-08-21 09:40:31 +0000485 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000486 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000487 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000488 DeclsInGroup.push_back(ThisDecl);
489 }
490
491 if (DeclEnd)
492 *DeclEnd = Tok.getLocation();
493
494 if (Context != Declarator::ForContext &&
495 ExpectAndConsume(tok::semi,
496 Context == Declarator::FileContext
497 ? diag::err_invalid_token_after_toplevel_declarator
498 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000499 // Okay, there was no semicolon and one was expected. If we see a
500 // declaration specifier, just assume it was missing and continue parsing.
501 // Otherwise things are very confused and we skip to recover.
502 if (!isDeclarationSpecifier()) {
503 SkipUntil(tok::r_brace, true, true);
504 if (Tok.is(tok::semi))
505 ConsumeToken();
506 }
John McCalld5a36322009-11-03 19:26:08 +0000507 }
508
Douglas Gregor0be31a22010-07-02 17:43:08 +0000509 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000510 DeclsInGroup.data(),
511 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000512}
513
Douglas Gregor23996282009-05-12 21:31:51 +0000514/// \brief Parse 'declaration' after parsing 'declaration-specifiers
515/// declarator'. This method parses the remainder of the declaration
516/// (including any attributes or initializer, among other things) and
517/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000518///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000519/// init-declarator: [C99 6.7]
520/// declarator
521/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000522/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
523/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000524/// [C++] declarator initializer[opt]
525///
526/// [C++] initializer:
527/// [C++] '=' initializer-clause
528/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000529/// [C++0x] '=' 'default' [TODO]
530/// [C++0x] '=' 'delete'
531///
532/// According to the standard grammar, =default and =delete are function
533/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000534///
John McCall48871652010-08-21 09:40:31 +0000535Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000536 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000537 // If a simple-asm-expr is present, parse it.
538 if (Tok.is(tok::kw_asm)) {
539 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +0000540 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor23996282009-05-12 21:31:51 +0000541 if (AsmLabel.isInvalid()) {
542 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000543 return 0;
Douglas Gregor23996282009-05-12 21:31:51 +0000544 }
Mike Stump11289f42009-09-09 15:08:12 +0000545
Douglas Gregor23996282009-05-12 21:31:51 +0000546 D.setAsmLabel(AsmLabel.release());
547 D.SetRangeEnd(Loc);
548 }
Mike Stump11289f42009-09-09 15:08:12 +0000549
Douglas Gregor23996282009-05-12 21:31:51 +0000550 // If attributes are present, parse them.
551 if (Tok.is(tok::kw___attribute)) {
552 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000553 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor23996282009-05-12 21:31:51 +0000554 D.AddAttributes(AttrList, Loc);
555 }
Mike Stump11289f42009-09-09 15:08:12 +0000556
Douglas Gregor23996282009-05-12 21:31:51 +0000557 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +0000558 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000559 switch (TemplateInfo.Kind) {
560 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000561 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000562 break;
563
564 case ParsedTemplateInfo::Template:
565 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000566 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000567 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000568 TemplateInfo.TemplateParams->data(),
569 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000570 D);
571 break;
572
573 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +0000574 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000575 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000576 TemplateInfo.ExternLoc,
577 TemplateInfo.TemplateLoc,
578 D);
579 if (ThisRes.isInvalid()) {
580 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000581 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000582 }
583
584 ThisDecl = ThisRes.get();
585 break;
586 }
587 }
Mike Stump11289f42009-09-09 15:08:12 +0000588
Douglas Gregor23996282009-05-12 21:31:51 +0000589 // Parse declarator '=' initializer.
590 if (Tok.is(tok::equal)) {
591 ConsumeToken();
592 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
593 SourceLocation DelLoc = ConsumeToken();
594 Actions.SetDeclDeleted(ThisDecl, DelLoc);
595 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000596 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
597 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000598 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000599 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000600
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000601 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000602 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000603 ConsumeCodeCompletionToken();
604 SkipUntil(tok::comma, true, true);
605 return ThisDecl;
606 }
607
John McCalldadc5752010-08-24 06:29:42 +0000608 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000609
John McCall1f4ee7b2009-12-19 09:28:58 +0000610 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000611 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000612 ExitScope();
613 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000614
Douglas Gregor23996282009-05-12 21:31:51 +0000615 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000616 SkipUntil(tok::comma, true, true);
617 Actions.ActOnInitializerError(ThisDecl);
618 } else
John McCallb268a282010-08-23 23:25:46 +0000619 Actions.AddInitializerToDecl(ThisDecl, Init.take());
Douglas Gregor23996282009-05-12 21:31:51 +0000620 }
621 } else if (Tok.is(tok::l_paren)) {
622 // Parse C++ direct initializer: '(' expression-list ')'
623 SourceLocation LParenLoc = ConsumeParen();
624 ExprVector Exprs(Actions);
625 CommaLocsTy CommaLocs;
626
Douglas Gregor613bf102009-12-22 17:47:17 +0000627 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
628 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000629 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000630 }
631
Douglas Gregor23996282009-05-12 21:31:51 +0000632 if (ParseExpressionList(Exprs, CommaLocs)) {
633 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000634
635 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000636 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000637 ExitScope();
638 }
Douglas Gregor23996282009-05-12 21:31:51 +0000639 } else {
640 // Match the ')'.
641 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
642
643 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
644 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000645
646 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000647 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000648 ExitScope();
649 }
650
Douglas Gregor23996282009-05-12 21:31:51 +0000651 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
652 move_arg(Exprs),
Douglas Gregorce5aa332010-09-09 16:33:13 +0000653 RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000654 }
655 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000656 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000657 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
658 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000659 }
660
661 return ThisDecl;
662}
663
Chris Lattner1890ac82006-08-13 01:16:23 +0000664/// ParseSpecifierQualifierList
665/// specifier-qualifier-list:
666/// type-specifier specifier-qualifier-list[opt]
667/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000668/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000669///
670void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
671 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
672 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000673 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattner1890ac82006-08-13 01:16:23 +0000675 // Validate declspec for type-name.
676 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000677 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
678 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000679 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000680
Chris Lattner1b22eed2006-11-28 05:12:07 +0000681 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000682 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000683 if (DS.getStorageClassSpecLoc().isValid())
684 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
685 else
686 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000687 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000688 }
Mike Stump11289f42009-09-09 15:08:12 +0000689
Chris Lattner1b22eed2006-11-28 05:12:07 +0000690 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000691 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000692 if (DS.isInlineSpecified())
693 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
694 if (DS.isVirtualSpecified())
695 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
696 if (DS.isExplicitSpecified())
697 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000698 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000699 }
700}
Chris Lattner53361ac2006-08-10 05:19:57 +0000701
Chris Lattner6cc055a2009-04-12 20:42:31 +0000702/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
703/// specified token is valid after the identifier in a declarator which
704/// immediately follows the declspec. For example, these things are valid:
705///
706/// int x [ 4]; // direct-declarator
707/// int x ( int y); // direct-declarator
708/// int(int x ) // direct-declarator
709/// int x ; // simple-declaration
710/// int x = 17; // init-declarator-list
711/// int x , y; // init-declarator-list
712/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000713/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000714/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000715///
716/// This is not, because 'x' does not immediately follow the declspec (though
717/// ')' happens to be valid anyway).
718/// int (x)
719///
720static bool isValidAfterIdentifierInDeclarator(const Token &T) {
721 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
722 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000723 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000724}
725
Chris Lattner20a0c612009-04-14 21:34:55 +0000726
727/// ParseImplicitInt - This method is called when we have an non-typename
728/// identifier in a declspec (which normally terminates the decl spec) when
729/// the declspec has no type specifier. In this case, the declspec is either
730/// malformed or is "implicit int" (in K&R and C89).
731///
732/// This method handles diagnosing this prettily and returns false if the
733/// declspec is done being processed. If it recovers and thinks there may be
734/// other pieces of declspec after it, it returns true.
735///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000736bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000737 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000738 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000739 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000740
Chris Lattner20a0c612009-04-14 21:34:55 +0000741 SourceLocation Loc = Tok.getLocation();
742 // If we see an identifier that is not a type name, we normally would
743 // parse it as the identifer being declared. However, when a typename
744 // is typo'd or the definition is not included, this will incorrectly
745 // parse the typename as the identifier name and fall over misparsing
746 // later parts of the diagnostic.
747 //
748 // As such, we try to do some look-ahead in cases where this would
749 // otherwise be an "implicit-int" case to see if this is invalid. For
750 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
751 // an identifier with implicit int, we'd get a parse error because the
752 // next token is obviously invalid for a type. Parse these as a case
753 // with an invalid type specifier.
754 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000755
Chris Lattner20a0c612009-04-14 21:34:55 +0000756 // Since we know that this either implicit int (which is rare) or an
757 // error, we'd do lookahead to try to do better recovery.
758 if (isValidAfterIdentifierInDeclarator(NextToken())) {
759 // If this token is valid for implicit int, e.g. "static x = 4", then
760 // we just avoid eating the identifier, so it will be parsed as the
761 // identifier in the declarator.
762 return false;
763 }
Mike Stump11289f42009-09-09 15:08:12 +0000764
Chris Lattner20a0c612009-04-14 21:34:55 +0000765 // Otherwise, if we don't consume this token, we are going to emit an
766 // error anyway. Try to recover from various common problems. Check
767 // to see if this was a reference to a tag name without a tag specified.
768 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000769 //
770 // C++ doesn't need this, and isTagName doesn't take SS.
771 if (SS == 0) {
772 const char *TagName = 0;
773 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000774
Douglas Gregor0be31a22010-07-02 17:43:08 +0000775 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +0000776 default: break;
777 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
778 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
779 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
780 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
781 }
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000783 if (TagName) {
784 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +0000785 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +0000786 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000787
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000788 // Parse this as a tag as if the missing tag were present.
789 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +0000790 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000791 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000792 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000793 return true;
794 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000795 }
Mike Stump11289f42009-09-09 15:08:12 +0000796
Douglas Gregor15e56022009-10-13 23:27:22 +0000797 // This is almost certainly an invalid type name. Let the action emit a
798 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +0000799 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +0000800 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +0000801 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +0000802 // The action emitted a diagnostic, so we don't have to.
803 if (T) {
804 // The action has suggested that the type T could be used. Set that as
805 // the type in the declaration specifiers, consume the would-be type
806 // name token, and we're done.
807 const char *PrevSpec;
808 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +0000809 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +0000810 DS.SetRangeEnd(Tok.getLocation());
811 ConsumeToken();
812
813 // There may be other declaration specifiers after this.
814 return true;
815 }
816
817 // Fall through; the action had no suggestion for us.
818 } else {
819 // The action did not emit a diagnostic, so emit one now.
820 SourceRange R;
821 if (SS) R = SS->getRange();
822 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
823 }
Mike Stump11289f42009-09-09 15:08:12 +0000824
Douglas Gregor15e56022009-10-13 23:27:22 +0000825 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000826 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000827 unsigned DiagID;
828 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000829 DS.SetRangeEnd(Tok.getLocation());
830 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000831
Chris Lattner20a0c612009-04-14 21:34:55 +0000832 // TODO: Could inject an invalid typedef decl in an enclosing scope to
833 // avoid rippling error messages on subsequent uses of the same type,
834 // could be useful if #include was forgotten.
835 return false;
836}
837
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000838/// \brief Determine the declaration specifier context from the declarator
839/// context.
840///
841/// \param Context the declarator context, which is one of the
842/// Declarator::TheContext enumerator values.
843Parser::DeclSpecContext
844Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
845 if (Context == Declarator::MemberContext)
846 return DSC_class;
847 if (Context == Declarator::FileContext)
848 return DSC_top_level;
849 return DSC_normal;
850}
851
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000852/// ParseDeclarationSpecifiers
853/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000854/// storage-class-specifier declaration-specifiers[opt]
855/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000856/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000857/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000858///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000859/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000860/// 'typedef'
861/// 'extern'
862/// 'static'
863/// 'auto'
864/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000865/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000866/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000867/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000868/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000869/// [C++] 'virtual'
870/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000871/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000872/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000873
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000874///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000875void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000876 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000877 AccessSpecifier AS,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000878 DeclSpecContext DSContext) {
Chris Lattner2e232092008-03-13 06:29:04 +0000879 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000880 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000881 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000882 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000883 unsigned DiagID = 0;
884
Chris Lattner4d8f8732006-11-28 05:05:08 +0000885 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000886
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000887 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000888 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000889 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000890 // If this is not a declaration specifier token, we're done reading decl
891 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000892 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000893 return;
Mike Stump11289f42009-09-09 15:08:12 +0000894
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000895 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +0000896 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000897 if (DS.hasTypeSpecifier()) {
898 bool AllowNonIdentifiers
899 = (getCurScope()->getFlags() & (Scope::ControlScope |
900 Scope::BlockScope |
901 Scope::TemplateParamScope |
902 Scope::FunctionPrototypeScope |
903 Scope::AtCatchScope)) == 0;
904 bool AllowNestedNameSpecifiers
905 = DSContext == DSC_top_level ||
906 (DSContext == DSC_class && DS.isFriendSpecified());
907
Douglas Gregorbfcea8b2010-09-16 15:14:18 +0000908 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
909 AllowNonIdentifiers,
910 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000911 ConsumeCodeCompletionToken();
912 return;
913 }
914
915 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +0000916 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
917 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000918 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +0000919 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000920 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +0000921 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000922
923 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
924 ConsumeCodeCompletionToken();
925 return;
926 }
927
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000928 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +0000929 // C++ scope specifier. Annotate and loop, or bail out on error.
930 if (TryAnnotateCXXScopeToken(true)) {
931 if (!DS.hasTypeSpecifier())
932 DS.SetTypeSpecError();
933 goto DoneWithDeclSpec;
934 }
John McCall8bc2a702010-03-01 18:20:46 +0000935 if (Tok.is(tok::coloncolon)) // ::new or ::delete
936 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +0000937 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000938
939 case tok::annot_cxxscope: {
940 if (DS.hasTypeSpecifier())
941 goto DoneWithDeclSpec;
942
John McCall9dab4e62009-12-12 11:40:51 +0000943 CXXScopeSpec SS;
John McCall37ad5512010-08-23 06:44:23 +0000944 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCall9dab4e62009-12-12 11:40:51 +0000945 SS.setRange(Tok.getAnnotationRange());
946
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000947 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000948 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000949 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000950 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000951 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000952 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000953
954 // C++ [class.qual]p2:
955 // In a lookup in which the constructor is an acceptable lookup
956 // result and the nested-name-specifier nominates a class C:
957 //
958 // - if the name specified after the
959 // nested-name-specifier, when looked up in C, is the
960 // injected-class-name of C (Clause 9), or
961 //
962 // - if the name specified after the nested-name-specifier
963 // is the same as the identifier or the
964 // simple-template-id's template-name in the last
965 // component of the nested-name-specifier,
966 //
967 // the name is instead considered to name the constructor of
968 // class C.
969 //
970 // Thus, if the template-name is actually the constructor
971 // name, then the code is ill-formed; this interpretation is
972 // reinforced by the NAD status of core issue 635.
973 TemplateIdAnnotation *TemplateId
974 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +0000975 if ((DSContext == DSC_top_level ||
976 (DSContext == DSC_class && DS.isFriendSpecified())) &&
977 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000978 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000979 if (isConstructorDeclarator()) {
980 // The user meant this to be an out-of-line constructor
981 // definition, but template arguments are not allowed
982 // there. Just allow this as a constructor; we'll
983 // complain about it later.
984 goto DoneWithDeclSpec;
985 }
986
987 // The user meant this to name a type, but it actually names
988 // a constructor with some extraneous template
989 // arguments. Complain, then parse it as a type as the user
990 // intended.
991 Diag(TemplateId->TemplateNameLoc,
992 diag::err_out_of_line_template_id_names_constructor)
993 << TemplateId->Name;
994 }
995
John McCall9dab4e62009-12-12 11:40:51 +0000996 DS.getTypeSpecScope() = SS;
997 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +0000998 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000999 "ParseOptionalCXXScopeSpecifier not working");
1000 AnnotateTemplateIdTokenAsType(&SS);
1001 continue;
1002 }
1003
Douglas Gregorc5790df2009-09-28 07:26:33 +00001004 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001005 DS.getTypeSpecScope() = SS;
1006 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001007 if (Tok.getAnnotationValue()) {
1008 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregorc5790df2009-09-28 07:26:33 +00001009 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
John McCallba7bf592010-08-24 05:47:05 +00001010 PrevSpec, DiagID, T);
1011 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001012 else
1013 DS.SetTypeSpecError();
1014 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1015 ConsumeToken(); // The typename
1016 }
1017
Douglas Gregor167fa622009-03-25 15:40:00 +00001018 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001019 goto DoneWithDeclSpec;
1020
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001021 // If we're in a context where the identifier could be a class name,
1022 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001023 if ((DSContext == DSC_top_level ||
1024 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001025 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001026 &SS)) {
1027 if (isConstructorDeclarator())
1028 goto DoneWithDeclSpec;
1029
1030 // As noted in C++ [class.qual]p2 (cited above), when the name
1031 // of the class is qualified in a context where it could name
1032 // a constructor, its a constructor name. However, we've
1033 // looked at the declarator, and the user probably meant this
1034 // to be a type. Complain that it isn't supposed to be treated
1035 // as a type, then proceed to parse it as a type.
1036 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1037 << Next.getIdentifierInfo();
1038 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001039
John McCallba7bf592010-08-24 05:47:05 +00001040 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1041 Next.getLocation(),
1042 getCurScope(), &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001043
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001044 // If the referenced identifier is not a type, then this declspec is
1045 // erroneous: We already checked about that it has no type specifier, and
1046 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001047 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001048 if (TypeRep == 0) {
1049 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001050 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001051 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001052 }
Mike Stump11289f42009-09-09 15:08:12 +00001053
John McCall9dab4e62009-12-12 11:40:51 +00001054 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001055 ConsumeToken(); // The C++ scope.
1056
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001057 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001058 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001059 if (isInvalid)
1060 break;
Mike Stump11289f42009-09-09 15:08:12 +00001061
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001062 DS.SetRangeEnd(Tok.getLocation());
1063 ConsumeToken(); // The typename.
1064
1065 continue;
1066 }
Mike Stump11289f42009-09-09 15:08:12 +00001067
Chris Lattnere387d9e2009-01-21 19:48:37 +00001068 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001069 if (Tok.getAnnotationValue()) {
1070 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001071 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001072 DiagID, T);
1073 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001074 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001075
1076 if (isInvalid)
1077 break;
1078
Chris Lattnere387d9e2009-01-21 19:48:37 +00001079 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1080 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001081
Chris Lattnere387d9e2009-01-21 19:48:37 +00001082 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1083 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1084 // Objective-C interface. If we don't have Objective-C or a '<', this is
1085 // just a normal reference to a typedef name.
1086 if (!Tok.is(tok::less) || !getLang().ObjC1)
1087 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001088
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001089 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001090 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001091 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1092 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1093 LAngleLoc, EndProtoLoc);
1094 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1095 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001096
Chris Lattnere387d9e2009-01-21 19:48:37 +00001097 DS.SetRangeEnd(EndProtoLoc);
1098 continue;
1099 }
Mike Stump11289f42009-09-09 15:08:12 +00001100
Chris Lattner16fac4f2008-07-26 01:18:38 +00001101 // typedef-name
1102 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001103 // In C++, check to see if this is a scope specifier like foo::bar::, if
1104 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001105 if (getLang().CPlusPlus) {
1106 if (TryAnnotateCXXScopeToken(true)) {
1107 if (!DS.hasTypeSpecifier())
1108 DS.SetTypeSpecError();
1109 goto DoneWithDeclSpec;
1110 }
1111 if (!Tok.is(tok::identifier))
1112 continue;
1113 }
Mike Stump11289f42009-09-09 15:08:12 +00001114
Chris Lattner16fac4f2008-07-26 01:18:38 +00001115 // This identifier can only be a typedef name if we haven't already seen
1116 // a type-specifier. Without this check we misparse:
1117 // typedef int X; struct Y { short X; }; as 'short int'.
1118 if (DS.hasTypeSpecifier())
1119 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001120
John Thompson22334602010-02-05 00:12:22 +00001121 // Check for need to substitute AltiVec keyword tokens.
1122 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1123 break;
1124
Chris Lattner16fac4f2008-07-26 01:18:38 +00001125 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001126 ParsedType TypeRep =
1127 Actions.getTypeName(*Tok.getIdentifierInfo(),
1128 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001129
Chris Lattner6cc055a2009-04-12 20:42:31 +00001130 // If this is not a typedef name, don't parse it as part of the declspec,
1131 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001132 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001133 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001134 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001135 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001136
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001137 // If we're in a context where the identifier could be a class name,
1138 // check whether this is a constructor declaration.
1139 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001140 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001141 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001142 goto DoneWithDeclSpec;
1143
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001144 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001145 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001146 if (isInvalid)
1147 break;
Mike Stump11289f42009-09-09 15:08:12 +00001148
Chris Lattner16fac4f2008-07-26 01:18:38 +00001149 DS.SetRangeEnd(Tok.getLocation());
1150 ConsumeToken(); // The identifier
1151
1152 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1153 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1154 // Objective-C interface. If we don't have Objective-C or a '<', this is
1155 // just a normal reference to a typedef name.
1156 if (!Tok.is(tok::less) || !getLang().ObjC1)
1157 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001158
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001159 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001160 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001161 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1162 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1163 LAngleLoc, EndProtoLoc);
1164 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1165 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001166
Chris Lattner16fac4f2008-07-26 01:18:38 +00001167 DS.SetRangeEnd(EndProtoLoc);
1168
Steve Naroffcd5e7822008-09-22 10:28:57 +00001169 // Need to support trailing type qualifiers (e.g. "id<p> const").
1170 // If a type specifier follows, it will be diagnosed elsewhere.
1171 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001172 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001173
1174 // type-name
1175 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001176 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001177 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001178 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001179 // This template-id does not refer to a type name, so we're
1180 // done with the type-specifiers.
1181 goto DoneWithDeclSpec;
1182 }
1183
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001184 // If we're in a context where the template-id could be a
1185 // constructor name or specialization, check whether this is a
1186 // constructor declaration.
1187 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001188 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001189 isConstructorDeclarator())
1190 goto DoneWithDeclSpec;
1191
Douglas Gregor7f741122009-02-25 19:37:18 +00001192 // Turn the template-id annotation token into a type annotation
1193 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001194 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001195 continue;
1196 }
1197
Chris Lattnere37e2332006-08-15 04:50:22 +00001198 // GNU attributes support.
1199 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001200 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001201 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001202
1203 // Microsoft declspec support.
1204 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +00001205 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001206 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001207
Steve Naroff44ac7772008-12-25 14:16:32 +00001208 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001209 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001210 // FIXME: Add handling here!
1211 break;
1212
1213 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001214 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001215 case tok::kw___cdecl:
1216 case tok::kw___stdcall:
1217 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001218 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001219 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1220 continue;
1221
Dawn Perchik335e16b2010-09-03 01:29:35 +00001222 // Borland single token adornments.
1223 case tok::kw___pascal:
1224 DS.AddAttributes(ParseBorlandTypeAttributes());
1225 continue;
1226
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001227 // storage-class-specifier
1228 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001229 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1230 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001231 break;
1232 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001233 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001234 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001235 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1236 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001237 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001238 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001239 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001240 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001241 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001242 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001243 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001244 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001245 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1246 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001247 break;
1248 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001249 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001250 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1251 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001252 else
John McCall49bfce42009-08-03 20:12:06 +00001253 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1254 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001255 break;
1256 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001257 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1258 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001259 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001260 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001261 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1262 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001263 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001264 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001265 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001266 break;
Mike Stump11289f42009-09-09 15:08:12 +00001267
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001268 // function-specifier
1269 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001270 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001271 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001272 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001273 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001274 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001275 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001276 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001277 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001278
Anders Carlssoncd8db412009-05-06 04:46:28 +00001279 // friend
1280 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001281 if (DSContext == DSC_class)
1282 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1283 else {
1284 PrevSpec = ""; // not actually used by the diagnostic
1285 DiagID = diag::err_friend_invalid_in_context;
1286 isInvalid = true;
1287 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001288 break;
Mike Stump11289f42009-09-09 15:08:12 +00001289
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001290 // constexpr
1291 case tok::kw_constexpr:
1292 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1293 break;
1294
Chris Lattnere387d9e2009-01-21 19:48:37 +00001295 // type-specifier
1296 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001297 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1298 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001299 break;
1300 case tok::kw_long:
1301 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001302 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1303 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001304 else
John McCall49bfce42009-08-03 20:12:06 +00001305 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1306 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001307 break;
1308 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001309 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1310 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001311 break;
1312 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001313 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1314 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001315 break;
1316 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001317 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1318 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001319 break;
1320 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001321 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1322 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001323 break;
1324 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001325 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1326 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001327 break;
1328 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001329 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1330 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001331 break;
1332 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001333 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1334 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001335 break;
1336 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001337 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1338 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001339 break;
1340 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1342 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001343 break;
1344 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001345 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1346 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001347 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001348 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001349 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1350 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001351 break;
1352 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001353 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1354 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001355 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001356 case tok::kw_bool:
1357 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001358 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1359 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001360 break;
1361 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001362 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1363 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001364 break;
1365 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001366 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1367 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001368 break;
1369 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001370 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1371 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001372 break;
John Thompson22334602010-02-05 00:12:22 +00001373 case tok::kw___vector:
1374 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1375 break;
1376 case tok::kw___pixel:
1377 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1378 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001379
1380 // class-specifier:
1381 case tok::kw_class:
1382 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001383 case tok::kw_union: {
1384 tok::TokenKind Kind = Tok.getKind();
1385 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001386 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001387 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001388 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001389
1390 // enum-specifier:
1391 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001392 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001393 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001394 continue;
1395
1396 // cv-qualifier:
1397 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001398 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1399 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001400 break;
1401 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001402 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1403 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001404 break;
1405 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001406 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1407 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001408 break;
1409
Douglas Gregor333489b2009-03-27 23:10:48 +00001410 // C++ typename-specifier:
1411 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001412 if (TryAnnotateTypeOrScopeToken()) {
1413 DS.SetTypeSpecError();
1414 goto DoneWithDeclSpec;
1415 }
1416 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001417 continue;
1418 break;
1419
Chris Lattnere387d9e2009-01-21 19:48:37 +00001420 // GNU typeof support.
1421 case tok::kw_typeof:
1422 ParseTypeofSpecifier(DS);
1423 continue;
1424
Anders Carlsson74948d02009-06-24 17:47:40 +00001425 case tok::kw_decltype:
1426 ParseDecltypeSpecifier(DS);
1427 continue;
1428
Steve Naroffcfdf6162008-06-05 00:02:44 +00001429 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001430 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001431 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1432 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001433 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001434 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001435
Chris Lattner0974b232008-07-26 00:20:22 +00001436 {
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001437 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001438 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001439 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1440 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1441 LAngleLoc, EndProtoLoc);
1442 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1443 ProtocolLocs.data(), LAngleLoc);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001444 DS.SetRangeEnd(EndProtoLoc);
1445
Chris Lattner6d29c102008-11-18 07:48:38 +00001446 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregora771f462010-03-31 17:46:05 +00001447 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001448 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001449 // Need to support trailing type qualifiers (e.g. "id<p> const").
1450 // If a type specifier follows, it will be diagnosed elsewhere.
1451 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001452 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001453 }
John McCall49bfce42009-08-03 20:12:06 +00001454 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001455 if (isInvalid) {
1456 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001457 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001458
1459 if (DiagID == diag::ext_duplicate_declspec)
1460 Diag(Tok, DiagID)
1461 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1462 else
1463 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001464 }
Chris Lattner2e232092008-03-13 06:29:04 +00001465 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001466 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001467 }
1468}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001469
Chris Lattnera448d752009-01-06 06:59:53 +00001470/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001471/// primarily follow the C++ grammar with additions for C99 and GNU,
1472/// which together subsume the C grammar. Note that the C++
1473/// type-specifier also includes the C type-qualifier (for const,
1474/// volatile, and C99 restrict). Returns true if a type-specifier was
1475/// found (and parsed), false otherwise.
1476///
1477/// type-specifier: [C++ 7.1.5]
1478/// simple-type-specifier
1479/// class-specifier
1480/// enum-specifier
1481/// elaborated-type-specifier [TODO]
1482/// cv-qualifier
1483///
1484/// cv-qualifier: [C++ 7.1.5.1]
1485/// 'const'
1486/// 'volatile'
1487/// [C99] 'restrict'
1488///
1489/// simple-type-specifier: [ C++ 7.1.5.2]
1490/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1491/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1492/// 'char'
1493/// 'wchar_t'
1494/// 'bool'
1495/// 'short'
1496/// 'int'
1497/// 'long'
1498/// 'signed'
1499/// 'unsigned'
1500/// 'float'
1501/// 'double'
1502/// 'void'
1503/// [C99] '_Bool'
1504/// [C99] '_Complex'
1505/// [C99] '_Imaginary' // Removed in TC2?
1506/// [GNU] '_Decimal32'
1507/// [GNU] '_Decimal64'
1508/// [GNU] '_Decimal128'
1509/// [GNU] typeof-specifier
1510/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1511/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001512/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001513/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001514bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001515 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001516 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001517 const ParsedTemplateInfo &TemplateInfo,
1518 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001519 SourceLocation Loc = Tok.getLocation();
1520
1521 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001522 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001523 // If we already have a type specifier, this identifier is not a type.
1524 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1525 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1526 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1527 return false;
John Thompson22334602010-02-05 00:12:22 +00001528 // Check for need to substitute AltiVec keyword tokens.
1529 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1530 break;
1531 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001532 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001533 // Annotate typenames and C++ scope specifiers. If we get one, just
1534 // recurse to handle whatever we get.
1535 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001536 return true;
1537 if (Tok.is(tok::identifier))
1538 return false;
1539 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1540 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001541 case tok::coloncolon: // ::foo::bar
1542 if (NextToken().is(tok::kw_new) || // ::new
1543 NextToken().is(tok::kw_delete)) // ::delete
1544 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001545
Chris Lattner020bab92009-01-04 23:41:41 +00001546 // Annotate typenames and C++ scope specifiers. If we get one, just
1547 // recurse to handle whatever we get.
1548 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001549 return true;
1550 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1551 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001552
Douglas Gregor450c75a2008-11-07 15:42:26 +00001553 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001554 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001555 if (ParsedType T = getTypeAnnotation(Tok)) {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001556 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001557 DiagID, T);
1558 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001559 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001560 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1561 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001562
Douglas Gregor450c75a2008-11-07 15:42:26 +00001563 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1564 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1565 // Objective-C interface. If we don't have Objective-C or a '<', this is
1566 // just a normal reference to a typedef name.
1567 if (!Tok.is(tok::less) || !getLang().ObjC1)
1568 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001569
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001570 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +00001571 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001572 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1573 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1574 LAngleLoc, EndProtoLoc);
1575 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1576 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001577
Douglas Gregor450c75a2008-11-07 15:42:26 +00001578 DS.SetRangeEnd(EndProtoLoc);
1579 return true;
1580 }
1581
1582 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001583 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001584 break;
1585 case tok::kw_long:
1586 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001587 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1588 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001589 else
John McCall49bfce42009-08-03 20:12:06 +00001590 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1591 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001592 break;
1593 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001594 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001595 break;
1596 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001597 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1598 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001599 break;
1600 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001601 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1602 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001603 break;
1604 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001605 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1606 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001607 break;
1608 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001609 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001610 break;
1611 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001612 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001613 break;
1614 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001615 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001616 break;
1617 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001618 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001619 break;
1620 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001621 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001622 break;
1623 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001624 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001625 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001626 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001627 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001628 break;
1629 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001630 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001631 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001632 case tok::kw_bool:
1633 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001634 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001635 break;
1636 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001637 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1638 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001639 break;
1640 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001641 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1642 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001643 break;
1644 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001645 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1646 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001647 break;
John Thompson22334602010-02-05 00:12:22 +00001648 case tok::kw___vector:
1649 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1650 break;
1651 case tok::kw___pixel:
1652 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1653 break;
1654
Douglas Gregor450c75a2008-11-07 15:42:26 +00001655 // class-specifier:
1656 case tok::kw_class:
1657 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001658 case tok::kw_union: {
1659 tok::TokenKind Kind = Tok.getKind();
1660 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001661 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1662 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001663 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001664 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001665
1666 // enum-specifier:
1667 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001668 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001669 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001670 return true;
1671
1672 // cv-qualifier:
1673 case tok::kw_const:
1674 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001675 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001676 break;
1677 case tok::kw_volatile:
1678 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001679 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001680 break;
1681 case tok::kw_restrict:
1682 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001683 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001684 break;
1685
1686 // GNU typeof support.
1687 case tok::kw_typeof:
1688 ParseTypeofSpecifier(DS);
1689 return true;
1690
Anders Carlsson74948d02009-06-24 17:47:40 +00001691 // C++0x decltype support.
1692 case tok::kw_decltype:
1693 ParseDecltypeSpecifier(DS);
1694 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001695
Anders Carlssonbae27372009-06-26 23:44:14 +00001696 // C++0x auto support.
1697 case tok::kw_auto:
1698 if (!getLang().CPlusPlus0x)
1699 return false;
1700
John McCall49bfce42009-08-03 20:12:06 +00001701 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001702 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001703
Eli Friedman53339e02009-06-08 23:27:34 +00001704 case tok::kw___ptr64:
1705 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001706 case tok::kw___cdecl:
1707 case tok::kw___stdcall:
1708 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001709 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001710 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001711 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001712
Dawn Perchik335e16b2010-09-03 01:29:35 +00001713 case tok::kw___pascal:
1714 DS.AddAttributes(ParseBorlandTypeAttributes());
1715 return true;
1716
Douglas Gregor450c75a2008-11-07 15:42:26 +00001717 default:
1718 // Not a type-specifier; do nothing.
1719 return false;
1720 }
1721
1722 // If the specifier combination wasn't legal, issue a diagnostic.
1723 if (isInvalid) {
1724 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001725 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001726 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001727 }
1728 DS.SetRangeEnd(Tok.getLocation());
1729 ConsumeToken(); // whatever we parsed above.
1730 return true;
1731}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001732
Chris Lattner70ae4912007-10-29 04:42:53 +00001733/// ParseStructDeclaration - Parse a struct declaration without the terminating
1734/// semicolon.
1735///
Chris Lattner90a26b02007-01-23 04:38:16 +00001736/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001737/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001738/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001739/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001740/// struct-declarator-list:
1741/// struct-declarator
1742/// struct-declarator-list ',' struct-declarator
1743/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1744/// struct-declarator:
1745/// declarator
1746/// [GNU] declarator attributes[opt]
1747/// declarator[opt] ':' constant-expression
1748/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1749///
Chris Lattnera12405b2008-04-10 06:46:29 +00001750void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001751ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001752 if (Tok.is(tok::kw___extension__)) {
1753 // __extension__ silences extension warnings in the subexpression.
1754 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001755 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001756 return ParseStructDeclaration(DS, Fields);
1757 }
Mike Stump11289f42009-09-09 15:08:12 +00001758
Steve Naroff97170802007-08-20 22:28:22 +00001759 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001760 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001761 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001762
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001763 // If there are no declarators, this is a free-standing declaration
1764 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001765 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001766 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001767 return;
1768 }
1769
1770 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001771 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001772 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001773 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001774 FieldDeclarator DeclaratorInfo(DS);
1775
1776 // Attributes are only allowed here on successive declarators.
1777 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1778 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001779 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallcfefb6d2009-11-03 02:38:08 +00001780 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1781 }
Mike Stump11289f42009-09-09 15:08:12 +00001782
Steve Naroff97170802007-08-20 22:28:22 +00001783 /// struct-declarator: declarator
1784 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001785 if (Tok.isNot(tok::colon)) {
1786 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1787 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001788 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001789 }
Mike Stump11289f42009-09-09 15:08:12 +00001790
Chris Lattner76c72282007-10-09 17:33:22 +00001791 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001792 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001793 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001794 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001795 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001796 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001797 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001798 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001799
Steve Naroff97170802007-08-20 22:28:22 +00001800 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001801 if (Tok.is(tok::kw___attribute)) {
1802 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001803 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001804 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1805 }
1806
John McCallcfefb6d2009-11-03 02:38:08 +00001807 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00001808 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00001809 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001810
Steve Naroff97170802007-08-20 22:28:22 +00001811 // If we don't have a comma, it is either the end of the list (a ';')
1812 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001813 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001814 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001815
Steve Naroff97170802007-08-20 22:28:22 +00001816 // Consume the comma.
1817 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001818
John McCallcfefb6d2009-11-03 02:38:08 +00001819 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001820 }
Steve Naroff97170802007-08-20 22:28:22 +00001821}
1822
1823/// ParseStructUnionBody
1824/// struct-contents:
1825/// struct-declaration-list
1826/// [EXT] empty
1827/// [GNU] "struct-declaration-list" without terminatoring ';'
1828/// struct-declaration-list:
1829/// struct-declaration
1830/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001831/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001832///
Chris Lattner1300fb92007-01-23 23:42:53 +00001833void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001834 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00001835 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1836 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001837
Chris Lattner90a26b02007-01-23 04:38:16 +00001838 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001839
Douglas Gregor658b9552009-01-09 22:42:13 +00001840 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001841 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001842
Chris Lattner7b9ace62007-01-23 20:11:08 +00001843 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1844 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001845 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00001846 Diag(Tok, diag::ext_empty_struct_union)
1847 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001848
John McCall48871652010-08-21 09:40:31 +00001849 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001850
Chris Lattner7b9ace62007-01-23 20:11:08 +00001851 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001852 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001853 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001854
Chris Lattner736ed5d2007-06-09 05:59:07 +00001855 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001856 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001857 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00001858 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00001859 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001860 ConsumeToken();
1861 continue;
1862 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001863
1864 // Parse all the comma separated declarators.
1865 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001866
John McCallcfefb6d2009-11-03 02:38:08 +00001867 if (!Tok.is(tok::at)) {
1868 struct CFieldCallback : FieldCallback {
1869 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001870 Decl *TagDecl;
1871 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001872
John McCall48871652010-08-21 09:40:31 +00001873 CFieldCallback(Parser &P, Decl *TagDecl,
1874 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001875 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1876
John McCall48871652010-08-21 09:40:31 +00001877 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001878 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00001879 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00001880 FD.D.getDeclSpec().getSourceRange().getBegin(),
1881 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001882 FieldDecls.push_back(Field);
1883 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001884 }
John McCallcfefb6d2009-11-03 02:38:08 +00001885 } Callback(*this, TagDecl, FieldDecls);
1886
1887 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001888 } else { // Handle @defs
1889 ConsumeToken();
1890 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1891 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001892 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001893 continue;
1894 }
1895 ConsumeToken();
1896 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1897 if (!Tok.is(tok::identifier)) {
1898 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001899 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001900 continue;
1901 }
John McCall48871652010-08-21 09:40:31 +00001902 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001903 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001904 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001905 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1906 ConsumeToken();
1907 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001908 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001909
Chris Lattner76c72282007-10-09 17:33:22 +00001910 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001911 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001912 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001913 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001914 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001915 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001916 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1917 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001918 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001919 // If we stopped at a ';', eat it.
1920 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001921 }
1922 }
Mike Stump11289f42009-09-09 15:08:12 +00001923
Steve Naroff33a1e802007-10-29 21:38:07 +00001924 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001925
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001926 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner90a26b02007-01-23 04:38:16 +00001927 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001928 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001929 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar15619c72008-10-03 02:03:53 +00001930
Douglas Gregor0be31a22010-07-02 17:43:08 +00001931 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001932 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001933 LBraceLoc, RBraceLoc,
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001934 AttrList.get());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001935 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001936 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001937}
1938
1939
Chris Lattner3b561a32006-08-13 00:12:11 +00001940/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001941/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001942/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001943///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001944/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1945/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001946/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001947/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001948///
1949/// [C++] elaborated-type-specifier:
1950/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1951///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001952void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001953 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001954 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001955 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001956 if (Tok.is(tok::code_completion)) {
1957 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001958 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001959 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001960 }
1961
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001962 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001963 // If attributes exist after tag, parse them.
1964 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00001965 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001966
Abramo Bagnarad7548482010-05-19 21:37:53 +00001967 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00001968 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00001969 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00001970 return;
1971
1972 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001973 Diag(Tok, diag::err_expected_ident);
1974 if (Tok.isNot(tok::l_brace)) {
1975 // Has no name and is not a definition.
1976 // Skip the rest of this declarator, up until the comma or semicolon.
1977 SkipUntil(tok::comma, true);
1978 return;
1979 }
1980 }
1981 }
Mike Stump11289f42009-09-09 15:08:12 +00001982
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001983 // Must have either 'enum name' or 'enum {...}'.
1984 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1985 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001986
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001987 // Skip the rest of this declarator, up until the comma or semicolon.
1988 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001989 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001990 }
Mike Stump11289f42009-09-09 15:08:12 +00001991
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001992 // If an identifier is present, consume and remember it.
1993 IdentifierInfo *Name = 0;
1994 SourceLocation NameLoc;
1995 if (Tok.is(tok::identifier)) {
1996 Name = Tok.getIdentifierInfo();
1997 NameLoc = ConsumeToken();
1998 }
Mike Stump11289f42009-09-09 15:08:12 +00001999
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002000 // There are three options here. If we have 'enum foo;', then this is a
2001 // forward declaration. If we have 'enum foo {...' then this is a
2002 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2003 //
2004 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2005 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2006 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2007 //
John McCallfaf5fb42010-08-26 23:41:50 +00002008 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002009 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002010 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002011 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002012 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002013 else
John McCallfaf5fb42010-08-26 23:41:50 +00002014 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002015
2016 // enums cannot be templates, although they can be referenced from a
2017 // template.
2018 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002019 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002020 Diag(Tok, diag::err_enum_template);
2021
2022 // Skip the rest of this declarator, up until the comma or semicolon.
2023 SkipUntil(tok::comma, true);
2024 return;
2025 }
2026
Douglas Gregord6ab8742009-05-28 23:31:59 +00002027 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002028 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002029 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2030 const char *PrevSpec = 0;
2031 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002032 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
2033 StartLoc, SS, Name, NameLoc, Attr.get(),
2034 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002035 MultiTemplateParamsArg(Actions),
John McCall48871652010-08-21 09:40:31 +00002036 Owned, IsDependent);
Douglas Gregorba41d012010-04-24 16:38:41 +00002037 if (IsDependent) {
2038 // This enum has a dependent nested-name-specifier. Handle it as a
2039 // dependent tag.
2040 if (!Name) {
2041 DS.SetTypeSpecError();
2042 Diag(Tok, diag::err_expected_type_name_after_typename);
2043 return;
2044 }
2045
Douglas Gregor0be31a22010-07-02 17:43:08 +00002046 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002047 TUK, SS, Name, StartLoc,
2048 NameLoc);
2049 if (Type.isInvalid()) {
2050 DS.SetTypeSpecError();
2051 return;
2052 }
2053
2054 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallba7bf592010-08-24 05:47:05 +00002055 Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002056 Diag(StartLoc, DiagID) << PrevSpec;
2057
2058 return;
2059 }
Mike Stump11289f42009-09-09 15:08:12 +00002060
John McCall48871652010-08-21 09:40:31 +00002061 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002062 // The action failed to produce an enumeration tag. If this is a
2063 // definition, consume the entire definition.
2064 if (Tok.is(tok::l_brace)) {
2065 ConsumeBrace();
2066 SkipUntil(tok::r_brace);
2067 }
2068
2069 DS.SetTypeSpecError();
2070 return;
2071 }
2072
Chris Lattner76c72282007-10-09 17:33:22 +00002073 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002074 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002075
John McCallba7bf592010-08-24 05:47:05 +00002076 // FIXME: The DeclSpec should keep the locations of both the keyword
2077 // and the name (if there is one).
Douglas Gregor72100632010-01-25 16:33:23 +00002078 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCall48871652010-08-21 09:40:31 +00002079 TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002080 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002081}
2082
Chris Lattnerc1915e22007-01-25 07:29:02 +00002083/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2084/// enumerator-list:
2085/// enumerator
2086/// enumerator-list ',' enumerator
2087/// enumerator:
2088/// enumeration-constant
2089/// enumeration-constant '=' constant-expression
2090/// enumeration-constant:
2091/// identifier
2092///
John McCall48871652010-08-21 09:40:31 +00002093void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002094 // Enter the scope of the enum body and start the definition.
2095 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002096 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002097
Chris Lattnerc1915e22007-01-25 07:29:02 +00002098 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002099
Chris Lattner37256fb2007-08-27 17:24:30 +00002100 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002101 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002102 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002103
John McCall48871652010-08-21 09:40:31 +00002104 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002105
John McCall48871652010-08-21 09:40:31 +00002106 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002107
Chris Lattnerc1915e22007-01-25 07:29:02 +00002108 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002109 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002110 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2111 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002112
Chris Lattnerc1915e22007-01-25 07:29:02 +00002113 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002114 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002115 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002116 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002117 AssignedVal = ParseConstantExpression();
2118 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002119 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002120 }
Mike Stump11289f42009-09-09 15:08:12 +00002121
Chris Lattnerc1915e22007-01-25 07:29:02 +00002122 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002123 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2124 LastEnumConstDecl,
2125 IdentLoc, Ident,
2126 EqualLoc,
2127 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002128 EnumConstantDecls.push_back(EnumConstDecl);
2129 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002130
Douglas Gregorce66d022010-09-07 14:51:08 +00002131 if (Tok.is(tok::identifier)) {
2132 // We're missing a comma between enumerators.
2133 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2134 Diag(Loc, diag::err_enumerator_list_missing_comma)
2135 << FixItHint::CreateInsertion(Loc, ", ");
2136 continue;
2137 }
2138
Chris Lattner76c72282007-10-09 17:33:22 +00002139 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002140 break;
2141 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002142
2143 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002144 !(getLang().C99 || getLang().CPlusPlus0x))
2145 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2146 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002147 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002148 }
Mike Stump11289f42009-09-09 15:08:12 +00002149
Chris Lattnerc1915e22007-01-25 07:29:02 +00002150 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002151 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002152
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002153 llvm::OwningPtr<AttributeList> Attr;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002154 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00002155 if (Tok.is(tok::kw___attribute))
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002156 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002157
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002158 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2159 EnumConstantDecls.data(), EnumConstantDecls.size(),
Douglas Gregor0be31a22010-07-02 17:43:08 +00002160 getCurScope(), Attr.get());
Mike Stump11289f42009-09-09 15:08:12 +00002161
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002162 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002163 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002164}
Chris Lattner3b561a32006-08-13 00:12:11 +00002165
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002166/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002167/// start of a type-qualifier-list.
2168bool Parser::isTypeQualifier() const {
2169 switch (Tok.getKind()) {
2170 default: return false;
2171 // type-qualifier
2172 case tok::kw_const:
2173 case tok::kw_volatile:
2174 case tok::kw_restrict:
2175 return true;
2176 }
2177}
2178
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002179/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2180/// is definitely a type-specifier. Return false if it isn't part of a type
2181/// specifier or if we're not sure.
2182bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2183 switch (Tok.getKind()) {
2184 default: return false;
2185 // type-specifiers
2186 case tok::kw_short:
2187 case tok::kw_long:
2188 case tok::kw_signed:
2189 case tok::kw_unsigned:
2190 case tok::kw__Complex:
2191 case tok::kw__Imaginary:
2192 case tok::kw_void:
2193 case tok::kw_char:
2194 case tok::kw_wchar_t:
2195 case tok::kw_char16_t:
2196 case tok::kw_char32_t:
2197 case tok::kw_int:
2198 case tok::kw_float:
2199 case tok::kw_double:
2200 case tok::kw_bool:
2201 case tok::kw__Bool:
2202 case tok::kw__Decimal32:
2203 case tok::kw__Decimal64:
2204 case tok::kw__Decimal128:
2205 case tok::kw___vector:
2206
2207 // struct-or-union-specifier (C99) or class-specifier (C++)
2208 case tok::kw_class:
2209 case tok::kw_struct:
2210 case tok::kw_union:
2211 // enum-specifier
2212 case tok::kw_enum:
2213
2214 // typedef-name
2215 case tok::annot_typename:
2216 return true;
2217 }
2218}
2219
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002220/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002221/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002222bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002223 switch (Tok.getKind()) {
2224 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002225
Chris Lattner020bab92009-01-04 23:41:41 +00002226 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002227 if (TryAltiVecVectorToken())
2228 return true;
2229 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002230 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002231 // Annotate typenames and C++ scope specifiers. If we get one, just
2232 // recurse to handle whatever we get.
2233 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002234 return true;
2235 if (Tok.is(tok::identifier))
2236 return false;
2237 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002238
Chris Lattner020bab92009-01-04 23:41:41 +00002239 case tok::coloncolon: // ::foo::bar
2240 if (NextToken().is(tok::kw_new) || // ::new
2241 NextToken().is(tok::kw_delete)) // ::delete
2242 return false;
2243
Chris Lattner020bab92009-01-04 23:41:41 +00002244 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002245 return true;
2246 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002247
Chris Lattnere37e2332006-08-15 04:50:22 +00002248 // GNU attributes support.
2249 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002250 // GNU typeof support.
2251 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002252
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002253 // type-specifiers
2254 case tok::kw_short:
2255 case tok::kw_long:
2256 case tok::kw_signed:
2257 case tok::kw_unsigned:
2258 case tok::kw__Complex:
2259 case tok::kw__Imaginary:
2260 case tok::kw_void:
2261 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002262 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002263 case tok::kw_char16_t:
2264 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002265 case tok::kw_int:
2266 case tok::kw_float:
2267 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002268 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002269 case tok::kw__Bool:
2270 case tok::kw__Decimal32:
2271 case tok::kw__Decimal64:
2272 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002273 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002274
Chris Lattner861a2262008-04-13 18:59:07 +00002275 // struct-or-union-specifier (C99) or class-specifier (C++)
2276 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002277 case tok::kw_struct:
2278 case tok::kw_union:
2279 // enum-specifier
2280 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002281
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002282 // type-qualifier
2283 case tok::kw_const:
2284 case tok::kw_volatile:
2285 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002286
2287 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002288 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002289 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002290
Chris Lattner409bf7d2008-10-20 00:25:30 +00002291 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2292 case tok::less:
2293 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002294
Steve Naroff44ac7772008-12-25 14:16:32 +00002295 case tok::kw___cdecl:
2296 case tok::kw___stdcall:
2297 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002298 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002299 case tok::kw___w64:
2300 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002301 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002302 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002303 }
2304}
2305
Chris Lattneracd58a32006-08-06 17:24:14 +00002306/// isDeclarationSpecifier() - Return true if the current token is part of a
2307/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002308///
2309/// \param DisambiguatingWithExpression True to indicate that the purpose of
2310/// this check is to disambiguate between an expression and a declaration.
2311bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002312 switch (Tok.getKind()) {
2313 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002314
Chris Lattner020bab92009-01-04 23:41:41 +00002315 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002316 // Unfortunate hack to support "Class.factoryMethod" notation.
2317 if (getLang().ObjC1 && NextToken().is(tok::period))
2318 return false;
John Thompson22334602010-02-05 00:12:22 +00002319 if (TryAltiVecVectorToken())
2320 return true;
2321 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002322 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002323 // Annotate typenames and C++ scope specifiers. If we get one, just
2324 // recurse to handle whatever we get.
2325 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002326 return true;
2327 if (Tok.is(tok::identifier))
2328 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002329
2330 // If we're in Objective-C and we have an Objective-C class type followed
2331 // by an identifier and then either ':' or ']', in a place where an
2332 // expression is permitted, then this is probably a class message send
2333 // missing the initial '['. In this case, we won't consider this to be
2334 // the start of a declaration.
2335 if (DisambiguatingWithExpression &&
2336 isStartOfObjCClassMessageMissingOpenBracket())
2337 return false;
2338
John McCall1f476a12010-02-26 08:45:28 +00002339 return isDeclarationSpecifier();
2340
Chris Lattner020bab92009-01-04 23:41:41 +00002341 case tok::coloncolon: // ::foo::bar
2342 if (NextToken().is(tok::kw_new) || // ::new
2343 NextToken().is(tok::kw_delete)) // ::delete
2344 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002345
Chris Lattner020bab92009-01-04 23:41:41 +00002346 // Annotate typenames and C++ scope specifiers. If we get one, just
2347 // recurse to handle whatever we get.
2348 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002349 return true;
2350 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002351
Chris Lattneracd58a32006-08-06 17:24:14 +00002352 // storage-class-specifier
2353 case tok::kw_typedef:
2354 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002355 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002356 case tok::kw_static:
2357 case tok::kw_auto:
2358 case tok::kw_register:
2359 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002360
Chris Lattneracd58a32006-08-06 17:24:14 +00002361 // type-specifiers
2362 case tok::kw_short:
2363 case tok::kw_long:
2364 case tok::kw_signed:
2365 case tok::kw_unsigned:
2366 case tok::kw__Complex:
2367 case tok::kw__Imaginary:
2368 case tok::kw_void:
2369 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002370 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002371 case tok::kw_char16_t:
2372 case tok::kw_char32_t:
2373
Chris Lattneracd58a32006-08-06 17:24:14 +00002374 case tok::kw_int:
2375 case tok::kw_float:
2376 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002377 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002378 case tok::kw__Bool:
2379 case tok::kw__Decimal32:
2380 case tok::kw__Decimal64:
2381 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002382 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002383
Chris Lattner861a2262008-04-13 18:59:07 +00002384 // struct-or-union-specifier (C99) or class-specifier (C++)
2385 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002386 case tok::kw_struct:
2387 case tok::kw_union:
2388 // enum-specifier
2389 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002390
Chris Lattneracd58a32006-08-06 17:24:14 +00002391 // type-qualifier
2392 case tok::kw_const:
2393 case tok::kw_volatile:
2394 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002395
Chris Lattneracd58a32006-08-06 17:24:14 +00002396 // function-specifier
2397 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002398 case tok::kw_virtual:
2399 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002400
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002401 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002402 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002403
Chris Lattner599e47e2007-08-09 17:01:07 +00002404 // GNU typeof support.
2405 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002406
Chris Lattner599e47e2007-08-09 17:01:07 +00002407 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002408 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002409 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002410
Chris Lattner8b2ec162008-07-26 03:38:44 +00002411 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2412 case tok::less:
2413 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002414
Steve Narofff192fab2009-01-06 19:34:12 +00002415 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002416 case tok::kw___cdecl:
2417 case tok::kw___stdcall:
2418 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002419 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002420 case tok::kw___w64:
2421 case tok::kw___ptr64:
2422 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002423 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002424 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002425 }
2426}
2427
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002428bool Parser::isConstructorDeclarator() {
2429 TentativeParsingAction TPA(*this);
2430
2431 // Parse the C++ scope specifier.
2432 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002433 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00002434 TPA.Revert();
2435 return false;
2436 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002437
2438 // Parse the constructor name.
2439 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2440 // We already know that we have a constructor name; just consume
2441 // the token.
2442 ConsumeToken();
2443 } else {
2444 TPA.Revert();
2445 return false;
2446 }
2447
2448 // Current class name must be followed by a left parentheses.
2449 if (Tok.isNot(tok::l_paren)) {
2450 TPA.Revert();
2451 return false;
2452 }
2453 ConsumeParen();
2454
2455 // A right parentheses or ellipsis signals that we have a constructor.
2456 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2457 TPA.Revert();
2458 return true;
2459 }
2460
2461 // If we need to, enter the specified scope.
2462 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002463 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002464 DeclScopeObj.EnterDeclaratorScope();
2465
2466 // Check whether the next token(s) are part of a declaration
2467 // specifier, in which case we have the start of a parameter and,
2468 // therefore, we know that this is a constructor.
2469 bool IsConstructor = isDeclarationSpecifier();
2470 TPA.Revert();
2471 return IsConstructor;
2472}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002473
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002474/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00002475/// type-qualifier-list: [C99 6.7.5]
2476/// type-qualifier
2477/// [vendor] attributes
2478/// [ only if VendorAttributesAllowed=true ]
2479/// type-qualifier-list type-qualifier
2480/// [vendor] type-qualifier-list attributes
2481/// [ only if VendorAttributesAllowed=true ]
2482/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2483/// [ only if CXX0XAttributesAllowed=true ]
2484/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002485///
Dawn Perchik335e16b2010-09-03 01:29:35 +00002486void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2487 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00002488 bool CXX0XAttributesAllowed) {
2489 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2490 SourceLocation Loc = Tok.getLocation();
2491 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2492 if (CXX0XAttributesAllowed)
2493 DS.AddAttributes(Attr.AttrList);
2494 else
2495 Diag(Loc, diag::err_attributes_not_allowed);
2496 }
2497
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002498 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002499 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002500 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002501 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002502 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002503
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002504 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00002505 case tok::code_completion:
2506 Actions.CodeCompleteTypeQualifiers(DS);
2507 ConsumeCodeCompletionToken();
2508 break;
2509
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002510 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002511 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2512 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002513 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002514 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002515 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2516 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002517 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002518 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002519 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2520 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002521 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002522 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002523 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002524 case tok::kw___cdecl:
2525 case tok::kw___stdcall:
2526 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002527 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002528 if (VendorAttributesAllowed) {
Eli Friedman53339e02009-06-08 23:27:34 +00002529 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2530 continue;
2531 }
2532 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002533 case tok::kw___pascal:
2534 if (VendorAttributesAllowed) {
2535 DS.AddAttributes(ParseBorlandTypeAttributes());
2536 continue;
2537 }
2538 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002539 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002540 if (VendorAttributesAllowed) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002541 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002542 continue; // do *not* consume the next token!
2543 }
2544 // otherwise, FALL THROUGH!
2545 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002546 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002547 // If this is not a type-qualifier token, we're done reading type
2548 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002549 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002550 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002551 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002552
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002553 // If the specifier combination wasn't legal, issue a diagnostic.
2554 if (isInvalid) {
2555 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002556 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002557 }
2558 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002559 }
2560}
2561
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002562
2563/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2564///
2565void Parser::ParseDeclarator(Declarator &D) {
2566 /// This implements the 'declarator' production in the C grammar, then checks
2567 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002568 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002569}
2570
Sebastian Redlbd150f42008-11-21 19:14:01 +00002571/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2572/// is parsed by the function passed to it. Pass null, and the direct-declarator
2573/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002574/// ptr-operator production.
2575///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002576/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2577/// [C] pointer[opt] direct-declarator
2578/// [C++] direct-declarator
2579/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002580///
2581/// pointer: [C99 6.7.5]
2582/// '*' type-qualifier-list[opt]
2583/// '*' type-qualifier-list[opt] pointer
2584///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002585/// ptr-operator:
2586/// '*' cv-qualifier-seq[opt]
2587/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002588/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002589/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002590/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002591/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002592void Parser::ParseDeclaratorInternal(Declarator &D,
2593 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002594 if (Diags.hasAllExtensionsSilenced())
2595 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002596
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002597 // C++ member pointers start with a '::' or a nested-name.
2598 // Member pointers get special handling, since there's no place for the
2599 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002600 if (getLang().CPlusPlus &&
2601 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2602 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002603 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002604 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00002605
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002606 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002607 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002608 // The scope spec really belongs to the direct-declarator.
2609 D.getCXXScopeSpec() = SS;
2610 if (DirectDeclParser)
2611 (this->*DirectDeclParser)(D);
2612 return;
2613 }
2614
2615 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002616 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002617 DeclSpec DS;
2618 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002619 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002620
2621 // Recurse to parse whatever is left.
2622 ParseDeclaratorInternal(D, DirectDeclParser);
2623
2624 // Sema will have to catch (syntactically invalid) pointers into global
2625 // scope. It has to catch pointers into namespace scope anyway.
2626 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002627 Loc, DS.TakeAttributes()),
2628 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002629 return;
2630 }
2631 }
2632
2633 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002634 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002635 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002636 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002637 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002638 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002639 if (DirectDeclParser)
2640 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002641 return;
2642 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002643
Sebastian Redled0f3b02009-03-15 22:02:01 +00002644 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2645 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002646 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002647 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002648
Chris Lattner9eac9312009-03-27 04:18:06 +00002649 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002650 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002651 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002652
Bill Wendling3708c182007-05-27 10:15:43 +00002653 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002654 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002655
Bill Wendling3708c182007-05-27 10:15:43 +00002656 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002657 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002658 if (Kind == tok::star)
2659 // Remember that we parsed a pointer type, and remember the type-quals.
2660 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002661 DS.TakeAttributes()),
2662 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002663 else
2664 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002665 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002666 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002667 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002668 } else {
2669 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002670 DeclSpec DS;
2671
Sebastian Redl3b27be62009-03-23 00:00:23 +00002672 // Complain about rvalue references in C++03, but then go on and build
2673 // the declarator.
2674 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2675 Diag(Loc, diag::err_rvalue_reference);
2676
Bill Wendling93efb222007-06-02 23:28:54 +00002677 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2678 // cv-qualifiers are introduced through the use of a typedef or of a
2679 // template type argument, in which case the cv-qualifiers are ignored.
2680 //
2681 // [GNU] Retricted references are allowed.
2682 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002683 // [C++0x] Attributes on references are not allowed.
2684 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002685 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002686
2687 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2688 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2689 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002690 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002691 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2692 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002693 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002694 }
Bill Wendling3708c182007-05-27 10:15:43 +00002695
2696 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002697 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002698
Douglas Gregor66583c52008-11-03 15:51:28 +00002699 if (D.getNumTypeObjects() > 0) {
2700 // C++ [dcl.ref]p4: There shall be no references to references.
2701 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2702 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002703 if (const IdentifierInfo *II = D.getIdentifier())
2704 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2705 << II;
2706 else
2707 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2708 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002709
Sebastian Redlbd150f42008-11-21 19:14:01 +00002710 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002711 // can go ahead and build the (technically ill-formed)
2712 // declarator: reference collapsing will take care of it.
2713 }
2714 }
2715
Bill Wendling3708c182007-05-27 10:15:43 +00002716 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002717 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002718 DS.TakeAttributes(),
2719 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002720 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002721 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002722}
2723
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002724/// ParseDirectDeclarator
2725/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002726/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002727/// '(' declarator ')'
2728/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002729/// [C90] direct-declarator '[' constant-expression[opt] ']'
2730/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2731/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2732/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2733/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002734/// direct-declarator '(' parameter-type-list ')'
2735/// direct-declarator '(' identifier-list[opt] ')'
2736/// [GNU] direct-declarator '(' parameter-forward-declarations
2737/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002738/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2739/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002740/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002741///
2742/// declarator-id: [C++ 8]
2743/// id-expression
2744/// '::'[opt] nested-name-specifier[opt] type-name
2745///
2746/// id-expression: [C++ 5.1]
2747/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002748/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002749///
2750/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002751/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002752/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002753/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002754/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002755/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002756///
Chris Lattneracd58a32006-08-06 17:24:14 +00002757void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002758 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002759
Douglas Gregor7861a802009-11-03 01:35:08 +00002760 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2761 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002762 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00002763 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00002764 }
2765
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002766 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002767 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00002768 // Change the declaration context for name lookup, until this function
2769 // is exited (and the declarator has been parsed).
2770 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002771 }
2772
Douglas Gregor7861a802009-11-03 01:35:08 +00002773 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2774 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2775 // We found something that indicates the start of an unqualified-id.
2776 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002777 bool AllowConstructorName;
2778 if (D.getDeclSpec().hasTypeSpecifier())
2779 AllowConstructorName = false;
2780 else if (D.getCXXScopeSpec().isSet())
2781 AllowConstructorName =
2782 (D.getContext() == Declarator::FileContext ||
2783 (D.getContext() == Declarator::MemberContext &&
2784 D.getDeclSpec().isFriendSpecified()));
2785 else
2786 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2787
Douglas Gregor7861a802009-11-03 01:35:08 +00002788 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2789 /*EnteringContext=*/true,
2790 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002791 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002792 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002793 D.getName()) ||
2794 // Once we're past the identifier, if the scope was bad, mark the
2795 // whole declarator bad.
2796 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002797 D.SetIdentifier(0, Tok.getLocation());
2798 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002799 } else {
2800 // Parsed the unqualified-id; update range information and move along.
2801 if (D.getSourceRange().getBegin().isInvalid())
2802 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2803 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002804 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002805 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002806 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002807 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002808 assert(!getLang().CPlusPlus &&
2809 "There's a C++-specific check for tok::identifier above");
2810 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2811 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2812 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002813 goto PastIdentifier;
2814 }
2815
2816 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002817 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002818 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002819 // Example: 'char (*X)' or 'int (*XX)(void)'
2820 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002821
2822 // If the declarator was parenthesized, we entered the declarator
2823 // scope when parsing the parenthesized declarator, then exited
2824 // the scope already. Re-enter the scope, if we need to.
2825 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002826 // If there was an error parsing parenthesized declarator, declarator
2827 // scope may have been enterred before. Don't do it again.
2828 if (!D.isInvalidType() &&
2829 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002830 // Change the declaration context for name lookup, until this function
2831 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002832 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002833 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002834 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002835 // This could be something simple like "int" (in which case the declarator
2836 // portion is empty), if an abstract-declarator is allowed.
2837 D.SetIdentifier(0, Tok.getLocation());
2838 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002839 if (D.getContext() == Declarator::MemberContext)
2840 Diag(Tok, diag::err_expected_member_name_or_semi)
2841 << D.getDeclSpec().getSourceRange();
2842 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002843 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002844 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002845 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002846 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002847 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002848 }
Mike Stump11289f42009-09-09 15:08:12 +00002849
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002850 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002851 assert(D.isPastIdentifier() &&
2852 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002853
Alexis Hunt96d5c762009-11-21 08:43:09 +00002854 // Don't parse attributes unless we have an identifier.
Douglas Gregor0286b462010-02-19 16:47:56 +00002855 if (D.getIdentifier() && getLang().CPlusPlus0x
Alexis Hunt96d5c762009-11-21 08:43:09 +00002856 && isCXX0XAttributeSpecifier(true)) {
2857 SourceLocation AttrEndLoc;
2858 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2859 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2860 }
2861
Chris Lattneracd58a32006-08-06 17:24:14 +00002862 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002863 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002864 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2865 // In such a case, check if we actually have a function declarator; if it
2866 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002867 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2868 // When not in file scope, warn for ambiguous function declarators, just
2869 // in case the author intended it as a variable definition.
2870 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2871 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2872 break;
2873 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002874 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002875 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002876 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002877 } else {
2878 break;
2879 }
2880 }
2881}
2882
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002883/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2884/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002885/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002886/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2887///
2888/// direct-declarator:
2889/// '(' declarator ')'
2890/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002891/// direct-declarator '(' parameter-type-list ')'
2892/// direct-declarator '(' identifier-list[opt] ')'
2893/// [GNU] direct-declarator '(' parameter-forward-declarations
2894/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002895///
2896void Parser::ParseParenDeclarator(Declarator &D) {
2897 SourceLocation StartLoc = ConsumeParen();
2898 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002899
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002900 // Eat any attributes before we look at whether this is a grouping or function
2901 // declarator paren. If this is a grouping paren, the attribute applies to
2902 // the type being built up, for example:
2903 // int (__attribute__(()) *x)(long y)
2904 // If this ends up not being a grouping paren, the attribute applies to the
2905 // first argument, for example:
2906 // int (__attribute__(()) int x)
2907 // In either case, we need to eat any attributes to be able to determine what
2908 // sort of paren this is.
2909 //
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002910 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002911 bool RequiresArg = false;
2912 if (Tok.is(tok::kw___attribute)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002913 AttrList.reset(ParseGNUAttributes());
Mike Stump11289f42009-09-09 15:08:12 +00002914
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002915 // We require that the argument list (if this is a non-grouping paren) be
2916 // present even if the attribute list was empty.
2917 RequiresArg = true;
2918 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002919 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002920 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00002921 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2922 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002923 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman53339e02009-06-08 23:27:34 +00002924 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00002925 // Eat any Borland extensions.
2926 if (Tok.is(tok::kw___pascal)) {
2927 AttrList.reset(ParseBorlandTypeAttributes(AttrList.take()));
2928 }
Mike Stump11289f42009-09-09 15:08:12 +00002929
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002930 // If we haven't past the identifier yet (or where the identifier would be
2931 // stored, if this is an abstract declarator), then this is probably just
2932 // grouping parens. However, if this could be an abstract-declarator, then
2933 // this could also be the start of function arguments (consider 'void()').
2934 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002935
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002936 if (!D.mayOmitIdentifier()) {
2937 // If this can't be an abstract-declarator, this *must* be a grouping
2938 // paren, because we haven't seen the identifier yet.
2939 isGrouping = true;
2940 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002941 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002942 isDeclarationSpecifier()) { // 'int(int)' is a function.
2943 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2944 // considered to be a type, not a K&R identifier-list.
2945 isGrouping = false;
2946 } else {
2947 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2948 isGrouping = true;
2949 }
Mike Stump11289f42009-09-09 15:08:12 +00002950
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002951 // If this is a grouping paren, handle:
2952 // direct-declarator: '(' declarator ')'
2953 // direct-declarator: '(' attributes declarator ')'
2954 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002955 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002956 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002957 if (AttrList)
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002958 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002959
Sebastian Redlbd150f42008-11-21 19:14:01 +00002960 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002961 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002962 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002963
2964 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002965 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002966 return;
2967 }
Mike Stump11289f42009-09-09 15:08:12 +00002968
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002969 // Okay, if this wasn't a grouping paren, it must be the start of a function
2970 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002971 // identifier (and remember where it would have been), then call into
2972 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002973 D.SetIdentifier(0, Tok.getLocation());
2974
Ted Kremenekc162e8e2010-02-11 02:19:13 +00002975 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002976}
2977
2978/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2979/// declarator D up to a paren, which indicates that we are parsing function
2980/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002981///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002982/// If AttrList is non-null, then the caller parsed those arguments immediately
2983/// after the open paren - they should be considered to be the first argument of
2984/// a parameter. If RequiresArg is true, then the first argument of the
2985/// function is required to be present and required to not be an identifier
2986/// list.
2987///
Chris Lattneracd58a32006-08-06 17:24:14 +00002988/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002989/// parameter-type-list: [C99 6.7.5]
2990/// parameter-list
2991/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002992/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002993///
2994/// parameter-list: [C99 6.7.5]
2995/// parameter-declaration
2996/// parameter-list ',' parameter-declaration
2997///
2998/// parameter-declaration: [C99 6.7.5]
2999/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003000/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003001/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003002/// declaration-specifiers abstract-declarator[opt]
3003/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003004/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003005/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003006///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003007/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00003008/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003009///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003010void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3011 AttributeList *AttrList,
3012 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003013 // lparen is already consumed!
3014 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003015
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003016 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003017 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003018 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00003019 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003020 delete AttrList;
3021 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003022
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003023 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3024 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003025
3026 // cv-qualifier-seq[opt].
3027 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003028 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003029 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003030 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003031 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003032 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003033 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00003034 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003035 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003036 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003037
3038 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003039 if (Tok.is(tok::kw_throw)) {
3040 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003041 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003042 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003043 hasAnyExceptionSpec);
3044 assert(Exceptions.size() == ExceptionRanges.size() &&
3045 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003046 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003047 }
3048
Chris Lattner371ed4e2008-04-06 06:57:35 +00003049 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003050 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003051 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003052 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003053 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003054 /*arglist*/ 0, 0,
3055 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003056 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003057 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003058 Exceptions.data(),
3059 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003060 Exceptions.size(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003061 LParenLoc, RParenLoc, D),
3062 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003063 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003064 }
3065
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003066 // Alternatively, this parameter list may be an identifier list form for a
3067 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003068 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3069 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003070 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003071 // K&R identifier lists can't have typedefs as identifiers, per
3072 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00003073 if (RequiresArg) {
3074 Diag(Tok, diag::err_argument_required_after_attribute);
3075 delete AttrList;
3076 }
Chris Lattner9453ab82010-05-14 17:23:36 +00003077
Steve Naroffb0486722009-01-28 19:16:40 +00003078 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003079 // normal declarators, not for abstract-declarators. Get the first
3080 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003081 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003082 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003083
3084 // Identifier lists follow a really simple grammar: the identifiers can
3085 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3086 // identifier lists are really rare in the brave new modern world, and it
3087 // is very common for someone to typo a type in a non-k&r style list. If
3088 // we are presented with something like: "void foo(intptr x, float y)",
3089 // we don't want to start parsing the function declarator as though it is
3090 // a K&R style declarator just because intptr is an invalid type.
3091 //
3092 // To handle this, we check to see if the token after the first identifier
3093 // is a "," or ")". Only if so, do we parse it as an identifier list.
3094 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3095 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3096 FirstTok.getIdentifierInfo(),
3097 FirstTok.getLocation(), D);
3098
3099 // If we get here, the code is invalid. Push the first identifier back
3100 // into the token stream and parse the first argument as an (invalid)
3101 // normal argument declarator.
3102 PP.EnterToken(Tok);
3103 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003104 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003105 }
Mike Stump11289f42009-09-09 15:08:12 +00003106
Chris Lattner371ed4e2008-04-06 06:57:35 +00003107 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003108
Chris Lattner371ed4e2008-04-06 06:57:35 +00003109 // Build up an array of information about the parsed arguments.
3110 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003111
3112 // Enter function-declaration scope, limiting any declarators to the
3113 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003114 ParseScope PrototypeScope(this,
3115 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003116
Chris Lattner371ed4e2008-04-06 06:57:35 +00003117 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003118 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003119 while (1) {
3120 if (Tok.is(tok::ellipsis)) {
3121 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003122 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003123 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003124 }
Mike Stump11289f42009-09-09 15:08:12 +00003125
Chris Lattner371ed4e2008-04-06 06:57:35 +00003126 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00003127
Chris Lattner371ed4e2008-04-06 06:57:35 +00003128 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003129 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003130 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003131
3132 // If the caller parsed attributes for the first argument, add them now.
3133 if (AttrList) {
3134 DS.AddAttributes(AttrList);
3135 AttrList = 0; // Only apply the attributes to the first parameter.
3136 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003137 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003138
Chris Lattner371ed4e2008-04-06 06:57:35 +00003139 // Parse the declarator. This is "PrototypeContext", because we must
3140 // accept either 'declarator' or 'abstract-declarator' here.
3141 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3142 ParseDeclarator(ParmDecl);
3143
3144 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003145 if (Tok.is(tok::kw___attribute)) {
3146 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003147 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003148 ParmDecl.AddAttributes(AttrList, Loc);
3149 }
Mike Stump11289f42009-09-09 15:08:12 +00003150
Chris Lattner371ed4e2008-04-06 06:57:35 +00003151 // Remember this parsed parameter in ParamInfo.
3152 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003153
Douglas Gregor4d87df52008-12-16 21:30:33 +00003154 // DefArgToks is used when the parsing of default arguments needs
3155 // to be delayed.
3156 CachedTokens *DefArgToks = 0;
3157
Chris Lattner371ed4e2008-04-06 06:57:35 +00003158 // If no parameter was specified, verify that *something* was specified,
3159 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003160 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3161 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003162 // Completely missing, emit error.
3163 Diag(DSStart, diag::err_missing_param);
3164 } else {
3165 // Otherwise, we have something. Add it and let semantic analysis try
3166 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003167
Chris Lattner371ed4e2008-04-06 06:57:35 +00003168 // Inform the actions module about the parameter declarator, so it gets
3169 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003170 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003171
3172 // Parse the default argument, if any. We parse the default
3173 // arguments in all dialects; the semantic analysis in
3174 // ActOnParamDefaultArgument will reject the default argument in
3175 // C.
3176 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003177 SourceLocation EqualLoc = Tok.getLocation();
3178
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003179 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003180 if (D.getContext() == Declarator::MemberContext) {
3181 // If we're inside a class definition, cache the tokens
3182 // corresponding to the default argument. We'll actually parse
3183 // them when we see the end of the class definition.
3184 // FIXME: Templates will require something similar.
3185 // FIXME: Can we use a smart pointer for Toks?
3186 DefArgToks = new CachedTokens;
3187
Mike Stump11289f42009-09-09 15:08:12 +00003188 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003189 /*StopAtSemi=*/true,
3190 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003191 delete DefArgToks;
3192 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003193 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003194 } else {
3195 // Mark the end of the default argument so that we know when to
3196 // stop when we parse it later on.
3197 Token DefArgEnd;
3198 DefArgEnd.startToken();
3199 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3200 DefArgEnd.setLocation(Tok.getLocation());
3201 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003202 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003203 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003204 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003205 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003206 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003207 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003208
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003209 // The argument isn't actually potentially evaluated unless it is
3210 // used.
3211 EnterExpressionEvaluationContext Eval(Actions,
3212 Sema::PotentiallyEvaluatedIfUsed);
3213
John McCalldadc5752010-08-24 06:29:42 +00003214 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003215 if (DefArgResult.isInvalid()) {
3216 Actions.ActOnParamDefaultArgumentError(Param);
3217 SkipUntil(tok::comma, tok::r_paren, true, true);
3218 } else {
3219 // Inform the actions module about the default argument
3220 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003221 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003222 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003223 }
3224 }
Mike Stump11289f42009-09-09 15:08:12 +00003225
3226 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3227 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003228 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003229 }
3230
3231 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003232 if (Tok.isNot(tok::comma)) {
3233 if (Tok.is(tok::ellipsis)) {
3234 IsVariadic = true;
3235 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3236
3237 if (!getLang().CPlusPlus) {
3238 // We have ellipsis without a preceding ',', which is ill-formed
3239 // in C. Complain and provide the fix.
3240 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003241 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003242 }
3243 }
3244
3245 break;
3246 }
Mike Stump11289f42009-09-09 15:08:12 +00003247
Chris Lattner371ed4e2008-04-06 06:57:35 +00003248 // Consume the comma.
3249 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003250 }
Mike Stump11289f42009-09-09 15:08:12 +00003251
Chris Lattner371ed4e2008-04-06 06:57:35 +00003252 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00003253 PrototypeScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00003254
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003255 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003256 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3257 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003258
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003259 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003260 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003261 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003262 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003263 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003264 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003265
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003266 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003267 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003268 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003269 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003270 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003271
3272 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003273 if (Tok.is(tok::kw_throw)) {
3274 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003275 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003276 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003277 hasAnyExceptionSpec);
3278 assert(Exceptions.size() == ExceptionRanges.size() &&
3279 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003280 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003281 }
3282
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003283 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003284 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003285 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003286 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003287 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003288 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003289 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003290 Exceptions.data(),
3291 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003292 Exceptions.size(),
3293 LParenLoc, RParenLoc, D),
3294 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003295}
Chris Lattneracd58a32006-08-06 17:24:14 +00003296
Chris Lattner6c940e62008-04-06 06:34:08 +00003297/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3298/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003299/// first identifier has already been consumed, and the current token is the
3300/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003301///
3302/// identifier-list: [C99 6.7.5]
3303/// identifier
3304/// identifier-list ',' identifier
3305///
3306void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003307 IdentifierInfo *FirstIdent,
3308 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003309 Declarator &D) {
3310 // Build up an array of information about the parsed arguments.
3311 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3312 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003313
Chris Lattner6c940e62008-04-06 06:34:08 +00003314 // If there was no identifier specified for the declarator, either we are in
3315 // an abstract-declarator, or we are in a parameter declarator which was found
3316 // to be abstract. In abstract-declarators, identifier lists are not valid:
3317 // diagnose this.
3318 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003319 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003320
Chris Lattner9453ab82010-05-14 17:23:36 +00003321 // The first identifier was already read, and is known to be the first
3322 // identifier in the list. Remember this identifier in ParamInfo.
3323 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003324 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003325
Chris Lattner6c940e62008-04-06 06:34:08 +00003326 while (Tok.is(tok::comma)) {
3327 // Eat the comma.
3328 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003329
Chris Lattner9186f552008-04-06 06:39:19 +00003330 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003331 if (Tok.isNot(tok::identifier)) {
3332 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003333 SkipUntil(tok::r_paren);
3334 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003335 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003336
Chris Lattner6c940e62008-04-06 06:34:08 +00003337 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003338
3339 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003340 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003341 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003342
Chris Lattner6c940e62008-04-06 06:34:08 +00003343 // Verify that the argument identifier has not already been mentioned.
3344 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003345 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003346 } else {
3347 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003348 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003349 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003350 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003351 }
Mike Stump11289f42009-09-09 15:08:12 +00003352
Chris Lattner6c940e62008-04-06 06:34:08 +00003353 // Eat the identifier.
3354 ConsumeToken();
3355 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003356
3357 // If we have the closing ')', eat it and we're done.
3358 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3359
Chris Lattner9186f552008-04-06 06:39:19 +00003360 // Remember that we parsed a function type, and remember the attributes. This
3361 // function type is always a K&R style function type, which is not varargs and
3362 // has no prototype.
3363 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003364 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003365 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003366 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003367 /*exception*/false,
3368 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003369 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003370 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003371}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003372
Chris Lattnere8074e62006-08-06 18:30:15 +00003373/// [C90] direct-declarator '[' constant-expression[opt] ']'
3374/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3375/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3376/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3377/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3378void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003379 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003380
Chris Lattner84a11622008-12-18 07:27:21 +00003381 // C array syntax has many features, but by-far the most common is [] and [4].
3382 // This code does a fast path to handle some of the most obvious cases.
3383 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003384 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003385 //FIXME: Use these
3386 CXX0XAttributeList Attr;
3387 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3388 Attr = ParseCXX0XAttributes();
3389 }
3390
Chris Lattner84a11622008-12-18 07:27:21 +00003391 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00003392 ExprResult NumElements;
Douglas Gregor04318252009-07-06 15:59:29 +00003393 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3394 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003395 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003396 return;
3397 } else if (Tok.getKind() == tok::numeric_constant &&
3398 GetLookAheadToken(1).is(tok::r_square)) {
3399 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00003400 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003401 ConsumeToken();
3402
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003403 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003404 //FIXME: Use these
3405 CXX0XAttributeList Attr;
3406 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3407 Attr = ParseCXX0XAttributes();
3408 }
Chris Lattner84a11622008-12-18 07:27:21 +00003409
3410 // If there was an error parsing the assignment-expression, recover.
3411 if (ExprRes.isInvalid())
3412 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00003413
Chris Lattner84a11622008-12-18 07:27:21 +00003414 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00003415 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3416 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003417 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003418 return;
3419 }
Mike Stump11289f42009-09-09 15:08:12 +00003420
Chris Lattnere8074e62006-08-06 18:30:15 +00003421 // If valid, this location is the position where we read the 'static' keyword.
3422 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003423 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003424 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003425
Chris Lattnere8074e62006-08-06 18:30:15 +00003426 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003427 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003428 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003429 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003430
Chris Lattnere8074e62006-08-06 18:30:15 +00003431 // If we haven't already read 'static', check to see if there is one after the
3432 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003433 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003434 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003435
Chris Lattnere8074e62006-08-06 18:30:15 +00003436 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003437 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00003438 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00003439
Chris Lattner521ff2b2008-04-06 05:26:30 +00003440 // Handle the case where we have '[*]' as the array size. However, a leading
3441 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3442 // the the token after the star is a ']'. Since stars in arrays are
3443 // infrequent, use of lookahead is not costly here.
3444 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003445 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003446
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003447 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003448 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003449 StaticLoc = SourceLocation(); // Drop the static.
3450 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003451 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003452 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003453 // Note, in C89, this production uses the constant-expr production instead
3454 // of assignment-expr. The only difference is that assignment-expr allows
3455 // things like '=' and '*='. Sema rejects these in C89 mode because they
3456 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003457
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003458 // Parse the constant-expression or assignment-expression now (depending
3459 // on dialect).
3460 if (getLang().CPlusPlus)
3461 NumElements = ParseConstantExpression();
3462 else
3463 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003464 }
Mike Stump11289f42009-09-09 15:08:12 +00003465
Chris Lattner62591722006-08-12 18:40:58 +00003466 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003467 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003468 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003469 // If the expression was invalid, skip it.
3470 SkipUntil(tok::r_square);
3471 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003472 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003473
3474 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3475
Alexis Hunt96d5c762009-11-21 08:43:09 +00003476 //FIXME: Use these
3477 CXX0XAttributeList Attr;
3478 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3479 Attr = ParseCXX0XAttributes();
3480 }
3481
Chris Lattner84a11622008-12-18 07:27:21 +00003482 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00003483 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3484 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003485 NumElements.release(),
3486 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003487 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003488}
3489
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003490/// [GNU] typeof-specifier:
3491/// typeof ( expressions )
3492/// typeof ( type-name )
3493/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003494///
3495void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003496 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003497 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003498 SourceLocation StartLoc = ConsumeToken();
3499
John McCalle8595032010-01-13 20:03:27 +00003500 const bool hasParens = Tok.is(tok::l_paren);
3501
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003502 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00003503 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003504 SourceRange CastRange;
John McCalldadc5752010-08-24 06:29:42 +00003505 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall6caebb12010-08-25 02:45:51 +00003506 isCastExpr,
3507 CastTy,
3508 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003509 if (hasParens)
3510 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003511
3512 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003513 // FIXME: Not accurate, the range gets one token more than it should.
3514 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003515 else
3516 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003517
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003518 if (isCastExpr) {
3519 if (!CastTy) {
3520 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003521 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003522 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003523
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003524 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003525 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003526 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3527 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003528 DiagID, CastTy))
3529 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003530 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003531 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003532
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003533 // If we get here, the operand to the typeof was an expresion.
3534 if (Operand.isInvalid()) {
3535 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003536 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003537 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003538
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003539 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003540 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003541 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3542 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00003543 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00003544 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003545}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003546
3547
3548/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3549/// from TryAltiVecVectorToken.
3550bool Parser::TryAltiVecVectorTokenOutOfLine() {
3551 Token Next = NextToken();
3552 switch (Next.getKind()) {
3553 default: return false;
3554 case tok::kw_short:
3555 case tok::kw_long:
3556 case tok::kw_signed:
3557 case tok::kw_unsigned:
3558 case tok::kw_void:
3559 case tok::kw_char:
3560 case tok::kw_int:
3561 case tok::kw_float:
3562 case tok::kw_double:
3563 case tok::kw_bool:
3564 case tok::kw___pixel:
3565 Tok.setKind(tok::kw___vector);
3566 return true;
3567 case tok::identifier:
3568 if (Next.getIdentifierInfo() == Ident_pixel) {
3569 Tok.setKind(tok::kw___vector);
3570 return true;
3571 }
3572 return false;
3573 }
3574}
3575
3576bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3577 const char *&PrevSpec, unsigned &DiagID,
3578 bool &isInvalid) {
3579 if (Tok.getIdentifierInfo() == Ident_vector) {
3580 Token Next = NextToken();
3581 switch (Next.getKind()) {
3582 case tok::kw_short:
3583 case tok::kw_long:
3584 case tok::kw_signed:
3585 case tok::kw_unsigned:
3586 case tok::kw_void:
3587 case tok::kw_char:
3588 case tok::kw_int:
3589 case tok::kw_float:
3590 case tok::kw_double:
3591 case tok::kw_bool:
3592 case tok::kw___pixel:
3593 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3594 return true;
3595 case tok::identifier:
3596 if (Next.getIdentifierInfo() == Ident_pixel) {
3597 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3598 return true;
3599 }
3600 break;
3601 default:
3602 break;
3603 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00003604 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003605 DS.isTypeAltiVecVector()) {
3606 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3607 return true;
3608 }
3609 return false;
3610}
3611