blob: d97b4e30a0ae74211b53fb5abf449ea140cbb224 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000016#include "clang/Sema/Scope.h"
17#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000019#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/SmallSet.h"
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// C99 6.7: Declarations.
25//===----------------------------------------------------------------------===//
26
27/// ParseTypeName
28/// type-name: [C99 6.7.6]
29/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000030///
31/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000032TypeResult Parser::ParseTypeName(SourceRange *Range,
33 Declarator::TheContext Context) {
Reid Spencer5f016e22007-07-11 17:01:13 +000034 // Parse the common declaration-specifiers piece.
35 DeclSpec DS;
36 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000037
Reid Spencer5f016e22007-07-11 17:01:13 +000038 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000039 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000040 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000041 if (Range)
42 *Range = DeclaratorInfo.getSourceRange();
43
Chris Lattnereaaebc72009-04-25 08:06:05 +000044 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000045 return true;
46
Douglas Gregor23c94db2010-07-02 17:43:08 +000047 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000048}
49
Sean Huntbbd37c62009-11-21 08:43:09 +000050/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000051///
52/// [GNU] attributes:
53/// attribute
54/// attributes attribute
55///
56/// [GNU] attribute:
57/// '__attribute__' '(' '(' attribute-list ')' ')'
58///
59/// [GNU] attribute-list:
60/// attrib
61/// attribute_list ',' attrib
62///
63/// [GNU] attrib:
64/// empty
65/// attrib-name
66/// attrib-name '(' identifier ')'
67/// attrib-name '(' identifier ',' nonempty-expr-list ')'
68/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
69///
70/// [GNU] attrib-name:
71/// identifier
72/// typespec
73/// typequal
74/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000075///
Reid Spencer5f016e22007-07-11 17:01:13 +000076/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000077/// token lookahead. Comment from gcc: "If they start with an identifier
78/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000079/// start with that identifier; otherwise they are an expression list."
80///
81/// At the moment, I am not doing 2 token lookahead. I am also unaware of
82/// any attributes that don't work (based on my limited testing). Most
83/// attributes are very simple in practice. Until we find a bug, I don't see
84/// a pressing need to implement the 2 token lookahead.
85
John McCall7f040a92010-12-24 02:08:15 +000086void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
87 SourceLocation *endLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +000088 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner04d66662007-10-09 17:33:22 +000090 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000091 ConsumeToken();
92 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
93 "attribute")) {
94 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +000095 return;
Reid Spencer5f016e22007-07-11 17:01:13 +000096 }
97 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
98 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +000099 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 }
101 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000102 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
103 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000104
105 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Stump1eb44332009-09-09 15:08:12 +0000113
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000114 // check if we have a "parameterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000115 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Chris Lattner04d66662007-10-09 17:33:22 +0000118 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
120 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000121
122 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 // __attribute__(( mode(byte) ))
124 ConsumeParen(); // ignore the right paren loc for now
John McCall7f040a92010-12-24 02:08:15 +0000125 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
126 ParmName, ParmLoc, 0, 0));
Chris Lattner04d66662007-10-09 17:33:22 +0000127 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 ConsumeToken();
129 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000130 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 // now parse the non-empty comma separated list of expressions
134 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000135 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000136 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 ArgExprsOk = false;
138 SkipUntil(tok::r_paren);
139 break;
140 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000141 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 }
Chris Lattner04d66662007-10-09 17:33:22 +0000143 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 break;
145 ConsumeToken(); // Eat the comma, move to the next argument
146 }
Chris Lattner04d66662007-10-09 17:33:22 +0000147 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 ConsumeParen(); // ignore the right paren loc for now
John McCall7f040a92010-12-24 02:08:15 +0000149 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0,
150 AttrNameLoc, ParmName, ParmLoc,
151 ArgExprs.take(), ArgExprs.size()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 }
153 }
154 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000155 switch (Tok.getKind()) {
156 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 // __attribute__(( nonnull() ))
159 ConsumeParen(); // ignore the right paren loc for now
John McCall7f040a92010-12-24 02:08:15 +0000160 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
161 0, SourceLocation(), 0, 0));
Nate Begeman6f3d8382009-06-26 06:32:41 +0000162 break;
163 case tok::kw_char:
164 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000165 case tok::kw_char16_t:
166 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000167 case tok::kw_bool:
168 case tok::kw_short:
169 case tok::kw_int:
170 case tok::kw_long:
171 case tok::kw_signed:
172 case tok::kw_unsigned:
173 case tok::kw_float:
174 case tok::kw_double:
175 case tok::kw_void:
John McCall7f040a92010-12-24 02:08:15 +0000176 case tok::kw_typeof: {
177 AttributeList *attr
178 = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
179 0, SourceLocation(), 0, 0);
180 attrs.add(attr);
181 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian1b72fa72010-08-17 23:19:16 +0000182 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000183 // If it's a builtin type name, eat it and expect a rparen
184 // __attribute__(( vec_type_hint(char) ))
185 ConsumeToken();
Nate Begeman6f3d8382009-06-26 06:32:41 +0000186 if (Tok.is(tok::r_paren))
187 ConsumeParen();
188 break;
John McCall7f040a92010-12-24 02:08:15 +0000189 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000190 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000192 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 // now parse the list of expressions
196 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000197 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000198 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 ArgExprsOk = false;
200 SkipUntil(tok::r_paren);
201 break;
202 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000203 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 }
Chris Lattner04d66662007-10-09 17:33:22 +0000205 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 break;
207 ConsumeToken(); // Eat the comma, move to the next argument
208 }
209 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000210 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 ConsumeParen(); // ignore the right paren loc for now
John McCall7f040a92010-12-24 02:08:15 +0000212 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0,
213 AttrNameLoc, 0, SourceLocation(),
214 ArgExprs.take(), ArgExprs.size()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000216 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 }
218 }
219 } else {
John McCall7f040a92010-12-24 02:08:15 +0000220 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
221 0, SourceLocation(), 0, 0));
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 }
223 }
224 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000226 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000227 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
228 SkipUntil(tok::r_paren, false);
229 }
John McCall7f040a92010-12-24 02:08:15 +0000230 if (endLoc)
231 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000233}
234
Eli Friedmana23b4852009-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
John McCall7f040a92010-12-24 02:08:15 +0000244void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000245 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000246
Steve Narofff59e17e2008-12-24 20:59:21 +0000247 ConsumeToken();
Eli Friedmana23b4852009-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 ;
John McCall7f040a92010-12-24 02:08:15 +0000251 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000252 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000253 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-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 McCall60d7b3a2010-08-24 06:29:42 +0000260 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000261 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000262 Expr *ExprList = ArgExpr.take();
John McCall7f040a92010-12-24 02:08:15 +0000263 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
264 SourceLocation(), &ExprList, 1, true));
Eli Friedmana23b4852009-06-08 07:21:15 +0000265 }
266 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
267 SkipUntil(tok::r_paren, false);
268 } else {
John McCall7f040a92010-12-24 02:08:15 +0000269 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
270 0, SourceLocation(), 0, 0, true));
Eli Friedmana23b4852009-06-08 07:21:15 +0000271 }
272 }
273 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
274 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000275 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000276}
277
John McCall7f040a92010-12-24 02:08:15 +0000278void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000279 // Treat these like attributes
280 // FIXME: Allow Sema to distinguish between these and real attributes!
281 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000282 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
283 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000284 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
285 SourceLocation AttrNameLoc = ConsumeToken();
286 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
287 // FIXME: Support these properly!
288 continue;
John McCall7f040a92010-12-24 02:08:15 +0000289 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
290 SourceLocation(), 0, 0, true));
Eli Friedman290eeb02009-06-08 23:27:34 +0000291 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000292}
293
John McCall7f040a92010-12-24 02:08:15 +0000294void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000295 // Treat these like attributes
296 while (Tok.is(tok::kw___pascal)) {
297 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
298 SourceLocation AttrNameLoc = ConsumeToken();
John McCall7f040a92010-12-24 02:08:15 +0000299 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
300 SourceLocation(), 0, 0, true));
Dawn Perchik52fc3142010-09-03 01:29:35 +0000301 }
John McCall7f040a92010-12-24 02:08:15 +0000302}
303
304void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
305 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
306 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000307}
308
Reid Spencer5f016e22007-07-11 17:01:13 +0000309/// ParseDeclaration - Parse a full 'declaration', which consists of
310/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000311/// 'Context' should be a Declarator::TheContext value. This returns the
312/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000313///
314/// declaration: [C99 6.7]
315/// block-declaration ->
316/// simple-declaration
317/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000318/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000319/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000320/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000321/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000322/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000323/// others... [FIXME]
324///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000325Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
326 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000327 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000328 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000329 ParenBraceBracketBalancer BalancerRAIIObj(*this);
330
John McCalld226f652010-08-21 09:40:31 +0000331 Decl *SingleDecl = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000332 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000333 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000334 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000335 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000336 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000337 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000338 case tok::kw_inline:
Sebastian Redl88e64ca2010-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)) {
John McCall7f040a92010-12-24 02:08:15 +0000341 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000342 SourceLocation InlineLoc = ConsumeToken();
343 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
344 break;
345 }
John McCall7f040a92010-12-24 02:08:15 +0000346 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000347 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000348 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000349 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000350 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000351 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000352 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000353 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
John McCall7f040a92010-12-24 02:08:15 +0000354 DeclEnd, attrs);
Chris Lattner682bf922009-03-29 16:50:03 +0000355 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000356 case tok::kw_static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000357 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000358 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000359 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000360 default:
John McCall7f040a92010-12-24 02:08:15 +0000361 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000362 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000363
Chris Lattner682bf922009-03-29 16:50:03 +0000364 // This routine returns a DeclGroup, if the thing we parsed only contains a
365 // single decl, convert it now.
366 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000367}
368
369/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
370/// declaration-specifiers init-declarator-list[opt] ';'
371///[C90/C++]init-declarator-list ';' [TODO]
372/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000373///
374/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000375/// declaration. If it is true, it checks for and eats it.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000376Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
377 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000378 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000379 ParsedAttributes &attrs,
Chris Lattner5c5db552010-04-05 18:18:31 +0000380 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000382 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000383 DS.takeAttributesFrom(attrs);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000384 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
385 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000386 StmtResult R = Actions.ActOnVlaStmt(DS);
387 if (R.isUsable())
388 Stmts.push_back(R.release());
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
391 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000392 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000393 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000394 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallaec03712010-05-21 20:45:30 +0000395 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000396 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000397 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattner5c5db552010-04-05 18:18:31 +0000400 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld8ac0572009-11-03 19:26:08 +0000401}
Mike Stump1eb44332009-09-09 15:08:12 +0000402
John McCalld8ac0572009-11-03 19:26:08 +0000403/// ParseDeclGroup - Having concluded that this is either a function
404/// definition or a group of object declarations, actually parse the
405/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000406Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
407 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000408 bool AllowFunctionDefinitions,
409 SourceLocation *DeclEnd) {
410 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000411 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000412 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000413
John McCalld8ac0572009-11-03 19:26:08 +0000414 // Bail out if the first declarator didn't seem well-formed.
415 if (!D.hasName() && !D.mayOmitIdentifier()) {
416 // Skip until ; or }.
417 SkipUntil(tok::r_brace, true, true);
418 if (Tok.is(tok::semi))
419 ConsumeToken();
420 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Chris Lattnerc82daef2010-07-11 22:24:20 +0000423 // Check to see if we have a function *definition* which must have a body.
424 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
425 // Look at the next token to make sure that this isn't a function
426 // declaration. We have to check this because __attribute__ might be the
427 // start of a function definition in GCC-extended K&R C.
428 !isDeclarationAfterDeclarator()) {
429
Chris Lattner004659a2010-07-11 22:42:07 +0000430 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000431 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
432 Diag(Tok, diag::err_function_declared_typedef);
433
434 // Recover by treating the 'typedef' as spurious.
435 DS.ClearStorageClassSpecs();
436 }
437
John McCalld226f652010-08-21 09:40:31 +0000438 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000439 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000440 }
441
442 if (isDeclarationSpecifier()) {
443 // If there is an invalid declaration specifier right after the function
444 // prototype, then we must be in a missing semicolon case where this isn't
445 // actually a body. Just fall through into the code that handles it as a
446 // prototype, and let the top-level code handle the erroneous declspec
447 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000448 } else {
449 Diag(Tok, diag::err_expected_fn_body);
450 SkipUntil(tok::semi);
451 return DeclGroupPtrTy();
452 }
453 }
454
John McCalld226f652010-08-21 09:40:31 +0000455 llvm::SmallVector<Decl *, 8> DeclsInGroup;
456 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000457 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000458 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000459 DeclsInGroup.push_back(FirstDecl);
460
461 // If we don't have a comma, it is either the end of the list (a ';') or an
462 // error, bail out.
463 while (Tok.is(tok::comma)) {
464 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000465 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000466
467 // Parse the next declarator.
468 D.clear();
469
470 // Accept attributes in an init-declarator. In the first declarator in a
471 // declaration, these would be part of the declspec. In subsequent
472 // declarators, they become part of the declarator itself, so that they
473 // don't apply to declarators after *this* one. Examples:
474 // short __attribute__((common)) var; -> declspec
475 // short var __attribute__((common)); -> declarator
476 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +0000477 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +0000478
479 ParseDeclarator(D);
480
John McCalld226f652010-08-21 09:40:31 +0000481 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000482 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000483 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000484 DeclsInGroup.push_back(ThisDecl);
485 }
486
487 if (DeclEnd)
488 *DeclEnd = Tok.getLocation();
489
490 if (Context != Declarator::ForContext &&
491 ExpectAndConsume(tok::semi,
492 Context == Declarator::FileContext
493 ? diag::err_invalid_token_after_toplevel_declarator
494 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000495 // Okay, there was no semicolon and one was expected. If we see a
496 // declaration specifier, just assume it was missing and continue parsing.
497 // Otherwise things are very confused and we skip to recover.
498 if (!isDeclarationSpecifier()) {
499 SkipUntil(tok::r_brace, true, true);
500 if (Tok.is(tok::semi))
501 ConsumeToken();
502 }
John McCalld8ac0572009-11-03 19:26:08 +0000503 }
504
Douglas Gregor23c94db2010-07-02 17:43:08 +0000505 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000506 DeclsInGroup.data(),
507 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000508}
509
Douglas Gregor1426e532009-05-12 21:31:51 +0000510/// \brief Parse 'declaration' after parsing 'declaration-specifiers
511/// declarator'. This method parses the remainder of the declaration
512/// (including any attributes or initializer, among other things) and
513/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000514///
Reid Spencer5f016e22007-07-11 17:01:13 +0000515/// init-declarator: [C99 6.7]
516/// declarator
517/// declarator '=' initializer
518/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
519/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000520/// [C++] declarator initializer[opt]
521///
522/// [C++] initializer:
523/// [C++] '=' initializer-clause
524/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000525/// [C++0x] '=' 'default' [TODO]
526/// [C++0x] '=' 'delete'
527///
528/// According to the standard grammar, =default and =delete are function
529/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000530///
John McCalld226f652010-08-21 09:40:31 +0000531Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000532 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000533 // If a simple-asm-expr is present, parse it.
534 if (Tok.is(tok::kw_asm)) {
535 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000536 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor1426e532009-05-12 21:31:51 +0000537 if (AsmLabel.isInvalid()) {
538 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000539 return 0;
Douglas Gregor1426e532009-05-12 21:31:51 +0000540 }
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Douglas Gregor1426e532009-05-12 21:31:51 +0000542 D.setAsmLabel(AsmLabel.release());
543 D.SetRangeEnd(Loc);
544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
John McCall7f040a92010-12-24 02:08:15 +0000546 MaybeParseGNUAttributes(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Douglas Gregor1426e532009-05-12 21:31:51 +0000548 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000549 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000550 switch (TemplateInfo.Kind) {
551 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000552 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000553 break;
554
555 case ParsedTemplateInfo::Template:
556 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000557 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000558 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +0000559 TemplateInfo.TemplateParams->data(),
560 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000561 D);
562 break;
563
564 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000565 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000566 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000567 TemplateInfo.ExternLoc,
568 TemplateInfo.TemplateLoc,
569 D);
570 if (ThisRes.isInvalid()) {
571 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000572 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000573 }
574
575 ThisDecl = ThisRes.get();
576 break;
577 }
578 }
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Douglas Gregor1426e532009-05-12 21:31:51 +0000580 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000581 if (isTokenEqualOrMistypedEqualEqual(
582 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000583 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000584 if (Tok.is(tok::kw_delete)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000585 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000586
587 if (!getLang().CPlusPlus0x)
588 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
589
Douglas Gregor1426e532009-05-12 21:31:51 +0000590 Actions.SetDeclDeleted(ThisDecl, DelLoc);
591 } else {
John McCall731ad842009-12-19 09:28:58 +0000592 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
593 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000594 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000595 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000596
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000597 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000598 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000599 ConsumeCodeCompletionToken();
600 SkipUntil(tok::comma, true, true);
601 return ThisDecl;
602 }
603
John McCall60d7b3a2010-08-24 06:29:42 +0000604 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000605
John McCall731ad842009-12-19 09:28:58 +0000606 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000607 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000608 ExitScope();
609 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000610
Douglas Gregor1426e532009-05-12 21:31:51 +0000611 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000612 SkipUntil(tok::comma, true, true);
613 Actions.ActOnInitializerError(ThisDecl);
614 } else
John McCall9ae2f072010-08-23 23:25:46 +0000615 Actions.AddInitializerToDecl(ThisDecl, Init.take());
Douglas Gregor1426e532009-05-12 21:31:51 +0000616 }
617 } else if (Tok.is(tok::l_paren)) {
618 // Parse C++ direct initializer: '(' expression-list ')'
619 SourceLocation LParenLoc = ConsumeParen();
620 ExprVector Exprs(Actions);
621 CommaLocsTy CommaLocs;
622
Douglas Gregorb4debae2009-12-22 17:47:17 +0000623 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
624 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000625 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000626 }
627
Douglas Gregor1426e532009-05-12 21:31:51 +0000628 if (ParseExpressionList(Exprs, CommaLocs)) {
629 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000630
631 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000632 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000633 ExitScope();
634 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000635 } else {
636 // Match the ')'.
637 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
638
639 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
640 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000641
642 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000643 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000644 ExitScope();
645 }
646
Douglas Gregor1426e532009-05-12 21:31:51 +0000647 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
648 move_arg(Exprs),
Douglas Gregora1a04782010-09-09 16:33:13 +0000649 RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000650 }
651 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000652 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000653 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
654 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000655 }
656
657 return ThisDecl;
658}
659
Reid Spencer5f016e22007-07-11 17:01:13 +0000660/// ParseSpecifierQualifierList
661/// specifier-qualifier-list:
662/// type-specifier specifier-qualifier-list[opt]
663/// type-qualifier specifier-qualifier-list[opt]
664/// [GNU] attributes specifier-qualifier-list[opt]
665///
666void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
667 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
668 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Reid Spencer5f016e22007-07-11 17:01:13 +0000671 // Validate declspec for type-name.
672 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000673 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +0000674 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 // Issue diagnostic and remove storage class if present.
678 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
679 if (DS.getStorageClassSpecLoc().isValid())
680 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
681 else
682 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
683 DS.ClearStorageClassSpecs();
684 }
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 // Issue diagnostic and remove function specfier if present.
687 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000688 if (DS.isInlineSpecified())
689 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
690 if (DS.isVirtualSpecified())
691 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
692 if (DS.isExplicitSpecified())
693 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 DS.ClearFunctionSpecs();
695 }
696}
697
Chris Lattnerc199ab32009-04-12 20:42:31 +0000698/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
699/// specified token is valid after the identifier in a declarator which
700/// immediately follows the declspec. For example, these things are valid:
701///
702/// int x [ 4]; // direct-declarator
703/// int x ( int y); // direct-declarator
704/// int(int x ) // direct-declarator
705/// int x ; // simple-declaration
706/// int x = 17; // init-declarator-list
707/// int x , y; // init-declarator-list
708/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000709/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000710/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000711///
712/// This is not, because 'x' does not immediately follow the declspec (though
713/// ')' happens to be valid anyway).
714/// int (x)
715///
716static bool isValidAfterIdentifierInDeclarator(const Token &T) {
717 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
718 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000719 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000720}
721
Chris Lattnere40c2952009-04-14 21:34:55 +0000722
723/// ParseImplicitInt - This method is called when we have an non-typename
724/// identifier in a declspec (which normally terminates the decl spec) when
725/// the declspec has no type specifier. In this case, the declspec is either
726/// malformed or is "implicit int" (in K&R and C89).
727///
728/// This method handles diagnosing this prettily and returns false if the
729/// declspec is done being processed. If it recovers and thinks there may be
730/// other pieces of declspec after it, it returns true.
731///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000732bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000733 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000734 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000735 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Chris Lattnere40c2952009-04-14 21:34:55 +0000737 SourceLocation Loc = Tok.getLocation();
738 // If we see an identifier that is not a type name, we normally would
739 // parse it as the identifer being declared. However, when a typename
740 // is typo'd or the definition is not included, this will incorrectly
741 // parse the typename as the identifier name and fall over misparsing
742 // later parts of the diagnostic.
743 //
744 // As such, we try to do some look-ahead in cases where this would
745 // otherwise be an "implicit-int" case to see if this is invalid. For
746 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
747 // an identifier with implicit int, we'd get a parse error because the
748 // next token is obviously invalid for a type. Parse these as a case
749 // with an invalid type specifier.
750 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Chris Lattnere40c2952009-04-14 21:34:55 +0000752 // Since we know that this either implicit int (which is rare) or an
753 // error, we'd do lookahead to try to do better recovery.
754 if (isValidAfterIdentifierInDeclarator(NextToken())) {
755 // If this token is valid for implicit int, e.g. "static x = 4", then
756 // we just avoid eating the identifier, so it will be parsed as the
757 // identifier in the declarator.
758 return false;
759 }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Chris Lattnere40c2952009-04-14 21:34:55 +0000761 // Otherwise, if we don't consume this token, we are going to emit an
762 // error anyway. Try to recover from various common problems. Check
763 // to see if this was a reference to a tag name without a tag specified.
764 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000765 //
766 // C++ doesn't need this, and isTagName doesn't take SS.
767 if (SS == 0) {
768 const char *TagName = 0;
769 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Douglas Gregor23c94db2010-07-02 17:43:08 +0000771 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +0000772 default: break;
773 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
774 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
775 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
776 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Chris Lattnerf4382f52009-04-14 22:17:06 +0000779 if (TagName) {
780 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +0000781 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +0000782 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Chris Lattnerf4382f52009-04-14 22:17:06 +0000784 // Parse this as a tag as if the missing tag were present.
785 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +0000786 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000787 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000788 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000789 return true;
790 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000791 }
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Douglas Gregora786fdb2009-10-13 23:27:22 +0000793 // This is almost certainly an invalid type name. Let the action emit a
794 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +0000795 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +0000796 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +0000797 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +0000798 // The action emitted a diagnostic, so we don't have to.
799 if (T) {
800 // The action has suggested that the type T could be used. Set that as
801 // the type in the declaration specifiers, consume the would-be type
802 // name token, and we're done.
803 const char *PrevSpec;
804 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +0000805 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +0000806 DS.SetRangeEnd(Tok.getLocation());
807 ConsumeToken();
808
809 // There may be other declaration specifiers after this.
810 return true;
811 }
812
813 // Fall through; the action had no suggestion for us.
814 } else {
815 // The action did not emit a diagnostic, so emit one now.
816 SourceRange R;
817 if (SS) R = SS->getRange();
818 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
819 }
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Douglas Gregora786fdb2009-10-13 23:27:22 +0000821 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000822 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000823 unsigned DiagID;
824 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000825 DS.SetRangeEnd(Tok.getLocation());
826 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Chris Lattnere40c2952009-04-14 21:34:55 +0000828 // TODO: Could inject an invalid typedef decl in an enclosing scope to
829 // avoid rippling error messages on subsequent uses of the same type,
830 // could be useful if #include was forgotten.
831 return false;
832}
833
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000834/// \brief Determine the declaration specifier context from the declarator
835/// context.
836///
837/// \param Context the declarator context, which is one of the
838/// Declarator::TheContext enumerator values.
839Parser::DeclSpecContext
840Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
841 if (Context == Declarator::MemberContext)
842 return DSC_class;
843 if (Context == Declarator::FileContext)
844 return DSC_top_level;
845 return DSC_normal;
846}
847
Reid Spencer5f016e22007-07-11 17:01:13 +0000848/// ParseDeclarationSpecifiers
849/// declaration-specifiers: [C99 6.7]
850/// storage-class-specifier declaration-specifiers[opt]
851/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000852/// [C99] function-specifier declaration-specifiers[opt]
853/// [GNU] attributes declaration-specifiers[opt]
854///
855/// storage-class-specifier: [C99 6.7.1]
856/// 'typedef'
857/// 'extern'
858/// 'static'
859/// 'auto'
860/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000861/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000862/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000863/// function-specifier: [C99 6.7.4]
864/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000865/// [C++] 'virtual'
866/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000867/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000868/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000869
Reid Spencer5f016e22007-07-11 17:01:13 +0000870///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000871void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000872 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000873 AccessSpecifier AS,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000874 DeclSpecContext DSContext) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000875 DS.SetRangeStart(Tok.getLocation());
Chris Lattner729ad832010-11-09 20:14:26 +0000876 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000878 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000880 unsigned DiagID = 0;
881
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000883
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000885 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000886 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 // If this is not a declaration specifier token, we're done reading decl
888 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000889 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000892 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +0000893 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000894 if (DS.hasTypeSpecifier()) {
895 bool AllowNonIdentifiers
896 = (getCurScope()->getFlags() & (Scope::ControlScope |
897 Scope::BlockScope |
898 Scope::TemplateParamScope |
899 Scope::FunctionPrototypeScope |
900 Scope::AtCatchScope)) == 0;
901 bool AllowNestedNameSpecifiers
902 = DSContext == DSC_top_level ||
903 (DSContext == DSC_class && DS.isFriendSpecified());
904
Douglas Gregorc7b6d882010-09-16 15:14:18 +0000905 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
906 AllowNonIdentifiers,
907 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000908 ConsumeCodeCompletionToken();
909 return;
910 }
911
912 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +0000913 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
914 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000915 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +0000916 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000917 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +0000918 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000919
920 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
921 ConsumeCodeCompletionToken();
922 return;
923 }
924
Chris Lattner5e02c472009-01-05 00:07:25 +0000925 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +0000926 // C++ scope specifier. Annotate and loop, or bail out on error.
927 if (TryAnnotateCXXScopeToken(true)) {
928 if (!DS.hasTypeSpecifier())
929 DS.SetTypeSpecError();
930 goto DoneWithDeclSpec;
931 }
John McCall2e0a7152010-03-01 18:20:46 +0000932 if (Tok.is(tok::coloncolon)) // ::new or ::delete
933 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +0000934 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000935
936 case tok::annot_cxxscope: {
937 if (DS.hasTypeSpecifier())
938 goto DoneWithDeclSpec;
939
John McCallaa87d332009-12-12 11:40:51 +0000940 CXXScopeSpec SS;
John McCallca0408f2010-08-23 06:44:23 +0000941 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCallaa87d332009-12-12 11:40:51 +0000942 SS.setRange(Tok.getAnnotationRange());
943
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000944 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000945 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000946 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000947 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000948 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000949 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000950
951 // C++ [class.qual]p2:
952 // In a lookup in which the constructor is an acceptable lookup
953 // result and the nested-name-specifier nominates a class C:
954 //
955 // - if the name specified after the
956 // nested-name-specifier, when looked up in C, is the
957 // injected-class-name of C (Clause 9), or
958 //
959 // - if the name specified after the nested-name-specifier
960 // is the same as the identifier or the
961 // simple-template-id's template-name in the last
962 // component of the nested-name-specifier,
963 //
964 // the name is instead considered to name the constructor of
965 // class C.
966 //
967 // Thus, if the template-name is actually the constructor
968 // name, then the code is ill-formed; this interpretation is
969 // reinforced by the NAD status of core issue 635.
970 TemplateIdAnnotation *TemplateId
971 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +0000972 if ((DSContext == DSC_top_level ||
973 (DSContext == DSC_class && DS.isFriendSpecified())) &&
974 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000975 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000976 if (isConstructorDeclarator()) {
977 // The user meant this to be an out-of-line constructor
978 // definition, but template arguments are not allowed
979 // there. Just allow this as a constructor; we'll
980 // complain about it later.
981 goto DoneWithDeclSpec;
982 }
983
984 // The user meant this to name a type, but it actually names
985 // a constructor with some extraneous template
986 // arguments. Complain, then parse it as a type as the user
987 // intended.
988 Diag(TemplateId->TemplateNameLoc,
989 diag::err_out_of_line_template_id_names_constructor)
990 << TemplateId->Name;
991 }
992
John McCallaa87d332009-12-12 11:40:51 +0000993 DS.getTypeSpecScope() = SS;
994 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +0000995 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000996 "ParseOptionalCXXScopeSpecifier not working");
997 AnnotateTemplateIdTokenAsType(&SS);
998 continue;
999 }
1000
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001001 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001002 DS.getTypeSpecScope() = SS;
1003 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001004 if (Tok.getAnnotationValue()) {
1005 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001006 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1007 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001008 PrevSpec, DiagID, T);
1009 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001010 else
1011 DS.SetTypeSpecError();
1012 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1013 ConsumeToken(); // The typename
1014 }
1015
Douglas Gregor9135c722009-03-25 15:40:00 +00001016 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001017 goto DoneWithDeclSpec;
1018
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001019 // If we're in a context where the identifier could be a class name,
1020 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001021 if ((DSContext == DSC_top_level ||
1022 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001023 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001024 &SS)) {
1025 if (isConstructorDeclarator())
1026 goto DoneWithDeclSpec;
1027
1028 // As noted in C++ [class.qual]p2 (cited above), when the name
1029 // of the class is qualified in a context where it could name
1030 // a constructor, its a constructor name. However, we've
1031 // looked at the declarator, and the user probably meant this
1032 // to be a type. Complain that it isn't supposed to be treated
1033 // as a type, then proceed to parse it as a type.
1034 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1035 << Next.getIdentifierInfo();
1036 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001037
John McCallb3d87482010-08-24 05:47:05 +00001038 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1039 Next.getLocation(),
1040 getCurScope(), &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001041
Chris Lattnerf4382f52009-04-14 22:17:06 +00001042 // If the referenced identifier is not a type, then this declspec is
1043 // erroneous: We already checked about that it has no type specifier, and
1044 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001045 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001046 if (TypeRep == 0) {
1047 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001048 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001049 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001050 }
Mike Stump1eb44332009-09-09 15:08:12 +00001051
John McCallaa87d332009-12-12 11:40:51 +00001052 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001053 ConsumeToken(); // The C++ scope.
1054
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001055 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001056 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001057 if (isInvalid)
1058 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001060 DS.SetRangeEnd(Tok.getLocation());
1061 ConsumeToken(); // The typename.
1062
1063 continue;
1064 }
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Chris Lattner80d0c892009-01-21 19:48:37 +00001066 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001067 if (Tok.getAnnotationValue()) {
1068 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001069 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001070 DiagID, T);
1071 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001072 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001073
1074 if (isInvalid)
1075 break;
1076
Chris Lattner80d0c892009-01-21 19:48:37 +00001077 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1078 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Chris Lattner80d0c892009-01-21 19:48:37 +00001080 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1081 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001082 // Objective-C interface.
1083 if (Tok.is(tok::less) && getLang().ObjC1)
1084 ParseObjCProtocolQualifiers(DS);
1085
Chris Lattner80d0c892009-01-21 19:48:37 +00001086 continue;
1087 }
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Chris Lattner3bd934a2008-07-26 01:18:38 +00001089 // typedef-name
1090 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001091 // In C++, check to see if this is a scope specifier like foo::bar::, if
1092 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001093 if (getLang().CPlusPlus) {
1094 if (TryAnnotateCXXScopeToken(true)) {
1095 if (!DS.hasTypeSpecifier())
1096 DS.SetTypeSpecError();
1097 goto DoneWithDeclSpec;
1098 }
1099 if (!Tok.is(tok::identifier))
1100 continue;
1101 }
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Chris Lattner3bd934a2008-07-26 01:18:38 +00001103 // This identifier can only be a typedef name if we haven't already seen
1104 // a type-specifier. Without this check we misparse:
1105 // typedef int X; struct Y { short X; }; as 'short int'.
1106 if (DS.hasTypeSpecifier())
1107 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001108
John Thompson82287d12010-02-05 00:12:22 +00001109 // Check for need to substitute AltiVec keyword tokens.
1110 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1111 break;
1112
Chris Lattner3bd934a2008-07-26 01:18:38 +00001113 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001114 ParsedType TypeRep =
1115 Actions.getTypeName(*Tok.getIdentifierInfo(),
1116 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001117
Chris Lattnerc199ab32009-04-12 20:42:31 +00001118 // If this is not a typedef name, don't parse it as part of the declspec,
1119 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001120 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001121 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001122 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001123 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001124
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001125 // If we're in a context where the identifier could be a class name,
1126 // check whether this is a constructor declaration.
1127 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001128 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001129 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001130 goto DoneWithDeclSpec;
1131
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001132 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001133 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001134 if (isInvalid)
1135 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Chris Lattner3bd934a2008-07-26 01:18:38 +00001137 DS.SetRangeEnd(Tok.getLocation());
1138 ConsumeToken(); // The identifier
1139
1140 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1141 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001142 // Objective-C interface.
1143 if (Tok.is(tok::less) && getLang().ObjC1)
1144 ParseObjCProtocolQualifiers(DS);
1145
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001146 // Need to support trailing type qualifiers (e.g. "id<p> const").
1147 // If a type specifier follows, it will be diagnosed elsewhere.
1148 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001149 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001150
1151 // type-name
1152 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001153 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001154 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001155 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001156 // This template-id does not refer to a type name, so we're
1157 // done with the type-specifiers.
1158 goto DoneWithDeclSpec;
1159 }
1160
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001161 // If we're in a context where the template-id could be a
1162 // constructor name or specialization, check whether this is a
1163 // constructor declaration.
1164 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001165 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001166 isConstructorDeclarator())
1167 goto DoneWithDeclSpec;
1168
Douglas Gregor39a8de12009-02-25 19:37:18 +00001169 // Turn the template-id annotation token into a type annotation
1170 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001171 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001172 continue;
1173 }
1174
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 // GNU attributes support.
1176 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001177 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001178 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001179
1180 // Microsoft declspec support.
1181 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001182 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001183 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Steve Naroff239f0732008-12-25 14:16:32 +00001185 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001186 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001187 // FIXME: Add handling here!
1188 break;
1189
1190 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001191 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001192 case tok::kw___cdecl:
1193 case tok::kw___stdcall:
1194 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001195 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001196 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001197 continue;
1198
Dawn Perchik52fc3142010-09-03 01:29:35 +00001199 // Borland single token adornments.
1200 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001201 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001202 continue;
1203
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 // storage-class-specifier
1205 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001206 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001207 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 break;
1209 case tok::kw_extern:
1210 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001211 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001212 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001213 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001215 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001216 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001217 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001218 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 case tok::kw_static:
1220 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001221 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001222 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001223 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 break;
1225 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001226 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001227 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1228 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001229 else
John McCallfec54012009-08-03 20:12:06 +00001230 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001231 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 break;
1233 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001234 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001235 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001237 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001238 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001239 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001240 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001242 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 // function-specifier
1246 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001247 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001249 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001250 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001251 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001252 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001253 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001254 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001255
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001256 // friend
1257 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001258 if (DSContext == DSC_class)
1259 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1260 else {
1261 PrevSpec = ""; // not actually used by the diagnostic
1262 DiagID = diag::err_friend_invalid_in_context;
1263 isInvalid = true;
1264 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001265 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Sebastian Redl2ac67232009-11-05 15:47:02 +00001267 // constexpr
1268 case tok::kw_constexpr:
1269 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1270 break;
1271
Chris Lattner80d0c892009-01-21 19:48:37 +00001272 // type-specifier
1273 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001274 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1275 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001276 break;
1277 case tok::kw_long:
1278 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001279 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1280 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001281 else
John McCallfec54012009-08-03 20:12:06 +00001282 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1283 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001284 break;
1285 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001286 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1287 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001288 break;
1289 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001290 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1291 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001292 break;
1293 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001294 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1295 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001296 break;
1297 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001298 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1299 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001300 break;
1301 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001302 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1303 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001304 break;
1305 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001306 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1307 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001308 break;
1309 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001310 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1311 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001312 break;
1313 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001314 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1315 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001316 break;
1317 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001318 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1319 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001320 break;
1321 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001322 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1323 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001324 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001325 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001326 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1327 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001328 break;
1329 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001330 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1331 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001332 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001333 case tok::kw_bool:
1334 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001335 if (Tok.is(tok::kw_bool) &&
1336 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1337 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1338 PrevSpec = ""; // Not used by the diagnostic.
1339 DiagID = diag::err_bool_redeclaration;
1340 isInvalid = true;
1341 } else {
1342 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1343 DiagID);
1344 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001345 break;
1346 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001347 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1348 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001349 break;
1350 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001351 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1352 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001353 break;
1354 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001355 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1356 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001357 break;
John Thompson82287d12010-02-05 00:12:22 +00001358 case tok::kw___vector:
1359 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1360 break;
1361 case tok::kw___pixel:
1362 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1363 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001364
1365 // class-specifier:
1366 case tok::kw_class:
1367 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001368 case tok::kw_union: {
1369 tok::TokenKind Kind = Tok.getKind();
1370 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001371 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001372 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001373 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001374
1375 // enum-specifier:
1376 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001377 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001378 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001379 continue;
1380
1381 // cv-qualifier:
1382 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001383 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1384 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001385 break;
1386 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001387 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1388 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001389 break;
1390 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001391 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1392 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001393 break;
1394
Douglas Gregord57959a2009-03-27 23:10:48 +00001395 // C++ typename-specifier:
1396 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001397 if (TryAnnotateTypeOrScopeToken()) {
1398 DS.SetTypeSpecError();
1399 goto DoneWithDeclSpec;
1400 }
1401 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001402 continue;
1403 break;
1404
Chris Lattner80d0c892009-01-21 19:48:37 +00001405 // GNU typeof support.
1406 case tok::kw_typeof:
1407 ParseTypeofSpecifier(DS);
1408 continue;
1409
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001410 case tok::kw_decltype:
1411 ParseDecltypeSpecifier(DS);
1412 continue;
1413
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001414 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001415 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001416 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1417 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001418 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001419 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Douglas Gregor46f936e2010-11-19 17:10:50 +00001421 if (!ParseObjCProtocolQualifiers(DS))
1422 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1423 << FixItHint::CreateInsertion(Loc, "id")
1424 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001425
1426 // Need to support trailing type qualifiers (e.g. "id<p> const").
1427 // If a type specifier follows, it will be diagnosed elsewhere.
1428 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001429 }
John McCallfec54012009-08-03 20:12:06 +00001430 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001431 if (isInvalid) {
1432 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001433 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001434
1435 if (DiagID == diag::ext_duplicate_declspec)
1436 Diag(Tok, DiagID)
1437 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1438 else
1439 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001440 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001441 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001442 ConsumeToken();
1443 }
1444}
Douglas Gregoradcac882008-12-01 23:54:00 +00001445
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001446/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001447/// primarily follow the C++ grammar with additions for C99 and GNU,
1448/// which together subsume the C grammar. Note that the C++
1449/// type-specifier also includes the C type-qualifier (for const,
1450/// volatile, and C99 restrict). Returns true if a type-specifier was
1451/// found (and parsed), false otherwise.
1452///
1453/// type-specifier: [C++ 7.1.5]
1454/// simple-type-specifier
1455/// class-specifier
1456/// enum-specifier
1457/// elaborated-type-specifier [TODO]
1458/// cv-qualifier
1459///
1460/// cv-qualifier: [C++ 7.1.5.1]
1461/// 'const'
1462/// 'volatile'
1463/// [C99] 'restrict'
1464///
1465/// simple-type-specifier: [ C++ 7.1.5.2]
1466/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1467/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1468/// 'char'
1469/// 'wchar_t'
1470/// 'bool'
1471/// 'short'
1472/// 'int'
1473/// 'long'
1474/// 'signed'
1475/// 'unsigned'
1476/// 'float'
1477/// 'double'
1478/// 'void'
1479/// [C99] '_Bool'
1480/// [C99] '_Complex'
1481/// [C99] '_Imaginary' // Removed in TC2?
1482/// [GNU] '_Decimal32'
1483/// [GNU] '_Decimal64'
1484/// [GNU] '_Decimal128'
1485/// [GNU] typeof-specifier
1486/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1487/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001488/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001489/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001490bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001491 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001492 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001493 const ParsedTemplateInfo &TemplateInfo,
1494 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001495 SourceLocation Loc = Tok.getLocation();
1496
1497 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001498 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001499 // If we already have a type specifier, this identifier is not a type.
1500 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1501 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1502 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1503 return false;
John Thompson82287d12010-02-05 00:12:22 +00001504 // Check for need to substitute AltiVec keyword tokens.
1505 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1506 break;
1507 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001508 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001509 // Annotate typenames and C++ scope specifiers. If we get one, just
1510 // recurse to handle whatever we get.
1511 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001512 return true;
1513 if (Tok.is(tok::identifier))
1514 return false;
1515 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1516 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001517 case tok::coloncolon: // ::foo::bar
1518 if (NextToken().is(tok::kw_new) || // ::new
1519 NextToken().is(tok::kw_delete)) // ::delete
1520 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Chris Lattner166a8fc2009-01-04 23:41:41 +00001522 // Annotate typenames and C++ scope specifiers. If we get one, just
1523 // recurse to handle whatever we get.
1524 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001525 return true;
1526 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1527 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Douglas Gregor12e083c2008-11-07 15:42:26 +00001529 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001530 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001531 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00001532 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1533 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001534 DiagID, T);
1535 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001536 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001537 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1538 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Douglas Gregor12e083c2008-11-07 15:42:26 +00001540 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1541 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1542 // Objective-C interface. If we don't have Objective-C or a '<', this is
1543 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001544 if (Tok.is(tok::less) && getLang().ObjC1)
1545 ParseObjCProtocolQualifiers(DS);
1546
Douglas Gregor12e083c2008-11-07 15:42:26 +00001547 return true;
1548 }
1549
1550 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001551 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001552 break;
1553 case tok::kw_long:
1554 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001555 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1556 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001557 else
John McCallfec54012009-08-03 20:12:06 +00001558 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1559 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001560 break;
1561 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001562 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001563 break;
1564 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001565 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1566 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001567 break;
1568 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001569 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1570 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001571 break;
1572 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001573 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1574 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001575 break;
1576 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001577 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001578 break;
1579 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001580 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001581 break;
1582 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001583 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001584 break;
1585 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001586 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001587 break;
1588 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001589 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001590 break;
1591 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001592 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001593 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001594 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001595 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001596 break;
1597 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001598 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001599 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001600 case tok::kw_bool:
1601 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001602 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001603 break;
1604 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001605 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1606 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001607 break;
1608 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001609 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1610 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001611 break;
1612 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001613 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1614 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001615 break;
John Thompson82287d12010-02-05 00:12:22 +00001616 case tok::kw___vector:
1617 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1618 break;
1619 case tok::kw___pixel:
1620 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1621 break;
1622
Douglas Gregor12e083c2008-11-07 15:42:26 +00001623 // class-specifier:
1624 case tok::kw_class:
1625 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001626 case tok::kw_union: {
1627 tok::TokenKind Kind = Tok.getKind();
1628 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001629 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1630 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001631 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001632 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001633
1634 // enum-specifier:
1635 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001636 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001637 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001638 return true;
1639
1640 // cv-qualifier:
1641 case tok::kw_const:
1642 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001643 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001644 break;
1645 case tok::kw_volatile:
1646 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001647 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001648 break;
1649 case tok::kw_restrict:
1650 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001651 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001652 break;
1653
1654 // GNU typeof support.
1655 case tok::kw_typeof:
1656 ParseTypeofSpecifier(DS);
1657 return true;
1658
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001659 // C++0x decltype support.
1660 case tok::kw_decltype:
1661 ParseDecltypeSpecifier(DS);
1662 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001664 // C++0x auto support.
1665 case tok::kw_auto:
1666 if (!getLang().CPlusPlus0x)
1667 return false;
1668
John McCallfec54012009-08-03 20:12:06 +00001669 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001670 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001671
Eli Friedman290eeb02009-06-08 23:27:34 +00001672 case tok::kw___ptr64:
1673 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001674 case tok::kw___cdecl:
1675 case tok::kw___stdcall:
1676 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001677 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001678 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001679 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001680
Dawn Perchik52fc3142010-09-03 01:29:35 +00001681 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001682 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001683 return true;
1684
Douglas Gregor12e083c2008-11-07 15:42:26 +00001685 default:
1686 // Not a type-specifier; do nothing.
1687 return false;
1688 }
1689
1690 // If the specifier combination wasn't legal, issue a diagnostic.
1691 if (isInvalid) {
1692 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001693 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001694 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001695 }
1696 DS.SetRangeEnd(Tok.getLocation());
1697 ConsumeToken(); // whatever we parsed above.
1698 return true;
1699}
Reid Spencer5f016e22007-07-11 17:01:13 +00001700
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001701/// ParseStructDeclaration - Parse a struct declaration without the terminating
1702/// semicolon.
1703///
Reid Spencer5f016e22007-07-11 17:01:13 +00001704/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001705/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001706/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001707/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001708/// struct-declarator-list:
1709/// struct-declarator
1710/// struct-declarator-list ',' struct-declarator
1711/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1712/// struct-declarator:
1713/// declarator
1714/// [GNU] declarator attributes[opt]
1715/// declarator[opt] ':' constant-expression
1716/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1717///
Chris Lattnere1359422008-04-10 06:46:29 +00001718void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001719ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001720 if (Tok.is(tok::kw___extension__)) {
1721 // __extension__ silences extension warnings in the subexpression.
1722 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001723 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001724 return ParseStructDeclaration(DS, Fields);
1725 }
Mike Stump1eb44332009-09-09 15:08:12 +00001726
Steve Naroff28a7ca82007-08-20 22:28:22 +00001727 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001728 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001730 // If there are no declarators, this is a free-standing declaration
1731 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001732 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001733 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001734 return;
1735 }
1736
1737 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001738 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001739 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001740 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001741 FieldDeclarator DeclaratorInfo(DS);
1742
1743 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00001744 if (!FirstDeclarator)
1745 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00001746
Steve Naroff28a7ca82007-08-20 22:28:22 +00001747 /// struct-declarator: declarator
1748 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001749 if (Tok.isNot(tok::colon)) {
1750 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1751 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001752 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001753 }
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Chris Lattner04d66662007-10-09 17:33:22 +00001755 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001756 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001757 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001758 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001759 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001760 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001761 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001762 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001763
Steve Naroff28a7ca82007-08-20 22:28:22 +00001764 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001765 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001766
John McCallbdd563e2009-11-03 02:38:08 +00001767 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00001768 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00001769 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001770
Steve Naroff28a7ca82007-08-20 22:28:22 +00001771 // If we don't have a comma, it is either the end of the list (a ';')
1772 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001773 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001774 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001775
Steve Naroff28a7ca82007-08-20 22:28:22 +00001776 // Consume the comma.
1777 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001778
John McCallbdd563e2009-11-03 02:38:08 +00001779 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001780 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001781}
1782
1783/// ParseStructUnionBody
1784/// struct-contents:
1785/// struct-declaration-list
1786/// [EXT] empty
1787/// [GNU] "struct-declaration-list" without terminatoring ';'
1788/// struct-declaration-list:
1789/// struct-declaration
1790/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001791/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001792///
Reid Spencer5f016e22007-07-11 17:01:13 +00001793void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001794 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00001795 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1796 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001797
Reid Spencer5f016e22007-07-11 17:01:13 +00001798 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001800 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001801 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00001802
Reid Spencer5f016e22007-07-11 17:01:13 +00001803 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1804 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001805 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00001806 Diag(Tok, diag::ext_empty_struct_union)
1807 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00001808
John McCalld226f652010-08-21 09:40:31 +00001809 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001810
Reid Spencer5f016e22007-07-11 17:01:13 +00001811 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001812 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001813 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Reid Spencer5f016e22007-07-11 17:01:13 +00001815 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001816 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001817 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00001818 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00001819 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001820 ConsumeToken();
1821 continue;
1822 }
Chris Lattnere1359422008-04-10 06:46:29 +00001823
1824 // Parse all the comma separated declarators.
1825 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001826
John McCallbdd563e2009-11-03 02:38:08 +00001827 if (!Tok.is(tok::at)) {
1828 struct CFieldCallback : FieldCallback {
1829 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001830 Decl *TagDecl;
1831 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001832
John McCalld226f652010-08-21 09:40:31 +00001833 CFieldCallback(Parser &P, Decl *TagDecl,
1834 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001835 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1836
John McCalld226f652010-08-21 09:40:31 +00001837 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001838 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00001839 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00001840 FD.D.getDeclSpec().getSourceRange().getBegin(),
1841 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001842 FieldDecls.push_back(Field);
1843 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001844 }
John McCallbdd563e2009-11-03 02:38:08 +00001845 } Callback(*this, TagDecl, FieldDecls);
1846
1847 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001848 } else { // Handle @defs
1849 ConsumeToken();
1850 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1851 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001852 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001853 continue;
1854 }
1855 ConsumeToken();
1856 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1857 if (!Tok.is(tok::identifier)) {
1858 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001859 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001860 continue;
1861 }
John McCalld226f652010-08-21 09:40:31 +00001862 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001863 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001864 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001865 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1866 ConsumeToken();
1867 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001868 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001869
Chris Lattner04d66662007-10-09 17:33:22 +00001870 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001871 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001872 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001873 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 break;
1875 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001876 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1877 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001878 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001879 // If we stopped at a ';', eat it.
1880 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001881 }
1882 }
Mike Stump1eb44332009-09-09 15:08:12 +00001883
Steve Naroff60fccee2007-10-29 21:38:07 +00001884 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001885
John McCall7f040a92010-12-24 02:08:15 +00001886 ParsedAttributes attrs;
Reid Spencer5f016e22007-07-11 17:01:13 +00001887 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001888 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001889
Douglas Gregor23c94db2010-07-02 17:43:08 +00001890 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001891 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001892 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00001893 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00001894 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001895 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001896}
1897
Reid Spencer5f016e22007-07-11 17:01:13 +00001898/// ParseEnumSpecifier
1899/// enum-specifier: [C99 6.7.2.2]
1900/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001901///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001902/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1903/// '}' attributes[opt]
1904/// 'enum' identifier
1905/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001906///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001907/// [C++0x] enum-head '{' enumerator-list[opt] '}'
1908/// [C++0x] enum-head '{' enumerator-list ',' '}'
1909///
1910/// enum-head: [C++0x]
1911/// enum-key attributes[opt] identifier[opt] enum-base[opt]
1912/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
1913///
1914/// enum-key: [C++0x]
1915/// 'enum'
1916/// 'enum' 'class'
1917/// 'enum' 'struct'
1918///
1919/// enum-base: [C++0x]
1920/// ':' type-specifier-seq
1921///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001922/// [C++] elaborated-type-specifier:
1923/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1924///
Chris Lattner4c97d762009-04-12 21:49:30 +00001925void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001926 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00001927 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001928 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001929 if (Tok.is(tok::code_completion)) {
1930 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001931 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00001932 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00001933 }
1934
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001935 // If attributes exist after tag, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001936 ParsedAttributes attrs;
1937 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001938
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001939 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00001940 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00001941 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00001942 return;
1943
1944 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001945 Diag(Tok, diag::err_expected_ident);
1946 if (Tok.isNot(tok::l_brace)) {
1947 // Has no name and is not a definition.
1948 // Skip the rest of this declarator, up until the comma or semicolon.
1949 SkipUntil(tok::comma, true);
1950 return;
1951 }
1952 }
1953 }
Mike Stump1eb44332009-09-09 15:08:12 +00001954
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001955 bool IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001956 bool IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001957
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001958 if (getLang().CPlusPlus0x &&
1959 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001960 IsScopedEnum = true;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001961 IsScopedUsingClassTag = Tok.is(tok::kw_class);
1962 ConsumeToken();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001963 }
1964
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001965 // Must have either 'enum name' or 'enum {...}'.
1966 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1967 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001969 // Skip the rest of this declarator, up until the comma or semicolon.
1970 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001971 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001972 }
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001974 // If an identifier is present, consume and remember it.
1975 IdentifierInfo *Name = 0;
1976 SourceLocation NameLoc;
1977 if (Tok.is(tok::identifier)) {
1978 Name = Tok.getIdentifierInfo();
1979 NameLoc = ConsumeToken();
1980 }
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001982 if (!Name && IsScopedEnum) {
1983 // C++0x 7.2p2: The optional identifier shall not be omitted in the
1984 // declaration of a scoped enumeration.
1985 Diag(Tok, diag::err_scoped_enum_missing_identifier);
1986 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001987 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001988 }
1989
1990 TypeResult BaseType;
1991
Douglas Gregora61b3e72010-12-01 17:42:47 +00001992 // Parse the fixed underlying type.
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001993 if (getLang().CPlusPlus0x && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00001994 bool PossibleBitfield = false;
1995 if (getCurScope()->getFlags() & Scope::ClassScope) {
1996 // If we're in class scope, this can either be an enum declaration with
1997 // an underlying type, or a declaration of a bitfield member. We try to
1998 // use a simple disambiguation scheme first to catch the common cases
1999 // (integer literal, sizeof); if it's still ambiguous, we then consider
2000 // anything that's a simple-type-specifier followed by '(' as an
2001 // expression. This suffices because function types are not valid
2002 // underlying types anyway.
2003 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2004 // If the next token starts an expression, we know we're parsing a
2005 // bit-field. This is the common case.
2006 if (TPR == TPResult::True())
2007 PossibleBitfield = true;
2008 // If the next token starts a type-specifier-seq, it may be either a
2009 // a fixed underlying type or the start of a function-style cast in C++;
2010 // lookahead one more token to see if it's obvious that we have a
2011 // fixed underlying type.
2012 else if (TPR == TPResult::False() &&
2013 GetLookAheadToken(2).getKind() == tok::semi) {
2014 // Consume the ':'.
2015 ConsumeToken();
2016 } else {
2017 // We have the start of a type-specifier-seq, so we have to perform
2018 // tentative parsing to determine whether we have an expression or a
2019 // type.
2020 TentativeParsingAction TPA(*this);
2021
2022 // Consume the ':'.
2023 ConsumeToken();
2024
2025 if (isCXXDeclarationSpecifier() != TPResult::True()) {
2026 // We'll parse this as a bitfield later.
2027 PossibleBitfield = true;
2028 TPA.Revert();
2029 } else {
2030 // We have a type-specifier-seq.
2031 TPA.Commit();
2032 }
2033 }
2034 } else {
2035 // Consume the ':'.
2036 ConsumeToken();
2037 }
2038
2039 if (!PossibleBitfield) {
2040 SourceRange Range;
2041 BaseType = ParseTypeName(&Range);
2042 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002043 }
2044
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002045 // There are three options here. If we have 'enum foo;', then this is a
2046 // forward declaration. If we have 'enum foo {...' then this is a
2047 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2048 //
2049 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2050 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2051 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2052 //
John McCallf312b1e2010-08-26 23:41:50 +00002053 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002054 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002055 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002056 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002057 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002058 else
John McCallf312b1e2010-08-26 23:41:50 +00002059 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002060
2061 // enums cannot be templates, although they can be referenced from a
2062 // template.
2063 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002064 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002065 Diag(Tok, diag::err_enum_template);
2066
2067 // Skip the rest of this declarator, up until the comma or semicolon.
2068 SkipUntil(tok::comma, true);
2069 return;
2070 }
2071
Douglas Gregor402abb52009-05-28 23:31:59 +00002072 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002073 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002074 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2075 const char *PrevSpec = 0;
2076 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002077 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002078 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002079 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002080 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002081 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002082 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002083
Douglas Gregor48c89f42010-04-24 16:38:41 +00002084 if (IsDependent) {
2085 // This enum has a dependent nested-name-specifier. Handle it as a
2086 // dependent tag.
2087 if (!Name) {
2088 DS.SetTypeSpecError();
2089 Diag(Tok, diag::err_expected_type_name_after_typename);
2090 return;
2091 }
2092
Douglas Gregor23c94db2010-07-02 17:43:08 +00002093 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002094 TUK, SS, Name, StartLoc,
2095 NameLoc);
2096 if (Type.isInvalid()) {
2097 DS.SetTypeSpecError();
2098 return;
2099 }
2100
2101 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallb3d87482010-08-24 05:47:05 +00002102 Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002103 Diag(StartLoc, DiagID) << PrevSpec;
2104
2105 return;
2106 }
Mike Stump1eb44332009-09-09 15:08:12 +00002107
John McCalld226f652010-08-21 09:40:31 +00002108 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002109 // The action failed to produce an enumeration tag. If this is a
2110 // definition, consume the entire definition.
2111 if (Tok.is(tok::l_brace)) {
2112 ConsumeBrace();
2113 SkipUntil(tok::r_brace);
2114 }
2115
2116 DS.SetTypeSpecError();
2117 return;
2118 }
2119
Chris Lattner04d66662007-10-09 17:33:22 +00002120 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002121 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002122
John McCallb3d87482010-08-24 05:47:05 +00002123 // FIXME: The DeclSpec should keep the locations of both the keyword
2124 // and the name (if there is one).
Douglas Gregorb988f9c2010-01-25 16:33:23 +00002125 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCalld226f652010-08-21 09:40:31 +00002126 TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002127 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002128}
2129
2130/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2131/// enumerator-list:
2132/// enumerator
2133/// enumerator-list ',' enumerator
2134/// enumerator:
2135/// enumeration-constant
2136/// enumeration-constant '=' constant-expression
2137/// enumeration-constant:
2138/// identifier
2139///
John McCalld226f652010-08-21 09:40:31 +00002140void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002141 // Enter the scope of the enum body and start the definition.
2142 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002143 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002144
Reid Spencer5f016e22007-07-11 17:01:13 +00002145 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002146
Chris Lattner7946dd32007-08-27 17:24:30 +00002147 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002148 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002149 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002150
John McCalld226f652010-08-21 09:40:31 +00002151 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002152
John McCalld226f652010-08-21 09:40:31 +00002153 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002154
Reid Spencer5f016e22007-07-11 17:01:13 +00002155 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002156 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002157 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2158 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002159
John McCall5b629aa2010-10-22 23:36:17 +00002160 // If attributes exist after the enumerator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002161 ParsedAttributes attrs;
2162 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002163
Reid Spencer5f016e22007-07-11 17:01:13 +00002164 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002165 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002166 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002167 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002168 AssignedVal = ParseConstantExpression();
2169 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002170 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002171 }
Mike Stump1eb44332009-09-09 15:08:12 +00002172
Reid Spencer5f016e22007-07-11 17:01:13 +00002173 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002174 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2175 LastEnumConstDecl,
2176 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002177 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002178 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002179 EnumConstantDecls.push_back(EnumConstDecl);
2180 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002181
Douglas Gregor751f6922010-09-07 14:51:08 +00002182 if (Tok.is(tok::identifier)) {
2183 // We're missing a comma between enumerators.
2184 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2185 Diag(Loc, diag::err_enumerator_list_missing_comma)
2186 << FixItHint::CreateInsertion(Loc, ", ");
2187 continue;
2188 }
2189
Chris Lattner04d66662007-10-09 17:33:22 +00002190 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002191 break;
2192 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002193
2194 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002195 !(getLang().C99 || getLang().CPlusPlus0x))
2196 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2197 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002198 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002199 }
Mike Stump1eb44332009-09-09 15:08:12 +00002200
Reid Spencer5f016e22007-07-11 17:01:13 +00002201 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002202 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002203
Reid Spencer5f016e22007-07-11 17:01:13 +00002204 // If attributes exist after the identifier list, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002205 ParsedAttributes attrs;
2206 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002207
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002208 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2209 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002210 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002211
Douglas Gregor72de6672009-01-08 20:45:30 +00002212 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002213 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002214}
2215
2216/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002217/// start of a type-qualifier-list.
2218bool Parser::isTypeQualifier() const {
2219 switch (Tok.getKind()) {
2220 default: return false;
2221 // type-qualifier
2222 case tok::kw_const:
2223 case tok::kw_volatile:
2224 case tok::kw_restrict:
2225 return true;
2226 }
2227}
2228
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002229/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2230/// is definitely a type-specifier. Return false if it isn't part of a type
2231/// specifier or if we're not sure.
2232bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2233 switch (Tok.getKind()) {
2234 default: return false;
2235 // type-specifiers
2236 case tok::kw_short:
2237 case tok::kw_long:
2238 case tok::kw_signed:
2239 case tok::kw_unsigned:
2240 case tok::kw__Complex:
2241 case tok::kw__Imaginary:
2242 case tok::kw_void:
2243 case tok::kw_char:
2244 case tok::kw_wchar_t:
2245 case tok::kw_char16_t:
2246 case tok::kw_char32_t:
2247 case tok::kw_int:
2248 case tok::kw_float:
2249 case tok::kw_double:
2250 case tok::kw_bool:
2251 case tok::kw__Bool:
2252 case tok::kw__Decimal32:
2253 case tok::kw__Decimal64:
2254 case tok::kw__Decimal128:
2255 case tok::kw___vector:
2256
2257 // struct-or-union-specifier (C99) or class-specifier (C++)
2258 case tok::kw_class:
2259 case tok::kw_struct:
2260 case tok::kw_union:
2261 // enum-specifier
2262 case tok::kw_enum:
2263
2264 // typedef-name
2265 case tok::annot_typename:
2266 return true;
2267 }
2268}
2269
Steve Naroff5f8aa692008-02-11 23:15:56 +00002270/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002271/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002272bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002273 switch (Tok.getKind()) {
2274 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002275
Chris Lattner166a8fc2009-01-04 23:41:41 +00002276 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002277 if (TryAltiVecVectorToken())
2278 return true;
2279 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002280 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002281 // Annotate typenames and C++ scope specifiers. If we get one, just
2282 // recurse to handle whatever we get.
2283 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002284 return true;
2285 if (Tok.is(tok::identifier))
2286 return false;
2287 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002288
Chris Lattner166a8fc2009-01-04 23:41:41 +00002289 case tok::coloncolon: // ::foo::bar
2290 if (NextToken().is(tok::kw_new) || // ::new
2291 NextToken().is(tok::kw_delete)) // ::delete
2292 return false;
2293
Chris Lattner166a8fc2009-01-04 23:41:41 +00002294 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002295 return true;
2296 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002297
Reid Spencer5f016e22007-07-11 17:01:13 +00002298 // GNU attributes support.
2299 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002300 // GNU typeof support.
2301 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002302
Reid Spencer5f016e22007-07-11 17:01:13 +00002303 // type-specifiers
2304 case tok::kw_short:
2305 case tok::kw_long:
2306 case tok::kw_signed:
2307 case tok::kw_unsigned:
2308 case tok::kw__Complex:
2309 case tok::kw__Imaginary:
2310 case tok::kw_void:
2311 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002312 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002313 case tok::kw_char16_t:
2314 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002315 case tok::kw_int:
2316 case tok::kw_float:
2317 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002318 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002319 case tok::kw__Bool:
2320 case tok::kw__Decimal32:
2321 case tok::kw__Decimal64:
2322 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002323 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002324
Chris Lattner99dc9142008-04-13 18:59:07 +00002325 // struct-or-union-specifier (C99) or class-specifier (C++)
2326 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002327 case tok::kw_struct:
2328 case tok::kw_union:
2329 // enum-specifier
2330 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Reid Spencer5f016e22007-07-11 17:01:13 +00002332 // type-qualifier
2333 case tok::kw_const:
2334 case tok::kw_volatile:
2335 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002336
2337 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002338 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002339 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002340
Chris Lattner7c186be2008-10-20 00:25:30 +00002341 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2342 case tok::less:
2343 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002344
Steve Naroff239f0732008-12-25 14:16:32 +00002345 case tok::kw___cdecl:
2346 case tok::kw___stdcall:
2347 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002348 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002349 case tok::kw___w64:
2350 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002351 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002352 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002353 }
2354}
2355
2356/// isDeclarationSpecifier() - Return true if the current token is part of a
2357/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002358///
2359/// \param DisambiguatingWithExpression True to indicate that the purpose of
2360/// this check is to disambiguate between an expression and a declaration.
2361bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002362 switch (Tok.getKind()) {
2363 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002364
Chris Lattner166a8fc2009-01-04 23:41:41 +00002365 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002366 // Unfortunate hack to support "Class.factoryMethod" notation.
2367 if (getLang().ObjC1 && NextToken().is(tok::period))
2368 return false;
John Thompson82287d12010-02-05 00:12:22 +00002369 if (TryAltiVecVectorToken())
2370 return true;
2371 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002372 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002373 // Annotate typenames and C++ scope specifiers. If we get one, just
2374 // recurse to handle whatever we get.
2375 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002376 return true;
2377 if (Tok.is(tok::identifier))
2378 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002379
2380 // If we're in Objective-C and we have an Objective-C class type followed
2381 // by an identifier and then either ':' or ']', in a place where an
2382 // expression is permitted, then this is probably a class message send
2383 // missing the initial '['. In this case, we won't consider this to be
2384 // the start of a declaration.
2385 if (DisambiguatingWithExpression &&
2386 isStartOfObjCClassMessageMissingOpenBracket())
2387 return false;
2388
John McCall9ba61662010-02-26 08:45:28 +00002389 return isDeclarationSpecifier();
2390
Chris Lattner166a8fc2009-01-04 23:41:41 +00002391 case tok::coloncolon: // ::foo::bar
2392 if (NextToken().is(tok::kw_new) || // ::new
2393 NextToken().is(tok::kw_delete)) // ::delete
2394 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002395
Chris Lattner166a8fc2009-01-04 23:41:41 +00002396 // Annotate typenames and C++ scope specifiers. If we get one, just
2397 // recurse to handle whatever we get.
2398 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002399 return true;
2400 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002401
Reid Spencer5f016e22007-07-11 17:01:13 +00002402 // storage-class-specifier
2403 case tok::kw_typedef:
2404 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002405 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002406 case tok::kw_static:
2407 case tok::kw_auto:
2408 case tok::kw_register:
2409 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002410
Reid Spencer5f016e22007-07-11 17:01:13 +00002411 // type-specifiers
2412 case tok::kw_short:
2413 case tok::kw_long:
2414 case tok::kw_signed:
2415 case tok::kw_unsigned:
2416 case tok::kw__Complex:
2417 case tok::kw__Imaginary:
2418 case tok::kw_void:
2419 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002420 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002421 case tok::kw_char16_t:
2422 case tok::kw_char32_t:
2423
Reid Spencer5f016e22007-07-11 17:01:13 +00002424 case tok::kw_int:
2425 case tok::kw_float:
2426 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002427 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002428 case tok::kw__Bool:
2429 case tok::kw__Decimal32:
2430 case tok::kw__Decimal64:
2431 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002432 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002433
Chris Lattner99dc9142008-04-13 18:59:07 +00002434 // struct-or-union-specifier (C99) or class-specifier (C++)
2435 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002436 case tok::kw_struct:
2437 case tok::kw_union:
2438 // enum-specifier
2439 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002440
Reid Spencer5f016e22007-07-11 17:01:13 +00002441 // type-qualifier
2442 case tok::kw_const:
2443 case tok::kw_volatile:
2444 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002445
Reid Spencer5f016e22007-07-11 17:01:13 +00002446 // function-specifier
2447 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002448 case tok::kw_virtual:
2449 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002450
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002451 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002452 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002453
Chris Lattner1ef08762007-08-09 17:01:07 +00002454 // GNU typeof support.
2455 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002456
Chris Lattner1ef08762007-08-09 17:01:07 +00002457 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002458 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002459 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002460
Chris Lattnerf3948c42008-07-26 03:38:44 +00002461 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2462 case tok::less:
2463 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002464
Steve Naroff47f52092009-01-06 19:34:12 +00002465 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002466 case tok::kw___cdecl:
2467 case tok::kw___stdcall:
2468 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002469 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002470 case tok::kw___w64:
2471 case tok::kw___ptr64:
2472 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002473 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002474 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002475 }
2476}
2477
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002478bool Parser::isConstructorDeclarator() {
2479 TentativeParsingAction TPA(*this);
2480
2481 // Parse the C++ scope specifier.
2482 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002483 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00002484 TPA.Revert();
2485 return false;
2486 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002487
2488 // Parse the constructor name.
2489 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2490 // We already know that we have a constructor name; just consume
2491 // the token.
2492 ConsumeToken();
2493 } else {
2494 TPA.Revert();
2495 return false;
2496 }
2497
2498 // Current class name must be followed by a left parentheses.
2499 if (Tok.isNot(tok::l_paren)) {
2500 TPA.Revert();
2501 return false;
2502 }
2503 ConsumeParen();
2504
2505 // A right parentheses or ellipsis signals that we have a constructor.
2506 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2507 TPA.Revert();
2508 return true;
2509 }
2510
2511 // If we need to, enter the specified scope.
2512 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002513 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002514 DeclScopeObj.EnterDeclaratorScope();
2515
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00002516 // Optionally skip Microsoft attributes.
2517 ParsedAttributes Attrs;
2518 MaybeParseMicrosoftAttributes(Attrs);
2519
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002520 // Check whether the next token(s) are part of a declaration
2521 // specifier, in which case we have the start of a parameter and,
2522 // therefore, we know that this is a constructor.
2523 bool IsConstructor = isDeclarationSpecifier();
2524 TPA.Revert();
2525 return IsConstructor;
2526}
Reid Spencer5f016e22007-07-11 17:01:13 +00002527
2528/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00002529/// type-qualifier-list: [C99 6.7.5]
2530/// type-qualifier
2531/// [vendor] attributes
2532/// [ only if VendorAttributesAllowed=true ]
2533/// type-qualifier-list type-qualifier
2534/// [vendor] type-qualifier-list attributes
2535/// [ only if VendorAttributesAllowed=true ]
2536/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2537/// [ only if CXX0XAttributesAllowed=true ]
2538/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00002539///
Dawn Perchik52fc3142010-09-03 01:29:35 +00002540void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2541 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00002542 bool CXX0XAttributesAllowed) {
2543 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2544 SourceLocation Loc = Tok.getLocation();
John McCall7f040a92010-12-24 02:08:15 +00002545 ParsedAttributesWithRange attrs;
2546 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002547 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00002548 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002549 else
2550 Diag(Loc, diag::err_attributes_not_allowed);
2551 }
2552
Reid Spencer5f016e22007-07-11 17:01:13 +00002553 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002554 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002555 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002556 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002557 SourceLocation Loc = Tok.getLocation();
2558
2559 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00002560 case tok::code_completion:
2561 Actions.CodeCompleteTypeQualifiers(DS);
2562 ConsumeCodeCompletionToken();
2563 break;
2564
Reid Spencer5f016e22007-07-11 17:01:13 +00002565 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002566 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2567 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002568 break;
2569 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002570 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2571 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002572 break;
2573 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002574 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2575 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002576 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002577 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002578 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002579 case tok::kw___cdecl:
2580 case tok::kw___stdcall:
2581 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002582 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002583 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00002584 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002585 continue;
2586 }
2587 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002588 case tok::kw___pascal:
2589 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00002590 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002591 continue;
2592 }
2593 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002594 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002595 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00002596 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002597 continue; // do *not* consume the next token!
2598 }
2599 // otherwise, FALL THROUGH!
2600 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002601 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002602 // If this is not a type-qualifier token, we're done reading type
2603 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002604 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002605 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002606 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002607
Reid Spencer5f016e22007-07-11 17:01:13 +00002608 // If the specifier combination wasn't legal, issue a diagnostic.
2609 if (isInvalid) {
2610 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002611 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002612 }
2613 ConsumeToken();
2614 }
2615}
2616
2617
2618/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2619///
2620void Parser::ParseDeclarator(Declarator &D) {
2621 /// This implements the 'declarator' production in the C grammar, then checks
2622 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002623 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002624}
2625
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002626/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2627/// is parsed by the function passed to it. Pass null, and the direct-declarator
2628/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002629/// ptr-operator production.
2630///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002631/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2632/// [C] pointer[opt] direct-declarator
2633/// [C++] direct-declarator
2634/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002635///
2636/// pointer: [C99 6.7.5]
2637/// '*' type-qualifier-list[opt]
2638/// '*' type-qualifier-list[opt] pointer
2639///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002640/// ptr-operator:
2641/// '*' cv-qualifier-seq[opt]
2642/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002643/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002644/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002645/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002646/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002647void Parser::ParseDeclaratorInternal(Declarator &D,
2648 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002649 if (Diags.hasAllExtensionsSilenced())
2650 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002651
Sebastian Redlf30208a2009-01-24 21:16:55 +00002652 // C++ member pointers start with a '::' or a nested-name.
2653 // Member pointers get special handling, since there's no place for the
2654 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002655 if (getLang().CPlusPlus &&
2656 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2657 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002658 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002659 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00002660
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002661 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002662 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002663 // The scope spec really belongs to the direct-declarator.
2664 D.getCXXScopeSpec() = SS;
2665 if (DirectDeclParser)
2666 (this->*DirectDeclParser)(D);
2667 return;
2668 }
2669
2670 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002671 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002672 DeclSpec DS;
2673 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002674 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002675
2676 // Recurse to parse whatever is left.
2677 ParseDeclaratorInternal(D, DirectDeclParser);
2678
2679 // Sema will have to catch (syntactically invalid) pointers into global
2680 // scope. It has to catch pointers into namespace scope anyway.
2681 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall7f040a92010-12-24 02:08:15 +00002682 Loc, DS.takeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002683 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002684 return;
2685 }
2686 }
2687
2688 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002689 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002690 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002691 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002692 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002693 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002694 if (DirectDeclParser)
2695 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002696 return;
2697 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002698
Sebastian Redl05532f22009-03-15 22:02:01 +00002699 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2700 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002701 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002702 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002703
Chris Lattner9af55002009-03-27 04:18:06 +00002704 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002705 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002706 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002707
Reid Spencer5f016e22007-07-11 17:01:13 +00002708 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002709 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002710
Reid Spencer5f016e22007-07-11 17:01:13 +00002711 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002712 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002713 if (Kind == tok::star)
2714 // Remember that we parsed a pointer type, and remember the type-quals.
2715 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
John McCall7f040a92010-12-24 02:08:15 +00002716 DS.takeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002717 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002718 else
2719 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002720 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall7f040a92010-12-24 02:08:15 +00002721 Loc, DS.takeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002722 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002723 } else {
2724 // Is a reference
2725 DeclSpec DS;
2726
Sebastian Redl743de1f2009-03-23 00:00:23 +00002727 // Complain about rvalue references in C++03, but then go on and build
2728 // the declarator.
2729 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00002730 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00002731
Reid Spencer5f016e22007-07-11 17:01:13 +00002732 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2733 // cv-qualifiers are introduced through the use of a typedef or of a
2734 // template type argument, in which case the cv-qualifiers are ignored.
2735 //
2736 // [GNU] Retricted references are allowed.
2737 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002738 // [C++0x] Attributes on references are not allowed.
2739 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002740 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002741
2742 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2743 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2744 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002745 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002746 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2747 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002748 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002749 }
2750
2751 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002752 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002753
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002754 if (D.getNumTypeObjects() > 0) {
2755 // C++ [dcl.ref]p4: There shall be no references to references.
2756 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2757 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002758 if (const IdentifierInfo *II = D.getIdentifier())
2759 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2760 << II;
2761 else
2762 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2763 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002764
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002765 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002766 // can go ahead and build the (technically ill-formed)
2767 // declarator: reference collapsing will take care of it.
2768 }
2769 }
2770
Reid Spencer5f016e22007-07-11 17:01:13 +00002771 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002772 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
John McCall7f040a92010-12-24 02:08:15 +00002773 DS.takeAttributes(),
Sebastian Redl05532f22009-03-15 22:02:01 +00002774 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002775 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002776 }
2777}
2778
2779/// ParseDirectDeclarator
2780/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002781/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002782/// '(' declarator ')'
2783/// [GNU] '(' attributes declarator ')'
2784/// [C90] direct-declarator '[' constant-expression[opt] ']'
2785/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2786/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2787/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2788/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2789/// direct-declarator '(' parameter-type-list ')'
2790/// direct-declarator '(' identifier-list[opt] ')'
2791/// [GNU] direct-declarator '(' parameter-forward-declarations
2792/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002793/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2794/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002795/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002796///
2797/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002798/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00002799/// '::'[opt] nested-name-specifier[opt] type-name
2800///
2801/// id-expression: [C++ 5.1]
2802/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002803/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002804///
2805/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002806/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002807/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002808/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002809/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002810/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002811///
Reid Spencer5f016e22007-07-11 17:01:13 +00002812void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002813 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002814
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002815 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2816 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002817 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00002818 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00002819 }
2820
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002821 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002822 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00002823 // Change the declaration context for name lookup, until this function
2824 // is exited (and the declarator has been parsed).
2825 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002826 }
2827
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002828 // C++0x [dcl.fct]p14:
2829 // There is a syntactic ambiguity when an ellipsis occurs at the end
2830 // of a parameter-declaration-clause without a preceding comma. In
2831 // this case, the ellipsis is parsed as part of the
2832 // abstract-declarator if the type of the parameter names a template
2833 // parameter pack that has not been expanded; otherwise, it is parsed
2834 // as part of the parameter-declaration-clause.
2835 if (Tok.is(tok::ellipsis) &&
2836 !((D.getContext() == Declarator::PrototypeContext ||
2837 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002838 NextToken().is(tok::r_paren) &&
2839 !Actions.containsUnexpandedParameterPacks(D)))
2840 D.setEllipsisLoc(ConsumeToken());
2841
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002842 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2843 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2844 // We found something that indicates the start of an unqualified-id.
2845 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00002846 bool AllowConstructorName;
2847 if (D.getDeclSpec().hasTypeSpecifier())
2848 AllowConstructorName = false;
2849 else if (D.getCXXScopeSpec().isSet())
2850 AllowConstructorName =
2851 (D.getContext() == Declarator::FileContext ||
2852 (D.getContext() == Declarator::MemberContext &&
2853 D.getDeclSpec().isFriendSpecified()));
2854 else
2855 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2856
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002857 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2858 /*EnteringContext=*/true,
2859 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002860 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002861 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002862 D.getName()) ||
2863 // Once we're past the identifier, if the scope was bad, mark the
2864 // whole declarator bad.
2865 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002866 D.SetIdentifier(0, Tok.getLocation());
2867 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002868 } else {
2869 // Parsed the unqualified-id; update range information and move along.
2870 if (D.getSourceRange().getBegin().isInvalid())
2871 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2872 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002873 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002874 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002875 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002876 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002877 assert(!getLang().CPlusPlus &&
2878 "There's a C++-specific check for tok::identifier above");
2879 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2880 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2881 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002882 goto PastIdentifier;
2883 }
2884
2885 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002886 // direct-declarator: '(' declarator ')'
2887 // direct-declarator: '(' attributes declarator ')'
2888 // Example: 'char (*X)' or 'int (*XX)(void)'
2889 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002890
2891 // If the declarator was parenthesized, we entered the declarator
2892 // scope when parsing the parenthesized declarator, then exited
2893 // the scope already. Re-enter the scope, if we need to.
2894 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002895 // If there was an error parsing parenthesized declarator, declarator
2896 // scope may have been enterred before. Don't do it again.
2897 if (!D.isInvalidType() &&
2898 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002899 // Change the declaration context for name lookup, until this function
2900 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002901 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002902 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002903 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002904 // This could be something simple like "int" (in which case the declarator
2905 // portion is empty), if an abstract-declarator is allowed.
2906 D.SetIdentifier(0, Tok.getLocation());
2907 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002908 if (D.getContext() == Declarator::MemberContext)
2909 Diag(Tok, diag::err_expected_member_name_or_semi)
2910 << D.getDeclSpec().getSourceRange();
2911 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002912 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002913 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002914 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002915 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002916 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002917 }
Mike Stump1eb44332009-09-09 15:08:12 +00002918
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002919 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002920 assert(D.isPastIdentifier() &&
2921 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002922
Sean Huntbbd37c62009-11-21 08:43:09 +00002923 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00002924 if (D.getIdentifier())
2925 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00002926
Reid Spencer5f016e22007-07-11 17:01:13 +00002927 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002928 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002929 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2930 // In such a case, check if we actually have a function declarator; if it
2931 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002932 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2933 // When not in file scope, warn for ambiguous function declarators, just
2934 // in case the author intended it as a variable definition.
2935 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2936 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2937 break;
2938 }
John McCall7f040a92010-12-24 02:08:15 +00002939 ParsedAttributes attrs;
2940 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00002941 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002942 ParseBracketDeclarator(D);
2943 } else {
2944 break;
2945 }
2946 }
2947}
2948
Chris Lattneref4715c2008-04-06 05:45:57 +00002949/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2950/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002951/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002952/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2953///
2954/// direct-declarator:
2955/// '(' declarator ')'
2956/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002957/// direct-declarator '(' parameter-type-list ')'
2958/// direct-declarator '(' identifier-list[opt] ')'
2959/// [GNU] direct-declarator '(' parameter-forward-declarations
2960/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002961///
2962void Parser::ParseParenDeclarator(Declarator &D) {
2963 SourceLocation StartLoc = ConsumeParen();
2964 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002965
Chris Lattner7399ee02008-10-20 02:05:46 +00002966 // Eat any attributes before we look at whether this is a grouping or function
2967 // declarator paren. If this is a grouping paren, the attribute applies to
2968 // the type being built up, for example:
2969 // int (__attribute__(()) *x)(long y)
2970 // If this ends up not being a grouping paren, the attribute applies to the
2971 // first argument, for example:
2972 // int (__attribute__(()) int x)
2973 // In either case, we need to eat any attributes to be able to determine what
2974 // sort of paren this is.
2975 //
John McCall7f040a92010-12-24 02:08:15 +00002976 ParsedAttributes attrs;
Chris Lattner7399ee02008-10-20 02:05:46 +00002977 bool RequiresArg = false;
2978 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00002979 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002980
Chris Lattner7399ee02008-10-20 02:05:46 +00002981 // We require that the argument list (if this is a non-grouping paren) be
2982 // present even if the attribute list was empty.
2983 RequiresArg = true;
2984 }
Steve Naroff239f0732008-12-25 14:16:32 +00002985 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002986 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002987 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2988 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall7f040a92010-12-24 02:08:15 +00002989 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00002990 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00002991 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002992 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00002993 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002994
Chris Lattneref4715c2008-04-06 05:45:57 +00002995 // If we haven't past the identifier yet (or where the identifier would be
2996 // stored, if this is an abstract declarator), then this is probably just
2997 // grouping parens. However, if this could be an abstract-declarator, then
2998 // this could also be the start of function arguments (consider 'void()').
2999 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003000
Chris Lattneref4715c2008-04-06 05:45:57 +00003001 if (!D.mayOmitIdentifier()) {
3002 // If this can't be an abstract-declarator, this *must* be a grouping
3003 // paren, because we haven't seen the identifier yet.
3004 isGrouping = true;
3005 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003006 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003007 isDeclarationSpecifier()) { // 'int(int)' is a function.
3008 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3009 // considered to be a type, not a K&R identifier-list.
3010 isGrouping = false;
3011 } else {
3012 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3013 isGrouping = true;
3014 }
Mike Stump1eb44332009-09-09 15:08:12 +00003015
Chris Lattneref4715c2008-04-06 05:45:57 +00003016 // If this is a grouping paren, handle:
3017 // direct-declarator: '(' declarator ')'
3018 // direct-declarator: '(' attributes declarator ')'
3019 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003020 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003021 D.setGroupingParens(true);
John McCall7f040a92010-12-24 02:08:15 +00003022 if (!attrs.empty())
3023 D.addAttributes(attrs.getList(), SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003024
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003025 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003026 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003027 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
3028 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc), EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003029
3030 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003031 return;
3032 }
Mike Stump1eb44332009-09-09 15:08:12 +00003033
Chris Lattneref4715c2008-04-06 05:45:57 +00003034 // Okay, if this wasn't a grouping paren, it must be the start of a function
3035 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003036 // identifier (and remember where it would have been), then call into
3037 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003038 D.SetIdentifier(0, Tok.getLocation());
3039
John McCall7f040a92010-12-24 02:08:15 +00003040 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003041}
3042
3043/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3044/// declarator D up to a paren, which indicates that we are parsing function
3045/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003046///
Chris Lattner7399ee02008-10-20 02:05:46 +00003047/// If AttrList is non-null, then the caller parsed those arguments immediately
3048/// after the open paren - they should be considered to be the first argument of
3049/// a parameter. If RequiresArg is true, then the first argument of the
3050/// function is required to be present and required to not be an identifier
3051/// list.
3052///
Reid Spencer5f016e22007-07-11 17:01:13 +00003053/// This method also handles this portion of the grammar:
3054/// parameter-type-list: [C99 6.7.5]
3055/// parameter-list
3056/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003057/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003058///
3059/// parameter-list: [C99 6.7.5]
3060/// parameter-declaration
3061/// parameter-list ',' parameter-declaration
3062///
3063/// parameter-declaration: [C99 6.7.5]
3064/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003065/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003066/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003067/// declaration-specifiers abstract-declarator[opt]
3068/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003069/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003070/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3071///
Douglas Gregor83f51722011-01-26 03:43:54 +00003072/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3073/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003074///
Chris Lattner7399ee02008-10-20 02:05:46 +00003075void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall7f040a92010-12-24 02:08:15 +00003076 ParsedAttributes &attrs,
Chris Lattner7399ee02008-10-20 02:05:46 +00003077 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003078 // lparen is already consumed!
3079 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003080
Douglas Gregordab60ad2010-10-01 18:44:50 +00003081 ParsedType TrailingReturnType;
3082
Chris Lattner7399ee02008-10-20 02:05:46 +00003083 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003084 if (Tok.is(tok::r_paren)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003085 if (RequiresArg)
Chris Lattner1ab3b962008-11-18 07:48:38 +00003086 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003087
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003088 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3089 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003090
3091 // cv-qualifier-seq[opt].
3092 DeclSpec DS;
Douglas Gregor83f51722011-01-26 03:43:54 +00003093 SourceLocation RefQualifierLoc;
3094 bool RefQualifierIsLValueRef = true;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003095 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003096 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003097 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003098 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003099 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003100 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003101 MaybeParseCXX0XAttributes(attrs);
3102
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003103 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003104 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003105 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003106
Douglas Gregor83f51722011-01-26 03:43:54 +00003107 // Parse ref-qualifier[opt]
3108 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3109 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003110 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor83f51722011-01-26 03:43:54 +00003111
3112 RefQualifierIsLValueRef = Tok.is(tok::amp);
3113 RefQualifierLoc = ConsumeToken();
3114 EndLoc = RefQualifierLoc;
3115 }
3116
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003117 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003118 if (Tok.is(tok::kw_throw)) {
3119 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003120 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003121 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003122 hasAnyExceptionSpec);
3123 assert(Exceptions.size() == ExceptionRanges.size() &&
3124 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003125 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00003126
3127 // Parse trailing-return-type.
3128 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3129 TrailingReturnType = ParseTrailingReturnType().get();
3130 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003131 }
3132
Chris Lattnerf97409f2008-04-06 06:57:35 +00003133 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003134 // int() -> no prototype, no '...'.
John McCall7f040a92010-12-24 02:08:15 +00003135 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3136 /*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003137 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003138 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003139 /*arglist*/ 0, 0,
3140 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003141 RefQualifierIsLValueRef,
3142 RefQualifierLoc,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003143 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003144 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003145 Exceptions.data(),
3146 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003147 Exceptions.size(),
Douglas Gregordab60ad2010-10-01 18:44:50 +00003148 LParenLoc, RParenLoc, D,
3149 TrailingReturnType),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003150 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003151 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003152 }
3153
Chris Lattner7399ee02008-10-20 02:05:46 +00003154 // Alternatively, this parameter list may be an identifier list form for a
3155 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003156 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3157 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003158 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003159 // K&R identifier lists can't have typedefs as identifiers, per
3160 // C99 6.7.5.3p11.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003161 if (RequiresArg)
Steve Naroff2d081c42009-01-28 19:16:40 +00003162 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner83a94472010-05-14 17:23:36 +00003163
Steve Naroff2d081c42009-01-28 19:16:40 +00003164 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003165 // normal declarators, not for abstract-declarators. Get the first
3166 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003167 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003168 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003169
3170 // Identifier lists follow a really simple grammar: the identifiers can
3171 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3172 // identifier lists are really rare in the brave new modern world, and it
3173 // is very common for someone to typo a type in a non-k&r style list. If
3174 // we are presented with something like: "void foo(intptr x, float y)",
3175 // we don't want to start parsing the function declarator as though it is
3176 // a K&R style declarator just because intptr is an invalid type.
3177 //
3178 // To handle this, we check to see if the token after the first identifier
3179 // is a "," or ")". Only if so, do we parse it as an identifier list.
3180 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3181 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3182 FirstTok.getIdentifierInfo(),
3183 FirstTok.getLocation(), D);
3184
3185 // If we get here, the code is invalid. Push the first identifier back
3186 // into the token stream and parse the first argument as an (invalid)
3187 // normal argument declarator.
3188 PP.EnterToken(Tok);
3189 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003190 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003191 }
Mike Stump1eb44332009-09-09 15:08:12 +00003192
Chris Lattnerf97409f2008-04-06 06:57:35 +00003193 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003194
Chris Lattnerf97409f2008-04-06 06:57:35 +00003195 // Build up an array of information about the parsed arguments.
3196 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003197
3198 // Enter function-declaration scope, limiting any declarators to the
3199 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003200 ParseScope PrototypeScope(this,
3201 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003202
Chris Lattnerf97409f2008-04-06 06:57:35 +00003203 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003204 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003205 while (1) {
3206 if (Tok.is(tok::ellipsis)) {
3207 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003208 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003209 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003210 }
Mike Stump1eb44332009-09-09 15:08:12 +00003211
Chris Lattnerf97409f2008-04-06 06:57:35 +00003212 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003213 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003214 DeclSpec DS;
John McCall7f040a92010-12-24 02:08:15 +00003215
3216 // Skip any Microsoft attributes before a param.
3217 if (getLang().Microsoft && Tok.is(tok::l_square))
3218 ParseMicrosoftAttributes(DS.getAttributes());
3219
3220 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00003221
3222 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00003223 // Take them so that we only apply the attributes to the first parameter.
3224 DS.takeAttributesFrom(attrs);
3225
Chris Lattnere64c5492009-02-27 18:38:20 +00003226 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003227
Chris Lattnerf97409f2008-04-06 06:57:35 +00003228 // Parse the declarator. This is "PrototypeContext", because we must
3229 // accept either 'declarator' or 'abstract-declarator' here.
3230 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3231 ParseDeclarator(ParmDecl);
3232
3233 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00003234 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003235
Chris Lattnerf97409f2008-04-06 06:57:35 +00003236 // Remember this parsed parameter in ParamInfo.
3237 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003238
Douglas Gregor72b505b2008-12-16 21:30:33 +00003239 // DefArgToks is used when the parsing of default arguments needs
3240 // to be delayed.
3241 CachedTokens *DefArgToks = 0;
3242
Chris Lattnerf97409f2008-04-06 06:57:35 +00003243 // If no parameter was specified, verify that *something* was specified,
3244 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003245 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3246 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003247 // Completely missing, emit error.
3248 Diag(DSStart, diag::err_missing_param);
3249 } else {
3250 // Otherwise, we have something. Add it and let semantic analysis try
3251 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003252
Chris Lattnerf97409f2008-04-06 06:57:35 +00003253 // Inform the actions module about the parameter declarator, so it gets
3254 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003255 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003256
3257 // Parse the default argument, if any. We parse the default
3258 // arguments in all dialects; the semantic analysis in
3259 // ActOnParamDefaultArgument will reject the default argument in
3260 // C.
3261 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003262 SourceLocation EqualLoc = Tok.getLocation();
3263
Chris Lattner04421082008-04-08 04:40:51 +00003264 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003265 if (D.getContext() == Declarator::MemberContext) {
3266 // If we're inside a class definition, cache the tokens
3267 // corresponding to the default argument. We'll actually parse
3268 // them when we see the end of the class definition.
3269 // FIXME: Templates will require something similar.
3270 // FIXME: Can we use a smart pointer for Toks?
3271 DefArgToks = new CachedTokens;
3272
Mike Stump1eb44332009-09-09 15:08:12 +00003273 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003274 /*StopAtSemi=*/true,
3275 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003276 delete DefArgToks;
3277 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003278 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003279 } else {
3280 // Mark the end of the default argument so that we know when to
3281 // stop when we parse it later on.
3282 Token DefArgEnd;
3283 DefArgEnd.startToken();
3284 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3285 DefArgEnd.setLocation(Tok.getLocation());
3286 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003287 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003288 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003289 }
Chris Lattner04421082008-04-08 04:40:51 +00003290 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003291 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003292 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003293
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003294 // The argument isn't actually potentially evaluated unless it is
3295 // used.
3296 EnterExpressionEvaluationContext Eval(Actions,
3297 Sema::PotentiallyEvaluatedIfUsed);
3298
John McCall60d7b3a2010-08-24 06:29:42 +00003299 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003300 if (DefArgResult.isInvalid()) {
3301 Actions.ActOnParamDefaultArgumentError(Param);
3302 SkipUntil(tok::comma, tok::r_paren, true, true);
3303 } else {
3304 // Inform the actions module about the default argument
3305 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003306 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003307 }
Chris Lattner04421082008-04-08 04:40:51 +00003308 }
3309 }
Mike Stump1eb44332009-09-09 15:08:12 +00003310
3311 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3312 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003313 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003314 }
3315
3316 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003317 if (Tok.isNot(tok::comma)) {
3318 if (Tok.is(tok::ellipsis)) {
3319 IsVariadic = true;
3320 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3321
3322 if (!getLang().CPlusPlus) {
3323 // We have ellipsis without a preceding ',', which is ill-formed
3324 // in C. Complain and provide the fix.
3325 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003326 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003327 }
3328 }
3329
3330 break;
3331 }
Mike Stump1eb44332009-09-09 15:08:12 +00003332
Chris Lattnerf97409f2008-04-06 06:57:35 +00003333 // Consume the comma.
3334 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003335 }
Mike Stump1eb44332009-09-09 15:08:12 +00003336
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003337 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003338 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3339 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003340
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003341 DeclSpec DS;
Douglas Gregor83f51722011-01-26 03:43:54 +00003342 SourceLocation RefQualifierLoc;
3343 bool RefQualifierIsLValueRef = true;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003344 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003345 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003346 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003347 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003348 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00003349
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003350 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003351 MaybeParseCXX0XAttributes(attrs);
3352
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003353 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003354 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003355 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003356 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003357
Douglas Gregor83f51722011-01-26 03:43:54 +00003358 // Parse ref-qualifier[opt]
3359 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3360 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003361 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor83f51722011-01-26 03:43:54 +00003362
3363 RefQualifierIsLValueRef = Tok.is(tok::amp);
3364 RefQualifierLoc = ConsumeToken();
3365 EndLoc = RefQualifierLoc;
3366 }
3367
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003368 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003369 if (Tok.is(tok::kw_throw)) {
3370 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003371 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003372 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003373 hasAnyExceptionSpec);
3374 assert(Exceptions.size() == ExceptionRanges.size() &&
3375 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003376 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00003377
3378 // Parse trailing-return-type.
3379 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3380 TrailingReturnType = ParseTrailingReturnType().get();
3381 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003382 }
3383
Douglas Gregordab60ad2010-10-01 18:44:50 +00003384 // FIXME: We should leave the prototype scope before parsing the exception
3385 // specification, and then reenter it when parsing the trailing return type.
3386
3387 // Leave prototype scope.
3388 PrototypeScope.Exit();
3389
Reid Spencer5f016e22007-07-11 17:01:13 +00003390 // Remember that we parsed a function type, and remember the attributes.
John McCall7f040a92010-12-24 02:08:15 +00003391 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3392 /*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003393 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003394 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003395 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003396 RefQualifierIsLValueRef,
3397 RefQualifierLoc,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003398 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003399 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003400 Exceptions.data(),
3401 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003402 Exceptions.size(),
Douglas Gregordab60ad2010-10-01 18:44:50 +00003403 LParenLoc, RParenLoc, D,
3404 TrailingReturnType),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003405 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003406}
3407
Chris Lattner66d28652008-04-06 06:34:08 +00003408/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3409/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003410/// first identifier has already been consumed, and the current token is the
3411/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003412///
3413/// identifier-list: [C99 6.7.5]
3414/// identifier
3415/// identifier-list ',' identifier
3416///
3417void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003418 IdentifierInfo *FirstIdent,
3419 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003420 Declarator &D) {
3421 // Build up an array of information about the parsed arguments.
3422 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3423 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003424
Chris Lattner66d28652008-04-06 06:34:08 +00003425 // If there was no identifier specified for the declarator, either we are in
3426 // an abstract-declarator, or we are in a parameter declarator which was found
3427 // to be abstract. In abstract-declarators, identifier lists are not valid:
3428 // diagnose this.
3429 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003430 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003431
Chris Lattner83a94472010-05-14 17:23:36 +00003432 // The first identifier was already read, and is known to be the first
3433 // identifier in the list. Remember this identifier in ParamInfo.
3434 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00003435 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00003436
Chris Lattner66d28652008-04-06 06:34:08 +00003437 while (Tok.is(tok::comma)) {
3438 // Eat the comma.
3439 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003440
Chris Lattner50c64772008-04-06 06:39:19 +00003441 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003442 if (Tok.isNot(tok::identifier)) {
3443 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003444 SkipUntil(tok::r_paren);
3445 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003446 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003447
Chris Lattner66d28652008-04-06 06:34:08 +00003448 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003449
3450 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003451 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003452 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003453
Chris Lattner66d28652008-04-06 06:34:08 +00003454 // Verify that the argument identifier has not already been mentioned.
3455 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003456 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003457 } else {
3458 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003459 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003460 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00003461 0));
Chris Lattner50c64772008-04-06 06:39:19 +00003462 }
Mike Stump1eb44332009-09-09 15:08:12 +00003463
Chris Lattner66d28652008-04-06 06:34:08 +00003464 // Eat the identifier.
3465 ConsumeToken();
3466 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003467
3468 // If we have the closing ')', eat it and we're done.
3469 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3470
Chris Lattner50c64772008-04-06 06:39:19 +00003471 // Remember that we parsed a function type, and remember the attributes. This
3472 // function type is always a K&R style function type, which is not varargs and
3473 // has no prototype.
John McCall7f040a92010-12-24 02:08:15 +00003474 D.AddTypeInfo(DeclaratorChunk::getFunction(ParsedAttributes(),
3475 /*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003476 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003477 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003478 /*TypeQuals*/0,
Douglas Gregor83f51722011-01-26 03:43:54 +00003479 true, SourceLocation(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003480 /*exception*/false,
3481 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003482 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003483 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003484}
Chris Lattneref4715c2008-04-06 05:45:57 +00003485
Reid Spencer5f016e22007-07-11 17:01:13 +00003486/// [C90] direct-declarator '[' constant-expression[opt] ']'
3487/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3488/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3489/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3490/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3491void Parser::ParseBracketDeclarator(Declarator &D) {
3492 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003493
Chris Lattner378c7e42008-12-18 07:27:21 +00003494 // C array syntax has many features, but by-far the most common is [] and [4].
3495 // This code does a fast path to handle some of the most obvious cases.
3496 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003497 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall7f040a92010-12-24 02:08:15 +00003498 ParsedAttributes attrs;
3499 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003500
Chris Lattner378c7e42008-12-18 07:27:21 +00003501 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00003502 ExprResult NumElements;
John McCall7f040a92010-12-24 02:08:15 +00003503 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003504 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003505 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003506 return;
3507 } else if (Tok.getKind() == tok::numeric_constant &&
3508 GetLookAheadToken(1).is(tok::r_square)) {
3509 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00003510 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003511 ConsumeToken();
3512
Sebastian Redlab197ba2009-02-09 18:23:29 +00003513 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall7f040a92010-12-24 02:08:15 +00003514 ParsedAttributes attrs;
3515 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003516
Chris Lattner378c7e42008-12-18 07:27:21 +00003517 // Remember that we parsed a array type, and remember its features.
John McCall7f040a92010-12-24 02:08:15 +00003518 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, 0,
3519 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003520 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003521 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003522 return;
3523 }
Mike Stump1eb44332009-09-09 15:08:12 +00003524
Reid Spencer5f016e22007-07-11 17:01:13 +00003525 // If valid, this location is the position where we read the 'static' keyword.
3526 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003527 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003528 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003529
Reid Spencer5f016e22007-07-11 17:01:13 +00003530 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003531 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003532 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003533 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003534
Reid Spencer5f016e22007-07-11 17:01:13 +00003535 // If we haven't already read 'static', check to see if there is one after the
3536 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003537 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003538 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003539
Reid Spencer5f016e22007-07-11 17:01:13 +00003540 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3541 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00003542 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00003543
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003544 // Handle the case where we have '[*]' as the array size. However, a leading
3545 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3546 // the the token after the star is a ']'. Since stars in arrays are
3547 // infrequent, use of lookahead is not costly here.
3548 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003549 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003550
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003551 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003552 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003553 StaticLoc = SourceLocation(); // Drop the static.
3554 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003555 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003556 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003557 // Note, in C89, this production uses the constant-expr production instead
3558 // of assignment-expr. The only difference is that assignment-expr allows
3559 // things like '=' and '*='. Sema rejects these in C89 mode because they
3560 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003561
Douglas Gregore0762c92009-06-19 23:52:42 +00003562 // Parse the constant-expression or assignment-expression now (depending
3563 // on dialect).
3564 if (getLang().CPlusPlus)
3565 NumElements = ParseConstantExpression();
3566 else
3567 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003568 }
Mike Stump1eb44332009-09-09 15:08:12 +00003569
Reid Spencer5f016e22007-07-11 17:01:13 +00003570 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003571 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003572 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003573 // If the expression was invalid, skip it.
3574 SkipUntil(tok::r_square);
3575 return;
3576 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003577
3578 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3579
John McCall7f040a92010-12-24 02:08:15 +00003580 ParsedAttributes attrs;
3581 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003582
Chris Lattner378c7e42008-12-18 07:27:21 +00003583 // Remember that we parsed a array type, and remember its features.
John McCall7f040a92010-12-24 02:08:15 +00003584 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), attrs,
Reid Spencer5f016e22007-07-11 17:01:13 +00003585 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003586 NumElements.release(),
3587 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003588 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003589}
3590
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003591/// [GNU] typeof-specifier:
3592/// typeof ( expressions )
3593/// typeof ( type-name )
3594/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003595///
3596void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003597 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003598 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003599 SourceLocation StartLoc = ConsumeToken();
3600
John McCallcfb708c2010-01-13 20:03:27 +00003601 const bool hasParens = Tok.is(tok::l_paren);
3602
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003603 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00003604 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003605 SourceRange CastRange;
John McCall60d7b3a2010-08-24 06:29:42 +00003606 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall911093e2010-08-25 02:45:51 +00003607 isCastExpr,
3608 CastTy,
3609 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003610 if (hasParens)
3611 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003612
3613 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003614 // FIXME: Not accurate, the range gets one token more than it should.
3615 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003616 else
3617 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003618
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003619 if (isCastExpr) {
3620 if (!CastTy) {
3621 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003622 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003623 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003624
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003625 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003626 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003627 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3628 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003629 DiagID, CastTy))
3630 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003631 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003632 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003633
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003634 // If we get here, the operand to the typeof was an expresion.
3635 if (Operand.isInvalid()) {
3636 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003637 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003638 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003639
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003640 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003641 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003642 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3643 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00003644 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00003645 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003646}
Chris Lattner1b492422010-02-28 18:33:55 +00003647
3648
3649/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3650/// from TryAltiVecVectorToken.
3651bool Parser::TryAltiVecVectorTokenOutOfLine() {
3652 Token Next = NextToken();
3653 switch (Next.getKind()) {
3654 default: return false;
3655 case tok::kw_short:
3656 case tok::kw_long:
3657 case tok::kw_signed:
3658 case tok::kw_unsigned:
3659 case tok::kw_void:
3660 case tok::kw_char:
3661 case tok::kw_int:
3662 case tok::kw_float:
3663 case tok::kw_double:
3664 case tok::kw_bool:
3665 case tok::kw___pixel:
3666 Tok.setKind(tok::kw___vector);
3667 return true;
3668 case tok::identifier:
3669 if (Next.getIdentifierInfo() == Ident_pixel) {
3670 Tok.setKind(tok::kw___vector);
3671 return true;
3672 }
3673 return false;
3674 }
3675}
3676
3677bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3678 const char *&PrevSpec, unsigned &DiagID,
3679 bool &isInvalid) {
3680 if (Tok.getIdentifierInfo() == Ident_vector) {
3681 Token Next = NextToken();
3682 switch (Next.getKind()) {
3683 case tok::kw_short:
3684 case tok::kw_long:
3685 case tok::kw_signed:
3686 case tok::kw_unsigned:
3687 case tok::kw_void:
3688 case tok::kw_char:
3689 case tok::kw_int:
3690 case tok::kw_float:
3691 case tok::kw_double:
3692 case tok::kw_bool:
3693 case tok::kw___pixel:
3694 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3695 return true;
3696 case tok::identifier:
3697 if (Next.getIdentifierInfo() == Ident_pixel) {
3698 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3699 return true;
3700 }
3701 break;
3702 default:
3703 break;
3704 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00003705 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00003706 DS.isTypeAltiVecVector()) {
3707 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3708 return true;
3709 }
3710 return false;
3711}
3712