blob: ed58a92323d98bb59fd307f33b236b55ba806360 [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);
Douglas Gregorc5790df2009-09-28 07:26:33 +00001023 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
John McCallba7bf592010-08-24 05:47:05 +00001024 PrevSpec, DiagID, T);
1025 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001026 else
1027 DS.SetTypeSpecError();
1028 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1029 ConsumeToken(); // The typename
1030 }
1031
Douglas Gregor167fa622009-03-25 15:40:00 +00001032 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001033 goto DoneWithDeclSpec;
1034
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001035 // If we're in a context where the identifier could be a class name,
1036 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001037 if ((DSContext == DSC_top_level ||
1038 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001039 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001040 &SS)) {
1041 if (isConstructorDeclarator())
1042 goto DoneWithDeclSpec;
1043
1044 // As noted in C++ [class.qual]p2 (cited above), when the name
1045 // of the class is qualified in a context where it could name
1046 // a constructor, its a constructor name. However, we've
1047 // looked at the declarator, and the user probably meant this
1048 // to be a type. Complain that it isn't supposed to be treated
1049 // as a type, then proceed to parse it as a type.
1050 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1051 << Next.getIdentifierInfo();
1052 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001053
John McCallba7bf592010-08-24 05:47:05 +00001054 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1055 Next.getLocation(),
1056 getCurScope(), &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001057
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001058 // If the referenced identifier is not a type, then this declspec is
1059 // erroneous: We already checked about that it has no type specifier, and
1060 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001061 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001062 if (TypeRep == 0) {
1063 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001064 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001065 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001066 }
Mike Stump11289f42009-09-09 15:08:12 +00001067
John McCall9dab4e62009-12-12 11:40:51 +00001068 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001069 ConsumeToken(); // The C++ scope.
1070
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001071 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001072 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001073 if (isInvalid)
1074 break;
Mike Stump11289f42009-09-09 15:08:12 +00001075
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001076 DS.SetRangeEnd(Tok.getLocation());
1077 ConsumeToken(); // The typename.
1078
1079 continue;
1080 }
Mike Stump11289f42009-09-09 15:08:12 +00001081
Chris Lattnere387d9e2009-01-21 19:48:37 +00001082 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001083 if (Tok.getAnnotationValue()) {
1084 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001085 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001086 DiagID, T);
1087 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001088 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001089
1090 if (isInvalid)
1091 break;
1092
Chris Lattnere387d9e2009-01-21 19:48:37 +00001093 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1094 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001095
Chris Lattnere387d9e2009-01-21 19:48:37 +00001096 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1097 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001098 // Objective-C interface.
1099 if (Tok.is(tok::less) && getLang().ObjC1)
1100 ParseObjCProtocolQualifiers(DS);
1101
Chris Lattnere387d9e2009-01-21 19:48:37 +00001102 continue;
1103 }
Mike Stump11289f42009-09-09 15:08:12 +00001104
Chris Lattner16fac4f2008-07-26 01:18:38 +00001105 // typedef-name
1106 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001107 // In C++, check to see if this is a scope specifier like foo::bar::, if
1108 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001109 if (getLang().CPlusPlus) {
1110 if (TryAnnotateCXXScopeToken(true)) {
1111 if (!DS.hasTypeSpecifier())
1112 DS.SetTypeSpecError();
1113 goto DoneWithDeclSpec;
1114 }
1115 if (!Tok.is(tok::identifier))
1116 continue;
1117 }
Mike Stump11289f42009-09-09 15:08:12 +00001118
Chris Lattner16fac4f2008-07-26 01:18:38 +00001119 // This identifier can only be a typedef name if we haven't already seen
1120 // a type-specifier. Without this check we misparse:
1121 // typedef int X; struct Y { short X; }; as 'short int'.
1122 if (DS.hasTypeSpecifier())
1123 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001124
John Thompson22334602010-02-05 00:12:22 +00001125 // Check for need to substitute AltiVec keyword tokens.
1126 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1127 break;
1128
Chris Lattner16fac4f2008-07-26 01:18:38 +00001129 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001130 ParsedType TypeRep =
1131 Actions.getTypeName(*Tok.getIdentifierInfo(),
1132 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001133
Chris Lattner6cc055a2009-04-12 20:42:31 +00001134 // If this is not a typedef name, don't parse it as part of the declspec,
1135 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001136 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001137 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001138 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001139 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001140
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001141 // If we're in a context where the identifier could be a class name,
1142 // check whether this is a constructor declaration.
1143 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001144 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001145 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001146 goto DoneWithDeclSpec;
1147
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001148 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001149 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001150 if (isInvalid)
1151 break;
Mike Stump11289f42009-09-09 15:08:12 +00001152
Chris Lattner16fac4f2008-07-26 01:18:38 +00001153 DS.SetRangeEnd(Tok.getLocation());
1154 ConsumeToken(); // The identifier
1155
1156 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1157 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001158 // Objective-C interface.
1159 if (Tok.is(tok::less) && getLang().ObjC1)
1160 ParseObjCProtocolQualifiers(DS);
1161
Steve Naroffcd5e7822008-09-22 10:28:57 +00001162 // Need to support trailing type qualifiers (e.g. "id<p> const").
1163 // If a type specifier follows, it will be diagnosed elsewhere.
1164 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001165 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001166
1167 // type-name
1168 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001169 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001170 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001171 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001172 // This template-id does not refer to a type name, so we're
1173 // done with the type-specifiers.
1174 goto DoneWithDeclSpec;
1175 }
1176
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001177 // If we're in a context where the template-id could be a
1178 // constructor name or specialization, check whether this is a
1179 // constructor declaration.
1180 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001181 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001182 isConstructorDeclarator())
1183 goto DoneWithDeclSpec;
1184
Douglas Gregor7f741122009-02-25 19:37:18 +00001185 // Turn the template-id annotation token into a type annotation
1186 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001187 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001188 continue;
1189 }
1190
Chris Lattnere37e2332006-08-15 04:50:22 +00001191 // GNU attributes support.
1192 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001193 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001194 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001195
1196 // Microsoft declspec support.
1197 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +00001198 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001199 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001200
Steve Naroff44ac7772008-12-25 14:16:32 +00001201 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001202 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001203 // FIXME: Add handling here!
1204 break;
1205
1206 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001207 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001208 case tok::kw___cdecl:
1209 case tok::kw___stdcall:
1210 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001211 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001212 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1213 continue;
1214
Dawn Perchik335e16b2010-09-03 01:29:35 +00001215 // Borland single token adornments.
1216 case tok::kw___pascal:
1217 DS.AddAttributes(ParseBorlandTypeAttributes());
1218 continue;
1219
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001220 // storage-class-specifier
1221 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001222 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1223 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001224 break;
1225 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001226 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001227 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001228 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1229 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001230 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001231 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001232 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001233 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001234 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001235 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001236 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001237 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001238 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1239 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001240 break;
1241 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001242 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001243 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1244 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001245 else
John McCall49bfce42009-08-03 20:12:06 +00001246 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1247 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001248 break;
1249 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001250 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1251 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001252 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001253 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001254 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1255 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001256 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001257 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001258 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001259 break;
Mike Stump11289f42009-09-09 15:08:12 +00001260
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001261 // function-specifier
1262 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001263 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001264 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001265 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001266 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001267 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001268 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001269 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001270 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001271
Anders Carlssoncd8db412009-05-06 04:46:28 +00001272 // friend
1273 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001274 if (DSContext == DSC_class)
1275 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1276 else {
1277 PrevSpec = ""; // not actually used by the diagnostic
1278 DiagID = diag::err_friend_invalid_in_context;
1279 isInvalid = true;
1280 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001281 break;
Mike Stump11289f42009-09-09 15:08:12 +00001282
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001283 // constexpr
1284 case tok::kw_constexpr:
1285 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1286 break;
1287
Chris Lattnere387d9e2009-01-21 19:48:37 +00001288 // type-specifier
1289 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001290 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1291 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001292 break;
1293 case tok::kw_long:
1294 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001295 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1296 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001297 else
John McCall49bfce42009-08-03 20:12:06 +00001298 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1299 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001300 break;
1301 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001302 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1303 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001304 break;
1305 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001306 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1307 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001308 break;
1309 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001310 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1311 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001312 break;
1313 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001314 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1315 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001316 break;
1317 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001318 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1319 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001320 break;
1321 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001322 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1323 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001324 break;
1325 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001326 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1327 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001328 break;
1329 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001330 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1331 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001332 break;
1333 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001334 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1335 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001336 break;
1337 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001338 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1339 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001340 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001341 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001342 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1343 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001344 break;
1345 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001346 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1347 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001348 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001349 case tok::kw_bool:
1350 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001351 if (Tok.is(tok::kw_bool) &&
1352 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1353 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1354 PrevSpec = ""; // Not used by the diagnostic.
1355 DiagID = diag::err_bool_redeclaration;
1356 isInvalid = true;
1357 } else {
1358 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1359 DiagID);
1360 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001361 break;
1362 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001363 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1364 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001365 break;
1366 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001367 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1368 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001369 break;
1370 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001371 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1372 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001373 break;
John Thompson22334602010-02-05 00:12:22 +00001374 case tok::kw___vector:
1375 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1376 break;
1377 case tok::kw___pixel:
1378 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1379 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001380
1381 // class-specifier:
1382 case tok::kw_class:
1383 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001384 case tok::kw_union: {
1385 tok::TokenKind Kind = Tok.getKind();
1386 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001387 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001388 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001389 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001390
1391 // enum-specifier:
1392 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001393 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001394 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001395 continue;
1396
1397 // cv-qualifier:
1398 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001399 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1400 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001401 break;
1402 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001403 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1404 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001405 break;
1406 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001407 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1408 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001409 break;
1410
Douglas Gregor333489b2009-03-27 23:10:48 +00001411 // C++ typename-specifier:
1412 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001413 if (TryAnnotateTypeOrScopeToken()) {
1414 DS.SetTypeSpecError();
1415 goto DoneWithDeclSpec;
1416 }
1417 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001418 continue;
1419 break;
1420
Chris Lattnere387d9e2009-01-21 19:48:37 +00001421 // GNU typeof support.
1422 case tok::kw_typeof:
1423 ParseTypeofSpecifier(DS);
1424 continue;
1425
Anders Carlsson74948d02009-06-24 17:47:40 +00001426 case tok::kw_decltype:
1427 ParseDecltypeSpecifier(DS);
1428 continue;
1429
Steve Naroffcfdf6162008-06-05 00:02:44 +00001430 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001431 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001432 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1433 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001434 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001435 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001436
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001437 ParseObjCProtocolQualifiers(DS);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001438
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001439 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1440 << FixItHint::CreateInsertion(Loc, "id")
1441 << SourceRange(Loc, DS.getSourceRange().getEnd());
1442
1443 // Need to support trailing type qualifiers (e.g. "id<p> const").
1444 // If a type specifier follows, it will be diagnosed elsewhere.
1445 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001446 }
John McCall49bfce42009-08-03 20:12:06 +00001447 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001448 if (isInvalid) {
1449 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001450 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001451
1452 if (DiagID == diag::ext_duplicate_declspec)
1453 Diag(Tok, DiagID)
1454 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1455 else
1456 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001457 }
Chris Lattner2e232092008-03-13 06:29:04 +00001458 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001459 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001460 }
1461}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001462
Chris Lattnera448d752009-01-06 06:59:53 +00001463/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001464/// primarily follow the C++ grammar with additions for C99 and GNU,
1465/// which together subsume the C grammar. Note that the C++
1466/// type-specifier also includes the C type-qualifier (for const,
1467/// volatile, and C99 restrict). Returns true if a type-specifier was
1468/// found (and parsed), false otherwise.
1469///
1470/// type-specifier: [C++ 7.1.5]
1471/// simple-type-specifier
1472/// class-specifier
1473/// enum-specifier
1474/// elaborated-type-specifier [TODO]
1475/// cv-qualifier
1476///
1477/// cv-qualifier: [C++ 7.1.5.1]
1478/// 'const'
1479/// 'volatile'
1480/// [C99] 'restrict'
1481///
1482/// simple-type-specifier: [ C++ 7.1.5.2]
1483/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1484/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1485/// 'char'
1486/// 'wchar_t'
1487/// 'bool'
1488/// 'short'
1489/// 'int'
1490/// 'long'
1491/// 'signed'
1492/// 'unsigned'
1493/// 'float'
1494/// 'double'
1495/// 'void'
1496/// [C99] '_Bool'
1497/// [C99] '_Complex'
1498/// [C99] '_Imaginary' // Removed in TC2?
1499/// [GNU] '_Decimal32'
1500/// [GNU] '_Decimal64'
1501/// [GNU] '_Decimal128'
1502/// [GNU] typeof-specifier
1503/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1504/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001505/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001506/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001507bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001508 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001509 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001510 const ParsedTemplateInfo &TemplateInfo,
1511 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001512 SourceLocation Loc = Tok.getLocation();
1513
1514 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001515 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001516 // If we already have a type specifier, this identifier is not a type.
1517 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1518 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1519 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1520 return false;
John Thompson22334602010-02-05 00:12:22 +00001521 // Check for need to substitute AltiVec keyword tokens.
1522 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1523 break;
1524 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001525 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001526 // Annotate typenames and C++ scope specifiers. If we get one, just
1527 // recurse to handle whatever we get.
1528 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001529 return true;
1530 if (Tok.is(tok::identifier))
1531 return false;
1532 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1533 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001534 case tok::coloncolon: // ::foo::bar
1535 if (NextToken().is(tok::kw_new) || // ::new
1536 NextToken().is(tok::kw_delete)) // ::delete
1537 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001538
Chris Lattner020bab92009-01-04 23:41:41 +00001539 // Annotate typenames and C++ scope specifiers. If we get one, just
1540 // recurse to handle whatever we get.
1541 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001542 return true;
1543 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1544 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001545
Douglas Gregor450c75a2008-11-07 15:42:26 +00001546 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001547 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001548 if (ParsedType T = getTypeAnnotation(Tok)) {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001549 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001550 DiagID, T);
1551 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001552 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001553 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1554 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001555
Douglas Gregor450c75a2008-11-07 15:42:26 +00001556 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1557 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1558 // Objective-C interface. If we don't have Objective-C or a '<', this is
1559 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001560 if (Tok.is(tok::less) && getLang().ObjC1)
1561 ParseObjCProtocolQualifiers(DS);
1562
Douglas Gregor450c75a2008-11-07 15:42:26 +00001563 return true;
1564 }
1565
1566 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001567 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001568 break;
1569 case tok::kw_long:
1570 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001571 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1572 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001573 else
John McCall49bfce42009-08-03 20:12:06 +00001574 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1575 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001576 break;
1577 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001578 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001579 break;
1580 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001581 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1582 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001583 break;
1584 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001585 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1586 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001587 break;
1588 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001589 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1590 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001591 break;
1592 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001593 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001594 break;
1595 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001596 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001597 break;
1598 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001599 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001600 break;
1601 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001602 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001603 break;
1604 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001605 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001606 break;
1607 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001608 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001609 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001610 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001611 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001612 break;
1613 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001614 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001615 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001616 case tok::kw_bool:
1617 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001618 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001619 break;
1620 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001621 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1622 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001623 break;
1624 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001625 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1626 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001627 break;
1628 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001629 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1630 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001631 break;
John Thompson22334602010-02-05 00:12:22 +00001632 case tok::kw___vector:
1633 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1634 break;
1635 case tok::kw___pixel:
1636 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1637 break;
1638
Douglas Gregor450c75a2008-11-07 15:42:26 +00001639 // class-specifier:
1640 case tok::kw_class:
1641 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001642 case tok::kw_union: {
1643 tok::TokenKind Kind = Tok.getKind();
1644 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001645 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1646 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001647 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001648 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001649
1650 // enum-specifier:
1651 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001652 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001653 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001654 return true;
1655
1656 // cv-qualifier:
1657 case tok::kw_const:
1658 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001659 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001660 break;
1661 case tok::kw_volatile:
1662 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001663 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001664 break;
1665 case tok::kw_restrict:
1666 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001667 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001668 break;
1669
1670 // GNU typeof support.
1671 case tok::kw_typeof:
1672 ParseTypeofSpecifier(DS);
1673 return true;
1674
Anders Carlsson74948d02009-06-24 17:47:40 +00001675 // C++0x decltype support.
1676 case tok::kw_decltype:
1677 ParseDecltypeSpecifier(DS);
1678 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001679
Anders Carlssonbae27372009-06-26 23:44:14 +00001680 // C++0x auto support.
1681 case tok::kw_auto:
1682 if (!getLang().CPlusPlus0x)
1683 return false;
1684
John McCall49bfce42009-08-03 20:12:06 +00001685 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001686 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001687
Eli Friedman53339e02009-06-08 23:27:34 +00001688 case tok::kw___ptr64:
1689 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001690 case tok::kw___cdecl:
1691 case tok::kw___stdcall:
1692 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001693 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00001694 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001695 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001696
Dawn Perchik335e16b2010-09-03 01:29:35 +00001697 case tok::kw___pascal:
1698 DS.AddAttributes(ParseBorlandTypeAttributes());
1699 return true;
1700
Douglas Gregor450c75a2008-11-07 15:42:26 +00001701 default:
1702 // Not a type-specifier; do nothing.
1703 return false;
1704 }
1705
1706 // If the specifier combination wasn't legal, issue a diagnostic.
1707 if (isInvalid) {
1708 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001709 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001710 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001711 }
1712 DS.SetRangeEnd(Tok.getLocation());
1713 ConsumeToken(); // whatever we parsed above.
1714 return true;
1715}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001716
Chris Lattner70ae4912007-10-29 04:42:53 +00001717/// ParseStructDeclaration - Parse a struct declaration without the terminating
1718/// semicolon.
1719///
Chris Lattner90a26b02007-01-23 04:38:16 +00001720/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001721/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001722/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001723/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001724/// struct-declarator-list:
1725/// struct-declarator
1726/// struct-declarator-list ',' struct-declarator
1727/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1728/// struct-declarator:
1729/// declarator
1730/// [GNU] declarator attributes[opt]
1731/// declarator[opt] ':' constant-expression
1732/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1733///
Chris Lattnera12405b2008-04-10 06:46:29 +00001734void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001735ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001736 if (Tok.is(tok::kw___extension__)) {
1737 // __extension__ silences extension warnings in the subexpression.
1738 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001739 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001740 return ParseStructDeclaration(DS, Fields);
1741 }
Mike Stump11289f42009-09-09 15:08:12 +00001742
Steve Naroff97170802007-08-20 22:28:22 +00001743 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001744 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001745 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001746
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001747 // If there are no declarators, this is a free-standing declaration
1748 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001749 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001750 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001751 return;
1752 }
1753
1754 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001755 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001756 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001757 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001758 FieldDeclarator DeclaratorInfo(DS);
1759
1760 // Attributes are only allowed here on successive declarators.
1761 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1762 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001763 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallcfefb6d2009-11-03 02:38:08 +00001764 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1765 }
Mike Stump11289f42009-09-09 15:08:12 +00001766
Steve Naroff97170802007-08-20 22:28:22 +00001767 /// struct-declarator: declarator
1768 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001769 if (Tok.isNot(tok::colon)) {
1770 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1771 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001772 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001773 }
Mike Stump11289f42009-09-09 15:08:12 +00001774
Chris Lattner76c72282007-10-09 17:33:22 +00001775 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001776 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001777 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001778 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001779 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001780 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001781 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001782 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001783
Steve Naroff97170802007-08-20 22:28:22 +00001784 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001785 if (Tok.is(tok::kw___attribute)) {
1786 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001787 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001788 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1789 }
1790
John McCallcfefb6d2009-11-03 02:38:08 +00001791 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00001792 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00001793 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001794
Steve Naroff97170802007-08-20 22:28:22 +00001795 // If we don't have a comma, it is either the end of the list (a ';')
1796 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001797 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001798 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001799
Steve Naroff97170802007-08-20 22:28:22 +00001800 // Consume the comma.
1801 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001802
John McCallcfefb6d2009-11-03 02:38:08 +00001803 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001804 }
Steve Naroff97170802007-08-20 22:28:22 +00001805}
1806
1807/// ParseStructUnionBody
1808/// struct-contents:
1809/// struct-declaration-list
1810/// [EXT] empty
1811/// [GNU] "struct-declaration-list" without terminatoring ';'
1812/// struct-declaration-list:
1813/// struct-declaration
1814/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001815/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001816///
Chris Lattner1300fb92007-01-23 23:42:53 +00001817void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001818 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00001819 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1820 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001821
Chris Lattner90a26b02007-01-23 04:38:16 +00001822 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001823
Douglas Gregor658b9552009-01-09 22:42:13 +00001824 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001825 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001826
Chris Lattner7b9ace62007-01-23 20:11:08 +00001827 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1828 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001829 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00001830 Diag(Tok, diag::ext_empty_struct_union)
1831 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001832
John McCall48871652010-08-21 09:40:31 +00001833 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001834
Chris Lattner7b9ace62007-01-23 20:11:08 +00001835 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001836 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001837 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001838
Chris Lattner736ed5d2007-06-09 05:59:07 +00001839 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001840 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001841 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00001842 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00001843 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001844 ConsumeToken();
1845 continue;
1846 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001847
1848 // Parse all the comma separated declarators.
1849 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001850
John McCallcfefb6d2009-11-03 02:38:08 +00001851 if (!Tok.is(tok::at)) {
1852 struct CFieldCallback : FieldCallback {
1853 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001854 Decl *TagDecl;
1855 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001856
John McCall48871652010-08-21 09:40:31 +00001857 CFieldCallback(Parser &P, Decl *TagDecl,
1858 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001859 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1860
John McCall48871652010-08-21 09:40:31 +00001861 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001862 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00001863 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00001864 FD.D.getDeclSpec().getSourceRange().getBegin(),
1865 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001866 FieldDecls.push_back(Field);
1867 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001868 }
John McCallcfefb6d2009-11-03 02:38:08 +00001869 } Callback(*this, TagDecl, FieldDecls);
1870
1871 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001872 } else { // Handle @defs
1873 ConsumeToken();
1874 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1875 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001876 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001877 continue;
1878 }
1879 ConsumeToken();
1880 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1881 if (!Tok.is(tok::identifier)) {
1882 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001883 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001884 continue;
1885 }
John McCall48871652010-08-21 09:40:31 +00001886 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001887 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001888 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001889 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1890 ConsumeToken();
1891 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001892 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001893
Chris Lattner76c72282007-10-09 17:33:22 +00001894 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001895 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001896 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001897 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001898 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001899 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001900 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1901 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001902 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001903 // If we stopped at a ';', eat it.
1904 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001905 }
1906 }
Mike Stump11289f42009-09-09 15:08:12 +00001907
Steve Naroff33a1e802007-10-29 21:38:07 +00001908 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001909
Ted Kremenek5eec2b02010-11-10 05:59:39 +00001910 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +00001911 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001912 if (Tok.is(tok::kw___attribute))
Ted Kremenek5eec2b02010-11-10 05:59:39 +00001913 AttrList = ParseGNUAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +00001914
Douglas Gregor0be31a22010-07-02 17:43:08 +00001915 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001916 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001917 LBraceLoc, RBraceLoc,
Ted Kremenek5eec2b02010-11-10 05:59:39 +00001918 AttrList);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001919 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001920 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001921}
1922
1923
Chris Lattner3b561a32006-08-13 00:12:11 +00001924/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001925/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001926/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001927///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001928/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1929/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001930/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001931/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001932///
Douglas Gregor0bf31402010-10-08 23:50:27 +00001933/// [C++0x] enum-head '{' enumerator-list[opt] '}'
1934/// [C++0x] enum-head '{' enumerator-list ',' '}'
1935///
1936/// enum-head: [C++0x]
1937/// enum-key attributes[opt] identifier[opt] enum-base[opt]
1938/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
1939///
1940/// enum-key: [C++0x]
1941/// 'enum'
1942/// 'enum' 'class'
1943/// 'enum' 'struct'
1944///
1945/// enum-base: [C++0x]
1946/// ':' type-specifier-seq
1947///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001948/// [C++] elaborated-type-specifier:
1949/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1950///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001951void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001952 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001953 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001954 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001955 if (Tok.is(tok::code_completion)) {
1956 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001957 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001958 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001959 }
1960
Ted Kremenek5eec2b02010-11-10 05:59:39 +00001961 AttributeList *Attr = 0;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001962 // If attributes exist after tag, parse them.
1963 if (Tok.is(tok::kw___attribute))
Ted Kremenek5eec2b02010-11-10 05:59:39 +00001964 Attr = ParseGNUAttributes();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001965
Abramo Bagnarad7548482010-05-19 21:37:53 +00001966 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00001967 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00001968 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00001969 return;
1970
1971 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001972 Diag(Tok, diag::err_expected_ident);
1973 if (Tok.isNot(tok::l_brace)) {
1974 // Has no name and is not a definition.
1975 // Skip the rest of this declarator, up until the comma or semicolon.
1976 SkipUntil(tok::comma, true);
1977 return;
1978 }
1979 }
1980 }
Mike Stump11289f42009-09-09 15:08:12 +00001981
Douglas Gregor0bf31402010-10-08 23:50:27 +00001982 bool IsScopedEnum = false;
1983
1984 if (getLang().CPlusPlus0x && (Tok.is(tok::kw_class)
1985 || Tok.is(tok::kw_struct))) {
1986 ConsumeToken();
1987 IsScopedEnum = true;
1988 }
1989
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001990 // Must have either 'enum name' or 'enum {...}'.
1991 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1992 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001993
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001994 // Skip the rest of this declarator, up until the comma or semicolon.
1995 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001996 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001997 }
Mike Stump11289f42009-09-09 15:08:12 +00001998
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001999 // If an identifier is present, consume and remember it.
2000 IdentifierInfo *Name = 0;
2001 SourceLocation NameLoc;
2002 if (Tok.is(tok::identifier)) {
2003 Name = Tok.getIdentifierInfo();
2004 NameLoc = ConsumeToken();
2005 }
Mike Stump11289f42009-09-09 15:08:12 +00002006
Douglas Gregor0bf31402010-10-08 23:50:27 +00002007 if (!Name && IsScopedEnum) {
2008 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2009 // declaration of a scoped enumeration.
2010 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2011 IsScopedEnum = false;
2012 }
2013
2014 TypeResult BaseType;
2015
2016 if (getLang().CPlusPlus0x && Tok.is(tok::colon)) {
2017 ConsumeToken();
2018 SourceRange Range;
2019 BaseType = ParseTypeName(&Range);
2020 }
2021
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002022 // There are three options here. If we have 'enum foo;', then this is a
2023 // forward declaration. If we have 'enum foo {...' then this is a
2024 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2025 //
2026 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2027 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2028 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2029 //
John McCallfaf5fb42010-08-26 23:41:50 +00002030 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002031 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002032 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002033 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002034 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002035 else
John McCallfaf5fb42010-08-26 23:41:50 +00002036 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002037
2038 // enums cannot be templates, although they can be referenced from a
2039 // template.
2040 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002041 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002042 Diag(Tok, diag::err_enum_template);
2043
2044 // Skip the rest of this declarator, up until the comma or semicolon.
2045 SkipUntil(tok::comma, true);
2046 return;
2047 }
2048
Douglas Gregord6ab8742009-05-28 23:31:59 +00002049 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002050 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002051 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2052 const char *PrevSpec = 0;
2053 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002054 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002055 StartLoc, SS, Name, NameLoc, Attr,
John McCall48871652010-08-21 09:40:31 +00002056 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002057 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002058 Owned, IsDependent, IsScopedEnum,
2059 BaseType);
2060
Douglas Gregorba41d012010-04-24 16:38:41 +00002061 if (IsDependent) {
2062 // This enum has a dependent nested-name-specifier. Handle it as a
2063 // dependent tag.
2064 if (!Name) {
2065 DS.SetTypeSpecError();
2066 Diag(Tok, diag::err_expected_type_name_after_typename);
2067 return;
2068 }
2069
Douglas Gregor0be31a22010-07-02 17:43:08 +00002070 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002071 TUK, SS, Name, StartLoc,
2072 NameLoc);
2073 if (Type.isInvalid()) {
2074 DS.SetTypeSpecError();
2075 return;
2076 }
2077
2078 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallba7bf592010-08-24 05:47:05 +00002079 Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002080 Diag(StartLoc, DiagID) << PrevSpec;
2081
2082 return;
2083 }
Mike Stump11289f42009-09-09 15:08:12 +00002084
John McCall48871652010-08-21 09:40:31 +00002085 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002086 // The action failed to produce an enumeration tag. If this is a
2087 // definition, consume the entire definition.
2088 if (Tok.is(tok::l_brace)) {
2089 ConsumeBrace();
2090 SkipUntil(tok::r_brace);
2091 }
2092
2093 DS.SetTypeSpecError();
2094 return;
2095 }
2096
Chris Lattner76c72282007-10-09 17:33:22 +00002097 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002098 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002099
John McCallba7bf592010-08-24 05:47:05 +00002100 // FIXME: The DeclSpec should keep the locations of both the keyword
2101 // and the name (if there is one).
Douglas Gregor72100632010-01-25 16:33:23 +00002102 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCall48871652010-08-21 09:40:31 +00002103 TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002104 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002105}
2106
Chris Lattnerc1915e22007-01-25 07:29:02 +00002107/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2108/// enumerator-list:
2109/// enumerator
2110/// enumerator-list ',' enumerator
2111/// enumerator:
2112/// enumeration-constant
2113/// enumeration-constant '=' constant-expression
2114/// enumeration-constant:
2115/// identifier
2116///
John McCall48871652010-08-21 09:40:31 +00002117void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002118 // Enter the scope of the enum body and start the definition.
2119 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002120 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002121
Chris Lattnerc1915e22007-01-25 07:29:02 +00002122 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002123
Chris Lattner37256fb2007-08-27 17:24:30 +00002124 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002125 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002126 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002127
John McCall48871652010-08-21 09:40:31 +00002128 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002129
John McCall48871652010-08-21 09:40:31 +00002130 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002131
Chris Lattnerc1915e22007-01-25 07:29:02 +00002132 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002133 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002134 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2135 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002136
John McCall811a0f52010-10-22 23:36:17 +00002137 // If attributes exist after the enumerator, parse them.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002138 AttributeList *Attr = 0;
John McCall811a0f52010-10-22 23:36:17 +00002139 if (Tok.is(tok::kw___attribute))
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002140 Attr = ParseGNUAttributes();
John McCall811a0f52010-10-22 23:36:17 +00002141
Chris Lattnerc1915e22007-01-25 07:29:02 +00002142 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002143 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002144 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002145 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002146 AssignedVal = ParseConstantExpression();
2147 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002148 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002149 }
Mike Stump11289f42009-09-09 15:08:12 +00002150
Chris Lattnerc1915e22007-01-25 07:29:02 +00002151 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002152 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2153 LastEnumConstDecl,
2154 IdentLoc, Ident,
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002155 Attr, EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002156 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002157 EnumConstantDecls.push_back(EnumConstDecl);
2158 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002159
Douglas Gregorce66d022010-09-07 14:51:08 +00002160 if (Tok.is(tok::identifier)) {
2161 // We're missing a comma between enumerators.
2162 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2163 Diag(Loc, diag::err_enumerator_list_missing_comma)
2164 << FixItHint::CreateInsertion(Loc, ", ");
2165 continue;
2166 }
2167
Chris Lattner76c72282007-10-09 17:33:22 +00002168 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002169 break;
2170 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002171
2172 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002173 !(getLang().C99 || getLang().CPlusPlus0x))
2174 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2175 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002176 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002177 }
Mike Stump11289f42009-09-09 15:08:12 +00002178
Chris Lattnerc1915e22007-01-25 07:29:02 +00002179 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002180 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002181
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002182 AttributeList *Attr = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002183 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00002184 if (Tok.is(tok::kw___attribute))
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002185 Attr = ParseGNUAttributes(); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002186
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002187 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2188 EnumConstantDecls.data(), EnumConstantDecls.size(),
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002189 getCurScope(), Attr);
Mike Stump11289f42009-09-09 15:08:12 +00002190
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002191 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002192 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002193}
Chris Lattner3b561a32006-08-13 00:12:11 +00002194
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002195/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002196/// start of a type-qualifier-list.
2197bool Parser::isTypeQualifier() const {
2198 switch (Tok.getKind()) {
2199 default: return false;
2200 // type-qualifier
2201 case tok::kw_const:
2202 case tok::kw_volatile:
2203 case tok::kw_restrict:
2204 return true;
2205 }
2206}
2207
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002208/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2209/// is definitely a type-specifier. Return false if it isn't part of a type
2210/// specifier or if we're not sure.
2211bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2212 switch (Tok.getKind()) {
2213 default: return false;
2214 // type-specifiers
2215 case tok::kw_short:
2216 case tok::kw_long:
2217 case tok::kw_signed:
2218 case tok::kw_unsigned:
2219 case tok::kw__Complex:
2220 case tok::kw__Imaginary:
2221 case tok::kw_void:
2222 case tok::kw_char:
2223 case tok::kw_wchar_t:
2224 case tok::kw_char16_t:
2225 case tok::kw_char32_t:
2226 case tok::kw_int:
2227 case tok::kw_float:
2228 case tok::kw_double:
2229 case tok::kw_bool:
2230 case tok::kw__Bool:
2231 case tok::kw__Decimal32:
2232 case tok::kw__Decimal64:
2233 case tok::kw__Decimal128:
2234 case tok::kw___vector:
2235
2236 // struct-or-union-specifier (C99) or class-specifier (C++)
2237 case tok::kw_class:
2238 case tok::kw_struct:
2239 case tok::kw_union:
2240 // enum-specifier
2241 case tok::kw_enum:
2242
2243 // typedef-name
2244 case tok::annot_typename:
2245 return true;
2246 }
2247}
2248
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002249/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002250/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002251bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002252 switch (Tok.getKind()) {
2253 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002254
Chris Lattner020bab92009-01-04 23:41:41 +00002255 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002256 if (TryAltiVecVectorToken())
2257 return true;
2258 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002259 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002260 // Annotate typenames and C++ scope specifiers. If we get one, just
2261 // recurse to handle whatever we get.
2262 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002263 return true;
2264 if (Tok.is(tok::identifier))
2265 return false;
2266 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002267
Chris Lattner020bab92009-01-04 23:41:41 +00002268 case tok::coloncolon: // ::foo::bar
2269 if (NextToken().is(tok::kw_new) || // ::new
2270 NextToken().is(tok::kw_delete)) // ::delete
2271 return false;
2272
Chris Lattner020bab92009-01-04 23:41:41 +00002273 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002274 return true;
2275 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002276
Chris Lattnere37e2332006-08-15 04:50:22 +00002277 // GNU attributes support.
2278 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002279 // GNU typeof support.
2280 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002281
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002282 // type-specifiers
2283 case tok::kw_short:
2284 case tok::kw_long:
2285 case tok::kw_signed:
2286 case tok::kw_unsigned:
2287 case tok::kw__Complex:
2288 case tok::kw__Imaginary:
2289 case tok::kw_void:
2290 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002291 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002292 case tok::kw_char16_t:
2293 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002294 case tok::kw_int:
2295 case tok::kw_float:
2296 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002297 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002298 case tok::kw__Bool:
2299 case tok::kw__Decimal32:
2300 case tok::kw__Decimal64:
2301 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002302 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002303
Chris Lattner861a2262008-04-13 18:59:07 +00002304 // struct-or-union-specifier (C99) or class-specifier (C++)
2305 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002306 case tok::kw_struct:
2307 case tok::kw_union:
2308 // enum-specifier
2309 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002310
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002311 // type-qualifier
2312 case tok::kw_const:
2313 case tok::kw_volatile:
2314 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002315
2316 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002317 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002318 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002319
Chris Lattner409bf7d2008-10-20 00:25:30 +00002320 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2321 case tok::less:
2322 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002323
Steve Naroff44ac7772008-12-25 14:16:32 +00002324 case tok::kw___cdecl:
2325 case tok::kw___stdcall:
2326 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002327 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002328 case tok::kw___w64:
2329 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002330 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002331 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002332 }
2333}
2334
Chris Lattneracd58a32006-08-06 17:24:14 +00002335/// isDeclarationSpecifier() - Return true if the current token is part of a
2336/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002337///
2338/// \param DisambiguatingWithExpression True to indicate that the purpose of
2339/// this check is to disambiguate between an expression and a declaration.
2340bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002341 switch (Tok.getKind()) {
2342 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002343
Chris Lattner020bab92009-01-04 23:41:41 +00002344 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002345 // Unfortunate hack to support "Class.factoryMethod" notation.
2346 if (getLang().ObjC1 && NextToken().is(tok::period))
2347 return false;
John Thompson22334602010-02-05 00:12:22 +00002348 if (TryAltiVecVectorToken())
2349 return true;
2350 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002351 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002352 // Annotate typenames and C++ scope specifiers. If we get one, just
2353 // recurse to handle whatever we get.
2354 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002355 return true;
2356 if (Tok.is(tok::identifier))
2357 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002358
2359 // If we're in Objective-C and we have an Objective-C class type followed
2360 // by an identifier and then either ':' or ']', in a place where an
2361 // expression is permitted, then this is probably a class message send
2362 // missing the initial '['. In this case, we won't consider this to be
2363 // the start of a declaration.
2364 if (DisambiguatingWithExpression &&
2365 isStartOfObjCClassMessageMissingOpenBracket())
2366 return false;
2367
John McCall1f476a12010-02-26 08:45:28 +00002368 return isDeclarationSpecifier();
2369
Chris Lattner020bab92009-01-04 23:41:41 +00002370 case tok::coloncolon: // ::foo::bar
2371 if (NextToken().is(tok::kw_new) || // ::new
2372 NextToken().is(tok::kw_delete)) // ::delete
2373 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002374
Chris Lattner020bab92009-01-04 23:41:41 +00002375 // Annotate typenames and C++ scope specifiers. If we get one, just
2376 // recurse to handle whatever we get.
2377 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002378 return true;
2379 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002380
Chris Lattneracd58a32006-08-06 17:24:14 +00002381 // storage-class-specifier
2382 case tok::kw_typedef:
2383 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002384 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002385 case tok::kw_static:
2386 case tok::kw_auto:
2387 case tok::kw_register:
2388 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002389
Chris Lattneracd58a32006-08-06 17:24:14 +00002390 // type-specifiers
2391 case tok::kw_short:
2392 case tok::kw_long:
2393 case tok::kw_signed:
2394 case tok::kw_unsigned:
2395 case tok::kw__Complex:
2396 case tok::kw__Imaginary:
2397 case tok::kw_void:
2398 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002399 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002400 case tok::kw_char16_t:
2401 case tok::kw_char32_t:
2402
Chris Lattneracd58a32006-08-06 17:24:14 +00002403 case tok::kw_int:
2404 case tok::kw_float:
2405 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002406 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002407 case tok::kw__Bool:
2408 case tok::kw__Decimal32:
2409 case tok::kw__Decimal64:
2410 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002411 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002412
Chris Lattner861a2262008-04-13 18:59:07 +00002413 // struct-or-union-specifier (C99) or class-specifier (C++)
2414 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002415 case tok::kw_struct:
2416 case tok::kw_union:
2417 // enum-specifier
2418 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002419
Chris Lattneracd58a32006-08-06 17:24:14 +00002420 // type-qualifier
2421 case tok::kw_const:
2422 case tok::kw_volatile:
2423 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002424
Chris Lattneracd58a32006-08-06 17:24:14 +00002425 // function-specifier
2426 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002427 case tok::kw_virtual:
2428 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002429
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002430 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002431 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002432
Chris Lattner599e47e2007-08-09 17:01:07 +00002433 // GNU typeof support.
2434 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002435
Chris Lattner599e47e2007-08-09 17:01:07 +00002436 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002437 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002438 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002439
Chris Lattner8b2ec162008-07-26 03:38:44 +00002440 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2441 case tok::less:
2442 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002443
Steve Narofff192fab2009-01-06 19:34:12 +00002444 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002445 case tok::kw___cdecl:
2446 case tok::kw___stdcall:
2447 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002448 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002449 case tok::kw___w64:
2450 case tok::kw___ptr64:
2451 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002452 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002453 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002454 }
2455}
2456
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002457bool Parser::isConstructorDeclarator() {
2458 TentativeParsingAction TPA(*this);
2459
2460 // Parse the C++ scope specifier.
2461 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002462 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00002463 TPA.Revert();
2464 return false;
2465 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002466
2467 // Parse the constructor name.
2468 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2469 // We already know that we have a constructor name; just consume
2470 // the token.
2471 ConsumeToken();
2472 } else {
2473 TPA.Revert();
2474 return false;
2475 }
2476
2477 // Current class name must be followed by a left parentheses.
2478 if (Tok.isNot(tok::l_paren)) {
2479 TPA.Revert();
2480 return false;
2481 }
2482 ConsumeParen();
2483
2484 // A right parentheses or ellipsis signals that we have a constructor.
2485 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2486 TPA.Revert();
2487 return true;
2488 }
2489
2490 // If we need to, enter the specified scope.
2491 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002492 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002493 DeclScopeObj.EnterDeclaratorScope();
2494
2495 // Check whether the next token(s) are part of a declaration
2496 // specifier, in which case we have the start of a parameter and,
2497 // therefore, we know that this is a constructor.
2498 bool IsConstructor = isDeclarationSpecifier();
2499 TPA.Revert();
2500 return IsConstructor;
2501}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002502
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002503/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00002504/// type-qualifier-list: [C99 6.7.5]
2505/// type-qualifier
2506/// [vendor] attributes
2507/// [ only if VendorAttributesAllowed=true ]
2508/// type-qualifier-list type-qualifier
2509/// [vendor] type-qualifier-list attributes
2510/// [ only if VendorAttributesAllowed=true ]
2511/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2512/// [ only if CXX0XAttributesAllowed=true ]
2513/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002514///
Dawn Perchik335e16b2010-09-03 01:29:35 +00002515void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2516 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00002517 bool CXX0XAttributesAllowed) {
2518 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2519 SourceLocation Loc = Tok.getLocation();
2520 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2521 if (CXX0XAttributesAllowed)
2522 DS.AddAttributes(Attr.AttrList);
2523 else
2524 Diag(Loc, diag::err_attributes_not_allowed);
2525 }
2526
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002527 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002528 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002529 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002530 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002531 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002532
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002533 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00002534 case tok::code_completion:
2535 Actions.CodeCompleteTypeQualifiers(DS);
2536 ConsumeCodeCompletionToken();
2537 break;
2538
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002539 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002540 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2541 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002542 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002543 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002544 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2545 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002546 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002547 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002548 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2549 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002550 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002551 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002552 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002553 case tok::kw___cdecl:
2554 case tok::kw___stdcall:
2555 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002556 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002557 if (VendorAttributesAllowed) {
Eli Friedman53339e02009-06-08 23:27:34 +00002558 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2559 continue;
2560 }
2561 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002562 case tok::kw___pascal:
2563 if (VendorAttributesAllowed) {
2564 DS.AddAttributes(ParseBorlandTypeAttributes());
2565 continue;
2566 }
2567 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002568 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002569 if (VendorAttributesAllowed) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002570 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002571 continue; // do *not* consume the next token!
2572 }
2573 // otherwise, FALL THROUGH!
2574 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002575 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002576 // If this is not a type-qualifier token, we're done reading type
2577 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002578 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002579 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002580 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002581
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002582 // If the specifier combination wasn't legal, issue a diagnostic.
2583 if (isInvalid) {
2584 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002585 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002586 }
2587 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002588 }
2589}
2590
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002591
2592/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2593///
2594void Parser::ParseDeclarator(Declarator &D) {
2595 /// This implements the 'declarator' production in the C grammar, then checks
2596 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002597 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002598}
2599
Sebastian Redlbd150f42008-11-21 19:14:01 +00002600/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2601/// is parsed by the function passed to it. Pass null, and the direct-declarator
2602/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002603/// ptr-operator production.
2604///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002605/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2606/// [C] pointer[opt] direct-declarator
2607/// [C++] direct-declarator
2608/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002609///
2610/// pointer: [C99 6.7.5]
2611/// '*' type-qualifier-list[opt]
2612/// '*' type-qualifier-list[opt] pointer
2613///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002614/// ptr-operator:
2615/// '*' cv-qualifier-seq[opt]
2616/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002617/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002618/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002619/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002620/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002621void Parser::ParseDeclaratorInternal(Declarator &D,
2622 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002623 if (Diags.hasAllExtensionsSilenced())
2624 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002625
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002626 // C++ member pointers start with a '::' or a nested-name.
2627 // Member pointers get special handling, since there's no place for the
2628 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002629 if (getLang().CPlusPlus &&
2630 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2631 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002632 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002633 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00002634
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002635 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002636 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002637 // The scope spec really belongs to the direct-declarator.
2638 D.getCXXScopeSpec() = SS;
2639 if (DirectDeclParser)
2640 (this->*DirectDeclParser)(D);
2641 return;
2642 }
2643
2644 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002645 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002646 DeclSpec DS;
2647 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002648 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002649
2650 // Recurse to parse whatever is left.
2651 ParseDeclaratorInternal(D, DirectDeclParser);
2652
2653 // Sema will have to catch (syntactically invalid) pointers into global
2654 // scope. It has to catch pointers into namespace scope anyway.
2655 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002656 Loc, DS.TakeAttributes()),
2657 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002658 return;
2659 }
2660 }
2661
2662 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002663 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002664 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002665 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002666 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002667 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002668 if (DirectDeclParser)
2669 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002670 return;
2671 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002672
Sebastian Redled0f3b02009-03-15 22:02:01 +00002673 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2674 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002675 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002676 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002677
Chris Lattner9eac9312009-03-27 04:18:06 +00002678 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002679 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002680 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002681
Bill Wendling3708c182007-05-27 10:15:43 +00002682 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002683 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002684
Bill Wendling3708c182007-05-27 10:15:43 +00002685 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002686 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002687 if (Kind == tok::star)
2688 // Remember that we parsed a pointer type, and remember the type-quals.
2689 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002690 DS.TakeAttributes()),
2691 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002692 else
2693 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002694 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002695 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002696 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002697 } else {
2698 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002699 DeclSpec DS;
2700
Sebastian Redl3b27be62009-03-23 00:00:23 +00002701 // Complain about rvalue references in C++03, but then go on and build
2702 // the declarator.
2703 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2704 Diag(Loc, diag::err_rvalue_reference);
2705
Bill Wendling93efb222007-06-02 23:28:54 +00002706 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2707 // cv-qualifiers are introduced through the use of a typedef or of a
2708 // template type argument, in which case the cv-qualifiers are ignored.
2709 //
2710 // [GNU] Retricted references are allowed.
2711 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002712 // [C++0x] Attributes on references are not allowed.
2713 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002714 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002715
2716 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2717 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2718 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002719 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002720 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2721 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002722 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002723 }
Bill Wendling3708c182007-05-27 10:15:43 +00002724
2725 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002726 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002727
Douglas Gregor66583c52008-11-03 15:51:28 +00002728 if (D.getNumTypeObjects() > 0) {
2729 // C++ [dcl.ref]p4: There shall be no references to references.
2730 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2731 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002732 if (const IdentifierInfo *II = D.getIdentifier())
2733 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2734 << II;
2735 else
2736 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2737 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002738
Sebastian Redlbd150f42008-11-21 19:14:01 +00002739 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002740 // can go ahead and build the (technically ill-formed)
2741 // declarator: reference collapsing will take care of it.
2742 }
2743 }
2744
Bill Wendling3708c182007-05-27 10:15:43 +00002745 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002746 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002747 DS.TakeAttributes(),
2748 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002749 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002750 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002751}
2752
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002753/// ParseDirectDeclarator
2754/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002755/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002756/// '(' declarator ')'
2757/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002758/// [C90] direct-declarator '[' constant-expression[opt] ']'
2759/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2760/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2761/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2762/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002763/// direct-declarator '(' parameter-type-list ')'
2764/// direct-declarator '(' identifier-list[opt] ')'
2765/// [GNU] direct-declarator '(' parameter-forward-declarations
2766/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002767/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2768/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002769/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002770///
2771/// declarator-id: [C++ 8]
2772/// id-expression
2773/// '::'[opt] nested-name-specifier[opt] type-name
2774///
2775/// id-expression: [C++ 5.1]
2776/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002777/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002778///
2779/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002780/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002781/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002782/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002783/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002784/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002785///
Chris Lattneracd58a32006-08-06 17:24:14 +00002786void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002787 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002788
Douglas Gregor7861a802009-11-03 01:35:08 +00002789 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2790 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002791 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00002792 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00002793 }
2794
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002795 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002796 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00002797 // Change the declaration context for name lookup, until this function
2798 // is exited (and the declarator has been parsed).
2799 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002800 }
2801
Douglas Gregor7861a802009-11-03 01:35:08 +00002802 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2803 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2804 // We found something that indicates the start of an unqualified-id.
2805 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002806 bool AllowConstructorName;
2807 if (D.getDeclSpec().hasTypeSpecifier())
2808 AllowConstructorName = false;
2809 else if (D.getCXXScopeSpec().isSet())
2810 AllowConstructorName =
2811 (D.getContext() == Declarator::FileContext ||
2812 (D.getContext() == Declarator::MemberContext &&
2813 D.getDeclSpec().isFriendSpecified()));
2814 else
2815 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2816
Douglas Gregor7861a802009-11-03 01:35:08 +00002817 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2818 /*EnteringContext=*/true,
2819 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002820 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002821 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002822 D.getName()) ||
2823 // Once we're past the identifier, if the scope was bad, mark the
2824 // whole declarator bad.
2825 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002826 D.SetIdentifier(0, Tok.getLocation());
2827 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002828 } else {
2829 // Parsed the unqualified-id; update range information and move along.
2830 if (D.getSourceRange().getBegin().isInvalid())
2831 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2832 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002833 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002834 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002835 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002836 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002837 assert(!getLang().CPlusPlus &&
2838 "There's a C++-specific check for tok::identifier above");
2839 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2840 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2841 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002842 goto PastIdentifier;
2843 }
2844
2845 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002846 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002847 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002848 // Example: 'char (*X)' or 'int (*XX)(void)'
2849 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002850
2851 // If the declarator was parenthesized, we entered the declarator
2852 // scope when parsing the parenthesized declarator, then exited
2853 // the scope already. Re-enter the scope, if we need to.
2854 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002855 // If there was an error parsing parenthesized declarator, declarator
2856 // scope may have been enterred before. Don't do it again.
2857 if (!D.isInvalidType() &&
2858 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002859 // Change the declaration context for name lookup, until this function
2860 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002861 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002862 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002863 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002864 // This could be something simple like "int" (in which case the declarator
2865 // portion is empty), if an abstract-declarator is allowed.
2866 D.SetIdentifier(0, Tok.getLocation());
2867 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002868 if (D.getContext() == Declarator::MemberContext)
2869 Diag(Tok, diag::err_expected_member_name_or_semi)
2870 << D.getDeclSpec().getSourceRange();
2871 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002872 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002873 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002874 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002875 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002876 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002877 }
Mike Stump11289f42009-09-09 15:08:12 +00002878
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002879 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002880 assert(D.isPastIdentifier() &&
2881 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002882
Alexis Hunt96d5c762009-11-21 08:43:09 +00002883 // Don't parse attributes unless we have an identifier.
Douglas Gregor0286b462010-02-19 16:47:56 +00002884 if (D.getIdentifier() && getLang().CPlusPlus0x
Alexis Hunt96d5c762009-11-21 08:43:09 +00002885 && isCXX0XAttributeSpecifier(true)) {
2886 SourceLocation AttrEndLoc;
2887 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2888 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2889 }
2890
Chris Lattneracd58a32006-08-06 17:24:14 +00002891 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002892 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002893 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2894 // In such a case, check if we actually have a function declarator; if it
2895 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002896 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2897 // When not in file scope, warn for ambiguous function declarators, just
2898 // in case the author intended it as a variable definition.
2899 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2900 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2901 break;
2902 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002903 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002904 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002905 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002906 } else {
2907 break;
2908 }
2909 }
2910}
2911
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002912/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2913/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002914/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002915/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2916///
2917/// direct-declarator:
2918/// '(' declarator ')'
2919/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002920/// direct-declarator '(' parameter-type-list ')'
2921/// direct-declarator '(' identifier-list[opt] ')'
2922/// [GNU] direct-declarator '(' parameter-forward-declarations
2923/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002924///
2925void Parser::ParseParenDeclarator(Declarator &D) {
2926 SourceLocation StartLoc = ConsumeParen();
2927 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002928
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002929 // Eat any attributes before we look at whether this is a grouping or function
2930 // declarator paren. If this is a grouping paren, the attribute applies to
2931 // the type being built up, for example:
2932 // int (__attribute__(()) *x)(long y)
2933 // If this ends up not being a grouping paren, the attribute applies to the
2934 // first argument, for example:
2935 // int (__attribute__(()) int x)
2936 // In either case, we need to eat any attributes to be able to determine what
2937 // sort of paren this is.
2938 //
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002939 AttributeList *AttrList = 0;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002940 bool RequiresArg = false;
2941 if (Tok.is(tok::kw___attribute)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002942 AttrList = ParseGNUAttributes();
Mike Stump11289f42009-09-09 15:08:12 +00002943
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002944 // We require that the argument list (if this is a non-grouping paren) be
2945 // present even if the attribute list was empty.
2946 RequiresArg = true;
2947 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002948 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002949 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00002950 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2951 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002952 AttrList = ParseMicrosoftTypeAttributes(AttrList);
Eli Friedman53339e02009-06-08 23:27:34 +00002953 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00002954 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002955 if (Tok.is(tok::kw___pascal))
2956 AttrList = ParseBorlandTypeAttributes(AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00002957
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002958 // If we haven't past the identifier yet (or where the identifier would be
2959 // stored, if this is an abstract declarator), then this is probably just
2960 // grouping parens. However, if this could be an abstract-declarator, then
2961 // this could also be the start of function arguments (consider 'void()').
2962 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002963
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002964 if (!D.mayOmitIdentifier()) {
2965 // If this can't be an abstract-declarator, this *must* be a grouping
2966 // paren, because we haven't seen the identifier yet.
2967 isGrouping = true;
2968 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002969 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002970 isDeclarationSpecifier()) { // 'int(int)' is a function.
2971 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2972 // considered to be a type, not a K&R identifier-list.
2973 isGrouping = false;
2974 } else {
2975 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2976 isGrouping = true;
2977 }
Mike Stump11289f42009-09-09 15:08:12 +00002978
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002979 // If this is a grouping paren, handle:
2980 // direct-declarator: '(' declarator ')'
2981 // direct-declarator: '(' attributes declarator ')'
2982 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002983 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002984 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002985 if (AttrList)
Ted Kremenek5eec2b02010-11-10 05:59:39 +00002986 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002987
Sebastian Redlbd150f42008-11-21 19:14:01 +00002988 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002989 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002990 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002991
2992 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002993 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002994 return;
2995 }
Mike Stump11289f42009-09-09 15:08:12 +00002996
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002997 // Okay, if this wasn't a grouping paren, it must be the start of a function
2998 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002999 // identifier (and remember where it would have been), then call into
3000 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003001 D.SetIdentifier(0, Tok.getLocation());
3002
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003003 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003004}
3005
3006/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3007/// declarator D up to a paren, which indicates that we are parsing function
3008/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003009///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003010/// If AttrList is non-null, then the caller parsed those arguments immediately
3011/// after the open paren - they should be considered to be the first argument of
3012/// a parameter. If RequiresArg is true, then the first argument of the
3013/// function is required to be present and required to not be an identifier
3014/// list.
3015///
Chris Lattneracd58a32006-08-06 17:24:14 +00003016/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003017/// parameter-type-list: [C99 6.7.5]
3018/// parameter-list
3019/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003020/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003021///
3022/// parameter-list: [C99 6.7.5]
3023/// parameter-declaration
3024/// parameter-list ',' parameter-declaration
3025///
3026/// parameter-declaration: [C99 6.7.5]
3027/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003028/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003029/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003030/// declaration-specifiers abstract-declarator[opt]
3031/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003032/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003033/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003034///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003035/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00003036/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003037///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003038void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3039 AttributeList *AttrList,
3040 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003041 // lparen is already consumed!
3042 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003043
Douglas Gregor7fb25412010-10-01 18:44:50 +00003044 ParsedType TrailingReturnType;
3045
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003046 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003047 if (Tok.is(tok::r_paren)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003048 if (RequiresArg)
Chris Lattner6d29c102008-11-18 07:48:38 +00003049 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003050
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003051 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3052 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003053
3054 // cv-qualifier-seq[opt].
3055 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003056 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003057 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003058 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003059 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003060 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003061 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00003062 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003063 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003064 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003065
3066 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003067 if (Tok.is(tok::kw_throw)) {
3068 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003069 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003070 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003071 hasAnyExceptionSpec);
3072 assert(Exceptions.size() == ExceptionRanges.size() &&
3073 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003074 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003075
3076 // Parse trailing-return-type.
3077 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3078 TrailingReturnType = ParseTrailingReturnType().get();
3079 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003080 }
3081
Chris Lattner371ed4e2008-04-06 06:57:35 +00003082 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003083 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003084 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003085 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003086 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003087 /*arglist*/ 0, 0,
3088 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003089 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003090 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003091 Exceptions.data(),
3092 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003093 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003094 LParenLoc, RParenLoc, D,
3095 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003096 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003097 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003098 }
3099
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003100 // Alternatively, this parameter list may be an identifier list form for a
3101 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003102 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3103 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003104 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003105 // K&R identifier lists can't have typedefs as identifiers, per
3106 // C99 6.7.5.3p11.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003107 if (RequiresArg)
Steve Naroffb0486722009-01-28 19:16:40 +00003108 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner9453ab82010-05-14 17:23:36 +00003109
Steve Naroffb0486722009-01-28 19:16:40 +00003110 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003111 // normal declarators, not for abstract-declarators. Get the first
3112 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003113 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003114 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003115
3116 // Identifier lists follow a really simple grammar: the identifiers can
3117 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3118 // identifier lists are really rare in the brave new modern world, and it
3119 // is very common for someone to typo a type in a non-k&r style list. If
3120 // we are presented with something like: "void foo(intptr x, float y)",
3121 // we don't want to start parsing the function declarator as though it is
3122 // a K&R style declarator just because intptr is an invalid type.
3123 //
3124 // To handle this, we check to see if the token after the first identifier
3125 // is a "," or ")". Only if so, do we parse it as an identifier list.
3126 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3127 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3128 FirstTok.getIdentifierInfo(),
3129 FirstTok.getLocation(), D);
3130
3131 // If we get here, the code is invalid. Push the first identifier back
3132 // into the token stream and parse the first argument as an (invalid)
3133 // normal argument declarator.
3134 PP.EnterToken(Tok);
3135 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003136 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003137 }
Mike Stump11289f42009-09-09 15:08:12 +00003138
Chris Lattner371ed4e2008-04-06 06:57:35 +00003139 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003140
Chris Lattner371ed4e2008-04-06 06:57:35 +00003141 // Build up an array of information about the parsed arguments.
3142 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003143
3144 // Enter function-declaration scope, limiting any declarators to the
3145 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003146 ParseScope PrototypeScope(this,
3147 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003148
Chris Lattner371ed4e2008-04-06 06:57:35 +00003149 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003150 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003151 while (1) {
3152 if (Tok.is(tok::ellipsis)) {
3153 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003154 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003155 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003156 }
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003157
3158 // Skip any Microsoft attributes before a param.
3159 if (getLang().Microsoft && Tok.is(tok::l_square))
3160 ParseMicrosoftAttributes();
Mike Stump11289f42009-09-09 15:08:12 +00003161
Chris Lattner371ed4e2008-04-06 06:57:35 +00003162 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00003163
Chris Lattner371ed4e2008-04-06 06:57:35 +00003164 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003165 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003166 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003167
3168 // If the caller parsed attributes for the first argument, add them now.
3169 if (AttrList) {
3170 DS.AddAttributes(AttrList);
3171 AttrList = 0; // Only apply the attributes to the first parameter.
3172 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003173 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003174
Chris Lattner371ed4e2008-04-06 06:57:35 +00003175 // Parse the declarator. This is "PrototypeContext", because we must
3176 // accept either 'declarator' or 'abstract-declarator' here.
3177 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3178 ParseDeclarator(ParmDecl);
3179
3180 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003181 if (Tok.is(tok::kw___attribute)) {
3182 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003183 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003184 ParmDecl.AddAttributes(AttrList, Loc);
3185 }
Mike Stump11289f42009-09-09 15:08:12 +00003186
Chris Lattner371ed4e2008-04-06 06:57:35 +00003187 // Remember this parsed parameter in ParamInfo.
3188 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003189
Douglas Gregor4d87df52008-12-16 21:30:33 +00003190 // DefArgToks is used when the parsing of default arguments needs
3191 // to be delayed.
3192 CachedTokens *DefArgToks = 0;
3193
Chris Lattner371ed4e2008-04-06 06:57:35 +00003194 // If no parameter was specified, verify that *something* was specified,
3195 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003196 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3197 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003198 // Completely missing, emit error.
3199 Diag(DSStart, diag::err_missing_param);
3200 } else {
3201 // Otherwise, we have something. Add it and let semantic analysis try
3202 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003203
Chris Lattner371ed4e2008-04-06 06:57:35 +00003204 // Inform the actions module about the parameter declarator, so it gets
3205 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003206 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003207
3208 // Parse the default argument, if any. We parse the default
3209 // arguments in all dialects; the semantic analysis in
3210 // ActOnParamDefaultArgument will reject the default argument in
3211 // C.
3212 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003213 SourceLocation EqualLoc = Tok.getLocation();
3214
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003215 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003216 if (D.getContext() == Declarator::MemberContext) {
3217 // If we're inside a class definition, cache the tokens
3218 // corresponding to the default argument. We'll actually parse
3219 // them when we see the end of the class definition.
3220 // FIXME: Templates will require something similar.
3221 // FIXME: Can we use a smart pointer for Toks?
3222 DefArgToks = new CachedTokens;
3223
Mike Stump11289f42009-09-09 15:08:12 +00003224 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003225 /*StopAtSemi=*/true,
3226 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003227 delete DefArgToks;
3228 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003229 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003230 } else {
3231 // Mark the end of the default argument so that we know when to
3232 // stop when we parse it later on.
3233 Token DefArgEnd;
3234 DefArgEnd.startToken();
3235 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3236 DefArgEnd.setLocation(Tok.getLocation());
3237 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003238 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003239 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003240 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003241 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003242 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003243 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003244
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003245 // The argument isn't actually potentially evaluated unless it is
3246 // used.
3247 EnterExpressionEvaluationContext Eval(Actions,
3248 Sema::PotentiallyEvaluatedIfUsed);
3249
John McCalldadc5752010-08-24 06:29:42 +00003250 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003251 if (DefArgResult.isInvalid()) {
3252 Actions.ActOnParamDefaultArgumentError(Param);
3253 SkipUntil(tok::comma, tok::r_paren, true, true);
3254 } else {
3255 // Inform the actions module about the default argument
3256 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003257 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003258 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003259 }
3260 }
Mike Stump11289f42009-09-09 15:08:12 +00003261
3262 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3263 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003264 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003265 }
3266
3267 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003268 if (Tok.isNot(tok::comma)) {
3269 if (Tok.is(tok::ellipsis)) {
3270 IsVariadic = true;
3271 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3272
3273 if (!getLang().CPlusPlus) {
3274 // We have ellipsis without a preceding ',', which is ill-formed
3275 // in C. Complain and provide the fix.
3276 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003277 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003278 }
3279 }
3280
3281 break;
3282 }
Mike Stump11289f42009-09-09 15:08:12 +00003283
Chris Lattner371ed4e2008-04-06 06:57:35 +00003284 // Consume the comma.
3285 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003286 }
Mike Stump11289f42009-09-09 15:08:12 +00003287
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003288 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003289 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3290 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003291
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003292 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003293 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003294 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003295 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003296 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003297 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003298
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003299 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003300 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003301 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003302 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003303 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003304
3305 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003306 if (Tok.is(tok::kw_throw)) {
3307 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003308 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003309 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003310 hasAnyExceptionSpec);
3311 assert(Exceptions.size() == ExceptionRanges.size() &&
3312 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003313 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003314
3315 // Parse trailing-return-type.
3316 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3317 TrailingReturnType = ParseTrailingReturnType().get();
3318 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003319 }
3320
Douglas Gregor7fb25412010-10-01 18:44:50 +00003321 // FIXME: We should leave the prototype scope before parsing the exception
3322 // specification, and then reenter it when parsing the trailing return type.
3323
3324 // Leave prototype scope.
3325 PrototypeScope.Exit();
3326
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003327 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003328 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003329 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003330 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003331 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003332 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003333 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003334 Exceptions.data(),
3335 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003336 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003337 LParenLoc, RParenLoc, D,
3338 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003339 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003340}
Chris Lattneracd58a32006-08-06 17:24:14 +00003341
Chris Lattner6c940e62008-04-06 06:34:08 +00003342/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3343/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003344/// first identifier has already been consumed, and the current token is the
3345/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003346///
3347/// identifier-list: [C99 6.7.5]
3348/// identifier
3349/// identifier-list ',' identifier
3350///
3351void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003352 IdentifierInfo *FirstIdent,
3353 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003354 Declarator &D) {
3355 // Build up an array of information about the parsed arguments.
3356 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3357 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003358
Chris Lattner6c940e62008-04-06 06:34:08 +00003359 // If there was no identifier specified for the declarator, either we are in
3360 // an abstract-declarator, or we are in a parameter declarator which was found
3361 // to be abstract. In abstract-declarators, identifier lists are not valid:
3362 // diagnose this.
3363 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003364 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003365
Chris Lattner9453ab82010-05-14 17:23:36 +00003366 // The first identifier was already read, and is known to be the first
3367 // identifier in the list. Remember this identifier in ParamInfo.
3368 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003369 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003370
Chris Lattner6c940e62008-04-06 06:34:08 +00003371 while (Tok.is(tok::comma)) {
3372 // Eat the comma.
3373 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003374
Chris Lattner9186f552008-04-06 06:39:19 +00003375 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003376 if (Tok.isNot(tok::identifier)) {
3377 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003378 SkipUntil(tok::r_paren);
3379 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003380 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003381
Chris Lattner6c940e62008-04-06 06:34:08 +00003382 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003383
3384 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003385 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003386 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003387
Chris Lattner6c940e62008-04-06 06:34:08 +00003388 // Verify that the argument identifier has not already been mentioned.
3389 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003390 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003391 } else {
3392 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003393 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003394 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003395 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003396 }
Mike Stump11289f42009-09-09 15:08:12 +00003397
Chris Lattner6c940e62008-04-06 06:34:08 +00003398 // Eat the identifier.
3399 ConsumeToken();
3400 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003401
3402 // If we have the closing ')', eat it and we're done.
3403 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3404
Chris Lattner9186f552008-04-06 06:39:19 +00003405 // Remember that we parsed a function type, and remember the attributes. This
3406 // function type is always a K&R style function type, which is not varargs and
3407 // has no prototype.
3408 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003409 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003410 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003411 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003412 /*exception*/false,
3413 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003414 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003415 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003416}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003417
Chris Lattnere8074e62006-08-06 18:30:15 +00003418/// [C90] direct-declarator '[' constant-expression[opt] ']'
3419/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3420/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3421/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3422/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3423void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003424 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003425
Chris Lattner84a11622008-12-18 07:27:21 +00003426 // C array syntax has many features, but by-far the most common is [] and [4].
3427 // This code does a fast path to handle some of the most obvious cases.
3428 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003429 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003430 //FIXME: Use these
3431 CXX0XAttributeList Attr;
3432 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3433 Attr = ParseCXX0XAttributes();
3434 }
3435
Chris Lattner84a11622008-12-18 07:27:21 +00003436 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00003437 ExprResult NumElements;
Douglas Gregor04318252009-07-06 15:59:29 +00003438 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3439 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003440 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003441 return;
3442 } else if (Tok.getKind() == tok::numeric_constant &&
3443 GetLookAheadToken(1).is(tok::r_square)) {
3444 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00003445 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003446 ConsumeToken();
3447
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003448 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003449 //FIXME: Use these
3450 CXX0XAttributeList Attr;
3451 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3452 Attr = ParseCXX0XAttributes();
3453 }
Chris Lattner84a11622008-12-18 07:27:21 +00003454
3455 // If there was an error parsing the assignment-expression, recover.
3456 if (ExprRes.isInvalid())
3457 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00003458
Chris Lattner84a11622008-12-18 07:27:21 +00003459 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00003460 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3461 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003462 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003463 return;
3464 }
Mike Stump11289f42009-09-09 15:08:12 +00003465
Chris Lattnere8074e62006-08-06 18:30:15 +00003466 // If valid, this location is the position where we read the 'static' keyword.
3467 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003468 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003469 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003470
Chris Lattnere8074e62006-08-06 18:30:15 +00003471 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003472 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003473 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003474 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003475
Chris Lattnere8074e62006-08-06 18:30:15 +00003476 // If we haven't already read 'static', check to see if there is one after the
3477 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003478 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003479 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003480
Chris Lattnere8074e62006-08-06 18:30:15 +00003481 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003482 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00003483 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00003484
Chris Lattner521ff2b2008-04-06 05:26:30 +00003485 // Handle the case where we have '[*]' as the array size. However, a leading
3486 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3487 // the the token after the star is a ']'. Since stars in arrays are
3488 // infrequent, use of lookahead is not costly here.
3489 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003490 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003491
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003492 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003493 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003494 StaticLoc = SourceLocation(); // Drop the static.
3495 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003496 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003497 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003498 // Note, in C89, this production uses the constant-expr production instead
3499 // of assignment-expr. The only difference is that assignment-expr allows
3500 // things like '=' and '*='. Sema rejects these in C89 mode because they
3501 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003502
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003503 // Parse the constant-expression or assignment-expression now (depending
3504 // on dialect).
3505 if (getLang().CPlusPlus)
3506 NumElements = ParseConstantExpression();
3507 else
3508 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003509 }
Mike Stump11289f42009-09-09 15:08:12 +00003510
Chris Lattner62591722006-08-12 18:40:58 +00003511 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003512 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003513 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003514 // If the expression was invalid, skip it.
3515 SkipUntil(tok::r_square);
3516 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003517 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003518
3519 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3520
Alexis Hunt96d5c762009-11-21 08:43:09 +00003521 //FIXME: Use these
3522 CXX0XAttributeList Attr;
3523 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3524 Attr = ParseCXX0XAttributes();
3525 }
3526
Chris Lattner84a11622008-12-18 07:27:21 +00003527 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00003528 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3529 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003530 NumElements.release(),
3531 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003532 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003533}
3534
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003535/// [GNU] typeof-specifier:
3536/// typeof ( expressions )
3537/// typeof ( type-name )
3538/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003539///
3540void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003541 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003542 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003543 SourceLocation StartLoc = ConsumeToken();
3544
John McCalle8595032010-01-13 20:03:27 +00003545 const bool hasParens = Tok.is(tok::l_paren);
3546
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003547 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00003548 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003549 SourceRange CastRange;
John McCalldadc5752010-08-24 06:29:42 +00003550 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall6caebb12010-08-25 02:45:51 +00003551 isCastExpr,
3552 CastTy,
3553 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003554 if (hasParens)
3555 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003556
3557 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003558 // FIXME: Not accurate, the range gets one token more than it should.
3559 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003560 else
3561 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003562
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003563 if (isCastExpr) {
3564 if (!CastTy) {
3565 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003566 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003567 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003568
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003569 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003570 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003571 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3572 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003573 DiagID, CastTy))
3574 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003575 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003576 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003577
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003578 // If we get here, the operand to the typeof was an expresion.
3579 if (Operand.isInvalid()) {
3580 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003581 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003582 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003583
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003584 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003585 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003586 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3587 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00003588 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00003589 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003590}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003591
3592
3593/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3594/// from TryAltiVecVectorToken.
3595bool Parser::TryAltiVecVectorTokenOutOfLine() {
3596 Token Next = NextToken();
3597 switch (Next.getKind()) {
3598 default: return false;
3599 case tok::kw_short:
3600 case tok::kw_long:
3601 case tok::kw_signed:
3602 case tok::kw_unsigned:
3603 case tok::kw_void:
3604 case tok::kw_char:
3605 case tok::kw_int:
3606 case tok::kw_float:
3607 case tok::kw_double:
3608 case tok::kw_bool:
3609 case tok::kw___pixel:
3610 Tok.setKind(tok::kw___vector);
3611 return true;
3612 case tok::identifier:
3613 if (Next.getIdentifierInfo() == Ident_pixel) {
3614 Tok.setKind(tok::kw___vector);
3615 return true;
3616 }
3617 return false;
3618 }
3619}
3620
3621bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3622 const char *&PrevSpec, unsigned &DiagID,
3623 bool &isInvalid) {
3624 if (Tok.getIdentifierInfo() == Ident_vector) {
3625 Token Next = NextToken();
3626 switch (Next.getKind()) {
3627 case tok::kw_short:
3628 case tok::kw_long:
3629 case tok::kw_signed:
3630 case tok::kw_unsigned:
3631 case tok::kw_void:
3632 case tok::kw_char:
3633 case tok::kw_int:
3634 case tok::kw_float:
3635 case tok::kw_double:
3636 case tok::kw_bool:
3637 case tok::kw___pixel:
3638 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3639 return true;
3640 case tok::identifier:
3641 if (Next.getIdentifierInfo() == Ident_pixel) {
3642 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3643 return true;
3644 }
3645 break;
3646 default:
3647 break;
3648 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00003649 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003650 DS.isTypeAltiVecVector()) {
3651 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3652 return true;
3653 }
3654 return false;
3655}
3656