blob: 79fc198ab494e965106440d179a1cc9e131a82b8 [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
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000125 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
126 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
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000149 CurrAttr = AttrFactory.Create(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
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000161 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
162 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:
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000178 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
179 0, SourceLocation(), 0, 0, CurrAttr);
Fariborz Jahanian9d7d3d82010-08-17 23:19:16 +0000180 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
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000210 CurrAttr = AttrFactory.Create(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 {
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000219 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
220 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();
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000263 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
264 SourceLocation(), &ExprList, 1,
265 CurrAttr, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000266 }
267 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
268 SkipUntil(tok::r_paren, false);
269 } else {
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000270 CurrAttr = AttrFactory.Create(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;
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000290 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
291 SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedman53339e02009-06-08 23:27:34 +0000292 }
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();
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000301 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
302 SourceLocation(), 0, 0, CurrAttr, true);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000303 }
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///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000323Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
324 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000325 SourceLocation &DeclEnd,
326 CXX0XAttributeList Attr) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000327 ParenBraceBracketBalancer BalancerRAIIObj(*this);
328
John McCall48871652010-08-21 09:40:31 +0000329 Decl *SingleDecl = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000330 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000331 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000332 case tok::kw_export:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000333 if (Attr.HasAttr)
334 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
335 << Attr.Range;
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000336 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000337 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000338 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000339 // Could be the start of an inline namespace. Allowed as an ext in C++03.
340 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
Sebastian Redl67667942010-08-27 23:12:46 +0000341 if (Attr.HasAttr)
342 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
343 << Attr.Range;
344 SourceLocation InlineLoc = ConsumeToken();
345 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
346 break;
347 }
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000348 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, Attr.AttrList,
349 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000350 case tok::kw_namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000351 if (Attr.HasAttr)
352 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
353 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000354 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000355 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000356 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +0000357 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
358 DeclEnd, Attr);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000359 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000360 case tok::kw_static_assert:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000361 if (Attr.HasAttr)
362 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
363 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000364 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000365 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000366 default:
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000367 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, Attr.AttrList,
368 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000369 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000370
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000371 // This routine returns a DeclGroup, if the thing we parsed only contains a
372 // single decl, convert it now.
373 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000374}
375
376/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
377/// declaration-specifiers init-declarator-list[opt] ';'
378///[C90/C++]init-declarator-list ';' [TODO]
379/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000380///
381/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000382/// declaration. If it is true, it checks for and eats it.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000383Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
384 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000385 SourceLocation &DeclEnd,
Chris Lattner005fc1b2010-04-05 18:18:31 +0000386 AttributeList *Attr,
387 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000388 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000389 ParsingDeclSpec DS(*this);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000390 if (Attr)
391 DS.AddAttributes(Attr);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000392 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
393 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000394 StmtResult R = Actions.ActOnVlaStmt(DS);
395 if (R.isUsable())
396 Stmts.push_back(R.release());
Mike Stump11289f42009-09-09 15:08:12 +0000397
Chris Lattner0e894622006-08-13 19:58:17 +0000398 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
399 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000400 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000401 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000402 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallb54367d2010-05-21 20:45:30 +0000403 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000404 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000405 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000406 }
Mike Stump11289f42009-09-09 15:08:12 +0000407
Chris Lattner005fc1b2010-04-05 18:18:31 +0000408 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld5a36322009-11-03 19:26:08 +0000409}
Mike Stump11289f42009-09-09 15:08:12 +0000410
John McCalld5a36322009-11-03 19:26:08 +0000411/// ParseDeclGroup - Having concluded that this is either a function
412/// definition or a group of object declarations, actually parse the
413/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000414Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
415 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000416 bool AllowFunctionDefinitions,
417 SourceLocation *DeclEnd) {
418 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000419 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000420 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000421
John McCalld5a36322009-11-03 19:26:08 +0000422 // Bail out if the first declarator didn't seem well-formed.
423 if (!D.hasName() && !D.mayOmitIdentifier()) {
424 // Skip until ; or }.
425 SkipUntil(tok::r_brace, true, true);
426 if (Tok.is(tok::semi))
427 ConsumeToken();
428 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000429 }
Mike Stump11289f42009-09-09 15:08:12 +0000430
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000431 // Check to see if we have a function *definition* which must have a body.
432 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
433 // Look at the next token to make sure that this isn't a function
434 // declaration. We have to check this because __attribute__ might be the
435 // start of a function definition in GCC-extended K&R C.
436 !isDeclarationAfterDeclarator()) {
437
Chris Lattner13901342010-07-11 22:42:07 +0000438 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000439 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
440 Diag(Tok, diag::err_function_declared_typedef);
441
442 // Recover by treating the 'typedef' as spurious.
443 DS.ClearStorageClassSpecs();
444 }
445
John McCall48871652010-08-21 09:40:31 +0000446 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000447 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000448 }
449
450 if (isDeclarationSpecifier()) {
451 // If there is an invalid declaration specifier right after the function
452 // prototype, then we must be in a missing semicolon case where this isn't
453 // actually a body. Just fall through into the code that handles it as a
454 // prototype, and let the top-level code handle the erroneous declspec
455 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000456 } else {
457 Diag(Tok, diag::err_expected_fn_body);
458 SkipUntil(tok::semi);
459 return DeclGroupPtrTy();
460 }
461 }
462
John McCall48871652010-08-21 09:40:31 +0000463 llvm::SmallVector<Decl *, 8> DeclsInGroup;
464 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000465 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000466 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000467 DeclsInGroup.push_back(FirstDecl);
468
469 // If we don't have a comma, it is either the end of the list (a ';') or an
470 // error, bail out.
471 while (Tok.is(tok::comma)) {
472 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000473 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000474
475 // Parse the next declarator.
476 D.clear();
477
478 // Accept attributes in an init-declarator. In the first declarator in a
479 // declaration, these would be part of the declspec. In subsequent
480 // declarators, they become part of the declarator itself, so that they
481 // don't apply to declarators after *this* one. Examples:
482 // short __attribute__((common)) var; -> declspec
483 // short var __attribute__((common)); -> declarator
484 // short x, __attribute__((common)) var; -> declarator
485 if (Tok.is(tok::kw___attribute)) {
486 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000487 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld5a36322009-11-03 19:26:08 +0000488 D.AddAttributes(AttrList, Loc);
489 }
490
491 ParseDeclarator(D);
492
John McCall48871652010-08-21 09:40:31 +0000493 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000494 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000495 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000496 DeclsInGroup.push_back(ThisDecl);
497 }
498
499 if (DeclEnd)
500 *DeclEnd = Tok.getLocation();
501
502 if (Context != Declarator::ForContext &&
503 ExpectAndConsume(tok::semi,
504 Context == Declarator::FileContext
505 ? diag::err_invalid_token_after_toplevel_declarator
506 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000507 // Okay, there was no semicolon and one was expected. If we see a
508 // declaration specifier, just assume it was missing and continue parsing.
509 // Otherwise things are very confused and we skip to recover.
510 if (!isDeclarationSpecifier()) {
511 SkipUntil(tok::r_brace, true, true);
512 if (Tok.is(tok::semi))
513 ConsumeToken();
514 }
John McCalld5a36322009-11-03 19:26:08 +0000515 }
516
Douglas Gregor0be31a22010-07-02 17:43:08 +0000517 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000518 DeclsInGroup.data(),
519 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000520}
521
Douglas Gregor23996282009-05-12 21:31:51 +0000522/// \brief Parse 'declaration' after parsing 'declaration-specifiers
523/// declarator'. This method parses the remainder of the declaration
524/// (including any attributes or initializer, among other things) and
525/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000526///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000527/// init-declarator: [C99 6.7]
528/// declarator
529/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000530/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
531/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000532/// [C++] declarator initializer[opt]
533///
534/// [C++] initializer:
535/// [C++] '=' initializer-clause
536/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000537/// [C++0x] '=' 'default' [TODO]
538/// [C++0x] '=' 'delete'
539///
540/// According to the standard grammar, =default and =delete are function
541/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000542///
John McCall48871652010-08-21 09:40:31 +0000543Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000544 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000545 // If a simple-asm-expr is present, parse it.
546 if (Tok.is(tok::kw_asm)) {
547 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +0000548 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor23996282009-05-12 21:31:51 +0000549 if (AsmLabel.isInvalid()) {
550 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000551 return 0;
Douglas Gregor23996282009-05-12 21:31:51 +0000552 }
Mike Stump11289f42009-09-09 15:08:12 +0000553
Douglas Gregor23996282009-05-12 21:31:51 +0000554 D.setAsmLabel(AsmLabel.release());
555 D.SetRangeEnd(Loc);
556 }
Mike Stump11289f42009-09-09 15:08:12 +0000557
Douglas Gregor23996282009-05-12 21:31:51 +0000558 // If attributes are present, parse them.
559 if (Tok.is(tok::kw___attribute)) {
560 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000561 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor23996282009-05-12 21:31:51 +0000562 D.AddAttributes(AttrList, Loc);
563 }
Mike Stump11289f42009-09-09 15:08:12 +0000564
Douglas Gregor23996282009-05-12 21:31:51 +0000565 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +0000566 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000567 switch (TemplateInfo.Kind) {
568 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000569 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000570 break;
571
572 case ParsedTemplateInfo::Template:
573 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000574 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000575 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000576 TemplateInfo.TemplateParams->data(),
577 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000578 D);
579 break;
580
581 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +0000582 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000583 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000584 TemplateInfo.ExternLoc,
585 TemplateInfo.TemplateLoc,
586 D);
587 if (ThisRes.isInvalid()) {
588 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000589 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000590 }
591
592 ThisDecl = ThisRes.get();
593 break;
594 }
595 }
Mike Stump11289f42009-09-09 15:08:12 +0000596
Douglas Gregor23996282009-05-12 21:31:51 +0000597 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000598 if (isTokenEqualOrMistypedEqualEqual(
599 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000600 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000601 if (Tok.is(tok::kw_delete)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000602 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000603
604 if (!getLang().CPlusPlus0x)
605 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
606
Douglas Gregor23996282009-05-12 21:31:51 +0000607 Actions.SetDeclDeleted(ThisDecl, DelLoc);
608 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000609 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
610 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000611 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000612 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000613
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000614 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000615 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000616 ConsumeCodeCompletionToken();
617 SkipUntil(tok::comma, true, true);
618 return ThisDecl;
619 }
620
John McCalldadc5752010-08-24 06:29:42 +0000621 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000622
John McCall1f4ee7b2009-12-19 09:28:58 +0000623 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000624 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000625 ExitScope();
626 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000627
Douglas Gregor23996282009-05-12 21:31:51 +0000628 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000629 SkipUntil(tok::comma, true, true);
630 Actions.ActOnInitializerError(ThisDecl);
631 } else
John McCallb268a282010-08-23 23:25:46 +0000632 Actions.AddInitializerToDecl(ThisDecl, Init.take());
Douglas Gregor23996282009-05-12 21:31:51 +0000633 }
634 } else if (Tok.is(tok::l_paren)) {
635 // Parse C++ direct initializer: '(' expression-list ')'
636 SourceLocation LParenLoc = ConsumeParen();
637 ExprVector Exprs(Actions);
638 CommaLocsTy CommaLocs;
639
Douglas Gregor613bf102009-12-22 17:47:17 +0000640 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
641 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000642 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000643 }
644
Douglas Gregor23996282009-05-12 21:31:51 +0000645 if (ParseExpressionList(Exprs, CommaLocs)) {
646 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000647
648 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000649 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000650 ExitScope();
651 }
Douglas Gregor23996282009-05-12 21:31:51 +0000652 } else {
653 // Match the ')'.
654 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
655
656 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
657 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000658
659 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000660 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000661 ExitScope();
662 }
663
Douglas Gregor23996282009-05-12 21:31:51 +0000664 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
665 move_arg(Exprs),
Douglas Gregorce5aa332010-09-09 16:33:13 +0000666 RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000667 }
668 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000669 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000670 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
671 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000672 }
673
674 return ThisDecl;
675}
676
Chris Lattner1890ac82006-08-13 01:16:23 +0000677/// ParseSpecifierQualifierList
678/// specifier-qualifier-list:
679/// type-specifier specifier-qualifier-list[opt]
680/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000681/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000682///
683void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
684 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
685 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000686 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000687
Chris Lattner1890ac82006-08-13 01:16:23 +0000688 // Validate declspec for type-name.
689 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000690 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
691 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000692 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000693
Chris Lattner1b22eed2006-11-28 05:12:07 +0000694 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000695 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000696 if (DS.getStorageClassSpecLoc().isValid())
697 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
698 else
699 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000700 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000701 }
Mike Stump11289f42009-09-09 15:08:12 +0000702
Chris Lattner1b22eed2006-11-28 05:12:07 +0000703 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000704 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000705 if (DS.isInlineSpecified())
706 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
707 if (DS.isVirtualSpecified())
708 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
709 if (DS.isExplicitSpecified())
710 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000711 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000712 }
713}
Chris Lattner53361ac2006-08-10 05:19:57 +0000714
Chris Lattner6cc055a2009-04-12 20:42:31 +0000715/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
716/// specified token is valid after the identifier in a declarator which
717/// immediately follows the declspec. For example, these things are valid:
718///
719/// int x [ 4]; // direct-declarator
720/// int x ( int y); // direct-declarator
721/// int(int x ) // direct-declarator
722/// int x ; // simple-declaration
723/// int x = 17; // init-declarator-list
724/// int x , y; // init-declarator-list
725/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000726/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000727/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000728///
729/// This is not, because 'x' does not immediately follow the declspec (though
730/// ')' happens to be valid anyway).
731/// int (x)
732///
733static bool isValidAfterIdentifierInDeclarator(const Token &T) {
734 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
735 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000736 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000737}
738
Chris Lattner20a0c612009-04-14 21:34:55 +0000739
740/// ParseImplicitInt - This method is called when we have an non-typename
741/// identifier in a declspec (which normally terminates the decl spec) when
742/// the declspec has no type specifier. In this case, the declspec is either
743/// malformed or is "implicit int" (in K&R and C89).
744///
745/// This method handles diagnosing this prettily and returns false if the
746/// declspec is done being processed. If it recovers and thinks there may be
747/// other pieces of declspec after it, it returns true.
748///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000749bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000750 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000751 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000752 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000753
Chris Lattner20a0c612009-04-14 21:34:55 +0000754 SourceLocation Loc = Tok.getLocation();
755 // If we see an identifier that is not a type name, we normally would
756 // parse it as the identifer being declared. However, when a typename
757 // is typo'd or the definition is not included, this will incorrectly
758 // parse the typename as the identifier name and fall over misparsing
759 // later parts of the diagnostic.
760 //
761 // As such, we try to do some look-ahead in cases where this would
762 // otherwise be an "implicit-int" case to see if this is invalid. For
763 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
764 // an identifier with implicit int, we'd get a parse error because the
765 // next token is obviously invalid for a type. Parse these as a case
766 // with an invalid type specifier.
767 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000768
Chris Lattner20a0c612009-04-14 21:34:55 +0000769 // Since we know that this either implicit int (which is rare) or an
770 // error, we'd do lookahead to try to do better recovery.
771 if (isValidAfterIdentifierInDeclarator(NextToken())) {
772 // If this token is valid for implicit int, e.g. "static x = 4", then
773 // we just avoid eating the identifier, so it will be parsed as the
774 // identifier in the declarator.
775 return false;
776 }
Mike Stump11289f42009-09-09 15:08:12 +0000777
Chris Lattner20a0c612009-04-14 21:34:55 +0000778 // Otherwise, if we don't consume this token, we are going to emit an
779 // error anyway. Try to recover from various common problems. Check
780 // to see if this was a reference to a tag name without a tag specified.
781 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000782 //
783 // C++ doesn't need this, and isTagName doesn't take SS.
784 if (SS == 0) {
785 const char *TagName = 0;
786 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000787
Douglas Gregor0be31a22010-07-02 17:43:08 +0000788 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +0000789 default: break;
790 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
791 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
792 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
793 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
794 }
Mike Stump11289f42009-09-09 15:08:12 +0000795
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000796 if (TagName) {
797 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +0000798 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +0000799 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000800
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000801 // Parse this as a tag as if the missing tag were present.
802 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +0000803 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000804 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000805 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000806 return true;
807 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000808 }
Mike Stump11289f42009-09-09 15:08:12 +0000809
Douglas Gregor15e56022009-10-13 23:27:22 +0000810 // This is almost certainly an invalid type name. Let the action emit a
811 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +0000812 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +0000813 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +0000814 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +0000815 // The action emitted a diagnostic, so we don't have to.
816 if (T) {
817 // The action has suggested that the type T could be used. Set that as
818 // the type in the declaration specifiers, consume the would-be type
819 // name token, and we're done.
820 const char *PrevSpec;
821 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +0000822 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +0000823 DS.SetRangeEnd(Tok.getLocation());
824 ConsumeToken();
825
826 // There may be other declaration specifiers after this.
827 return true;
828 }
829
830 // Fall through; the action had no suggestion for us.
831 } else {
832 // The action did not emit a diagnostic, so emit one now.
833 SourceRange R;
834 if (SS) R = SS->getRange();
835 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
836 }
Mike Stump11289f42009-09-09 15:08:12 +0000837
Douglas Gregor15e56022009-10-13 23:27:22 +0000838 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000839 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000840 unsigned DiagID;
841 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000842 DS.SetRangeEnd(Tok.getLocation());
843 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000844
Chris Lattner20a0c612009-04-14 21:34:55 +0000845 // TODO: Could inject an invalid typedef decl in an enclosing scope to
846 // avoid rippling error messages on subsequent uses of the same type,
847 // could be useful if #include was forgotten.
848 return false;
849}
850
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000851/// \brief Determine the declaration specifier context from the declarator
852/// context.
853///
854/// \param Context the declarator context, which is one of the
855/// Declarator::TheContext enumerator values.
856Parser::DeclSpecContext
857Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
858 if (Context == Declarator::MemberContext)
859 return DSC_class;
860 if (Context == Declarator::FileContext)
861 return DSC_top_level;
862 return DSC_normal;
863}
864
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000865/// ParseDeclarationSpecifiers
866/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000867/// storage-class-specifier declaration-specifiers[opt]
868/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000869/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000870/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000871///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000872/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000873/// 'typedef'
874/// 'extern'
875/// 'static'
876/// 'auto'
877/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000878/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000879/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000880/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000881/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000882/// [C++] 'virtual'
883/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000884/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000885/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000886
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000887///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000888void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000889 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000890 AccessSpecifier AS,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000891 DeclSpecContext DSContext) {
Chris Lattner2e232092008-03-13 06:29:04 +0000892 DS.SetRangeStart(Tok.getLocation());
Chris Lattner07865442010-11-09 20:14:26 +0000893 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000894 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000895 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000896 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000897 unsigned DiagID = 0;
898
Chris Lattner4d8f8732006-11-28 05:05:08 +0000899 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000900
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000901 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000902 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000903 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000904 // If this is not a declaration specifier token, we're done reading decl
905 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000906 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000907 return;
Mike Stump11289f42009-09-09 15:08:12 +0000908
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000909 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +0000910 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000911 if (DS.hasTypeSpecifier()) {
912 bool AllowNonIdentifiers
913 = (getCurScope()->getFlags() & (Scope::ControlScope |
914 Scope::BlockScope |
915 Scope::TemplateParamScope |
916 Scope::FunctionPrototypeScope |
917 Scope::AtCatchScope)) == 0;
918 bool AllowNestedNameSpecifiers
919 = DSContext == DSC_top_level ||
920 (DSContext == DSC_class && DS.isFriendSpecified());
921
Douglas Gregorbfcea8b2010-09-16 15:14:18 +0000922 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
923 AllowNonIdentifiers,
924 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000925 ConsumeCodeCompletionToken();
926 return;
927 }
928
929 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +0000930 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
931 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000932 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +0000933 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000934 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +0000935 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000936
937 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
938 ConsumeCodeCompletionToken();
939 return;
940 }
941
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000942 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +0000943 // C++ scope specifier. Annotate and loop, or bail out on error.
944 if (TryAnnotateCXXScopeToken(true)) {
945 if (!DS.hasTypeSpecifier())
946 DS.SetTypeSpecError();
947 goto DoneWithDeclSpec;
948 }
John McCall8bc2a702010-03-01 18:20:46 +0000949 if (Tok.is(tok::coloncolon)) // ::new or ::delete
950 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +0000951 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000952
953 case tok::annot_cxxscope: {
954 if (DS.hasTypeSpecifier())
955 goto DoneWithDeclSpec;
956
John McCall9dab4e62009-12-12 11:40:51 +0000957 CXXScopeSpec SS;
John McCall37ad5512010-08-23 06:44:23 +0000958 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCall9dab4e62009-12-12 11:40:51 +0000959 SS.setRange(Tok.getAnnotationRange());
960
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000961 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000962 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000963 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000964 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000965 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000966 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000967
968 // C++ [class.qual]p2:
969 // In a lookup in which the constructor is an acceptable lookup
970 // result and the nested-name-specifier nominates a class C:
971 //
972 // - if the name specified after the
973 // nested-name-specifier, when looked up in C, is the
974 // injected-class-name of C (Clause 9), or
975 //
976 // - if the name specified after the nested-name-specifier
977 // is the same as the identifier or the
978 // simple-template-id's template-name in the last
979 // component of the nested-name-specifier,
980 //
981 // the name is instead considered to name the constructor of
982 // class C.
983 //
984 // Thus, if the template-name is actually the constructor
985 // name, then the code is ill-formed; this interpretation is
986 // reinforced by the NAD status of core issue 635.
987 TemplateIdAnnotation *TemplateId
988 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +0000989 if ((DSContext == DSC_top_level ||
990 (DSContext == DSC_class && DS.isFriendSpecified())) &&
991 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000992 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000993 if (isConstructorDeclarator()) {
994 // The user meant this to be an out-of-line constructor
995 // definition, but template arguments are not allowed
996 // there. Just allow this as a constructor; we'll
997 // complain about it later.
998 goto DoneWithDeclSpec;
999 }
1000
1001 // The user meant this to name a type, but it actually names
1002 // a constructor with some extraneous template
1003 // arguments. Complain, then parse it as a type as the user
1004 // intended.
1005 Diag(TemplateId->TemplateNameLoc,
1006 diag::err_out_of_line_template_id_names_constructor)
1007 << TemplateId->Name;
1008 }
1009
John McCall9dab4e62009-12-12 11:40:51 +00001010 DS.getTypeSpecScope() = SS;
1011 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001012 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001013 "ParseOptionalCXXScopeSpecifier not working");
1014 AnnotateTemplateIdTokenAsType(&SS);
1015 continue;
1016 }
1017
Douglas Gregorc5790df2009-09-28 07:26:33 +00001018 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001019 DS.getTypeSpecScope() = SS;
1020 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001021 if (Tok.getAnnotationValue()) {
1022 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001023 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1024 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001025 PrevSpec, DiagID, T);
1026 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001027 else
1028 DS.SetTypeSpecError();
1029 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1030 ConsumeToken(); // The typename
1031 }
1032
Douglas Gregor167fa622009-03-25 15:40:00 +00001033 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001034 goto DoneWithDeclSpec;
1035
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001036 // If we're in a context where the identifier could be a class name,
1037 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001038 if ((DSContext == DSC_top_level ||
1039 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001040 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001041 &SS)) {
1042 if (isConstructorDeclarator())
1043 goto DoneWithDeclSpec;
1044
1045 // As noted in C++ [class.qual]p2 (cited above), when the name
1046 // of the class is qualified in a context where it could name
1047 // a constructor, its a constructor name. However, we've
1048 // looked at the declarator, and the user probably meant this
1049 // to be a type. Complain that it isn't supposed to be treated
1050 // as a type, then proceed to parse it as a type.
1051 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1052 << Next.getIdentifierInfo();
1053 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001054
John McCallba7bf592010-08-24 05:47:05 +00001055 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1056 Next.getLocation(),
1057 getCurScope(), &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001058
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001059 // If the referenced identifier is not a type, then this declspec is
1060 // erroneous: We already checked about that it has no type specifier, and
1061 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001062 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001063 if (TypeRep == 0) {
1064 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001065 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001066 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001067 }
Mike Stump11289f42009-09-09 15:08:12 +00001068
John McCall9dab4e62009-12-12 11:40:51 +00001069 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001070 ConsumeToken(); // The C++ scope.
1071
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001072 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001073 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001074 if (isInvalid)
1075 break;
Mike Stump11289f42009-09-09 15:08:12 +00001076
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001077 DS.SetRangeEnd(Tok.getLocation());
1078 ConsumeToken(); // The typename.
1079
1080 continue;
1081 }
Mike Stump11289f42009-09-09 15:08:12 +00001082
Chris Lattnere387d9e2009-01-21 19:48:37 +00001083 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001084 if (Tok.getAnnotationValue()) {
1085 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001086 // FIXME: This should probably pass getAnnotationEndLoc() instead of
1087 // Loc, but that breaks test/Index/recursive-cxx-member-calls.cpp.
1088 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001089 DiagID, T);
1090 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001091 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001092
1093 if (isInvalid)
1094 break;
1095
Chris Lattnere387d9e2009-01-21 19:48:37 +00001096 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1097 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001098
Chris Lattnere387d9e2009-01-21 19:48:37 +00001099 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1100 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001101 // Objective-C interface.
1102 if (Tok.is(tok::less) && getLang().ObjC1)
1103 ParseObjCProtocolQualifiers(DS);
1104
Chris Lattnere387d9e2009-01-21 19:48:37 +00001105 continue;
1106 }
Mike Stump11289f42009-09-09 15:08:12 +00001107
Chris Lattner16fac4f2008-07-26 01:18:38 +00001108 // typedef-name
1109 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001110 // In C++, check to see if this is a scope specifier like foo::bar::, if
1111 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001112 if (getLang().CPlusPlus) {
1113 if (TryAnnotateCXXScopeToken(true)) {
1114 if (!DS.hasTypeSpecifier())
1115 DS.SetTypeSpecError();
1116 goto DoneWithDeclSpec;
1117 }
1118 if (!Tok.is(tok::identifier))
1119 continue;
1120 }
Mike Stump11289f42009-09-09 15:08:12 +00001121
Chris Lattner16fac4f2008-07-26 01:18:38 +00001122 // This identifier can only be a typedef name if we haven't already seen
1123 // a type-specifier. Without this check we misparse:
1124 // typedef int X; struct Y { short X; }; as 'short int'.
1125 if (DS.hasTypeSpecifier())
1126 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001127
John Thompson22334602010-02-05 00:12:22 +00001128 // Check for need to substitute AltiVec keyword tokens.
1129 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1130 break;
1131
Chris Lattner16fac4f2008-07-26 01:18:38 +00001132 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001133 ParsedType TypeRep =
1134 Actions.getTypeName(*Tok.getIdentifierInfo(),
1135 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001136
Chris Lattner6cc055a2009-04-12 20:42:31 +00001137 // If this is not a typedef name, don't parse it as part of the declspec,
1138 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001139 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001140 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001141 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001142 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001143
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001144 // If we're in a context where the identifier could be a class name,
1145 // check whether this is a constructor declaration.
1146 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001147 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001148 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001149 goto DoneWithDeclSpec;
1150
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001151 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001152 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001153 if (isInvalid)
1154 break;
Mike Stump11289f42009-09-09 15:08:12 +00001155
Chris Lattner16fac4f2008-07-26 01:18:38 +00001156 DS.SetRangeEnd(Tok.getLocation());
1157 ConsumeToken(); // The identifier
1158
1159 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1160 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001161 // Objective-C interface.
1162 if (Tok.is(tok::less) && getLang().ObjC1)
1163 ParseObjCProtocolQualifiers(DS);
1164
Steve Naroffcd5e7822008-09-22 10:28:57 +00001165 // Need to support trailing type qualifiers (e.g. "id<p> const").
1166 // If a type specifier follows, it will be diagnosed elsewhere.
1167 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001168 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001169
1170 // type-name
1171 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001172 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001173 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001174 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001175 // This template-id does not refer to a type name, so we're
1176 // done with the type-specifiers.
1177 goto DoneWithDeclSpec;
1178 }
1179
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001180 // If we're in a context where the template-id could be a
1181 // constructor name or specialization, check whether this is a
1182 // constructor declaration.
1183 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001184 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001185 isConstructorDeclarator())
1186 goto DoneWithDeclSpec;
1187
Douglas Gregor7f741122009-02-25 19:37:18 +00001188 // Turn the template-id annotation token into a type annotation
1189 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001190 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001191 continue;
1192 }
1193
Chris Lattnere37e2332006-08-15 04:50:22 +00001194 // GNU attributes support.
1195 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001196 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001197 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001198
1199 // Microsoft declspec support.
1200 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +00001201 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001202 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001203
Steve Naroff44ac7772008-12-25 14:16:32 +00001204 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001205 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001206 // FIXME: Add handling here!
1207 break;
1208
1209 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001210 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001211 case tok::kw___cdecl:
1212 case tok::kw___stdcall:
1213 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001214 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001215 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1216 continue;
1217
Dawn Perchik335e16b2010-09-03 01:29:35 +00001218 // Borland single token adornments.
1219 case tok::kw___pascal:
1220 DS.AddAttributes(ParseBorlandTypeAttributes());
1221 continue;
1222
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001223 // storage-class-specifier
1224 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001225 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1226 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001227 break;
1228 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001229 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001230 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001231 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1232 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001233 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001234 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001235 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001236 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001237 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001238 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001239 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001240 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001241 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1242 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001243 break;
1244 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001245 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001246 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1247 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001248 else
John McCall49bfce42009-08-03 20:12:06 +00001249 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1250 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001251 break;
1252 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001253 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1254 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001255 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001256 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001257 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1258 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001259 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001260 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001261 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001262 break;
Mike Stump11289f42009-09-09 15:08:12 +00001263
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001264 // function-specifier
1265 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001266 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001267 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001268 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001269 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001270 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001271 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001272 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001273 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001274
Anders Carlssoncd8db412009-05-06 04:46:28 +00001275 // friend
1276 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001277 if (DSContext == DSC_class)
1278 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1279 else {
1280 PrevSpec = ""; // not actually used by the diagnostic
1281 DiagID = diag::err_friend_invalid_in_context;
1282 isInvalid = true;
1283 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001284 break;
Mike Stump11289f42009-09-09 15:08:12 +00001285
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001286 // constexpr
1287 case tok::kw_constexpr:
1288 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1289 break;
1290
Chris Lattnere387d9e2009-01-21 19:48:37 +00001291 // type-specifier
1292 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001293 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1294 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001295 break;
1296 case tok::kw_long:
1297 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001298 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1299 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001300 else
John McCall49bfce42009-08-03 20:12:06 +00001301 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1302 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001303 break;
1304 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001305 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1306 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001307 break;
1308 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001309 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1310 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001311 break;
1312 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001313 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1314 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001315 break;
1316 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001317 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1318 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001319 break;
1320 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001321 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1322 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001323 break;
1324 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001325 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1326 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001327 break;
1328 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001329 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1330 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001331 break;
1332 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001333 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1334 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001335 break;
1336 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001337 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1338 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001339 break;
1340 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1342 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001343 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001344 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001345 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1346 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001347 break;
1348 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001349 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1350 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001351 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001352 case tok::kw_bool:
1353 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001354 if (Tok.is(tok::kw_bool) &&
1355 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1356 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1357 PrevSpec = ""; // Not used by the diagnostic.
1358 DiagID = diag::err_bool_redeclaration;
1359 isInvalid = true;
1360 } else {
1361 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1362 DiagID);
1363 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001364 break;
1365 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001366 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1367 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001368 break;
1369 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001370 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1371 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001372 break;
1373 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001374 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1375 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001376 break;
John Thompson22334602010-02-05 00:12:22 +00001377 case tok::kw___vector:
1378 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1379 break;
1380 case tok::kw___pixel:
1381 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1382 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001383
1384 // class-specifier:
1385 case tok::kw_class:
1386 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001387 case tok::kw_union: {
1388 tok::TokenKind Kind = Tok.getKind();
1389 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001390 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001391 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001392 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001393
1394 // enum-specifier:
1395 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001396 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001397 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001398 continue;
1399
1400 // cv-qualifier:
1401 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001402 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1403 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001404 break;
1405 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001406 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1407 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001408 break;
1409 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001410 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1411 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001412 break;
1413
Douglas Gregor333489b2009-03-27 23:10:48 +00001414 // C++ typename-specifier:
1415 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001416 if (TryAnnotateTypeOrScopeToken()) {
1417 DS.SetTypeSpecError();
1418 goto DoneWithDeclSpec;
1419 }
1420 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001421 continue;
1422 break;
1423
Chris Lattnere387d9e2009-01-21 19:48:37 +00001424 // GNU typeof support.
1425 case tok::kw_typeof:
1426 ParseTypeofSpecifier(DS);
1427 continue;
1428
Anders Carlsson74948d02009-06-24 17:47:40 +00001429 case tok::kw_decltype:
1430 ParseDecltypeSpecifier(DS);
1431 continue;
1432
Steve Naroffcfdf6162008-06-05 00:02:44 +00001433 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001434 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001435 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1436 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001437 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001438 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001439
Douglas Gregor3a001f42010-11-19 17:10:50 +00001440 if (!ParseObjCProtocolQualifiers(DS))
1441 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1442 << FixItHint::CreateInsertion(Loc, "id")
1443 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001444
1445 // Need to support trailing type qualifiers (e.g. "id<p> const").
1446 // If a type specifier follows, it will be diagnosed elsewhere.
1447 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001448 }
John McCall49bfce42009-08-03 20:12:06 +00001449 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001450 if (isInvalid) {
1451 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001452 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001453
1454 if (DiagID == diag::ext_duplicate_declspec)
1455 Diag(Tok, DiagID)
1456 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1457 else
1458 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001459 }
Chris Lattner2e232092008-03-13 06:29:04 +00001460 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001461 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001462 }
1463}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001464
Chris Lattnera448d752009-01-06 06:59:53 +00001465/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001466/// primarily follow the C++ grammar with additions for C99 and GNU,
1467/// which together subsume the C grammar. Note that the C++
1468/// type-specifier also includes the C type-qualifier (for const,
1469/// volatile, and C99 restrict). Returns true if a type-specifier was
1470/// found (and parsed), false otherwise.
1471///
1472/// type-specifier: [C++ 7.1.5]
1473/// simple-type-specifier
1474/// class-specifier
1475/// enum-specifier
1476/// elaborated-type-specifier [TODO]
1477/// cv-qualifier
1478///
1479/// cv-qualifier: [C++ 7.1.5.1]
1480/// 'const'
1481/// 'volatile'
1482/// [C99] 'restrict'
1483///
1484/// simple-type-specifier: [ C++ 7.1.5.2]
1485/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1486/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1487/// 'char'
1488/// 'wchar_t'
1489/// 'bool'
1490/// 'short'
1491/// 'int'
1492/// 'long'
1493/// 'signed'
1494/// 'unsigned'
1495/// 'float'
1496/// 'double'
1497/// 'void'
1498/// [C99] '_Bool'
1499/// [C99] '_Complex'
1500/// [C99] '_Imaginary' // Removed in TC2?
1501/// [GNU] '_Decimal32'
1502/// [GNU] '_Decimal64'
1503/// [GNU] '_Decimal128'
1504/// [GNU] typeof-specifier
1505/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1506/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001507/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001508/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001509bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001510 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001511 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001512 const ParsedTemplateInfo &TemplateInfo,
1513 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001514 SourceLocation Loc = Tok.getLocation();
1515
1516 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001517 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001518 // If we already have a type specifier, this identifier is not a type.
1519 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1520 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1521 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1522 return false;
John Thompson22334602010-02-05 00:12:22 +00001523 // Check for need to substitute AltiVec keyword tokens.
1524 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1525 break;
1526 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001527 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001528 // Annotate typenames and C++ scope specifiers. If we get one, just
1529 // recurse to handle whatever we get.
1530 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001531 return true;
1532 if (Tok.is(tok::identifier))
1533 return false;
1534 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1535 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001536 case tok::coloncolon: // ::foo::bar
1537 if (NextToken().is(tok::kw_new) || // ::new
1538 NextToken().is(tok::kw_delete)) // ::delete
1539 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001540
Chris Lattner020bab92009-01-04 23:41:41 +00001541 // Annotate typenames and C++ scope specifiers. If we get one, just
1542 // recurse to handle whatever we get.
1543 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001544 return true;
1545 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1546 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001547
Douglas Gregor450c75a2008-11-07 15:42:26 +00001548 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001549 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001550 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00001551 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1552 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001553 DiagID, T);
1554 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001555 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001556 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1557 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001558
Douglas Gregor450c75a2008-11-07 15:42:26 +00001559 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1560 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1561 // Objective-C interface. If we don't have Objective-C or a '<', this is
1562 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001563 if (Tok.is(tok::less) && getLang().ObjC1)
1564 ParseObjCProtocolQualifiers(DS);
1565
Douglas Gregor450c75a2008-11-07 15:42:26 +00001566 return true;
1567 }
1568
1569 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001570 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001571 break;
1572 case tok::kw_long:
1573 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001574 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1575 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001576 else
John McCall49bfce42009-08-03 20:12:06 +00001577 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1578 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001579 break;
1580 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001581 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001582 break;
1583 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001584 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1585 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001586 break;
1587 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001588 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1589 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001590 break;
1591 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001592 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1593 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001594 break;
1595 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001596 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001597 break;
1598 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001599 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001600 break;
1601 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001602 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001603 break;
1604 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001605 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001606 break;
1607 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001608 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001609 break;
1610 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001611 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001612 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001613 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001614 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001615 break;
1616 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001617 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001618 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001619 case tok::kw_bool:
1620 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001621 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001622 break;
1623 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001624 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1625 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001626 break;
1627 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001628 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1629 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001630 break;
1631 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001632 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1633 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001634 break;
John Thompson22334602010-02-05 00:12:22 +00001635 case tok::kw___vector:
1636 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1637 break;
1638 case tok::kw___pixel:
1639 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1640 break;
1641
Douglas Gregor450c75a2008-11-07 15:42:26 +00001642 // class-specifier:
1643 case tok::kw_class:
1644 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001645 case tok::kw_union: {
1646 tok::TokenKind Kind = Tok.getKind();
1647 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001648 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1649 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001650 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001651 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001652
1653 // enum-specifier:
1654 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001655 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001656 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001657 return true;
1658
1659 // cv-qualifier:
1660 case tok::kw_const:
1661 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001662 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001663 break;
1664 case tok::kw_volatile:
1665 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001666 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001667 break;
1668 case tok::kw_restrict:
1669 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001670 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001671 break;
1672
1673 // GNU typeof support.
1674 case tok::kw_typeof:
1675 ParseTypeofSpecifier(DS);
1676 return true;
1677
Anders Carlsson74948d02009-06-24 17:47:40 +00001678 // C++0x decltype support.
1679 case tok::kw_decltype:
1680 ParseDecltypeSpecifier(DS);
1681 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001682
Anders Carlssonbae27372009-06-26 23:44:14 +00001683 // C++0x auto support.
1684 case tok::kw_auto:
1685 if (!getLang().CPlusPlus0x)
1686 return false;
1687
John McCall49bfce42009-08-03 20:12:06 +00001688 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001689 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001690
Eli Friedman53339e02009-06-08 23:27:34 +00001691 case tok::kw___ptr64:
1692 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001693 case tok::kw___cdecl:
1694 case tok::kw___stdcall:
1695 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001696 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001697 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001698 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001699
Dawn Perchik335e16b2010-09-03 01:29:35 +00001700 case tok::kw___pascal:
1701 DS.AddAttributes(ParseBorlandTypeAttributes());
1702 return true;
1703
Douglas Gregor450c75a2008-11-07 15:42:26 +00001704 default:
1705 // Not a type-specifier; do nothing.
1706 return false;
1707 }
1708
1709 // If the specifier combination wasn't legal, issue a diagnostic.
1710 if (isInvalid) {
1711 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001712 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001713 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001714 }
1715 DS.SetRangeEnd(Tok.getLocation());
1716 ConsumeToken(); // whatever we parsed above.
1717 return true;
1718}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001719
Chris Lattner70ae4912007-10-29 04:42:53 +00001720/// ParseStructDeclaration - Parse a struct declaration without the terminating
1721/// semicolon.
1722///
Chris Lattner90a26b02007-01-23 04:38:16 +00001723/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001724/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001725/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001726/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001727/// struct-declarator-list:
1728/// struct-declarator
1729/// struct-declarator-list ',' struct-declarator
1730/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1731/// struct-declarator:
1732/// declarator
1733/// [GNU] declarator attributes[opt]
1734/// declarator[opt] ':' constant-expression
1735/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1736///
Chris Lattnera12405b2008-04-10 06:46:29 +00001737void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001738ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001739 if (Tok.is(tok::kw___extension__)) {
1740 // __extension__ silences extension warnings in the subexpression.
1741 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001742 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001743 return ParseStructDeclaration(DS, Fields);
1744 }
Mike Stump11289f42009-09-09 15:08:12 +00001745
Steve Naroff97170802007-08-20 22:28:22 +00001746 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001747 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001748 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001749
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001750 // If there are no declarators, this is a free-standing declaration
1751 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001752 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001753 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001754 return;
1755 }
1756
1757 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001758 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001759 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001760 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001761 FieldDeclarator DeclaratorInfo(DS);
1762
1763 // Attributes are only allowed here on successive declarators.
1764 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1765 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001766 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallcfefb6d2009-11-03 02:38:08 +00001767 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1768 }
Mike Stump11289f42009-09-09 15:08:12 +00001769
Steve Naroff97170802007-08-20 22:28:22 +00001770 /// struct-declarator: declarator
1771 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001772 if (Tok.isNot(tok::colon)) {
1773 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1774 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001775 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001776 }
Mike Stump11289f42009-09-09 15:08:12 +00001777
Chris Lattner76c72282007-10-09 17:33:22 +00001778 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001779 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001780 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001781 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001782 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001783 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001784 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001785 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001786
Steve Naroff97170802007-08-20 22:28:22 +00001787 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001788 if (Tok.is(tok::kw___attribute)) {
1789 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001790 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001791 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1792 }
1793
John McCallcfefb6d2009-11-03 02:38:08 +00001794 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00001795 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00001796 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001797
Steve Naroff97170802007-08-20 22:28:22 +00001798 // If we don't have a comma, it is either the end of the list (a ';')
1799 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001800 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001801 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001802
Steve Naroff97170802007-08-20 22:28:22 +00001803 // Consume the comma.
1804 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001805
John McCallcfefb6d2009-11-03 02:38:08 +00001806 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001807 }
Steve Naroff97170802007-08-20 22:28:22 +00001808}
1809
1810/// ParseStructUnionBody
1811/// struct-contents:
1812/// struct-declaration-list
1813/// [EXT] empty
1814/// [GNU] "struct-declaration-list" without terminatoring ';'
1815/// struct-declaration-list:
1816/// struct-declaration
1817/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001818/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001819///
Chris Lattner1300fb92007-01-23 23:42:53 +00001820void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001821 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00001822 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1823 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001824
Chris Lattner90a26b02007-01-23 04:38:16 +00001825 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001826
Douglas Gregor658b9552009-01-09 22:42:13 +00001827 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001828 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001829
Chris Lattner7b9ace62007-01-23 20:11:08 +00001830 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1831 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001832 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00001833 Diag(Tok, diag::ext_empty_struct_union)
1834 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001835
John McCall48871652010-08-21 09:40:31 +00001836 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001837
Chris Lattner7b9ace62007-01-23 20:11:08 +00001838 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001839 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001840 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001841
Chris Lattner736ed5d2007-06-09 05:59:07 +00001842 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001843 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001844 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00001845 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00001846 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001847 ConsumeToken();
1848 continue;
1849 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001850
1851 // Parse all the comma separated declarators.
1852 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001853
John McCallcfefb6d2009-11-03 02:38:08 +00001854 if (!Tok.is(tok::at)) {
1855 struct CFieldCallback : FieldCallback {
1856 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001857 Decl *TagDecl;
1858 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001859
John McCall48871652010-08-21 09:40:31 +00001860 CFieldCallback(Parser &P, Decl *TagDecl,
1861 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001862 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1863
John McCall48871652010-08-21 09:40:31 +00001864 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001865 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00001866 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00001867 FD.D.getDeclSpec().getSourceRange().getBegin(),
1868 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001869 FieldDecls.push_back(Field);
1870 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001871 }
John McCallcfefb6d2009-11-03 02:38:08 +00001872 } Callback(*this, TagDecl, FieldDecls);
1873
1874 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001875 } else { // Handle @defs
1876 ConsumeToken();
1877 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1878 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001879 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001880 continue;
1881 }
1882 ConsumeToken();
1883 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1884 if (!Tok.is(tok::identifier)) {
1885 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001886 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001887 continue;
1888 }
John McCall48871652010-08-21 09:40:31 +00001889 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001890 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001891 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001892 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1893 ConsumeToken();
1894 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001895 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001896
Chris Lattner76c72282007-10-09 17:33:22 +00001897 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001898 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001899 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001900 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001901 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001902 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001903 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1904 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001905 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001906 // If we stopped at a ';', eat it.
1907 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001908 }
1909 }
Mike Stump11289f42009-09-09 15:08:12 +00001910
Steve Naroff33a1e802007-10-29 21:38:07 +00001911 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001912
Ted Kremenek5eec2b02010-11-10 05:59:39 +00001913 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +00001914 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001915 if (Tok.is(tok::kw___attribute))
Ted Kremenek5eec2b02010-11-10 05:59:39 +00001916 AttrList = ParseGNUAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +00001917
Douglas Gregor0be31a22010-07-02 17:43:08 +00001918 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001919 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001920 LBraceLoc, RBraceLoc,
Ted Kremenek5eec2b02010-11-10 05:59:39 +00001921 AttrList);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001922 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001923 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001924}
1925
1926
Chris Lattner3b561a32006-08-13 00:12:11 +00001927/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001928/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001929/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001930///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001931/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1932/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001933/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001934/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001935///
Douglas Gregor0bf31402010-10-08 23:50:27 +00001936/// [C++0x] enum-head '{' enumerator-list[opt] '}'
1937/// [C++0x] enum-head '{' enumerator-list ',' '}'
1938///
1939/// enum-head: [C++0x]
1940/// enum-key attributes[opt] identifier[opt] enum-base[opt]
1941/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
1942///
1943/// enum-key: [C++0x]
1944/// 'enum'
1945/// 'enum' 'class'
1946/// 'enum' 'struct'
1947///
1948/// enum-base: [C++0x]
1949/// ':' type-specifier-seq
1950///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001951/// [C++] elaborated-type-specifier:
1952/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1953///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001954void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001955 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001956 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001957 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001958 if (Tok.is(tok::code_completion)) {
1959 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001960 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001961 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001962 }
1963
Ted Kremenek5eec2b02010-11-10 05:59:39 +00001964 AttributeList *Attr = 0;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001965 // If attributes exist after tag, parse them.
1966 if (Tok.is(tok::kw___attribute))
Ted Kremenek5eec2b02010-11-10 05:59:39 +00001967 Attr = ParseGNUAttributes();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001968
Abramo Bagnarad7548482010-05-19 21:37:53 +00001969 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00001970 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00001971 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00001972 return;
1973
1974 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001975 Diag(Tok, diag::err_expected_ident);
1976 if (Tok.isNot(tok::l_brace)) {
1977 // Has no name and is not a definition.
1978 // Skip the rest of this declarator, up until the comma or semicolon.
1979 SkipUntil(tok::comma, true);
1980 return;
1981 }
1982 }
1983 }
Mike Stump11289f42009-09-09 15:08:12 +00001984
Douglas Gregor0bf31402010-10-08 23:50:27 +00001985 bool IsScopedEnum = false;
1986
1987 if (getLang().CPlusPlus0x && (Tok.is(tok::kw_class)
1988 || Tok.is(tok::kw_struct))) {
1989 ConsumeToken();
1990 IsScopedEnum = true;
1991 }
1992
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001993 // Must have either 'enum name' or 'enum {...}'.
1994 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1995 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001996
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001997 // Skip the rest of this declarator, up until the comma or semicolon.
1998 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001999 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002000 }
Mike Stump11289f42009-09-09 15:08:12 +00002001
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002002 // If an identifier is present, consume and remember it.
2003 IdentifierInfo *Name = 0;
2004 SourceLocation NameLoc;
2005 if (Tok.is(tok::identifier)) {
2006 Name = Tok.getIdentifierInfo();
2007 NameLoc = ConsumeToken();
2008 }
Mike Stump11289f42009-09-09 15:08:12 +00002009
Douglas Gregor0bf31402010-10-08 23:50:27 +00002010 if (!Name && IsScopedEnum) {
2011 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2012 // declaration of a scoped enumeration.
2013 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2014 IsScopedEnum = false;
2015 }
2016
2017 TypeResult BaseType;
2018
2019 if (getLang().CPlusPlus0x && Tok.is(tok::colon)) {
2020 ConsumeToken();
2021 SourceRange Range;
2022 BaseType = ParseTypeName(&Range);
2023 }
2024
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002025 // There are three options here. If we have 'enum foo;', then this is a
2026 // forward declaration. If we have 'enum foo {...' then this is a
2027 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2028 //
2029 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2030 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2031 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2032 //
John McCallfaf5fb42010-08-26 23:41:50 +00002033 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002034 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002035 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002036 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002037 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002038 else
John McCallfaf5fb42010-08-26 23:41:50 +00002039 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002040
2041 // enums cannot be templates, although they can be referenced from a
2042 // template.
2043 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002044 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002045 Diag(Tok, diag::err_enum_template);
2046
2047 // Skip the rest of this declarator, up until the comma or semicolon.
2048 SkipUntil(tok::comma, true);
2049 return;
2050 }
2051
Douglas Gregord6ab8742009-05-28 23:31:59 +00002052 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002053 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002054 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2055 const char *PrevSpec = 0;
2056 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002057 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002058 StartLoc, SS, Name, NameLoc, Attr,
John McCall48871652010-08-21 09:40:31 +00002059 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002060 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002061 Owned, IsDependent, IsScopedEnum,
2062 BaseType);
2063
Douglas Gregorba41d012010-04-24 16:38:41 +00002064 if (IsDependent) {
2065 // This enum has a dependent nested-name-specifier. Handle it as a
2066 // dependent tag.
2067 if (!Name) {
2068 DS.SetTypeSpecError();
2069 Diag(Tok, diag::err_expected_type_name_after_typename);
2070 return;
2071 }
2072
Douglas Gregor0be31a22010-07-02 17:43:08 +00002073 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002074 TUK, SS, Name, StartLoc,
2075 NameLoc);
2076 if (Type.isInvalid()) {
2077 DS.SetTypeSpecError();
2078 return;
2079 }
2080
2081 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallba7bf592010-08-24 05:47:05 +00002082 Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002083 Diag(StartLoc, DiagID) << PrevSpec;
2084
2085 return;
2086 }
Mike Stump11289f42009-09-09 15:08:12 +00002087
John McCall48871652010-08-21 09:40:31 +00002088 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002089 // The action failed to produce an enumeration tag. If this is a
2090 // definition, consume the entire definition.
2091 if (Tok.is(tok::l_brace)) {
2092 ConsumeBrace();
2093 SkipUntil(tok::r_brace);
2094 }
2095
2096 DS.SetTypeSpecError();
2097 return;
2098 }
2099
Chris Lattner76c72282007-10-09 17:33:22 +00002100 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002101 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002102
John McCallba7bf592010-08-24 05:47:05 +00002103 // FIXME: The DeclSpec should keep the locations of both the keyword
2104 // and the name (if there is one).
Douglas Gregor72100632010-01-25 16:33:23 +00002105 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCall48871652010-08-21 09:40:31 +00002106 TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002107 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002108}
2109
Chris Lattnerc1915e22007-01-25 07:29:02 +00002110/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2111/// enumerator-list:
2112/// enumerator
2113/// enumerator-list ',' enumerator
2114/// enumerator:
2115/// enumeration-constant
2116/// enumeration-constant '=' constant-expression
2117/// enumeration-constant:
2118/// identifier
2119///
John McCall48871652010-08-21 09:40:31 +00002120void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002121 // Enter the scope of the enum body and start the definition.
2122 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002123 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002124
Chris Lattnerc1915e22007-01-25 07:29:02 +00002125 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002126
Chris Lattner37256fb2007-08-27 17:24:30 +00002127 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002128 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002129 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002130
John McCall48871652010-08-21 09:40:31 +00002131 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002132
John McCall48871652010-08-21 09:40:31 +00002133 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002134
Chris Lattnerc1915e22007-01-25 07:29:02 +00002135 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002136 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002137 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2138 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002139
John McCall811a0f52010-10-22 23:36:17 +00002140 // If attributes exist after the enumerator, parse them.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002141 AttributeList *Attr = 0;
John McCall811a0f52010-10-22 23:36:17 +00002142 if (Tok.is(tok::kw___attribute))
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002143 Attr = ParseGNUAttributes();
John McCall811a0f52010-10-22 23:36:17 +00002144
Chris Lattnerc1915e22007-01-25 07:29:02 +00002145 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002146 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002147 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002148 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002149 AssignedVal = ParseConstantExpression();
2150 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002151 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002152 }
Mike Stump11289f42009-09-09 15:08:12 +00002153
Chris Lattnerc1915e22007-01-25 07:29:02 +00002154 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002155 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2156 LastEnumConstDecl,
2157 IdentLoc, Ident,
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002158 Attr, EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002159 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002160 EnumConstantDecls.push_back(EnumConstDecl);
2161 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002162
Douglas Gregorce66d022010-09-07 14:51:08 +00002163 if (Tok.is(tok::identifier)) {
2164 // We're missing a comma between enumerators.
2165 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2166 Diag(Loc, diag::err_enumerator_list_missing_comma)
2167 << FixItHint::CreateInsertion(Loc, ", ");
2168 continue;
2169 }
2170
Chris Lattner76c72282007-10-09 17:33:22 +00002171 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002172 break;
2173 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002174
2175 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002176 !(getLang().C99 || getLang().CPlusPlus0x))
2177 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2178 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002179 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002180 }
Mike Stump11289f42009-09-09 15:08:12 +00002181
Chris Lattnerc1915e22007-01-25 07:29:02 +00002182 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002183 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002184
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002185 AttributeList *Attr = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002186 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00002187 if (Tok.is(tok::kw___attribute))
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002188 Attr = ParseGNUAttributes(); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002189
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002190 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2191 EnumConstantDecls.data(), EnumConstantDecls.size(),
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002192 getCurScope(), Attr);
Mike Stump11289f42009-09-09 15:08:12 +00002193
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002194 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002195 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002196}
Chris Lattner3b561a32006-08-13 00:12:11 +00002197
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002198/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002199/// start of a type-qualifier-list.
2200bool Parser::isTypeQualifier() const {
2201 switch (Tok.getKind()) {
2202 default: return false;
2203 // type-qualifier
2204 case tok::kw_const:
2205 case tok::kw_volatile:
2206 case tok::kw_restrict:
2207 return true;
2208 }
2209}
2210
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002211/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2212/// is definitely a type-specifier. Return false if it isn't part of a type
2213/// specifier or if we're not sure.
2214bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2215 switch (Tok.getKind()) {
2216 default: return false;
2217 // type-specifiers
2218 case tok::kw_short:
2219 case tok::kw_long:
2220 case tok::kw_signed:
2221 case tok::kw_unsigned:
2222 case tok::kw__Complex:
2223 case tok::kw__Imaginary:
2224 case tok::kw_void:
2225 case tok::kw_char:
2226 case tok::kw_wchar_t:
2227 case tok::kw_char16_t:
2228 case tok::kw_char32_t:
2229 case tok::kw_int:
2230 case tok::kw_float:
2231 case tok::kw_double:
2232 case tok::kw_bool:
2233 case tok::kw__Bool:
2234 case tok::kw__Decimal32:
2235 case tok::kw__Decimal64:
2236 case tok::kw__Decimal128:
2237 case tok::kw___vector:
2238
2239 // struct-or-union-specifier (C99) or class-specifier (C++)
2240 case tok::kw_class:
2241 case tok::kw_struct:
2242 case tok::kw_union:
2243 // enum-specifier
2244 case tok::kw_enum:
2245
2246 // typedef-name
2247 case tok::annot_typename:
2248 return true;
2249 }
2250}
2251
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002252/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002253/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002254bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002255 switch (Tok.getKind()) {
2256 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002257
Chris Lattner020bab92009-01-04 23:41:41 +00002258 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002259 if (TryAltiVecVectorToken())
2260 return true;
2261 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002262 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002263 // Annotate typenames and C++ scope specifiers. If we get one, just
2264 // recurse to handle whatever we get.
2265 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002266 return true;
2267 if (Tok.is(tok::identifier))
2268 return false;
2269 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002270
Chris Lattner020bab92009-01-04 23:41:41 +00002271 case tok::coloncolon: // ::foo::bar
2272 if (NextToken().is(tok::kw_new) || // ::new
2273 NextToken().is(tok::kw_delete)) // ::delete
2274 return false;
2275
Chris Lattner020bab92009-01-04 23:41:41 +00002276 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002277 return true;
2278 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002279
Chris Lattnere37e2332006-08-15 04:50:22 +00002280 // GNU attributes support.
2281 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002282 // GNU typeof support.
2283 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002284
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002285 // type-specifiers
2286 case tok::kw_short:
2287 case tok::kw_long:
2288 case tok::kw_signed:
2289 case tok::kw_unsigned:
2290 case tok::kw__Complex:
2291 case tok::kw__Imaginary:
2292 case tok::kw_void:
2293 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002294 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002295 case tok::kw_char16_t:
2296 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002297 case tok::kw_int:
2298 case tok::kw_float:
2299 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002300 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002301 case tok::kw__Bool:
2302 case tok::kw__Decimal32:
2303 case tok::kw__Decimal64:
2304 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002305 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002306
Chris Lattner861a2262008-04-13 18:59:07 +00002307 // struct-or-union-specifier (C99) or class-specifier (C++)
2308 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002309 case tok::kw_struct:
2310 case tok::kw_union:
2311 // enum-specifier
2312 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002313
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002314 // type-qualifier
2315 case tok::kw_const:
2316 case tok::kw_volatile:
2317 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002318
2319 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002320 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002321 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002322
Chris Lattner409bf7d2008-10-20 00:25:30 +00002323 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2324 case tok::less:
2325 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002326
Steve Naroff44ac7772008-12-25 14:16:32 +00002327 case tok::kw___cdecl:
2328 case tok::kw___stdcall:
2329 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002330 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002331 case tok::kw___w64:
2332 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002333 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002334 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002335 }
2336}
2337
Chris Lattneracd58a32006-08-06 17:24:14 +00002338/// isDeclarationSpecifier() - Return true if the current token is part of a
2339/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002340///
2341/// \param DisambiguatingWithExpression True to indicate that the purpose of
2342/// this check is to disambiguate between an expression and a declaration.
2343bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002344 switch (Tok.getKind()) {
2345 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002346
Chris Lattner020bab92009-01-04 23:41:41 +00002347 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002348 // Unfortunate hack to support "Class.factoryMethod" notation.
2349 if (getLang().ObjC1 && NextToken().is(tok::period))
2350 return false;
John Thompson22334602010-02-05 00:12:22 +00002351 if (TryAltiVecVectorToken())
2352 return true;
2353 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002354 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002355 // Annotate typenames and C++ scope specifiers. If we get one, just
2356 // recurse to handle whatever we get.
2357 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002358 return true;
2359 if (Tok.is(tok::identifier))
2360 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002361
2362 // If we're in Objective-C and we have an Objective-C class type followed
2363 // by an identifier and then either ':' or ']', in a place where an
2364 // expression is permitted, then this is probably a class message send
2365 // missing the initial '['. In this case, we won't consider this to be
2366 // the start of a declaration.
2367 if (DisambiguatingWithExpression &&
2368 isStartOfObjCClassMessageMissingOpenBracket())
2369 return false;
2370
John McCall1f476a12010-02-26 08:45:28 +00002371 return isDeclarationSpecifier();
2372
Chris Lattner020bab92009-01-04 23:41:41 +00002373 case tok::coloncolon: // ::foo::bar
2374 if (NextToken().is(tok::kw_new) || // ::new
2375 NextToken().is(tok::kw_delete)) // ::delete
2376 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002377
Chris Lattner020bab92009-01-04 23:41:41 +00002378 // Annotate typenames and C++ scope specifiers. If we get one, just
2379 // recurse to handle whatever we get.
2380 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002381 return true;
2382 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002383
Chris Lattneracd58a32006-08-06 17:24:14 +00002384 // storage-class-specifier
2385 case tok::kw_typedef:
2386 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002387 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002388 case tok::kw_static:
2389 case tok::kw_auto:
2390 case tok::kw_register:
2391 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002392
Chris Lattneracd58a32006-08-06 17:24:14 +00002393 // type-specifiers
2394 case tok::kw_short:
2395 case tok::kw_long:
2396 case tok::kw_signed:
2397 case tok::kw_unsigned:
2398 case tok::kw__Complex:
2399 case tok::kw__Imaginary:
2400 case tok::kw_void:
2401 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002402 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002403 case tok::kw_char16_t:
2404 case tok::kw_char32_t:
2405
Chris Lattneracd58a32006-08-06 17:24:14 +00002406 case tok::kw_int:
2407 case tok::kw_float:
2408 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002409 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002410 case tok::kw__Bool:
2411 case tok::kw__Decimal32:
2412 case tok::kw__Decimal64:
2413 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002414 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002415
Chris Lattner861a2262008-04-13 18:59:07 +00002416 // struct-or-union-specifier (C99) or class-specifier (C++)
2417 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002418 case tok::kw_struct:
2419 case tok::kw_union:
2420 // enum-specifier
2421 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002422
Chris Lattneracd58a32006-08-06 17:24:14 +00002423 // type-qualifier
2424 case tok::kw_const:
2425 case tok::kw_volatile:
2426 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002427
Chris Lattneracd58a32006-08-06 17:24:14 +00002428 // function-specifier
2429 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002430 case tok::kw_virtual:
2431 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002432
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002433 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002434 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002435
Chris Lattner599e47e2007-08-09 17:01:07 +00002436 // GNU typeof support.
2437 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002438
Chris Lattner599e47e2007-08-09 17:01:07 +00002439 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002440 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002441 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002442
Chris Lattner8b2ec162008-07-26 03:38:44 +00002443 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2444 case tok::less:
2445 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002446
Steve Narofff192fab2009-01-06 19:34:12 +00002447 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002448 case tok::kw___cdecl:
2449 case tok::kw___stdcall:
2450 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002451 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002452 case tok::kw___w64:
2453 case tok::kw___ptr64:
2454 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002455 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002456 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002457 }
2458}
2459
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002460bool Parser::isConstructorDeclarator() {
2461 TentativeParsingAction TPA(*this);
2462
2463 // Parse the C++ scope specifier.
2464 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002465 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00002466 TPA.Revert();
2467 return false;
2468 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002469
2470 // Parse the constructor name.
2471 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2472 // We already know that we have a constructor name; just consume
2473 // the token.
2474 ConsumeToken();
2475 } else {
2476 TPA.Revert();
2477 return false;
2478 }
2479
2480 // Current class name must be followed by a left parentheses.
2481 if (Tok.isNot(tok::l_paren)) {
2482 TPA.Revert();
2483 return false;
2484 }
2485 ConsumeParen();
2486
2487 // A right parentheses or ellipsis signals that we have a constructor.
2488 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2489 TPA.Revert();
2490 return true;
2491 }
2492
2493 // If we need to, enter the specified scope.
2494 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002495 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002496 DeclScopeObj.EnterDeclaratorScope();
2497
2498 // Check whether the next token(s) are part of a declaration
2499 // specifier, in which case we have the start of a parameter and,
2500 // therefore, we know that this is a constructor.
2501 bool IsConstructor = isDeclarationSpecifier();
2502 TPA.Revert();
2503 return IsConstructor;
2504}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002505
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002506/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00002507/// type-qualifier-list: [C99 6.7.5]
2508/// type-qualifier
2509/// [vendor] attributes
2510/// [ only if VendorAttributesAllowed=true ]
2511/// type-qualifier-list type-qualifier
2512/// [vendor] type-qualifier-list attributes
2513/// [ only if VendorAttributesAllowed=true ]
2514/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2515/// [ only if CXX0XAttributesAllowed=true ]
2516/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002517///
Dawn Perchik335e16b2010-09-03 01:29:35 +00002518void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2519 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00002520 bool CXX0XAttributesAllowed) {
2521 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2522 SourceLocation Loc = Tok.getLocation();
2523 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2524 if (CXX0XAttributesAllowed)
2525 DS.AddAttributes(Attr.AttrList);
2526 else
2527 Diag(Loc, diag::err_attributes_not_allowed);
2528 }
2529
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002530 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002531 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002532 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002533 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002534 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002535
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002536 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00002537 case tok::code_completion:
2538 Actions.CodeCompleteTypeQualifiers(DS);
2539 ConsumeCodeCompletionToken();
2540 break;
2541
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002542 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002543 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2544 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002545 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002546 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002547 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2548 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002549 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002550 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002551 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2552 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002553 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002554 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002555 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002556 case tok::kw___cdecl:
2557 case tok::kw___stdcall:
2558 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002559 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002560 if (VendorAttributesAllowed) {
Eli Friedman53339e02009-06-08 23:27:34 +00002561 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2562 continue;
2563 }
2564 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002565 case tok::kw___pascal:
2566 if (VendorAttributesAllowed) {
2567 DS.AddAttributes(ParseBorlandTypeAttributes());
2568 continue;
2569 }
2570 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002571 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002572 if (VendorAttributesAllowed) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002573 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002574 continue; // do *not* consume the next token!
2575 }
2576 // otherwise, FALL THROUGH!
2577 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002578 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002579 // If this is not a type-qualifier token, we're done reading type
2580 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002581 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002582 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002583 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002584
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002585 // If the specifier combination wasn't legal, issue a diagnostic.
2586 if (isInvalid) {
2587 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002588 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002589 }
2590 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002591 }
2592}
2593
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002594
2595/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2596///
2597void Parser::ParseDeclarator(Declarator &D) {
2598 /// This implements the 'declarator' production in the C grammar, then checks
2599 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002600 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002601}
2602
Sebastian Redlbd150f42008-11-21 19:14:01 +00002603/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2604/// is parsed by the function passed to it. Pass null, and the direct-declarator
2605/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002606/// ptr-operator production.
2607///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002608/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2609/// [C] pointer[opt] direct-declarator
2610/// [C++] direct-declarator
2611/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002612///
2613/// pointer: [C99 6.7.5]
2614/// '*' type-qualifier-list[opt]
2615/// '*' type-qualifier-list[opt] pointer
2616///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002617/// ptr-operator:
2618/// '*' cv-qualifier-seq[opt]
2619/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002620/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002621/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002622/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002623/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002624void Parser::ParseDeclaratorInternal(Declarator &D,
2625 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002626 if (Diags.hasAllExtensionsSilenced())
2627 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002628
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002629 // C++ member pointers start with a '::' or a nested-name.
2630 // Member pointers get special handling, since there's no place for the
2631 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002632 if (getLang().CPlusPlus &&
2633 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2634 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002635 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002636 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00002637
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002638 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002639 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002640 // The scope spec really belongs to the direct-declarator.
2641 D.getCXXScopeSpec() = SS;
2642 if (DirectDeclParser)
2643 (this->*DirectDeclParser)(D);
2644 return;
2645 }
2646
2647 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002648 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002649 DeclSpec DS;
2650 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002651 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002652
2653 // Recurse to parse whatever is left.
2654 ParseDeclaratorInternal(D, DirectDeclParser);
2655
2656 // Sema will have to catch (syntactically invalid) pointers into global
2657 // scope. It has to catch pointers into namespace scope anyway.
2658 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002659 Loc, DS.TakeAttributes()),
2660 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002661 return;
2662 }
2663 }
2664
2665 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002666 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002667 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002668 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002669 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002670 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002671 if (DirectDeclParser)
2672 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002673 return;
2674 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002675
Sebastian Redled0f3b02009-03-15 22:02:01 +00002676 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2677 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002678 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002679 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002680
Chris Lattner9eac9312009-03-27 04:18:06 +00002681 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002682 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002683 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002684
Bill Wendling3708c182007-05-27 10:15:43 +00002685 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002686 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002687
Bill Wendling3708c182007-05-27 10:15:43 +00002688 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002689 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002690 if (Kind == tok::star)
2691 // Remember that we parsed a pointer type, and remember the type-quals.
2692 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002693 DS.TakeAttributes()),
2694 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002695 else
2696 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002697 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002698 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002699 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002700 } else {
2701 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002702 DeclSpec DS;
2703
Sebastian Redl3b27be62009-03-23 00:00:23 +00002704 // Complain about rvalue references in C++03, but then go on and build
2705 // the declarator.
2706 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2707 Diag(Loc, diag::err_rvalue_reference);
2708
Bill Wendling93efb222007-06-02 23:28:54 +00002709 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2710 // cv-qualifiers are introduced through the use of a typedef or of a
2711 // template type argument, in which case the cv-qualifiers are ignored.
2712 //
2713 // [GNU] Retricted references are allowed.
2714 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002715 // [C++0x] Attributes on references are not allowed.
2716 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002717 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002718
2719 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2720 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2721 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002722 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002723 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2724 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002725 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002726 }
Bill Wendling3708c182007-05-27 10:15:43 +00002727
2728 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002729 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002730
Douglas Gregor66583c52008-11-03 15:51:28 +00002731 if (D.getNumTypeObjects() > 0) {
2732 // C++ [dcl.ref]p4: There shall be no references to references.
2733 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2734 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002735 if (const IdentifierInfo *II = D.getIdentifier())
2736 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2737 << II;
2738 else
2739 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2740 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002741
Sebastian Redlbd150f42008-11-21 19:14:01 +00002742 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002743 // can go ahead and build the (technically ill-formed)
2744 // declarator: reference collapsing will take care of it.
2745 }
2746 }
2747
Bill Wendling3708c182007-05-27 10:15:43 +00002748 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002749 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002750 DS.TakeAttributes(),
2751 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002752 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002753 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002754}
2755
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002756/// ParseDirectDeclarator
2757/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002758/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002759/// '(' declarator ')'
2760/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002761/// [C90] direct-declarator '[' constant-expression[opt] ']'
2762/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2763/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2764/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2765/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002766/// direct-declarator '(' parameter-type-list ')'
2767/// direct-declarator '(' identifier-list[opt] ')'
2768/// [GNU] direct-declarator '(' parameter-forward-declarations
2769/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002770/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2771/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002772/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002773///
2774/// declarator-id: [C++ 8]
2775/// id-expression
2776/// '::'[opt] nested-name-specifier[opt] type-name
2777///
2778/// id-expression: [C++ 5.1]
2779/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002780/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002781///
2782/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002783/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002784/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002785/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002786/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002787/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002788///
Chris Lattneracd58a32006-08-06 17:24:14 +00002789void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002790 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002791
Douglas Gregor7861a802009-11-03 01:35:08 +00002792 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2793 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002794 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00002795 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00002796 }
2797
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002798 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002799 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00002800 // Change the declaration context for name lookup, until this function
2801 // is exited (and the declarator has been parsed).
2802 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002803 }
2804
Douglas Gregor7861a802009-11-03 01:35:08 +00002805 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2806 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2807 // We found something that indicates the start of an unqualified-id.
2808 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002809 bool AllowConstructorName;
2810 if (D.getDeclSpec().hasTypeSpecifier())
2811 AllowConstructorName = false;
2812 else if (D.getCXXScopeSpec().isSet())
2813 AllowConstructorName =
2814 (D.getContext() == Declarator::FileContext ||
2815 (D.getContext() == Declarator::MemberContext &&
2816 D.getDeclSpec().isFriendSpecified()));
2817 else
2818 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2819
Douglas Gregor7861a802009-11-03 01:35:08 +00002820 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2821 /*EnteringContext=*/true,
2822 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002823 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002824 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002825 D.getName()) ||
2826 // Once we're past the identifier, if the scope was bad, mark the
2827 // whole declarator bad.
2828 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002829 D.SetIdentifier(0, Tok.getLocation());
2830 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002831 } else {
2832 // Parsed the unqualified-id; update range information and move along.
2833 if (D.getSourceRange().getBegin().isInvalid())
2834 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2835 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002836 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002837 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002838 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002839 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002840 assert(!getLang().CPlusPlus &&
2841 "There's a C++-specific check for tok::identifier above");
2842 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2843 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2844 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002845 goto PastIdentifier;
2846 }
2847
2848 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002849 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002850 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002851 // Example: 'char (*X)' or 'int (*XX)(void)'
2852 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002853
2854 // If the declarator was parenthesized, we entered the declarator
2855 // scope when parsing the parenthesized declarator, then exited
2856 // the scope already. Re-enter the scope, if we need to.
2857 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002858 // If there was an error parsing parenthesized declarator, declarator
2859 // scope may have been enterred before. Don't do it again.
2860 if (!D.isInvalidType() &&
2861 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002862 // Change the declaration context for name lookup, until this function
2863 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002864 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002865 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002866 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002867 // This could be something simple like "int" (in which case the declarator
2868 // portion is empty), if an abstract-declarator is allowed.
2869 D.SetIdentifier(0, Tok.getLocation());
2870 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002871 if (D.getContext() == Declarator::MemberContext)
2872 Diag(Tok, diag::err_expected_member_name_or_semi)
2873 << D.getDeclSpec().getSourceRange();
2874 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002875 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002876 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002877 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002878 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002879 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002880 }
Mike Stump11289f42009-09-09 15:08:12 +00002881
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002882 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002883 assert(D.isPastIdentifier() &&
2884 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002885
Alexis Hunt96d5c762009-11-21 08:43:09 +00002886 // Don't parse attributes unless we have an identifier.
Douglas Gregor0286b462010-02-19 16:47:56 +00002887 if (D.getIdentifier() && getLang().CPlusPlus0x
Alexis Hunt96d5c762009-11-21 08:43:09 +00002888 && isCXX0XAttributeSpecifier(true)) {
2889 SourceLocation AttrEndLoc;
2890 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2891 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2892 }
2893
Chris Lattneracd58a32006-08-06 17:24:14 +00002894 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002895 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002896 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2897 // In such a case, check if we actually have a function declarator; if it
2898 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002899 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2900 // When not in file scope, warn for ambiguous function declarators, just
2901 // in case the author intended it as a variable definition.
2902 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2903 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2904 break;
2905 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002906 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002907 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002908 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002909 } else {
2910 break;
2911 }
2912 }
2913}
2914
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002915/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2916/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002917/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002918/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2919///
2920/// direct-declarator:
2921/// '(' declarator ')'
2922/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002923/// direct-declarator '(' parameter-type-list ')'
2924/// direct-declarator '(' identifier-list[opt] ')'
2925/// [GNU] direct-declarator '(' parameter-forward-declarations
2926/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002927///
2928void Parser::ParseParenDeclarator(Declarator &D) {
2929 SourceLocation StartLoc = ConsumeParen();
2930 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002931
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002932 // Eat any attributes before we look at whether this is a grouping or function
2933 // declarator paren. If this is a grouping paren, the attribute applies to
2934 // the type being built up, for example:
2935 // int (__attribute__(()) *x)(long y)
2936 // If this ends up not being a grouping paren, the attribute applies to the
2937 // first argument, for example:
2938 // int (__attribute__(()) int x)
2939 // In either case, we need to eat any attributes to be able to determine what
2940 // sort of paren this is.
2941 //
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002942 AttributeList *AttrList = 0;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002943 bool RequiresArg = false;
2944 if (Tok.is(tok::kw___attribute)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002945 AttrList = ParseGNUAttributes();
Mike Stump11289f42009-09-09 15:08:12 +00002946
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002947 // We require that the argument list (if this is a non-grouping paren) be
2948 // present even if the attribute list was empty.
2949 RequiresArg = true;
2950 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002951 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002952 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00002953 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2954 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002955 AttrList = ParseMicrosoftTypeAttributes(AttrList);
Eli Friedman53339e02009-06-08 23:27:34 +00002956 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00002957 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002958 if (Tok.is(tok::kw___pascal))
2959 AttrList = ParseBorlandTypeAttributes(AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00002960
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002961 // If we haven't past the identifier yet (or where the identifier would be
2962 // stored, if this is an abstract declarator), then this is probably just
2963 // grouping parens. However, if this could be an abstract-declarator, then
2964 // this could also be the start of function arguments (consider 'void()').
2965 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002966
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002967 if (!D.mayOmitIdentifier()) {
2968 // If this can't be an abstract-declarator, this *must* be a grouping
2969 // paren, because we haven't seen the identifier yet.
2970 isGrouping = true;
2971 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002972 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002973 isDeclarationSpecifier()) { // 'int(int)' is a function.
2974 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2975 // considered to be a type, not a K&R identifier-list.
2976 isGrouping = false;
2977 } else {
2978 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2979 isGrouping = true;
2980 }
Mike Stump11289f42009-09-09 15:08:12 +00002981
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002982 // If this is a grouping paren, handle:
2983 // direct-declarator: '(' declarator ')'
2984 // direct-declarator: '(' attributes declarator ')'
2985 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002986 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002987 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002988 if (AttrList)
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002989 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002990
Sebastian Redlbd150f42008-11-21 19:14:01 +00002991 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002992 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002993 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002994
2995 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002996 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002997 return;
2998 }
Mike Stump11289f42009-09-09 15:08:12 +00002999
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003000 // Okay, if this wasn't a grouping paren, it must be the start of a function
3001 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003002 // identifier (and remember where it would have been), then call into
3003 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003004 D.SetIdentifier(0, Tok.getLocation());
3005
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003006 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003007}
3008
3009/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3010/// declarator D up to a paren, which indicates that we are parsing function
3011/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003012///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003013/// If AttrList is non-null, then the caller parsed those arguments immediately
3014/// after the open paren - they should be considered to be the first argument of
3015/// a parameter. If RequiresArg is true, then the first argument of the
3016/// function is required to be present and required to not be an identifier
3017/// list.
3018///
Chris Lattneracd58a32006-08-06 17:24:14 +00003019/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003020/// parameter-type-list: [C99 6.7.5]
3021/// parameter-list
3022/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003023/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003024///
3025/// parameter-list: [C99 6.7.5]
3026/// parameter-declaration
3027/// parameter-list ',' parameter-declaration
3028///
3029/// parameter-declaration: [C99 6.7.5]
3030/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003031/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003032/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003033/// declaration-specifiers abstract-declarator[opt]
3034/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003035/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003036/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003037///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003038/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00003039/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003040///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003041void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3042 AttributeList *AttrList,
3043 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003044 // lparen is already consumed!
3045 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003046
Douglas Gregor7fb25412010-10-01 18:44:50 +00003047 ParsedType TrailingReturnType;
3048
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003049 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003050 if (Tok.is(tok::r_paren)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003051 if (RequiresArg)
Chris Lattner6d29c102008-11-18 07:48:38 +00003052 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003053
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003054 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3055 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003056
3057 // cv-qualifier-seq[opt].
3058 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003059 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003060 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003061 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003062 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003063 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003064 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00003065 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003066 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003067 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003068
3069 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003070 if (Tok.is(tok::kw_throw)) {
3071 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003072 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003073 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003074 hasAnyExceptionSpec);
3075 assert(Exceptions.size() == ExceptionRanges.size() &&
3076 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003077 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003078
3079 // Parse trailing-return-type.
3080 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3081 TrailingReturnType = ParseTrailingReturnType().get();
3082 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003083 }
3084
Chris Lattner371ed4e2008-04-06 06:57:35 +00003085 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003086 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003087 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003088 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003089 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003090 /*arglist*/ 0, 0,
3091 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003092 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003093 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003094 Exceptions.data(),
3095 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003096 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003097 LParenLoc, RParenLoc, D,
3098 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003099 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003100 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003101 }
3102
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003103 // Alternatively, this parameter list may be an identifier list form for a
3104 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003105 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3106 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003107 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003108 // K&R identifier lists can't have typedefs as identifiers, per
3109 // C99 6.7.5.3p11.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003110 if (RequiresArg)
Steve Naroffb0486722009-01-28 19:16:40 +00003111 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner9453ab82010-05-14 17:23:36 +00003112
Steve Naroffb0486722009-01-28 19:16:40 +00003113 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003114 // normal declarators, not for abstract-declarators. Get the first
3115 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003116 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003117 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003118
3119 // Identifier lists follow a really simple grammar: the identifiers can
3120 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3121 // identifier lists are really rare in the brave new modern world, and it
3122 // is very common for someone to typo a type in a non-k&r style list. If
3123 // we are presented with something like: "void foo(intptr x, float y)",
3124 // we don't want to start parsing the function declarator as though it is
3125 // a K&R style declarator just because intptr is an invalid type.
3126 //
3127 // To handle this, we check to see if the token after the first identifier
3128 // is a "," or ")". Only if so, do we parse it as an identifier list.
3129 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3130 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3131 FirstTok.getIdentifierInfo(),
3132 FirstTok.getLocation(), D);
3133
3134 // If we get here, the code is invalid. Push the first identifier back
3135 // into the token stream and parse the first argument as an (invalid)
3136 // normal argument declarator.
3137 PP.EnterToken(Tok);
3138 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003139 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003140 }
Mike Stump11289f42009-09-09 15:08:12 +00003141
Chris Lattner371ed4e2008-04-06 06:57:35 +00003142 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003143
Chris Lattner371ed4e2008-04-06 06:57:35 +00003144 // Build up an array of information about the parsed arguments.
3145 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003146
3147 // Enter function-declaration scope, limiting any declarators to the
3148 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003149 ParseScope PrototypeScope(this,
3150 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003151
Chris Lattner371ed4e2008-04-06 06:57:35 +00003152 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003153 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003154 while (1) {
3155 if (Tok.is(tok::ellipsis)) {
3156 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003157 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003158 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003159 }
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003160
3161 // Skip any Microsoft attributes before a param.
3162 if (getLang().Microsoft && Tok.is(tok::l_square))
3163 ParseMicrosoftAttributes();
Mike Stump11289f42009-09-09 15:08:12 +00003164
Chris Lattner371ed4e2008-04-06 06:57:35 +00003165 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00003166
Chris Lattner371ed4e2008-04-06 06:57:35 +00003167 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003168 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003169 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003170
3171 // If the caller parsed attributes for the first argument, add them now.
3172 if (AttrList) {
3173 DS.AddAttributes(AttrList);
3174 AttrList = 0; // Only apply the attributes to the first parameter.
3175 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003176 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003177
Chris Lattner371ed4e2008-04-06 06:57:35 +00003178 // Parse the declarator. This is "PrototypeContext", because we must
3179 // accept either 'declarator' or 'abstract-declarator' here.
3180 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3181 ParseDeclarator(ParmDecl);
3182
3183 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003184 if (Tok.is(tok::kw___attribute)) {
3185 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003186 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003187 ParmDecl.AddAttributes(AttrList, Loc);
3188 }
Mike Stump11289f42009-09-09 15:08:12 +00003189
Chris Lattner371ed4e2008-04-06 06:57:35 +00003190 // Remember this parsed parameter in ParamInfo.
3191 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003192
Douglas Gregor4d87df52008-12-16 21:30:33 +00003193 // DefArgToks is used when the parsing of default arguments needs
3194 // to be delayed.
3195 CachedTokens *DefArgToks = 0;
3196
Chris Lattner371ed4e2008-04-06 06:57:35 +00003197 // If no parameter was specified, verify that *something* was specified,
3198 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003199 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3200 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003201 // Completely missing, emit error.
3202 Diag(DSStart, diag::err_missing_param);
3203 } else {
3204 // Otherwise, we have something. Add it and let semantic analysis try
3205 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003206
Chris Lattner371ed4e2008-04-06 06:57:35 +00003207 // Inform the actions module about the parameter declarator, so it gets
3208 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003209 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003210
3211 // Parse the default argument, if any. We parse the default
3212 // arguments in all dialects; the semantic analysis in
3213 // ActOnParamDefaultArgument will reject the default argument in
3214 // C.
3215 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003216 SourceLocation EqualLoc = Tok.getLocation();
3217
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003218 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003219 if (D.getContext() == Declarator::MemberContext) {
3220 // If we're inside a class definition, cache the tokens
3221 // corresponding to the default argument. We'll actually parse
3222 // them when we see the end of the class definition.
3223 // FIXME: Templates will require something similar.
3224 // FIXME: Can we use a smart pointer for Toks?
3225 DefArgToks = new CachedTokens;
3226
Mike Stump11289f42009-09-09 15:08:12 +00003227 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003228 /*StopAtSemi=*/true,
3229 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003230 delete DefArgToks;
3231 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003232 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003233 } else {
3234 // Mark the end of the default argument so that we know when to
3235 // stop when we parse it later on.
3236 Token DefArgEnd;
3237 DefArgEnd.startToken();
3238 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3239 DefArgEnd.setLocation(Tok.getLocation());
3240 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003241 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003242 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003243 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003244 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003245 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003246 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003247
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003248 // The argument isn't actually potentially evaluated unless it is
3249 // used.
3250 EnterExpressionEvaluationContext Eval(Actions,
3251 Sema::PotentiallyEvaluatedIfUsed);
3252
John McCalldadc5752010-08-24 06:29:42 +00003253 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003254 if (DefArgResult.isInvalid()) {
3255 Actions.ActOnParamDefaultArgumentError(Param);
3256 SkipUntil(tok::comma, tok::r_paren, true, true);
3257 } else {
3258 // Inform the actions module about the default argument
3259 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003260 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003261 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003262 }
3263 }
Mike Stump11289f42009-09-09 15:08:12 +00003264
3265 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3266 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003267 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003268 }
3269
3270 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003271 if (Tok.isNot(tok::comma)) {
3272 if (Tok.is(tok::ellipsis)) {
3273 IsVariadic = true;
3274 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3275
3276 if (!getLang().CPlusPlus) {
3277 // We have ellipsis without a preceding ',', which is ill-formed
3278 // in C. Complain and provide the fix.
3279 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003280 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003281 }
3282 }
3283
3284 break;
3285 }
Mike Stump11289f42009-09-09 15:08:12 +00003286
Chris Lattner371ed4e2008-04-06 06:57:35 +00003287 // Consume the comma.
3288 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003289 }
Mike Stump11289f42009-09-09 15:08:12 +00003290
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003291 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003292 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3293 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003294
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003295 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003296 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003297 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003298 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003299 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003300 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003301
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003302 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003303 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003304 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003305 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003306 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003307
3308 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003309 if (Tok.is(tok::kw_throw)) {
3310 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003311 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003312 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003313 hasAnyExceptionSpec);
3314 assert(Exceptions.size() == ExceptionRanges.size() &&
3315 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003316 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003317
3318 // Parse trailing-return-type.
3319 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3320 TrailingReturnType = ParseTrailingReturnType().get();
3321 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003322 }
3323
Douglas Gregor7fb25412010-10-01 18:44:50 +00003324 // FIXME: We should leave the prototype scope before parsing the exception
3325 // specification, and then reenter it when parsing the trailing return type.
3326
3327 // Leave prototype scope.
3328 PrototypeScope.Exit();
3329
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003330 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003331 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003332 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003333 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003334 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003335 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003336 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003337 Exceptions.data(),
3338 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003339 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003340 LParenLoc, RParenLoc, D,
3341 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003342 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003343}
Chris Lattneracd58a32006-08-06 17:24:14 +00003344
Chris Lattner6c940e62008-04-06 06:34:08 +00003345/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3346/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003347/// first identifier has already been consumed, and the current token is the
3348/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003349///
3350/// identifier-list: [C99 6.7.5]
3351/// identifier
3352/// identifier-list ',' identifier
3353///
3354void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003355 IdentifierInfo *FirstIdent,
3356 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003357 Declarator &D) {
3358 // Build up an array of information about the parsed arguments.
3359 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3360 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003361
Chris Lattner6c940e62008-04-06 06:34:08 +00003362 // If there was no identifier specified for the declarator, either we are in
3363 // an abstract-declarator, or we are in a parameter declarator which was found
3364 // to be abstract. In abstract-declarators, identifier lists are not valid:
3365 // diagnose this.
3366 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003367 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003368
Chris Lattner9453ab82010-05-14 17:23:36 +00003369 // The first identifier was already read, and is known to be the first
3370 // identifier in the list. Remember this identifier in ParamInfo.
3371 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003372 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003373
Chris Lattner6c940e62008-04-06 06:34:08 +00003374 while (Tok.is(tok::comma)) {
3375 // Eat the comma.
3376 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003377
Chris Lattner9186f552008-04-06 06:39:19 +00003378 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003379 if (Tok.isNot(tok::identifier)) {
3380 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003381 SkipUntil(tok::r_paren);
3382 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003383 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003384
Chris Lattner6c940e62008-04-06 06:34:08 +00003385 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003386
3387 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003388 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003389 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003390
Chris Lattner6c940e62008-04-06 06:34:08 +00003391 // Verify that the argument identifier has not already been mentioned.
3392 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003393 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003394 } else {
3395 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003396 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003397 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003398 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003399 }
Mike Stump11289f42009-09-09 15:08:12 +00003400
Chris Lattner6c940e62008-04-06 06:34:08 +00003401 // Eat the identifier.
3402 ConsumeToken();
3403 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003404
3405 // If we have the closing ')', eat it and we're done.
3406 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3407
Chris Lattner9186f552008-04-06 06:39:19 +00003408 // Remember that we parsed a function type, and remember the attributes. This
3409 // function type is always a K&R style function type, which is not varargs and
3410 // has no prototype.
3411 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003412 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003413 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003414 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003415 /*exception*/false,
3416 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003417 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003418 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003419}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003420
Chris Lattnere8074e62006-08-06 18:30:15 +00003421/// [C90] direct-declarator '[' constant-expression[opt] ']'
3422/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3423/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3424/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3425/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3426void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003427 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003428
Chris Lattner84a11622008-12-18 07:27:21 +00003429 // C array syntax has many features, but by-far the most common is [] and [4].
3430 // This code does a fast path to handle some of the most obvious cases.
3431 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003432 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003433 //FIXME: Use these
3434 CXX0XAttributeList Attr;
3435 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3436 Attr = ParseCXX0XAttributes();
3437 }
3438
Chris Lattner84a11622008-12-18 07:27:21 +00003439 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00003440 ExprResult NumElements;
Douglas Gregor04318252009-07-06 15:59:29 +00003441 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3442 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003443 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003444 return;
3445 } else if (Tok.getKind() == tok::numeric_constant &&
3446 GetLookAheadToken(1).is(tok::r_square)) {
3447 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00003448 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003449 ConsumeToken();
3450
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003451 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003452 //FIXME: Use these
3453 CXX0XAttributeList Attr;
3454 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3455 Attr = ParseCXX0XAttributes();
3456 }
Chris Lattner84a11622008-12-18 07:27:21 +00003457
3458 // If there was an error parsing the assignment-expression, recover.
3459 if (ExprRes.isInvalid())
3460 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00003461
Chris Lattner84a11622008-12-18 07:27:21 +00003462 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00003463 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3464 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003465 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003466 return;
3467 }
Mike Stump11289f42009-09-09 15:08:12 +00003468
Chris Lattnere8074e62006-08-06 18:30:15 +00003469 // If valid, this location is the position where we read the 'static' keyword.
3470 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003471 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003472 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003473
Chris Lattnere8074e62006-08-06 18:30:15 +00003474 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003475 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003476 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003477 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003478
Chris Lattnere8074e62006-08-06 18:30:15 +00003479 // If we haven't already read 'static', check to see if there is one after the
3480 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003481 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003482 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003483
Chris Lattnere8074e62006-08-06 18:30:15 +00003484 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003485 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00003486 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00003487
Chris Lattner521ff2b2008-04-06 05:26:30 +00003488 // Handle the case where we have '[*]' as the array size. However, a leading
3489 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3490 // the the token after the star is a ']'. Since stars in arrays are
3491 // infrequent, use of lookahead is not costly here.
3492 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003493 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003494
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003495 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003496 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003497 StaticLoc = SourceLocation(); // Drop the static.
3498 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003499 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003500 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003501 // Note, in C89, this production uses the constant-expr production instead
3502 // of assignment-expr. The only difference is that assignment-expr allows
3503 // things like '=' and '*='. Sema rejects these in C89 mode because they
3504 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003505
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003506 // Parse the constant-expression or assignment-expression now (depending
3507 // on dialect).
3508 if (getLang().CPlusPlus)
3509 NumElements = ParseConstantExpression();
3510 else
3511 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003512 }
Mike Stump11289f42009-09-09 15:08:12 +00003513
Chris Lattner62591722006-08-12 18:40:58 +00003514 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003515 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003516 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003517 // If the expression was invalid, skip it.
3518 SkipUntil(tok::r_square);
3519 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003520 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003521
3522 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3523
Alexis Hunt96d5c762009-11-21 08:43:09 +00003524 //FIXME: Use these
3525 CXX0XAttributeList Attr;
3526 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3527 Attr = ParseCXX0XAttributes();
3528 }
3529
Chris Lattner84a11622008-12-18 07:27:21 +00003530 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00003531 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3532 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003533 NumElements.release(),
3534 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003535 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003536}
3537
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003538/// [GNU] typeof-specifier:
3539/// typeof ( expressions )
3540/// typeof ( type-name )
3541/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003542///
3543void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003544 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003545 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003546 SourceLocation StartLoc = ConsumeToken();
3547
John McCalle8595032010-01-13 20:03:27 +00003548 const bool hasParens = Tok.is(tok::l_paren);
3549
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003550 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00003551 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003552 SourceRange CastRange;
John McCalldadc5752010-08-24 06:29:42 +00003553 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall6caebb12010-08-25 02:45:51 +00003554 isCastExpr,
3555 CastTy,
3556 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003557 if (hasParens)
3558 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003559
3560 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003561 // FIXME: Not accurate, the range gets one token more than it should.
3562 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003563 else
3564 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003565
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003566 if (isCastExpr) {
3567 if (!CastTy) {
3568 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003569 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003570 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003571
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003572 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003573 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003574 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3575 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003576 DiagID, CastTy))
3577 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003578 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003579 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003580
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003581 // If we get here, the operand to the typeof was an expresion.
3582 if (Operand.isInvalid()) {
3583 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003584 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003585 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003586
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003587 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003588 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003589 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3590 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00003591 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00003592 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003593}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003594
3595
3596/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3597/// from TryAltiVecVectorToken.
3598bool Parser::TryAltiVecVectorTokenOutOfLine() {
3599 Token Next = NextToken();
3600 switch (Next.getKind()) {
3601 default: return false;
3602 case tok::kw_short:
3603 case tok::kw_long:
3604 case tok::kw_signed:
3605 case tok::kw_unsigned:
3606 case tok::kw_void:
3607 case tok::kw_char:
3608 case tok::kw_int:
3609 case tok::kw_float:
3610 case tok::kw_double:
3611 case tok::kw_bool:
3612 case tok::kw___pixel:
3613 Tok.setKind(tok::kw___vector);
3614 return true;
3615 case tok::identifier:
3616 if (Next.getIdentifierInfo() == Ident_pixel) {
3617 Tok.setKind(tok::kw___vector);
3618 return true;
3619 }
3620 return false;
3621 }
3622}
3623
3624bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3625 const char *&PrevSpec, unsigned &DiagID,
3626 bool &isInvalid) {
3627 if (Tok.getIdentifierInfo() == Ident_vector) {
3628 Token Next = NextToken();
3629 switch (Next.getKind()) {
3630 case tok::kw_short:
3631 case tok::kw_long:
3632 case tok::kw_signed:
3633 case tok::kw_unsigned:
3634 case tok::kw_void:
3635 case tok::kw_char:
3636 case tok::kw_int:
3637 case tok::kw_float:
3638 case tok::kw_double:
3639 case tok::kw_bool:
3640 case tok::kw___pixel:
3641 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3642 return true;
3643 case tok::identifier:
3644 if (Next.getIdentifierInfo() == Ident_pixel) {
3645 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3646 return true;
3647 }
3648 break;
3649 default:
3650 break;
3651 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00003652 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003653 DS.isTypeAltiVecVector()) {
3654 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3655 return true;
3656 }
3657 return false;
3658}
3659