blob: 9af7345fdd63a09c3bad6d806d983996832da1ce [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"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallSet.h"
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.7: Declarations.
26//===----------------------------------------------------------------------===//
27
28/// ParseTypeName
29/// type-name: [C99 6.7.6]
30/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000031///
32/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000033TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000034 Declarator::TheContext Context,
35 ObjCDeclSpec *objcQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +000036 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000037 DeclSpec DS(AttrFactory);
John McCallf85e1932011-06-15 23:02:42 +000038 DS.setObjCQualifiers(objcQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +000039 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000040
Reid Spencer5f016e22007-07-11 17:01:13 +000041 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000042 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000043 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000044 if (Range)
45 *Range = DeclaratorInfo.getSourceRange();
46
Chris Lattnereaaebc72009-04-25 08:06:05 +000047 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000048 return true;
49
Douglas Gregor23c94db2010-07-02 17:43:08 +000050 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000051}
52
Sean Huntbbd37c62009-11-21 08:43:09 +000053/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000054///
55/// [GNU] attributes:
56/// attribute
57/// attributes attribute
58///
59/// [GNU] attribute:
60/// '__attribute__' '(' '(' attribute-list ')' ')'
61///
62/// [GNU] attribute-list:
63/// attrib
64/// attribute_list ',' attrib
65///
66/// [GNU] attrib:
67/// empty
68/// attrib-name
69/// attrib-name '(' identifier ')'
70/// attrib-name '(' identifier ',' nonempty-expr-list ')'
71/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
72///
73/// [GNU] attrib-name:
74/// identifier
75/// typespec
76/// typequal
77/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000078///
Reid Spencer5f016e22007-07-11 17:01:13 +000079/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000080/// token lookahead. Comment from gcc: "If they start with an identifier
81/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000082/// start with that identifier; otherwise they are an expression list."
83///
84/// At the moment, I am not doing 2 token lookahead. I am also unaware of
85/// any attributes that don't work (based on my limited testing). Most
86/// attributes are very simple in practice. Until we find a bug, I don't see
87/// a pressing need to implement the 2 token lookahead.
88
John McCall7f040a92010-12-24 02:08:15 +000089void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
90 SourceLocation *endLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +000091 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000092
Chris Lattner04d66662007-10-09 17:33:22 +000093 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000094 ConsumeToken();
95 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
96 "attribute")) {
97 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +000098 return;
Reid Spencer5f016e22007-07-11 17:01:13 +000099 }
100 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
101 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000102 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 }
104 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000105 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
106 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000107
108 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
110 ConsumeToken();
111 continue;
112 }
113 // we have an identifier or declaration specifier (const, int, etc.)
114 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
115 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000117 // Availability attributes have their own grammar.
118 if (AttrName->isStr("availability"))
119 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, attrs, endLoc);
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000120 // check if we have a "parameterized" attribute
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000121 else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Chris Lattner04d66662007-10-09 17:33:22 +0000124 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
126 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000127
128 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 // __attribute__(( mode(byte) ))
130 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000131 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
132 ParmName, ParmLoc, 0, 0);
Chris Lattner04d66662007-10-09 17:33:22 +0000133 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 ConsumeToken();
135 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000136 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 // now parse the non-empty comma separated list of expressions
140 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000141 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000142 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 ArgExprsOk = false;
144 SkipUntil(tok::r_paren);
145 break;
146 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000147 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 }
Chris Lattner04d66662007-10-09 17:33:22 +0000149 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 break;
151 ConsumeToken(); // Eat the comma, move to the next argument
152 }
Chris Lattner04d66662007-10-09 17:33:22 +0000153 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000155 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
156 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 }
158 }
159 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000160 switch (Tok.getKind()) {
161 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 // __attribute__(( nonnull() ))
164 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000165 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
166 0, SourceLocation(), 0, 0);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000167 break;
168 case tok::kw_char:
169 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000170 case tok::kw_char16_t:
171 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000172 case tok::kw_bool:
173 case tok::kw_short:
174 case tok::kw_int:
175 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000176 case tok::kw___int64:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000177 case tok::kw_signed:
178 case tok::kw_unsigned:
179 case tok::kw_float:
180 case tok::kw_double:
181 case tok::kw_void:
John McCall7f040a92010-12-24 02:08:15 +0000182 case tok::kw_typeof: {
183 AttributeList *attr
John McCall0b7e6782011-03-24 11:26:52 +0000184 = attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
185 0, SourceLocation(), 0, 0);
John McCall7f040a92010-12-24 02:08:15 +0000186 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian1b72fa72010-08-17 23:19:16 +0000187 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000188 // If it's a builtin type name, eat it and expect a rparen
189 // __attribute__(( vec_type_hint(char) ))
190 ConsumeToken();
Nate Begeman6f3d8382009-06-26 06:32:41 +0000191 if (Tok.is(tok::r_paren))
192 ConsumeParen();
193 break;
John McCall7f040a92010-12-24 02:08:15 +0000194 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000195 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000197 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 // now parse the list of expressions
201 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000202 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000203 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 ArgExprsOk = false;
205 SkipUntil(tok::r_paren);
206 break;
207 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000208 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 }
Chris Lattner04d66662007-10-09 17:33:22 +0000210 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 break;
212 ConsumeToken(); // Eat the comma, move to the next argument
213 }
214 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000215 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000217 attrs.addNew(AttrName, AttrNameLoc, 0,
218 AttrNameLoc, 0, SourceLocation(),
219 ArgExprs.take(), ArgExprs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000221 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 }
223 }
224 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000225 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
226 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 }
228 }
229 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000231 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000232 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
233 SkipUntil(tok::r_paren, false);
234 }
John McCall7f040a92010-12-24 02:08:15 +0000235 if (endLoc)
236 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000238}
239
Eli Friedmana23b4852009-06-08 07:21:15 +0000240/// ParseMicrosoftDeclSpec - Parse an __declspec construct
241///
242/// [MS] decl-specifier:
243/// __declspec ( extended-decl-modifier-seq )
244///
245/// [MS] extended-decl-modifier-seq:
246/// extended-decl-modifier[opt]
247/// extended-decl-modifier extended-decl-modifier-seq
248
John McCall7f040a92010-12-24 02:08:15 +0000249void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000250 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000251
Steve Narofff59e17e2008-12-24 20:59:21 +0000252 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000253 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
254 "declspec")) {
255 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000256 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000257 }
Francois Pichet373197b2011-05-07 19:04:49 +0000258
Eli Friedman290eeb02009-06-08 23:27:34 +0000259 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000260 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
261 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000262
263 // FIXME: Remove this when we have proper __declspec(property()) support.
264 // Just skip everything inside property().
265 if (AttrName->getName() == "property") {
266 ConsumeParen();
267 SkipUntil(tok::r_paren);
268 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000269 if (Tok.is(tok::l_paren)) {
270 ConsumeParen();
271 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
272 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000273 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000274 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000275 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000276 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
277 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000278 }
279 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
280 SkipUntil(tok::r_paren, false);
281 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000282 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
283 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000284 }
285 }
286 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
287 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000288 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000289}
290
John McCall7f040a92010-12-24 02:08:15 +0000291void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000292 // Treat these like attributes
293 // FIXME: Allow Sema to distinguish between these and real attributes!
294 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000295 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
296 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000297 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
298 SourceLocation AttrNameLoc = ConsumeToken();
299 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
300 // FIXME: Support these properly!
301 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000302 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
303 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000304 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000305}
306
John McCall7f040a92010-12-24 02:08:15 +0000307void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000308 // Treat these like attributes
309 while (Tok.is(tok::kw___pascal)) {
310 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
311 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000312 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
313 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000314 }
John McCall7f040a92010-12-24 02:08:15 +0000315}
316
Peter Collingbournef315fa82011-02-14 01:42:53 +0000317void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
318 // Treat these like attributes
319 while (Tok.is(tok::kw___kernel)) {
320 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000321 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
322 AttrNameLoc, 0, AttrNameLoc, 0,
323 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000324 }
325}
326
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000327void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
328 SourceLocation Loc = Tok.getLocation();
329 switch(Tok.getKind()) {
330 // OpenCL qualifiers:
331 case tok::kw___private:
332 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000333 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000334 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000335 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000336 break;
337
338 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000339 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000340 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000341 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000342 break;
343
344 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000345 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000346 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000347 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000348 break;
349
350 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000351 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000352 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000353 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000354 break;
355
356 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000357 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000358 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000359 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000360 break;
361
362 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000363 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000364 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000365 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000366 break;
367
368 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000369 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000370 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000371 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000372 break;
373 default: break;
374 }
375}
376
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000377/// \brief Parse a version number.
378///
379/// version:
380/// simple-integer
381/// simple-integer ',' simple-integer
382/// simple-integer ',' simple-integer ',' simple-integer
383VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
384 Range = Tok.getLocation();
385
386 if (!Tok.is(tok::numeric_constant)) {
387 Diag(Tok, diag::err_expected_version);
388 SkipUntil(tok::comma, tok::r_paren, true, true, true);
389 return VersionTuple();
390 }
391
392 // Parse the major (and possibly minor and subminor) versions, which
393 // are stored in the numeric constant. We utilize a quirk of the
394 // lexer, which is that it handles something like 1.2.3 as a single
395 // numeric constant, rather than two separate tokens.
396 llvm::SmallString<512> Buffer;
397 Buffer.resize(Tok.getLength()+1);
398 const char *ThisTokBegin = &Buffer[0];
399
400 // Get the spelling of the token, which eliminates trigraphs, etc.
401 bool Invalid = false;
402 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
403 if (Invalid)
404 return VersionTuple();
405
406 // Parse the major version.
407 unsigned AfterMajor = 0;
408 unsigned Major = 0;
409 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
410 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
411 ++AfterMajor;
412 }
413
414 if (AfterMajor == 0) {
415 Diag(Tok, diag::err_expected_version);
416 SkipUntil(tok::comma, tok::r_paren, true, true, true);
417 return VersionTuple();
418 }
419
420 if (AfterMajor == ActualLength) {
421 ConsumeToken();
422
423 // We only had a single version component.
424 if (Major == 0) {
425 Diag(Tok, diag::err_zero_version);
426 return VersionTuple();
427 }
428
429 return VersionTuple(Major);
430 }
431
432 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
433 Diag(Tok, diag::err_expected_version);
434 SkipUntil(tok::comma, tok::r_paren, true, true, true);
435 return VersionTuple();
436 }
437
438 // Parse the minor version.
439 unsigned AfterMinor = AfterMajor + 1;
440 unsigned Minor = 0;
441 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
442 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
443 ++AfterMinor;
444 }
445
446 if (AfterMinor == ActualLength) {
447 ConsumeToken();
448
449 // We had major.minor.
450 if (Major == 0 && Minor == 0) {
451 Diag(Tok, diag::err_zero_version);
452 return VersionTuple();
453 }
454
455 return VersionTuple(Major, Minor);
456 }
457
458 // If what follows is not a '.', we have a problem.
459 if (ThisTokBegin[AfterMinor] != '.') {
460 Diag(Tok, diag::err_expected_version);
461 SkipUntil(tok::comma, tok::r_paren, true, true, true);
462 return VersionTuple();
463 }
464
465 // Parse the subminor version.
466 unsigned AfterSubminor = AfterMinor + 1;
467 unsigned Subminor = 0;
468 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
469 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
470 ++AfterSubminor;
471 }
472
473 if (AfterSubminor != ActualLength) {
474 Diag(Tok, diag::err_expected_version);
475 SkipUntil(tok::comma, tok::r_paren, true, true, true);
476 return VersionTuple();
477 }
478 ConsumeToken();
479 return VersionTuple(Major, Minor, Subminor);
480}
481
482/// \brief Parse the contents of the "availability" attribute.
483///
484/// availability-attribute:
485/// 'availability' '(' platform ',' version-arg-list ')'
486///
487/// platform:
488/// identifier
489///
490/// version-arg-list:
491/// version-arg
492/// version-arg ',' version-arg-list
493///
494/// version-arg:
495/// 'introduced' '=' version
496/// 'deprecated' '=' version
497/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000498/// 'unavailable'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000499void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
500 SourceLocation AvailabilityLoc,
501 ParsedAttributes &attrs,
502 SourceLocation *endLoc) {
503 SourceLocation PlatformLoc;
504 IdentifierInfo *Platform = 0;
505
506 enum { Introduced, Deprecated, Obsoleted, Unknown };
507 AvailabilityChange Changes[Unknown];
508
509 // Opening '('.
510 SourceLocation LParenLoc;
511 if (!Tok.is(tok::l_paren)) {
512 Diag(Tok, diag::err_expected_lparen);
513 return;
514 }
515 LParenLoc = ConsumeParen();
516
517 // Parse the platform name,
518 if (Tok.isNot(tok::identifier)) {
519 Diag(Tok, diag::err_availability_expected_platform);
520 SkipUntil(tok::r_paren);
521 return;
522 }
523 Platform = Tok.getIdentifierInfo();
524 PlatformLoc = ConsumeToken();
525
526 // Parse the ',' following the platform name.
527 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
528 return;
529
530 // If we haven't grabbed the pointers for the identifiers
531 // "introduced", "deprecated", and "obsoleted", do so now.
532 if (!Ident_introduced) {
533 Ident_introduced = PP.getIdentifierInfo("introduced");
534 Ident_deprecated = PP.getIdentifierInfo("deprecated");
535 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000536 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000537 }
538
539 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000540 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000541 do {
542 if (Tok.isNot(tok::identifier)) {
543 Diag(Tok, diag::err_availability_expected_change);
544 SkipUntil(tok::r_paren);
545 return;
546 }
547 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
548 SourceLocation KeywordLoc = ConsumeToken();
549
Douglas Gregorb53e4172011-03-26 03:35:55 +0000550 if (Keyword == Ident_unavailable) {
551 if (UnavailableLoc.isValid()) {
552 Diag(KeywordLoc, diag::err_availability_redundant)
553 << Keyword << SourceRange(UnavailableLoc);
554 }
555 UnavailableLoc = KeywordLoc;
556
557 if (Tok.isNot(tok::comma))
558 break;
559
560 ConsumeToken();
561 continue;
562 }
563
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000564 if (Tok.isNot(tok::equal)) {
565 Diag(Tok, diag::err_expected_equal_after)
566 << Keyword;
567 SkipUntil(tok::r_paren);
568 return;
569 }
570 ConsumeToken();
571
572 SourceRange VersionRange;
573 VersionTuple Version = ParseVersionTuple(VersionRange);
574
575 if (Version.empty()) {
576 SkipUntil(tok::r_paren);
577 return;
578 }
579
580 unsigned Index;
581 if (Keyword == Ident_introduced)
582 Index = Introduced;
583 else if (Keyword == Ident_deprecated)
584 Index = Deprecated;
585 else if (Keyword == Ident_obsoleted)
586 Index = Obsoleted;
587 else
588 Index = Unknown;
589
590 if (Index < Unknown) {
591 if (!Changes[Index].KeywordLoc.isInvalid()) {
592 Diag(KeywordLoc, diag::err_availability_redundant)
593 << Keyword
594 << SourceRange(Changes[Index].KeywordLoc,
595 Changes[Index].VersionRange.getEnd());
596 }
597
598 Changes[Index].KeywordLoc = KeywordLoc;
599 Changes[Index].Version = Version;
600 Changes[Index].VersionRange = VersionRange;
601 } else {
602 Diag(KeywordLoc, diag::err_availability_unknown_change)
603 << Keyword << VersionRange;
604 }
605
606 if (Tok.isNot(tok::comma))
607 break;
608
609 ConsumeToken();
610 } while (true);
611
612 // Closing ')'.
613 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
614 if (RParenLoc.isInvalid())
615 return;
616
617 if (endLoc)
618 *endLoc = RParenLoc;
619
Douglas Gregorb53e4172011-03-26 03:35:55 +0000620 // The 'unavailable' availability cannot be combined with any other
621 // availability changes. Make sure that hasn't happened.
622 if (UnavailableLoc.isValid()) {
623 bool Complained = false;
624 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
625 if (Changes[Index].KeywordLoc.isValid()) {
626 if (!Complained) {
627 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
628 << SourceRange(Changes[Index].KeywordLoc,
629 Changes[Index].VersionRange.getEnd());
630 Complained = true;
631 }
632
633 // Clear out the availability.
634 Changes[Index] = AvailabilityChange();
635 }
636 }
637 }
638
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000639 // Record this attribute
Douglas Gregorb53e4172011-03-26 03:35:55 +0000640 attrs.addNew(&Availability, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000641 0, SourceLocation(),
642 Platform, PlatformLoc,
643 Changes[Introduced],
644 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000645 Changes[Obsoleted],
646 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000647}
648
John McCall7f040a92010-12-24 02:08:15 +0000649void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
650 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
651 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000652}
653
Reid Spencer5f016e22007-07-11 17:01:13 +0000654/// ParseDeclaration - Parse a full 'declaration', which consists of
655/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000656/// 'Context' should be a Declarator::TheContext value. This returns the
657/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000658///
659/// declaration: [C99 6.7]
660/// block-declaration ->
661/// simple-declaration
662/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000663/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000664/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000665/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000666/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000667/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000668/// others... [FIXME]
669///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000670Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
671 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000672 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000673 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000674 ParenBraceBracketBalancer BalancerRAIIObj(*this);
675
John McCalld226f652010-08-21 09:40:31 +0000676 Decl *SingleDecl = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000677 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000678 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000679 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000680 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000681 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000682 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000683 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000684 // Could be the start of an inline namespace. Allowed as an ext in C++03.
685 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000686 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000687 SourceLocation InlineLoc = ConsumeToken();
688 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
689 break;
690 }
John McCall7f040a92010-12-24 02:08:15 +0000691 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000692 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000693 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000694 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000695 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000696 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000697 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000698 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
John McCall7f040a92010-12-24 02:08:15 +0000699 DeclEnd, attrs);
Chris Lattner682bf922009-03-29 16:50:03 +0000700 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000701 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000702 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000703 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000704 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000705 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000706 default:
John McCall7f040a92010-12-24 02:08:15 +0000707 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000708 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000709
Chris Lattner682bf922009-03-29 16:50:03 +0000710 // This routine returns a DeclGroup, if the thing we parsed only contains a
711 // single decl, convert it now.
712 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000713}
714
715/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
716/// declaration-specifiers init-declarator-list[opt] ';'
717///[C90/C++]init-declarator-list ';' [TODO]
718/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000719///
Richard Smithad762fc2011-04-14 22:09:26 +0000720/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
721/// attribute-specifier-seq[opt] type-specifier-seq declarator
722///
Chris Lattnercd147752009-03-29 17:27:48 +0000723/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000724/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000725///
726/// If FRI is non-null, we might be parsing a for-range-declaration instead
727/// of a simple-declaration. If we find that we are, we also parse the
728/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000729Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
730 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000731 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000732 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000733 bool RequireSemi,
734 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000736 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000737 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000738
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000739 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000740 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000741 StmtResult R = Actions.ActOnVlaStmt(DS);
742 if (R.isUsable())
743 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000744
Reid Spencer5f016e22007-07-11 17:01:13 +0000745 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
746 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000747 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000748 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000749 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000750 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000751 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000752 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000754
755 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000756}
Mike Stump1eb44332009-09-09 15:08:12 +0000757
John McCalld8ac0572009-11-03 19:26:08 +0000758/// ParseDeclGroup - Having concluded that this is either a function
759/// definition or a group of object declarations, actually parse the
760/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000761Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
762 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000763 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +0000764 SourceLocation *DeclEnd,
765 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +0000766 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000767 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000768 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000769
John McCalld8ac0572009-11-03 19:26:08 +0000770 // Bail out if the first declarator didn't seem well-formed.
771 if (!D.hasName() && !D.mayOmitIdentifier()) {
772 // Skip until ; or }.
773 SkipUntil(tok::r_brace, true, true);
774 if (Tok.is(tok::semi))
775 ConsumeToken();
776 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Chris Lattnerc82daef2010-07-11 22:24:20 +0000779 // Check to see if we have a function *definition* which must have a body.
780 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
781 // Look at the next token to make sure that this isn't a function
782 // declaration. We have to check this because __attribute__ might be the
783 // start of a function definition in GCC-extended K&R C.
784 !isDeclarationAfterDeclarator()) {
785
Chris Lattner004659a2010-07-11 22:42:07 +0000786 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000787 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
788 Diag(Tok, diag::err_function_declared_typedef);
789
790 // Recover by treating the 'typedef' as spurious.
791 DS.ClearStorageClassSpecs();
792 }
793
John McCalld226f652010-08-21 09:40:31 +0000794 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000795 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000796 }
797
798 if (isDeclarationSpecifier()) {
799 // If there is an invalid declaration specifier right after the function
800 // prototype, then we must be in a missing semicolon case where this isn't
801 // actually a body. Just fall through into the code that handles it as a
802 // prototype, and let the top-level code handle the erroneous declspec
803 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000804 } else {
805 Diag(Tok, diag::err_expected_fn_body);
806 SkipUntil(tok::semi);
807 return DeclGroupPtrTy();
808 }
809 }
810
Richard Smithad762fc2011-04-14 22:09:26 +0000811 if (ParseAttributesAfterDeclarator(D))
812 return DeclGroupPtrTy();
813
814 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
815 // must parse and analyze the for-range-initializer before the declaration is
816 // analyzed.
817 if (FRI && Tok.is(tok::colon)) {
818 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000819 if (Tok.is(tok::l_brace))
820 FRI->RangeExpr = ParseBraceInitializer();
821 else
822 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +0000823 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
824 Actions.ActOnCXXForRangeDecl(ThisDecl);
825 Actions.FinalizeDeclaration(ThisDecl);
826 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
827 }
828
John McCalld226f652010-08-21 09:40:31 +0000829 llvm::SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +0000830 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +0000831 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000832 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000833 DeclsInGroup.push_back(FirstDecl);
834
835 // If we don't have a comma, it is either the end of the list (a ';') or an
836 // error, bail out.
837 while (Tok.is(tok::comma)) {
838 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000839 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000840
841 // Parse the next declarator.
842 D.clear();
843
844 // Accept attributes in an init-declarator. In the first declarator in a
845 // declaration, these would be part of the declspec. In subsequent
846 // declarators, they become part of the declarator itself, so that they
847 // don't apply to declarators after *this* one. Examples:
848 // short __attribute__((common)) var; -> declspec
849 // short var __attribute__((common)); -> declarator
850 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +0000851 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +0000852
853 ParseDeclarator(D);
854
John McCalld226f652010-08-21 09:40:31 +0000855 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000856 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000857 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000858 DeclsInGroup.push_back(ThisDecl);
859 }
860
861 if (DeclEnd)
862 *DeclEnd = Tok.getLocation();
863
864 if (Context != Declarator::ForContext &&
865 ExpectAndConsume(tok::semi,
866 Context == Declarator::FileContext
867 ? diag::err_invalid_token_after_toplevel_declarator
868 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000869 // Okay, there was no semicolon and one was expected. If we see a
870 // declaration specifier, just assume it was missing and continue parsing.
871 // Otherwise things are very confused and we skip to recover.
872 if (!isDeclarationSpecifier()) {
873 SkipUntil(tok::r_brace, true, true);
874 if (Tok.is(tok::semi))
875 ConsumeToken();
876 }
John McCalld8ac0572009-11-03 19:26:08 +0000877 }
878
Douglas Gregor23c94db2010-07-02 17:43:08 +0000879 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000880 DeclsInGroup.data(),
881 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000882}
883
Richard Smithad762fc2011-04-14 22:09:26 +0000884/// Parse an optional simple-asm-expr and attributes, and attach them to a
885/// declarator. Returns true on an error.
886bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
887 // If a simple-asm-expr is present, parse it.
888 if (Tok.is(tok::kw_asm)) {
889 SourceLocation Loc;
890 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
891 if (AsmLabel.isInvalid()) {
892 SkipUntil(tok::semi, true, true);
893 return true;
894 }
895
896 D.setAsmLabel(AsmLabel.release());
897 D.SetRangeEnd(Loc);
898 }
899
900 MaybeParseGNUAttributes(D);
901 return false;
902}
903
Douglas Gregor1426e532009-05-12 21:31:51 +0000904/// \brief Parse 'declaration' after parsing 'declaration-specifiers
905/// declarator'. This method parses the remainder of the declaration
906/// (including any attributes or initializer, among other things) and
907/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000908///
Reid Spencer5f016e22007-07-11 17:01:13 +0000909/// init-declarator: [C99 6.7]
910/// declarator
911/// declarator '=' initializer
912/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
913/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000914/// [C++] declarator initializer[opt]
915///
916/// [C++] initializer:
917/// [C++] '=' initializer-clause
918/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000919/// [C++0x] '=' 'default' [TODO]
920/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000921/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +0000922///
923/// According to the standard grammar, =default and =delete are function
924/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000925///
John McCalld226f652010-08-21 09:40:31 +0000926Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000927 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +0000928 if (ParseAttributesAfterDeclarator(D))
929 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Richard Smithad762fc2011-04-14 22:09:26 +0000931 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
932}
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Richard Smithad762fc2011-04-14 22:09:26 +0000934Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
935 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000936 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000937 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000938 switch (TemplateInfo.Kind) {
939 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000940 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000941 break;
942
943 case ParsedTemplateInfo::Template:
944 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000945 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000946 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +0000947 TemplateInfo.TemplateParams->data(),
948 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000949 D);
950 break;
951
952 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000953 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000954 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000955 TemplateInfo.ExternLoc,
956 TemplateInfo.TemplateLoc,
957 D);
958 if (ThisRes.isInvalid()) {
959 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000960 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000961 }
962
963 ThisDecl = ThisRes.get();
964 break;
965 }
966 }
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Richard Smith34b41d92011-02-20 03:19:35 +0000968 bool TypeContainsAuto =
969 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
970
Douglas Gregor1426e532009-05-12 21:31:51 +0000971 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000972 if (isTokenEqualOrMistypedEqualEqual(
973 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000974 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000975 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +0000976 if (D.isFunctionDeclarator())
977 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
978 << 1 /* delete */;
979 else
980 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +0000981 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +0000982 if (D.isFunctionDeclarator())
983 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
984 << 1 /* delete */;
985 else
986 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +0000987 } else {
John McCall731ad842009-12-19 09:28:58 +0000988 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
989 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000990 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000991 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000992
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000993 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000994 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000995 ConsumeCodeCompletionToken();
996 SkipUntil(tok::comma, true, true);
997 return ThisDecl;
998 }
999
John McCall60d7b3a2010-08-24 06:29:42 +00001000 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001001
John McCall731ad842009-12-19 09:28:58 +00001002 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001003 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001004 ExitScope();
1005 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001006
Douglas Gregor1426e532009-05-12 21:31:51 +00001007 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001008 SkipUntil(tok::comma, true, true);
1009 Actions.ActOnInitializerError(ThisDecl);
1010 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001011 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1012 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001013 }
1014 } else if (Tok.is(tok::l_paren)) {
1015 // Parse C++ direct initializer: '(' expression-list ')'
1016 SourceLocation LParenLoc = ConsumeParen();
1017 ExprVector Exprs(Actions);
1018 CommaLocsTy CommaLocs;
1019
Douglas Gregorb4debae2009-12-22 17:47:17 +00001020 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1021 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001022 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001023 }
1024
Douglas Gregor1426e532009-05-12 21:31:51 +00001025 if (ParseExpressionList(Exprs, CommaLocs)) {
1026 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001027
1028 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001029 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001030 ExitScope();
1031 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001032 } else {
1033 // Match the ')'.
1034 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1035
1036 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1037 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001038
1039 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001040 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001041 ExitScope();
1042 }
1043
Douglas Gregor1426e532009-05-12 21:31:51 +00001044 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1045 move_arg(Exprs),
Richard Smith34b41d92011-02-20 03:19:35 +00001046 RParenLoc,
1047 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001048 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001049 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1050 // Parse C++0x braced-init-list.
1051 if (D.getCXXScopeSpec().isSet()) {
1052 EnterScope(0);
1053 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1054 }
1055
1056 ExprResult Init(ParseBraceInitializer());
1057
1058 if (D.getCXXScopeSpec().isSet()) {
1059 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1060 ExitScope();
1061 }
1062
1063 if (Init.isInvalid()) {
1064 Actions.ActOnInitializerError(ThisDecl);
1065 } else
1066 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1067 /*DirectInit=*/true, TypeContainsAuto);
1068
Douglas Gregor1426e532009-05-12 21:31:51 +00001069 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001070 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001071 }
1072
Richard Smith483b9f32011-02-21 20:05:19 +00001073 Actions.FinalizeDeclaration(ThisDecl);
1074
Douglas Gregor1426e532009-05-12 21:31:51 +00001075 return ThisDecl;
1076}
1077
Reid Spencer5f016e22007-07-11 17:01:13 +00001078/// ParseSpecifierQualifierList
1079/// specifier-qualifier-list:
1080/// type-specifier specifier-qualifier-list[opt]
1081/// type-qualifier specifier-qualifier-list[opt]
1082/// [GNU] attributes specifier-qualifier-list[opt]
1083///
1084void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
1085 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1086 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 // Validate declspec for type-name.
1090 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001091 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001092 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Reid Spencer5f016e22007-07-11 17:01:13 +00001095 // Issue diagnostic and remove storage class if present.
1096 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1097 if (DS.getStorageClassSpecLoc().isValid())
1098 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1099 else
1100 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1101 DS.ClearStorageClassSpecs();
1102 }
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 // Issue diagnostic and remove function specfier if present.
1105 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001106 if (DS.isInlineSpecified())
1107 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1108 if (DS.isVirtualSpecified())
1109 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1110 if (DS.isExplicitSpecified())
1111 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001112 DS.ClearFunctionSpecs();
1113 }
1114}
1115
Chris Lattnerc199ab32009-04-12 20:42:31 +00001116/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1117/// specified token is valid after the identifier in a declarator which
1118/// immediately follows the declspec. For example, these things are valid:
1119///
1120/// int x [ 4]; // direct-declarator
1121/// int x ( int y); // direct-declarator
1122/// int(int x ) // direct-declarator
1123/// int x ; // simple-declaration
1124/// int x = 17; // init-declarator-list
1125/// int x , y; // init-declarator-list
1126/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001127/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001128/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001129///
1130/// This is not, because 'x' does not immediately follow the declspec (though
1131/// ')' happens to be valid anyway).
1132/// int (x)
1133///
1134static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1135 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1136 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001137 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001138}
1139
Chris Lattnere40c2952009-04-14 21:34:55 +00001140
1141/// ParseImplicitInt - This method is called when we have an non-typename
1142/// identifier in a declspec (which normally terminates the decl spec) when
1143/// the declspec has no type specifier. In this case, the declspec is either
1144/// malformed or is "implicit int" (in K&R and C89).
1145///
1146/// This method handles diagnosing this prettily and returns false if the
1147/// declspec is done being processed. If it recovers and thinks there may be
1148/// other pieces of declspec after it, it returns true.
1149///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001150bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001151 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001152 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001153 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Chris Lattnere40c2952009-04-14 21:34:55 +00001155 SourceLocation Loc = Tok.getLocation();
1156 // If we see an identifier that is not a type name, we normally would
1157 // parse it as the identifer being declared. However, when a typename
1158 // is typo'd or the definition is not included, this will incorrectly
1159 // parse the typename as the identifier name and fall over misparsing
1160 // later parts of the diagnostic.
1161 //
1162 // As such, we try to do some look-ahead in cases where this would
1163 // otherwise be an "implicit-int" case to see if this is invalid. For
1164 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1165 // an identifier with implicit int, we'd get a parse error because the
1166 // next token is obviously invalid for a type. Parse these as a case
1167 // with an invalid type specifier.
1168 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Chris Lattnere40c2952009-04-14 21:34:55 +00001170 // Since we know that this either implicit int (which is rare) or an
1171 // error, we'd do lookahead to try to do better recovery.
1172 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1173 // If this token is valid for implicit int, e.g. "static x = 4", then
1174 // we just avoid eating the identifier, so it will be parsed as the
1175 // identifier in the declarator.
1176 return false;
1177 }
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Chris Lattnere40c2952009-04-14 21:34:55 +00001179 // Otherwise, if we don't consume this token, we are going to emit an
1180 // error anyway. Try to recover from various common problems. Check
1181 // to see if this was a reference to a tag name without a tag specified.
1182 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001183 //
1184 // C++ doesn't need this, and isTagName doesn't take SS.
1185 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001186 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001187 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Douglas Gregor23c94db2010-07-02 17:43:08 +00001189 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001190 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001191 case DeclSpec::TST_enum:
1192 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1193 case DeclSpec::TST_union:
1194 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1195 case DeclSpec::TST_struct:
1196 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1197 case DeclSpec::TST_class:
1198 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001199 }
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Chris Lattnerf4382f52009-04-14 22:17:06 +00001201 if (TagName) {
1202 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001203 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001204 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Chris Lattnerf4382f52009-04-14 22:17:06 +00001206 // Parse this as a tag as if the missing tag were present.
1207 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001208 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001209 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001210 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001211 return true;
1212 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001213 }
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Douglas Gregora786fdb2009-10-13 23:27:22 +00001215 // This is almost certainly an invalid type name. Let the action emit a
1216 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001217 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001218 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001219 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001220 // The action emitted a diagnostic, so we don't have to.
1221 if (T) {
1222 // The action has suggested that the type T could be used. Set that as
1223 // the type in the declaration specifiers, consume the would-be type
1224 // name token, and we're done.
1225 const char *PrevSpec;
1226 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001227 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001228 DS.SetRangeEnd(Tok.getLocation());
1229 ConsumeToken();
1230
1231 // There may be other declaration specifiers after this.
1232 return true;
1233 }
1234
1235 // Fall through; the action had no suggestion for us.
1236 } else {
1237 // The action did not emit a diagnostic, so emit one now.
1238 SourceRange R;
1239 if (SS) R = SS->getRange();
1240 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1241 }
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Douglas Gregora786fdb2009-10-13 23:27:22 +00001243 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001244 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001245 unsigned DiagID;
1246 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001247 DS.SetRangeEnd(Tok.getLocation());
1248 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Chris Lattnere40c2952009-04-14 21:34:55 +00001250 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1251 // avoid rippling error messages on subsequent uses of the same type,
1252 // could be useful if #include was forgotten.
1253 return false;
1254}
1255
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001256/// \brief Determine the declaration specifier context from the declarator
1257/// context.
1258///
1259/// \param Context the declarator context, which is one of the
1260/// Declarator::TheContext enumerator values.
1261Parser::DeclSpecContext
1262Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1263 if (Context == Declarator::MemberContext)
1264 return DSC_class;
1265 if (Context == Declarator::FileContext)
1266 return DSC_top_level;
1267 return DSC_normal;
1268}
1269
Reid Spencer5f016e22007-07-11 17:01:13 +00001270/// ParseDeclarationSpecifiers
1271/// declaration-specifiers: [C99 6.7]
1272/// storage-class-specifier declaration-specifiers[opt]
1273/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001274/// [C99] function-specifier declaration-specifiers[opt]
1275/// [GNU] attributes declaration-specifiers[opt]
1276///
1277/// storage-class-specifier: [C99 6.7.1]
1278/// 'typedef'
1279/// 'extern'
1280/// 'static'
1281/// 'auto'
1282/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001283/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001284/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001285/// function-specifier: [C99 6.7.4]
1286/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001287/// [C++] 'virtual'
1288/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001289/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001290/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001291/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001292
Reid Spencer5f016e22007-07-11 17:01:13 +00001293///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001294void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001295 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001296 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001297 DeclSpecContext DSContext) {
1298 if (DS.getSourceRange().isInvalid()) {
1299 DS.SetRangeStart(Tok.getLocation());
1300 DS.SetRangeEnd(Tok.getLocation());
1301 }
1302
Reid Spencer5f016e22007-07-11 17:01:13 +00001303 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001304 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001305 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001306 unsigned DiagID = 0;
1307
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001309
Reid Spencer5f016e22007-07-11 17:01:13 +00001310 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001311 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001312 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +00001313 // If this is not a declaration specifier token, we're done reading decl
1314 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001315 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001316 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001318 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001319 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001320 if (DS.hasTypeSpecifier()) {
1321 bool AllowNonIdentifiers
1322 = (getCurScope()->getFlags() & (Scope::ControlScope |
1323 Scope::BlockScope |
1324 Scope::TemplateParamScope |
1325 Scope::FunctionPrototypeScope |
1326 Scope::AtCatchScope)) == 0;
1327 bool AllowNestedNameSpecifiers
1328 = DSContext == DSC_top_level ||
1329 (DSContext == DSC_class && DS.isFriendSpecified());
1330
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001331 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1332 AllowNonIdentifiers,
1333 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001334 ConsumeCodeCompletionToken();
1335 return;
1336 }
1337
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001338 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1339 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1340 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001341 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1342 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001343 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001344 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001345 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001346 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001347
1348 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1349 ConsumeCodeCompletionToken();
1350 return;
1351 }
1352
Chris Lattner5e02c472009-01-05 00:07:25 +00001353 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001354 // C++ scope specifier. Annotate and loop, or bail out on error.
1355 if (TryAnnotateCXXScopeToken(true)) {
1356 if (!DS.hasTypeSpecifier())
1357 DS.SetTypeSpecError();
1358 goto DoneWithDeclSpec;
1359 }
John McCall2e0a7152010-03-01 18:20:46 +00001360 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1361 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001362 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001363
1364 case tok::annot_cxxscope: {
1365 if (DS.hasTypeSpecifier())
1366 goto DoneWithDeclSpec;
1367
John McCallaa87d332009-12-12 11:40:51 +00001368 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001369 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1370 Tok.getAnnotationRange(),
1371 SS);
John McCallaa87d332009-12-12 11:40:51 +00001372
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001373 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001374 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001375 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001376 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001377 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001378 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001379
1380 // C++ [class.qual]p2:
1381 // In a lookup in which the constructor is an acceptable lookup
1382 // result and the nested-name-specifier nominates a class C:
1383 //
1384 // - if the name specified after the
1385 // nested-name-specifier, when looked up in C, is the
1386 // injected-class-name of C (Clause 9), or
1387 //
1388 // - if the name specified after the nested-name-specifier
1389 // is the same as the identifier or the
1390 // simple-template-id's template-name in the last
1391 // component of the nested-name-specifier,
1392 //
1393 // the name is instead considered to name the constructor of
1394 // class C.
1395 //
1396 // Thus, if the template-name is actually the constructor
1397 // name, then the code is ill-formed; this interpretation is
1398 // reinforced by the NAD status of core issue 635.
1399 TemplateIdAnnotation *TemplateId
1400 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +00001401 if ((DSContext == DSC_top_level ||
1402 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1403 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001404 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001405 if (isConstructorDeclarator()) {
1406 // The user meant this to be an out-of-line constructor
1407 // definition, but template arguments are not allowed
1408 // there. Just allow this as a constructor; we'll
1409 // complain about it later.
1410 goto DoneWithDeclSpec;
1411 }
1412
1413 // The user meant this to name a type, but it actually names
1414 // a constructor with some extraneous template
1415 // arguments. Complain, then parse it as a type as the user
1416 // intended.
1417 Diag(TemplateId->TemplateNameLoc,
1418 diag::err_out_of_line_template_id_names_constructor)
1419 << TemplateId->Name;
1420 }
1421
John McCallaa87d332009-12-12 11:40:51 +00001422 DS.getTypeSpecScope() = SS;
1423 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001424 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001425 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001426 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001427 continue;
1428 }
1429
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001430 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001431 DS.getTypeSpecScope() = SS;
1432 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001433 if (Tok.getAnnotationValue()) {
1434 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001435 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1436 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001437 PrevSpec, DiagID, T);
1438 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001439 else
1440 DS.SetTypeSpecError();
1441 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1442 ConsumeToken(); // The typename
1443 }
1444
Douglas Gregor9135c722009-03-25 15:40:00 +00001445 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001446 goto DoneWithDeclSpec;
1447
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001448 // If we're in a context where the identifier could be a class name,
1449 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001450 if ((DSContext == DSC_top_level ||
1451 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001452 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001453 &SS)) {
1454 if (isConstructorDeclarator())
1455 goto DoneWithDeclSpec;
1456
1457 // As noted in C++ [class.qual]p2 (cited above), when the name
1458 // of the class is qualified in a context where it could name
1459 // a constructor, its a constructor name. However, we've
1460 // looked at the declarator, and the user probably meant this
1461 // to be a type. Complain that it isn't supposed to be treated
1462 // as a type, then proceed to parse it as a type.
1463 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1464 << Next.getIdentifierInfo();
1465 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001466
John McCallb3d87482010-08-24 05:47:05 +00001467 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1468 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001469 getCurScope(), &SS,
1470 false, false, ParsedType(),
1471 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001472
Chris Lattnerf4382f52009-04-14 22:17:06 +00001473 // If the referenced identifier is not a type, then this declspec is
1474 // erroneous: We already checked about that it has no type specifier, and
1475 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001476 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001477 if (TypeRep == 0) {
1478 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001479 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001480 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001481 }
Mike Stump1eb44332009-09-09 15:08:12 +00001482
John McCallaa87d332009-12-12 11:40:51 +00001483 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001484 ConsumeToken(); // The C++ scope.
1485
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001486 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001487 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001488 if (isInvalid)
1489 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001491 DS.SetRangeEnd(Tok.getLocation());
1492 ConsumeToken(); // The typename.
1493
1494 continue;
1495 }
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Chris Lattner80d0c892009-01-21 19:48:37 +00001497 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001498 if (Tok.getAnnotationValue()) {
1499 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001500 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001501 DiagID, T);
1502 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001503 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001504
1505 if (isInvalid)
1506 break;
1507
Chris Lattner80d0c892009-01-21 19:48:37 +00001508 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1509 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Chris Lattner80d0c892009-01-21 19:48:37 +00001511 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1512 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001513 // Objective-C interface.
1514 if (Tok.is(tok::less) && getLang().ObjC1)
1515 ParseObjCProtocolQualifiers(DS);
1516
Chris Lattner80d0c892009-01-21 19:48:37 +00001517 continue;
1518 }
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Douglas Gregorbfad9152011-04-28 15:48:45 +00001520 case tok::kw___is_signed:
1521 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1522 // typically treats it as a trait. If we see __is_signed as it appears
1523 // in libstdc++, e.g.,
1524 //
1525 // static const bool __is_signed;
1526 //
1527 // then treat __is_signed as an identifier rather than as a keyword.
1528 if (DS.getTypeSpecType() == TST_bool &&
1529 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1530 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1531 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1532 Tok.setKind(tok::identifier);
1533 }
1534
1535 // We're done with the declaration-specifiers.
1536 goto DoneWithDeclSpec;
1537
Chris Lattner3bd934a2008-07-26 01:18:38 +00001538 // typedef-name
1539 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001540 // In C++, check to see if this is a scope specifier like foo::bar::, if
1541 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001542 if (getLang().CPlusPlus) {
1543 if (TryAnnotateCXXScopeToken(true)) {
1544 if (!DS.hasTypeSpecifier())
1545 DS.SetTypeSpecError();
1546 goto DoneWithDeclSpec;
1547 }
1548 if (!Tok.is(tok::identifier))
1549 continue;
1550 }
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Chris Lattner3bd934a2008-07-26 01:18:38 +00001552 // This identifier can only be a typedef name if we haven't already seen
1553 // a type-specifier. Without this check we misparse:
1554 // typedef int X; struct Y { short X; }; as 'short int'.
1555 if (DS.hasTypeSpecifier())
1556 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001557
John Thompson82287d12010-02-05 00:12:22 +00001558 // Check for need to substitute AltiVec keyword tokens.
1559 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1560 break;
1561
Chris Lattner3bd934a2008-07-26 01:18:38 +00001562 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001563 ParsedType TypeRep =
1564 Actions.getTypeName(*Tok.getIdentifierInfo(),
1565 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001566
Chris Lattnerc199ab32009-04-12 20:42:31 +00001567 // If this is not a typedef name, don't parse it as part of the declspec,
1568 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001569 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001570 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001571 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001572 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001573
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001574 // If we're in a context where the identifier could be a class name,
1575 // check whether this is a constructor declaration.
1576 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001577 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001578 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001579 goto DoneWithDeclSpec;
1580
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001581 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001582 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001583 if (isInvalid)
1584 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Chris Lattner3bd934a2008-07-26 01:18:38 +00001586 DS.SetRangeEnd(Tok.getLocation());
1587 ConsumeToken(); // The identifier
1588
1589 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1590 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001591 // Objective-C interface.
1592 if (Tok.is(tok::less) && getLang().ObjC1)
1593 ParseObjCProtocolQualifiers(DS);
1594
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001595 // Need to support trailing type qualifiers (e.g. "id<p> const").
1596 // If a type specifier follows, it will be diagnosed elsewhere.
1597 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001598 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001599
1600 // type-name
1601 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001602 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001603 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001604 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001605 // This template-id does not refer to a type name, so we're
1606 // done with the type-specifiers.
1607 goto DoneWithDeclSpec;
1608 }
1609
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001610 // If we're in a context where the template-id could be a
1611 // constructor name or specialization, check whether this is a
1612 // constructor declaration.
1613 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001614 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001615 isConstructorDeclarator())
1616 goto DoneWithDeclSpec;
1617
Douglas Gregor39a8de12009-02-25 19:37:18 +00001618 // Turn the template-id annotation token into a type annotation
1619 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001620 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001621 continue;
1622 }
1623
Reid Spencer5f016e22007-07-11 17:01:13 +00001624 // GNU attributes support.
1625 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001626 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001627 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001628
1629 // Microsoft declspec support.
1630 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001631 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001632 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Steve Naroff239f0732008-12-25 14:16:32 +00001634 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001635 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001636 // FIXME: Add handling here!
1637 break;
1638
1639 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001640 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001641 case tok::kw___cdecl:
1642 case tok::kw___stdcall:
1643 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001644 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001645 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001646 continue;
1647
Dawn Perchik52fc3142010-09-03 01:29:35 +00001648 // Borland single token adornments.
1649 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001650 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001651 continue;
1652
Peter Collingbournef315fa82011-02-14 01:42:53 +00001653 // OpenCL single token adornments.
1654 case tok::kw___kernel:
1655 ParseOpenCLAttributes(DS.getAttributes());
1656 continue;
1657
Reid Spencer5f016e22007-07-11 17:01:13 +00001658 // storage-class-specifier
1659 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001660 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001661 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001662 break;
1663 case tok::kw_extern:
1664 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001665 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001666 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001667 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001668 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001669 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001670 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001671 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001672 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 case tok::kw_static:
1674 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001675 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001676 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001677 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001678 break;
1679 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001680 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001681 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1682 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1683 DiagID, getLang());
1684 if (!isInvalid)
1685 Diag(Tok, diag::auto_storage_class)
1686 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1687 }
1688 else
1689 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1690 DiagID);
1691 }
Anders Carlssone89d1592009-06-26 18:41:36 +00001692 else
John McCallfec54012009-08-03 20:12:06 +00001693 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001694 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001695 break;
1696 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001697 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001698 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001699 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001700 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001701 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001702 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001703 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001705 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001706 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001707
Reid Spencer5f016e22007-07-11 17:01:13 +00001708 // function-specifier
1709 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001710 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001711 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001712 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001713 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001714 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001715 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001716 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001717 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001718
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001719 // friend
1720 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001721 if (DSContext == DSC_class)
1722 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1723 else {
1724 PrevSpec = ""; // not actually used by the diagnostic
1725 DiagID = diag::err_friend_invalid_in_context;
1726 isInvalid = true;
1727 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001728 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Sebastian Redl2ac67232009-11-05 15:47:02 +00001730 // constexpr
1731 case tok::kw_constexpr:
1732 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1733 break;
1734
Chris Lattner80d0c892009-01-21 19:48:37 +00001735 // type-specifier
1736 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001737 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1738 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001739 break;
1740 case tok::kw_long:
1741 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001742 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1743 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001744 else
John McCallfec54012009-08-03 20:12:06 +00001745 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1746 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001747 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001748 case tok::kw___int64:
1749 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1750 DiagID);
1751 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001752 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001753 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1754 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001755 break;
1756 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001757 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1758 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001759 break;
1760 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001761 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1762 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001763 break;
1764 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001765 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1766 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001767 break;
1768 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001769 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1770 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001771 break;
1772 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001773 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1774 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001775 break;
1776 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001777 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1778 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001779 break;
1780 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001781 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1782 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001783 break;
1784 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001785 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1786 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001787 break;
1788 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001789 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1790 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001791 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001792 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001793 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1794 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001795 break;
1796 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001797 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1798 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001799 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001800 case tok::kw_bool:
1801 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001802 if (Tok.is(tok::kw_bool) &&
1803 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1804 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1805 PrevSpec = ""; // Not used by the diagnostic.
1806 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001807 // For better error recovery.
1808 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001809 isInvalid = true;
1810 } else {
1811 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1812 DiagID);
1813 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001814 break;
1815 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001816 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1817 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001818 break;
1819 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001820 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1821 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001822 break;
1823 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001824 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1825 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001826 break;
John Thompson82287d12010-02-05 00:12:22 +00001827 case tok::kw___vector:
1828 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1829 break;
1830 case tok::kw___pixel:
1831 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1832 break;
John McCalla5fc4722011-04-09 22:50:59 +00001833 case tok::kw___unknown_anytype:
1834 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1835 PrevSpec, DiagID);
1836 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001837
1838 // class-specifier:
1839 case tok::kw_class:
1840 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001841 case tok::kw_union: {
1842 tok::TokenKind Kind = Tok.getKind();
1843 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001844 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001845 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001846 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001847
1848 // enum-specifier:
1849 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001850 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001851 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001852 continue;
1853
1854 // cv-qualifier:
1855 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001856 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1857 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001858 break;
1859 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001860 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1861 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001862 break;
1863 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001864 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1865 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001866 break;
1867
Douglas Gregord57959a2009-03-27 23:10:48 +00001868 // C++ typename-specifier:
1869 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001870 if (TryAnnotateTypeOrScopeToken()) {
1871 DS.SetTypeSpecError();
1872 goto DoneWithDeclSpec;
1873 }
1874 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001875 continue;
1876 break;
1877
Chris Lattner80d0c892009-01-21 19:48:37 +00001878 // GNU typeof support.
1879 case tok::kw_typeof:
1880 ParseTypeofSpecifier(DS);
1881 continue;
1882
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001883 case tok::kw_decltype:
1884 ParseDecltypeSpecifier(DS);
1885 continue;
1886
Sean Huntdb5d44b2011-05-19 05:37:45 +00001887 case tok::kw___underlying_type:
1888 ParseUnderlyingTypeSpecifier(DS);
1889
Peter Collingbourne207f4d82011-03-18 22:38:29 +00001890 // OpenCL qualifiers:
1891 case tok::kw_private:
1892 if (!getLang().OpenCL)
1893 goto DoneWithDeclSpec;
1894 case tok::kw___private:
1895 case tok::kw___global:
1896 case tok::kw___local:
1897 case tok::kw___constant:
1898 case tok::kw___read_only:
1899 case tok::kw___write_only:
1900 case tok::kw___read_write:
1901 ParseOpenCLQualifiers(DS);
1902 break;
1903
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001904 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001905 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001906 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1907 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001908 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001909 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Douglas Gregor46f936e2010-11-19 17:10:50 +00001911 if (!ParseObjCProtocolQualifiers(DS))
1912 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1913 << FixItHint::CreateInsertion(Loc, "id")
1914 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001915
1916 // Need to support trailing type qualifiers (e.g. "id<p> const").
1917 // If a type specifier follows, it will be diagnosed elsewhere.
1918 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001919 }
John McCallfec54012009-08-03 20:12:06 +00001920 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001921 if (isInvalid) {
1922 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001923 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001924
1925 if (DiagID == diag::ext_duplicate_declspec)
1926 Diag(Tok, DiagID)
1927 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1928 else
1929 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001930 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001931
Chris Lattner81c018d2008-03-13 06:29:04 +00001932 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001933 if (DiagID != diag::err_bool_redeclaration)
1934 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001935 }
1936}
Douglas Gregoradcac882008-12-01 23:54:00 +00001937
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001938/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001939/// primarily follow the C++ grammar with additions for C99 and GNU,
1940/// which together subsume the C grammar. Note that the C++
1941/// type-specifier also includes the C type-qualifier (for const,
1942/// volatile, and C99 restrict). Returns true if a type-specifier was
1943/// found (and parsed), false otherwise.
1944///
1945/// type-specifier: [C++ 7.1.5]
1946/// simple-type-specifier
1947/// class-specifier
1948/// enum-specifier
1949/// elaborated-type-specifier [TODO]
1950/// cv-qualifier
1951///
1952/// cv-qualifier: [C++ 7.1.5.1]
1953/// 'const'
1954/// 'volatile'
1955/// [C99] 'restrict'
1956///
1957/// simple-type-specifier: [ C++ 7.1.5.2]
1958/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1959/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1960/// 'char'
1961/// 'wchar_t'
1962/// 'bool'
1963/// 'short'
1964/// 'int'
1965/// 'long'
1966/// 'signed'
1967/// 'unsigned'
1968/// 'float'
1969/// 'double'
1970/// 'void'
1971/// [C99] '_Bool'
1972/// [C99] '_Complex'
1973/// [C99] '_Imaginary' // Removed in TC2?
1974/// [GNU] '_Decimal32'
1975/// [GNU] '_Decimal64'
1976/// [GNU] '_Decimal128'
1977/// [GNU] typeof-specifier
1978/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1979/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001980/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001981/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001982bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001983 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001984 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001985 const ParsedTemplateInfo &TemplateInfo,
1986 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001987 SourceLocation Loc = Tok.getLocation();
1988
1989 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001990 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001991 // If we already have a type specifier, this identifier is not a type.
1992 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1993 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1994 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1995 return false;
John Thompson82287d12010-02-05 00:12:22 +00001996 // Check for need to substitute AltiVec keyword tokens.
1997 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1998 break;
1999 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002000 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002001 // Annotate typenames and C++ scope specifiers. If we get one, just
2002 // recurse to handle whatever we get.
2003 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002004 return true;
2005 if (Tok.is(tok::identifier))
2006 return false;
2007 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2008 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002009 case tok::coloncolon: // ::foo::bar
2010 if (NextToken().is(tok::kw_new) || // ::new
2011 NextToken().is(tok::kw_delete)) // ::delete
2012 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002013
Chris Lattner166a8fc2009-01-04 23:41:41 +00002014 // Annotate typenames and C++ scope specifiers. If we get one, just
2015 // recurse to handle whatever we get.
2016 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002017 return true;
2018 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2019 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Douglas Gregor12e083c2008-11-07 15:42:26 +00002021 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002022 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002023 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002024 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2025 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002026 DiagID, T);
2027 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002028 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002029 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2030 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002031
Douglas Gregor12e083c2008-11-07 15:42:26 +00002032 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2033 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2034 // Objective-C interface. If we don't have Objective-C or a '<', this is
2035 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002036 if (Tok.is(tok::less) && getLang().ObjC1)
2037 ParseObjCProtocolQualifiers(DS);
2038
Douglas Gregor12e083c2008-11-07 15:42:26 +00002039 return true;
2040 }
2041
2042 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002043 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002044 break;
2045 case tok::kw_long:
2046 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002047 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2048 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002049 else
John McCallfec54012009-08-03 20:12:06 +00002050 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2051 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002052 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002053 case tok::kw___int64:
2054 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2055 DiagID);
2056 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002057 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002058 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002059 break;
2060 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002061 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2062 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002063 break;
2064 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002065 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2066 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002067 break;
2068 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002069 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2070 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002071 break;
2072 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002073 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002074 break;
2075 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002076 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002077 break;
2078 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002079 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002080 break;
2081 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002082 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002083 break;
2084 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002085 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002086 break;
2087 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002088 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002089 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002090 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002091 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002092 break;
2093 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002094 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002095 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002096 case tok::kw_bool:
2097 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002098 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002099 break;
2100 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002101 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2102 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002103 break;
2104 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002105 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2106 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002107 break;
2108 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002109 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2110 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002111 break;
John Thompson82287d12010-02-05 00:12:22 +00002112 case tok::kw___vector:
2113 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2114 break;
2115 case tok::kw___pixel:
2116 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2117 break;
2118
Douglas Gregor12e083c2008-11-07 15:42:26 +00002119 // class-specifier:
2120 case tok::kw_class:
2121 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002122 case tok::kw_union: {
2123 tok::TokenKind Kind = Tok.getKind();
2124 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002125 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2126 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002127 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002128 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002129
2130 // enum-specifier:
2131 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002132 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002133 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002134 return true;
2135
2136 // cv-qualifier:
2137 case tok::kw_const:
2138 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002139 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002140 break;
2141 case tok::kw_volatile:
2142 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002143 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002144 break;
2145 case tok::kw_restrict:
2146 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002147 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002148 break;
2149
2150 // GNU typeof support.
2151 case tok::kw_typeof:
2152 ParseTypeofSpecifier(DS);
2153 return true;
2154
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002155 // C++0x decltype support.
2156 case tok::kw_decltype:
2157 ParseDecltypeSpecifier(DS);
2158 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002159
Sean Huntdb5d44b2011-05-19 05:37:45 +00002160 // C++0x type traits support.
2161 case tok::kw___underlying_type:
2162 ParseUnderlyingTypeSpecifier(DS);
2163 return true;
2164
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002165 // OpenCL qualifiers:
2166 case tok::kw_private:
2167 if (!getLang().OpenCL)
2168 return false;
2169 case tok::kw___private:
2170 case tok::kw___global:
2171 case tok::kw___local:
2172 case tok::kw___constant:
2173 case tok::kw___read_only:
2174 case tok::kw___write_only:
2175 case tok::kw___read_write:
2176 ParseOpenCLQualifiers(DS);
2177 break;
2178
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002179 // C++0x auto support.
2180 case tok::kw_auto:
2181 if (!getLang().CPlusPlus0x)
2182 return false;
2183
John McCallfec54012009-08-03 20:12:06 +00002184 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002185 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002186
Eli Friedman290eeb02009-06-08 23:27:34 +00002187 case tok::kw___ptr64:
2188 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002189 case tok::kw___cdecl:
2190 case tok::kw___stdcall:
2191 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002192 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00002193 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002194 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002195
Dawn Perchik52fc3142010-09-03 01:29:35 +00002196 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002197 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002198 return true;
2199
Douglas Gregor12e083c2008-11-07 15:42:26 +00002200 default:
2201 // Not a type-specifier; do nothing.
2202 return false;
2203 }
2204
2205 // If the specifier combination wasn't legal, issue a diagnostic.
2206 if (isInvalid) {
2207 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002208 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002209 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002210 }
2211 DS.SetRangeEnd(Tok.getLocation());
2212 ConsumeToken(); // whatever we parsed above.
2213 return true;
2214}
Reid Spencer5f016e22007-07-11 17:01:13 +00002215
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002216/// ParseStructDeclaration - Parse a struct declaration without the terminating
2217/// semicolon.
2218///
Reid Spencer5f016e22007-07-11 17:01:13 +00002219/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002220/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002221/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002222/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002223/// struct-declarator-list:
2224/// struct-declarator
2225/// struct-declarator-list ',' struct-declarator
2226/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2227/// struct-declarator:
2228/// declarator
2229/// [GNU] declarator attributes[opt]
2230/// declarator[opt] ':' constant-expression
2231/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2232///
Chris Lattnere1359422008-04-10 06:46:29 +00002233void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002234ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002235 if (Tok.is(tok::kw___extension__)) {
2236 // __extension__ silences extension warnings in the subexpression.
2237 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002238 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002239 return ParseStructDeclaration(DS, Fields);
2240 }
Mike Stump1eb44332009-09-09 15:08:12 +00002241
Steve Naroff28a7ca82007-08-20 22:28:22 +00002242 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002243 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002244
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002245 // If there are no declarators, this is a free-standing declaration
2246 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002247 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002248 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002249 return;
2250 }
2251
2252 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002253 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002254 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002255 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002256 FieldDeclarator DeclaratorInfo(DS);
2257
2258 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002259 if (!FirstDeclarator)
2260 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002261
Steve Naroff28a7ca82007-08-20 22:28:22 +00002262 /// struct-declarator: declarator
2263 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002264 if (Tok.isNot(tok::colon)) {
2265 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2266 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002267 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002268 }
Mike Stump1eb44332009-09-09 15:08:12 +00002269
Chris Lattner04d66662007-10-09 17:33:22 +00002270 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002271 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002272 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002273 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002274 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002275 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002276 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002277 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002278
Steve Naroff28a7ca82007-08-20 22:28:22 +00002279 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002280 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002281
John McCallbdd563e2009-11-03 02:38:08 +00002282 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002283 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002284 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002285
Steve Naroff28a7ca82007-08-20 22:28:22 +00002286 // If we don't have a comma, it is either the end of the list (a ';')
2287 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002288 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002289 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002290
Steve Naroff28a7ca82007-08-20 22:28:22 +00002291 // Consume the comma.
2292 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002293
John McCallbdd563e2009-11-03 02:38:08 +00002294 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002295 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002296}
2297
2298/// ParseStructUnionBody
2299/// struct-contents:
2300/// struct-declaration-list
2301/// [EXT] empty
2302/// [GNU] "struct-declaration-list" without terminatoring ';'
2303/// struct-declaration-list:
2304/// struct-declaration
2305/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002306/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002307///
Reid Spencer5f016e22007-07-11 17:01:13 +00002308void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002309 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002310 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2311 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002312
Reid Spencer5f016e22007-07-11 17:01:13 +00002313 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002314
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002315 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002316 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002317
Reid Spencer5f016e22007-07-11 17:01:13 +00002318 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2319 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002320 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002321 Diag(Tok, diag::ext_empty_struct_union)
2322 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002323
John McCalld226f652010-08-21 09:40:31 +00002324 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002325
Reid Spencer5f016e22007-07-11 17:01:13 +00002326 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002327 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002328 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002329
Reid Spencer5f016e22007-07-11 17:01:13 +00002330 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002331 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002332 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002333 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002334 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002335 ConsumeToken();
2336 continue;
2337 }
Chris Lattnere1359422008-04-10 06:46:29 +00002338
2339 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002340 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002341
John McCallbdd563e2009-11-03 02:38:08 +00002342 if (!Tok.is(tok::at)) {
2343 struct CFieldCallback : FieldCallback {
2344 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002345 Decl *TagDecl;
2346 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002347
John McCalld226f652010-08-21 09:40:31 +00002348 CFieldCallback(Parser &P, Decl *TagDecl,
2349 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002350 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2351
John McCalld226f652010-08-21 09:40:31 +00002352 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002353 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002354 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002355 FD.D.getDeclSpec().getSourceRange().getBegin(),
2356 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002357 FieldDecls.push_back(Field);
2358 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002359 }
John McCallbdd563e2009-11-03 02:38:08 +00002360 } Callback(*this, TagDecl, FieldDecls);
2361
2362 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002363 } else { // Handle @defs
2364 ConsumeToken();
2365 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2366 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002367 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002368 continue;
2369 }
2370 ConsumeToken();
2371 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2372 if (!Tok.is(tok::identifier)) {
2373 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002374 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002375 continue;
2376 }
John McCalld226f652010-08-21 09:40:31 +00002377 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002378 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002379 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002380 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2381 ConsumeToken();
2382 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002383 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002384
Chris Lattner04d66662007-10-09 17:33:22 +00002385 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002386 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002387 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002388 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002389 break;
2390 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002391 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2392 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002393 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002394 // If we stopped at a ';', eat it.
2395 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002396 }
2397 }
Mike Stump1eb44332009-09-09 15:08:12 +00002398
Steve Naroff60fccee2007-10-29 21:38:07 +00002399 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002400
John McCall0b7e6782011-03-24 11:26:52 +00002401 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002402 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002403 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002404
Douglas Gregor23c94db2010-07-02 17:43:08 +00002405 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00002406 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002407 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00002408 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002409 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002410 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002411}
2412
Reid Spencer5f016e22007-07-11 17:01:13 +00002413/// ParseEnumSpecifier
2414/// enum-specifier: [C99 6.7.2.2]
2415/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002416///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002417/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2418/// '}' attributes[opt]
2419/// 'enum' identifier
2420/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002421///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002422/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2423/// [C++0x] enum-head '{' enumerator-list ',' '}'
2424///
2425/// enum-head: [C++0x]
2426/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2427/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2428///
2429/// enum-key: [C++0x]
2430/// 'enum'
2431/// 'enum' 'class'
2432/// 'enum' 'struct'
2433///
2434/// enum-base: [C++0x]
2435/// ':' type-specifier-seq
2436///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002437/// [C++] elaborated-type-specifier:
2438/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2439///
Chris Lattner4c97d762009-04-12 21:49:30 +00002440void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002441 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002442 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002443 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002444 if (Tok.is(tok::code_completion)) {
2445 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002446 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00002447 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00002448 }
2449
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002450 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002451 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002452 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002453
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002454 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002455 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00002456 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002457 return;
2458
2459 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002460 Diag(Tok, diag::err_expected_ident);
2461 if (Tok.isNot(tok::l_brace)) {
2462 // Has no name and is not a definition.
2463 // Skip the rest of this declarator, up until the comma or semicolon.
2464 SkipUntil(tok::comma, true);
2465 return;
2466 }
2467 }
2468 }
Mike Stump1eb44332009-09-09 15:08:12 +00002469
Douglas Gregor86f208c2011-02-22 20:32:04 +00002470 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002471 bool IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002472 bool IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002473
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002474 if (getLang().CPlusPlus0x &&
2475 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002476 IsScopedEnum = true;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002477 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2478 ConsumeToken();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002479 }
2480
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002481 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002482 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2483 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002484 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002485
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002486 // Skip the rest of this declarator, up until the comma or semicolon.
2487 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002488 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002489 }
Mike Stump1eb44332009-09-09 15:08:12 +00002490
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002491 // If an identifier is present, consume and remember it.
2492 IdentifierInfo *Name = 0;
2493 SourceLocation NameLoc;
2494 if (Tok.is(tok::identifier)) {
2495 Name = Tok.getIdentifierInfo();
2496 NameLoc = ConsumeToken();
2497 }
Mike Stump1eb44332009-09-09 15:08:12 +00002498
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002499 if (!Name && IsScopedEnum) {
2500 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2501 // declaration of a scoped enumeration.
2502 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2503 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002504 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002505 }
2506
2507 TypeResult BaseType;
2508
Douglas Gregora61b3e72010-12-01 17:42:47 +00002509 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002510 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002511 bool PossibleBitfield = false;
2512 if (getCurScope()->getFlags() & Scope::ClassScope) {
2513 // If we're in class scope, this can either be an enum declaration with
2514 // an underlying type, or a declaration of a bitfield member. We try to
2515 // use a simple disambiguation scheme first to catch the common cases
2516 // (integer literal, sizeof); if it's still ambiguous, we then consider
2517 // anything that's a simple-type-specifier followed by '(' as an
2518 // expression. This suffices because function types are not valid
2519 // underlying types anyway.
2520 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2521 // If the next token starts an expression, we know we're parsing a
2522 // bit-field. This is the common case.
2523 if (TPR == TPResult::True())
2524 PossibleBitfield = true;
2525 // If the next token starts a type-specifier-seq, it may be either a
2526 // a fixed underlying type or the start of a function-style cast in C++;
2527 // lookahead one more token to see if it's obvious that we have a
2528 // fixed underlying type.
2529 else if (TPR == TPResult::False() &&
2530 GetLookAheadToken(2).getKind() == tok::semi) {
2531 // Consume the ':'.
2532 ConsumeToken();
2533 } else {
2534 // We have the start of a type-specifier-seq, so we have to perform
2535 // tentative parsing to determine whether we have an expression or a
2536 // type.
2537 TentativeParsingAction TPA(*this);
2538
2539 // Consume the ':'.
2540 ConsumeToken();
2541
Douglas Gregor86f208c2011-02-22 20:32:04 +00002542 if ((getLang().CPlusPlus &&
2543 isCXXDeclarationSpecifier() != TPResult::True()) ||
2544 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002545 // We'll parse this as a bitfield later.
2546 PossibleBitfield = true;
2547 TPA.Revert();
2548 } else {
2549 // We have a type-specifier-seq.
2550 TPA.Commit();
2551 }
2552 }
2553 } else {
2554 // Consume the ':'.
2555 ConsumeToken();
2556 }
2557
2558 if (!PossibleBitfield) {
2559 SourceRange Range;
2560 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002561
2562 if (!getLang().CPlusPlus0x)
2563 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2564 << Range;
Douglas Gregora61b3e72010-12-01 17:42:47 +00002565 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002566 }
2567
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002568 // There are three options here. If we have 'enum foo;', then this is a
2569 // forward declaration. If we have 'enum foo {...' then this is a
2570 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2571 //
2572 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2573 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2574 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2575 //
John McCallf312b1e2010-08-26 23:41:50 +00002576 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002577 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002578 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002579 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002580 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002581 else
John McCallf312b1e2010-08-26 23:41:50 +00002582 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002583
2584 // enums cannot be templates, although they can be referenced from a
2585 // template.
2586 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002587 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002588 Diag(Tok, diag::err_enum_template);
2589
2590 // Skip the rest of this declarator, up until the comma or semicolon.
2591 SkipUntil(tok::comma, true);
2592 return;
2593 }
2594
Douglas Gregorb9075602011-02-22 02:55:24 +00002595 if (!Name && TUK != Sema::TUK_Definition) {
2596 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2597
2598 // Skip the rest of this declarator, up until the comma or semicolon.
2599 SkipUntil(tok::comma, true);
2600 return;
2601 }
2602
Douglas Gregor402abb52009-05-28 23:31:59 +00002603 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002604 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002605 const char *PrevSpec = 0;
2606 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002607 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002608 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002609 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002610 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002611 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002612 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002613
Douglas Gregor48c89f42010-04-24 16:38:41 +00002614 if (IsDependent) {
2615 // This enum has a dependent nested-name-specifier. Handle it as a
2616 // dependent tag.
2617 if (!Name) {
2618 DS.SetTypeSpecError();
2619 Diag(Tok, diag::err_expected_type_name_after_typename);
2620 return;
2621 }
2622
Douglas Gregor23c94db2010-07-02 17:43:08 +00002623 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002624 TUK, SS, Name, StartLoc,
2625 NameLoc);
2626 if (Type.isInvalid()) {
2627 DS.SetTypeSpecError();
2628 return;
2629 }
2630
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002631 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2632 NameLoc.isValid() ? NameLoc : StartLoc,
2633 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002634 Diag(StartLoc, DiagID) << PrevSpec;
2635
2636 return;
2637 }
Mike Stump1eb44332009-09-09 15:08:12 +00002638
John McCalld226f652010-08-21 09:40:31 +00002639 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002640 // The action failed to produce an enumeration tag. If this is a
2641 // definition, consume the entire definition.
2642 if (Tok.is(tok::l_brace)) {
2643 ConsumeBrace();
2644 SkipUntil(tok::r_brace);
2645 }
2646
2647 DS.SetTypeSpecError();
2648 return;
2649 }
2650
Chris Lattner04d66662007-10-09 17:33:22 +00002651 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002652 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002653
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002654 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2655 NameLoc.isValid() ? NameLoc : StartLoc,
2656 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002657 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002658}
2659
2660/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2661/// enumerator-list:
2662/// enumerator
2663/// enumerator-list ',' enumerator
2664/// enumerator:
2665/// enumeration-constant
2666/// enumeration-constant '=' constant-expression
2667/// enumeration-constant:
2668/// identifier
2669///
John McCalld226f652010-08-21 09:40:31 +00002670void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002671 // Enter the scope of the enum body and start the definition.
2672 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002673 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002674
Reid Spencer5f016e22007-07-11 17:01:13 +00002675 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002676
Chris Lattner7946dd32007-08-27 17:24:30 +00002677 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002678 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002679 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002680
John McCalld226f652010-08-21 09:40:31 +00002681 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002682
John McCalld226f652010-08-21 09:40:31 +00002683 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002684
Reid Spencer5f016e22007-07-11 17:01:13 +00002685 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002686 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002687 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2688 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002689
John McCall5b629aa2010-10-22 23:36:17 +00002690 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002691 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002692 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002693
Reid Spencer5f016e22007-07-11 17:01:13 +00002694 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002695 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002696 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002697 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002698 AssignedVal = ParseConstantExpression();
2699 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002700 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002701 }
Mike Stump1eb44332009-09-09 15:08:12 +00002702
Reid Spencer5f016e22007-07-11 17:01:13 +00002703 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002704 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2705 LastEnumConstDecl,
2706 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002707 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002708 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002709 EnumConstantDecls.push_back(EnumConstDecl);
2710 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002711
Douglas Gregor751f6922010-09-07 14:51:08 +00002712 if (Tok.is(tok::identifier)) {
2713 // We're missing a comma between enumerators.
2714 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2715 Diag(Loc, diag::err_enumerator_list_missing_comma)
2716 << FixItHint::CreateInsertion(Loc, ", ");
2717 continue;
2718 }
2719
Chris Lattner04d66662007-10-09 17:33:22 +00002720 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002721 break;
2722 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002723
2724 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002725 !(getLang().C99 || getLang().CPlusPlus0x))
2726 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2727 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002728 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002729 }
Mike Stump1eb44332009-09-09 15:08:12 +00002730
Reid Spencer5f016e22007-07-11 17:01:13 +00002731 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002732 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002733
Reid Spencer5f016e22007-07-11 17:01:13 +00002734 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002735 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002736 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002737
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002738 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2739 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002740 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002741
Douglas Gregor72de6672009-01-08 20:45:30 +00002742 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002743 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002744}
2745
2746/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002747/// start of a type-qualifier-list.
2748bool Parser::isTypeQualifier() const {
2749 switch (Tok.getKind()) {
2750 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002751
2752 // type-qualifier only in OpenCL
2753 case tok::kw_private:
2754 return getLang().OpenCL;
2755
Steve Naroff5f8aa692008-02-11 23:15:56 +00002756 // type-qualifier
2757 case tok::kw_const:
2758 case tok::kw_volatile:
2759 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002760 case tok::kw___private:
2761 case tok::kw___local:
2762 case tok::kw___global:
2763 case tok::kw___constant:
2764 case tok::kw___read_only:
2765 case tok::kw___read_write:
2766 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00002767 return true;
2768 }
2769}
2770
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002771/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2772/// is definitely a type-specifier. Return false if it isn't part of a type
2773/// specifier or if we're not sure.
2774bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2775 switch (Tok.getKind()) {
2776 default: return false;
2777 // type-specifiers
2778 case tok::kw_short:
2779 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002780 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002781 case tok::kw_signed:
2782 case tok::kw_unsigned:
2783 case tok::kw__Complex:
2784 case tok::kw__Imaginary:
2785 case tok::kw_void:
2786 case tok::kw_char:
2787 case tok::kw_wchar_t:
2788 case tok::kw_char16_t:
2789 case tok::kw_char32_t:
2790 case tok::kw_int:
2791 case tok::kw_float:
2792 case tok::kw_double:
2793 case tok::kw_bool:
2794 case tok::kw__Bool:
2795 case tok::kw__Decimal32:
2796 case tok::kw__Decimal64:
2797 case tok::kw__Decimal128:
2798 case tok::kw___vector:
2799
2800 // struct-or-union-specifier (C99) or class-specifier (C++)
2801 case tok::kw_class:
2802 case tok::kw_struct:
2803 case tok::kw_union:
2804 // enum-specifier
2805 case tok::kw_enum:
2806
2807 // typedef-name
2808 case tok::annot_typename:
2809 return true;
2810 }
2811}
2812
Steve Naroff5f8aa692008-02-11 23:15:56 +00002813/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002814/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002815bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002816 switch (Tok.getKind()) {
2817 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002818
Chris Lattner166a8fc2009-01-04 23:41:41 +00002819 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002820 if (TryAltiVecVectorToken())
2821 return true;
2822 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002823 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002824 // Annotate typenames and C++ scope specifiers. If we get one, just
2825 // recurse to handle whatever we get.
2826 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002827 return true;
2828 if (Tok.is(tok::identifier))
2829 return false;
2830 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002831
Chris Lattner166a8fc2009-01-04 23:41:41 +00002832 case tok::coloncolon: // ::foo::bar
2833 if (NextToken().is(tok::kw_new) || // ::new
2834 NextToken().is(tok::kw_delete)) // ::delete
2835 return false;
2836
Chris Lattner166a8fc2009-01-04 23:41:41 +00002837 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002838 return true;
2839 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002840
Reid Spencer5f016e22007-07-11 17:01:13 +00002841 // GNU attributes support.
2842 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002843 // GNU typeof support.
2844 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002845
Reid Spencer5f016e22007-07-11 17:01:13 +00002846 // type-specifiers
2847 case tok::kw_short:
2848 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002849 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002850 case tok::kw_signed:
2851 case tok::kw_unsigned:
2852 case tok::kw__Complex:
2853 case tok::kw__Imaginary:
2854 case tok::kw_void:
2855 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002856 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002857 case tok::kw_char16_t:
2858 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002859 case tok::kw_int:
2860 case tok::kw_float:
2861 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002862 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002863 case tok::kw__Bool:
2864 case tok::kw__Decimal32:
2865 case tok::kw__Decimal64:
2866 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002867 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002868
Chris Lattner99dc9142008-04-13 18:59:07 +00002869 // struct-or-union-specifier (C99) or class-specifier (C++)
2870 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002871 case tok::kw_struct:
2872 case tok::kw_union:
2873 // enum-specifier
2874 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002875
Reid Spencer5f016e22007-07-11 17:01:13 +00002876 // type-qualifier
2877 case tok::kw_const:
2878 case tok::kw_volatile:
2879 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002880
2881 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002882 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002883 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002884
Chris Lattner7c186be2008-10-20 00:25:30 +00002885 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2886 case tok::less:
2887 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002888
Steve Naroff239f0732008-12-25 14:16:32 +00002889 case tok::kw___cdecl:
2890 case tok::kw___stdcall:
2891 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002892 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002893 case tok::kw___w64:
2894 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002895 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002896
2897 case tok::kw___private:
2898 case tok::kw___local:
2899 case tok::kw___global:
2900 case tok::kw___constant:
2901 case tok::kw___read_only:
2902 case tok::kw___read_write:
2903 case tok::kw___write_only:
2904
Eli Friedman290eeb02009-06-08 23:27:34 +00002905 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002906
2907 case tok::kw_private:
2908 return getLang().OpenCL;
Reid Spencer5f016e22007-07-11 17:01:13 +00002909 }
2910}
2911
2912/// isDeclarationSpecifier() - Return true if the current token is part of a
2913/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002914///
2915/// \param DisambiguatingWithExpression True to indicate that the purpose of
2916/// this check is to disambiguate between an expression and a declaration.
2917bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002918 switch (Tok.getKind()) {
2919 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002920
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002921 case tok::kw_private:
2922 return getLang().OpenCL;
2923
Chris Lattner166a8fc2009-01-04 23:41:41 +00002924 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002925 // Unfortunate hack to support "Class.factoryMethod" notation.
2926 if (getLang().ObjC1 && NextToken().is(tok::period))
2927 return false;
John Thompson82287d12010-02-05 00:12:22 +00002928 if (TryAltiVecVectorToken())
2929 return true;
2930 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002931 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002932 // Annotate typenames and C++ scope specifiers. If we get one, just
2933 // recurse to handle whatever we get.
2934 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002935 return true;
2936 if (Tok.is(tok::identifier))
2937 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002938
2939 // If we're in Objective-C and we have an Objective-C class type followed
2940 // by an identifier and then either ':' or ']', in a place where an
2941 // expression is permitted, then this is probably a class message send
2942 // missing the initial '['. In this case, we won't consider this to be
2943 // the start of a declaration.
2944 if (DisambiguatingWithExpression &&
2945 isStartOfObjCClassMessageMissingOpenBracket())
2946 return false;
2947
John McCall9ba61662010-02-26 08:45:28 +00002948 return isDeclarationSpecifier();
2949
Chris Lattner166a8fc2009-01-04 23:41:41 +00002950 case tok::coloncolon: // ::foo::bar
2951 if (NextToken().is(tok::kw_new) || // ::new
2952 NextToken().is(tok::kw_delete)) // ::delete
2953 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002954
Chris Lattner166a8fc2009-01-04 23:41:41 +00002955 // Annotate typenames and C++ scope specifiers. If we get one, just
2956 // recurse to handle whatever we get.
2957 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002958 return true;
2959 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002960
Reid Spencer5f016e22007-07-11 17:01:13 +00002961 // storage-class-specifier
2962 case tok::kw_typedef:
2963 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002964 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002965 case tok::kw_static:
2966 case tok::kw_auto:
2967 case tok::kw_register:
2968 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002969
Reid Spencer5f016e22007-07-11 17:01:13 +00002970 // type-specifiers
2971 case tok::kw_short:
2972 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002973 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002974 case tok::kw_signed:
2975 case tok::kw_unsigned:
2976 case tok::kw__Complex:
2977 case tok::kw__Imaginary:
2978 case tok::kw_void:
2979 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002980 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002981 case tok::kw_char16_t:
2982 case tok::kw_char32_t:
2983
Reid Spencer5f016e22007-07-11 17:01:13 +00002984 case tok::kw_int:
2985 case tok::kw_float:
2986 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002987 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002988 case tok::kw__Bool:
2989 case tok::kw__Decimal32:
2990 case tok::kw__Decimal64:
2991 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002992 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002993
Chris Lattner99dc9142008-04-13 18:59:07 +00002994 // struct-or-union-specifier (C99) or class-specifier (C++)
2995 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002996 case tok::kw_struct:
2997 case tok::kw_union:
2998 // enum-specifier
2999 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003000
Reid Spencer5f016e22007-07-11 17:01:13 +00003001 // type-qualifier
3002 case tok::kw_const:
3003 case tok::kw_volatile:
3004 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003005
Reid Spencer5f016e22007-07-11 17:01:13 +00003006 // function-specifier
3007 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003008 case tok::kw_virtual:
3009 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003010
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003011 // static_assert-declaration
3012 case tok::kw__Static_assert:
3013
Chris Lattner1ef08762007-08-09 17:01:07 +00003014 // GNU typeof support.
3015 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003016
Chris Lattner1ef08762007-08-09 17:01:07 +00003017 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003018 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003019 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003020
Francois Pichete3d49b42011-06-19 08:02:06 +00003021 // C++0x decltype.
3022 case tok::kw_decltype:
3023 return true;
3024
Chris Lattnerf3948c42008-07-26 03:38:44 +00003025 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3026 case tok::less:
3027 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003028
Douglas Gregord9d75e52011-04-27 05:41:15 +00003029 // typedef-name
3030 case tok::annot_typename:
3031 return !DisambiguatingWithExpression ||
3032 !isStartOfObjCClassMessageMissingOpenBracket();
3033
Steve Naroff47f52092009-01-06 19:34:12 +00003034 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003035 case tok::kw___cdecl:
3036 case tok::kw___stdcall:
3037 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003038 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003039 case tok::kw___w64:
3040 case tok::kw___ptr64:
3041 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003042 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003043
3044 case tok::kw___private:
3045 case tok::kw___local:
3046 case tok::kw___global:
3047 case tok::kw___constant:
3048 case tok::kw___read_only:
3049 case tok::kw___read_write:
3050 case tok::kw___write_only:
3051
Eli Friedman290eeb02009-06-08 23:27:34 +00003052 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003053 }
3054}
3055
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003056bool Parser::isConstructorDeclarator() {
3057 TentativeParsingAction TPA(*this);
3058
3059 // Parse the C++ scope specifier.
3060 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003061 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003062 TPA.Revert();
3063 return false;
3064 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003065
3066 // Parse the constructor name.
3067 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3068 // We already know that we have a constructor name; just consume
3069 // the token.
3070 ConsumeToken();
3071 } else {
3072 TPA.Revert();
3073 return false;
3074 }
3075
3076 // Current class name must be followed by a left parentheses.
3077 if (Tok.isNot(tok::l_paren)) {
3078 TPA.Revert();
3079 return false;
3080 }
3081 ConsumeParen();
3082
3083 // A right parentheses or ellipsis signals that we have a constructor.
3084 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3085 TPA.Revert();
3086 return true;
3087 }
3088
3089 // If we need to, enter the specified scope.
3090 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003091 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003092 DeclScopeObj.EnterDeclaratorScope();
3093
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003094 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003095 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003096 MaybeParseMicrosoftAttributes(Attrs);
3097
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003098 // Check whether the next token(s) are part of a declaration
3099 // specifier, in which case we have the start of a parameter and,
3100 // therefore, we know that this is a constructor.
3101 bool IsConstructor = isDeclarationSpecifier();
3102 TPA.Revert();
3103 return IsConstructor;
3104}
Reid Spencer5f016e22007-07-11 17:01:13 +00003105
3106/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003107/// type-qualifier-list: [C99 6.7.5]
3108/// type-qualifier
3109/// [vendor] attributes
3110/// [ only if VendorAttributesAllowed=true ]
3111/// type-qualifier-list type-qualifier
3112/// [vendor] type-qualifier-list attributes
3113/// [ only if VendorAttributesAllowed=true ]
3114/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3115/// [ only if CXX0XAttributesAllowed=true ]
3116/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003117///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003118void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3119 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003120 bool CXX0XAttributesAllowed) {
3121 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3122 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003123 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003124 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003125 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003126 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003127 else
3128 Diag(Loc, diag::err_attributes_not_allowed);
3129 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003130
3131 SourceLocation EndLoc;
3132
Reid Spencer5f016e22007-07-11 17:01:13 +00003133 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003134 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003135 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003136 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003137 SourceLocation Loc = Tok.getLocation();
3138
3139 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003140 case tok::code_completion:
3141 Actions.CodeCompleteTypeQualifiers(DS);
3142 ConsumeCodeCompletionToken();
3143 break;
3144
Reid Spencer5f016e22007-07-11 17:01:13 +00003145 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003146 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3147 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003148 break;
3149 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003150 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3151 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003152 break;
3153 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003154 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3155 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003156 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003157
3158 // OpenCL qualifiers:
3159 case tok::kw_private:
3160 if (!getLang().OpenCL)
3161 goto DoneWithTypeQuals;
3162 case tok::kw___private:
3163 case tok::kw___global:
3164 case tok::kw___local:
3165 case tok::kw___constant:
3166 case tok::kw___read_only:
3167 case tok::kw___write_only:
3168 case tok::kw___read_write:
3169 ParseOpenCLQualifiers(DS);
3170 break;
3171
Eli Friedman290eeb02009-06-08 23:27:34 +00003172 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003173 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00003174 case tok::kw___cdecl:
3175 case tok::kw___stdcall:
3176 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003177 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003178 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003179 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003180 continue;
3181 }
3182 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003183 case tok::kw___pascal:
3184 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003185 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003186 continue;
3187 }
3188 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003189 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003190 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003191 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003192 continue; // do *not* consume the next token!
3193 }
3194 // otherwise, FALL THROUGH!
3195 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003196 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003197 // If this is not a type-qualifier token, we're done reading type
3198 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003199 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003200 if (EndLoc.isValid())
3201 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003202 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003203 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003204
Reid Spencer5f016e22007-07-11 17:01:13 +00003205 // If the specifier combination wasn't legal, issue a diagnostic.
3206 if (isInvalid) {
3207 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003208 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003209 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003210 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003211 }
3212}
3213
3214
3215/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3216///
3217void Parser::ParseDeclarator(Declarator &D) {
3218 /// This implements the 'declarator' production in the C grammar, then checks
3219 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003220 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003221}
3222
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003223/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3224/// is parsed by the function passed to it. Pass null, and the direct-declarator
3225/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003226/// ptr-operator production.
3227///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003228/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3229/// [C] pointer[opt] direct-declarator
3230/// [C++] direct-declarator
3231/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003232///
3233/// pointer: [C99 6.7.5]
3234/// '*' type-qualifier-list[opt]
3235/// '*' type-qualifier-list[opt] pointer
3236///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003237/// ptr-operator:
3238/// '*' cv-qualifier-seq[opt]
3239/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003240/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003241/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003242/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003243/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003244void Parser::ParseDeclaratorInternal(Declarator &D,
3245 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003246 if (Diags.hasAllExtensionsSilenced())
3247 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003248
Sebastian Redlf30208a2009-01-24 21:16:55 +00003249 // C++ member pointers start with a '::' or a nested-name.
3250 // Member pointers get special handling, since there's no place for the
3251 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003252 if (getLang().CPlusPlus &&
3253 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3254 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003255 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003256 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003257
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003258 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003259 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003260 // The scope spec really belongs to the direct-declarator.
3261 D.getCXXScopeSpec() = SS;
3262 if (DirectDeclParser)
3263 (this->*DirectDeclParser)(D);
3264 return;
3265 }
3266
3267 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003268 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003269 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003270 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003271 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003272
3273 // Recurse to parse whatever is left.
3274 ParseDeclaratorInternal(D, DirectDeclParser);
3275
3276 // Sema will have to catch (syntactically invalid) pointers into global
3277 // scope. It has to catch pointers into namespace scope anyway.
3278 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003279 Loc),
3280 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003281 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003282 return;
3283 }
3284 }
3285
3286 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003287 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003288 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003289 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003290 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003291 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003292 if (DirectDeclParser)
3293 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003294 return;
3295 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003296
Sebastian Redl05532f22009-03-15 22:02:01 +00003297 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3298 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003299 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003300 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003301
Chris Lattner9af55002009-03-27 04:18:06 +00003302 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003303 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003304 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003305
Reid Spencer5f016e22007-07-11 17:01:13 +00003306 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003307 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003308
Reid Spencer5f016e22007-07-11 17:01:13 +00003309 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003310 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003311 if (Kind == tok::star)
3312 // Remember that we parsed a pointer type, and remember the type-quals.
3313 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003314 DS.getConstSpecLoc(),
3315 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003316 DS.getRestrictSpecLoc()),
3317 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003318 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003319 else
3320 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003321 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003322 Loc),
3323 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003324 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003325 } else {
3326 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003327 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003328
Sebastian Redl743de1f2009-03-23 00:00:23 +00003329 // Complain about rvalue references in C++03, but then go on and build
3330 // the declarator.
3331 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00003332 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003333
Reid Spencer5f016e22007-07-11 17:01:13 +00003334 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3335 // cv-qualifiers are introduced through the use of a typedef or of a
3336 // template type argument, in which case the cv-qualifiers are ignored.
3337 //
3338 // [GNU] Retricted references are allowed.
3339 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003340 // [C++0x] Attributes on references are not allowed.
3341 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003342 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003343
3344 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3345 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3346 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003347 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003348 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3349 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003350 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003351 }
3352
3353 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003354 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003355
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003356 if (D.getNumTypeObjects() > 0) {
3357 // C++ [dcl.ref]p4: There shall be no references to references.
3358 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3359 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003360 if (const IdentifierInfo *II = D.getIdentifier())
3361 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3362 << II;
3363 else
3364 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3365 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003366
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003367 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003368 // can go ahead and build the (technically ill-formed)
3369 // declarator: reference collapsing will take care of it.
3370 }
3371 }
3372
Reid Spencer5f016e22007-07-11 17:01:13 +00003373 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003374 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003375 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003376 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003377 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003378 }
3379}
3380
3381/// ParseDirectDeclarator
3382/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003383/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003384/// '(' declarator ')'
3385/// [GNU] '(' attributes declarator ')'
3386/// [C90] direct-declarator '[' constant-expression[opt] ']'
3387/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3388/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3389/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3390/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3391/// direct-declarator '(' parameter-type-list ')'
3392/// direct-declarator '(' identifier-list[opt] ')'
3393/// [GNU] direct-declarator '(' parameter-forward-declarations
3394/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003395/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3396/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003397/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003398///
3399/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003400/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003401/// '::'[opt] nested-name-specifier[opt] type-name
3402///
3403/// id-expression: [C++ 5.1]
3404/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003405/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003406///
3407/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003408/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003409/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003410/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003411/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003412/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003413///
Reid Spencer5f016e22007-07-11 17:01:13 +00003414void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003415 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003416
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003417 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3418 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003419 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003420 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003421 }
3422
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003423 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003424 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003425 // Change the declaration context for name lookup, until this function
3426 // is exited (and the declarator has been parsed).
3427 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003428 }
3429
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003430 // C++0x [dcl.fct]p14:
3431 // There is a syntactic ambiguity when an ellipsis occurs at the end
3432 // of a parameter-declaration-clause without a preceding comma. In
3433 // this case, the ellipsis is parsed as part of the
3434 // abstract-declarator if the type of the parameter names a template
3435 // parameter pack that has not been expanded; otherwise, it is parsed
3436 // as part of the parameter-declaration-clause.
3437 if (Tok.is(tok::ellipsis) &&
3438 !((D.getContext() == Declarator::PrototypeContext ||
3439 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003440 NextToken().is(tok::r_paren) &&
3441 !Actions.containsUnexpandedParameterPacks(D)))
3442 D.setEllipsisLoc(ConsumeToken());
3443
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003444 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3445 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3446 // We found something that indicates the start of an unqualified-id.
3447 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003448 bool AllowConstructorName;
3449 if (D.getDeclSpec().hasTypeSpecifier())
3450 AllowConstructorName = false;
3451 else if (D.getCXXScopeSpec().isSet())
3452 AllowConstructorName =
3453 (D.getContext() == Declarator::FileContext ||
3454 (D.getContext() == Declarator::MemberContext &&
3455 D.getDeclSpec().isFriendSpecified()));
3456 else
3457 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3458
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003459 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3460 /*EnteringContext=*/true,
3461 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003462 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003463 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003464 D.getName()) ||
3465 // Once we're past the identifier, if the scope was bad, mark the
3466 // whole declarator bad.
3467 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003468 D.SetIdentifier(0, Tok.getLocation());
3469 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003470 } else {
3471 // Parsed the unqualified-id; update range information and move along.
3472 if (D.getSourceRange().getBegin().isInvalid())
3473 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3474 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003475 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003476 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003477 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003478 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003479 assert(!getLang().CPlusPlus &&
3480 "There's a C++-specific check for tok::identifier above");
3481 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3482 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3483 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003484 goto PastIdentifier;
3485 }
3486
3487 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003488 // direct-declarator: '(' declarator ')'
3489 // direct-declarator: '(' attributes declarator ')'
3490 // Example: 'char (*X)' or 'int (*XX)(void)'
3491 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003492
3493 // If the declarator was parenthesized, we entered the declarator
3494 // scope when parsing the parenthesized declarator, then exited
3495 // the scope already. Re-enter the scope, if we need to.
3496 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003497 // If there was an error parsing parenthesized declarator, declarator
3498 // scope may have been enterred before. Don't do it again.
3499 if (!D.isInvalidType() &&
3500 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003501 // Change the declaration context for name lookup, until this function
3502 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003503 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003504 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003505 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003506 // This could be something simple like "int" (in which case the declarator
3507 // portion is empty), if an abstract-declarator is allowed.
3508 D.SetIdentifier(0, Tok.getLocation());
3509 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003510 if (D.getContext() == Declarator::MemberContext)
3511 Diag(Tok, diag::err_expected_member_name_or_semi)
3512 << D.getDeclSpec().getSourceRange();
3513 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003514 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003515 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003516 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003517 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003518 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003519 }
Mike Stump1eb44332009-09-09 15:08:12 +00003520
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003521 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003522 assert(D.isPastIdentifier() &&
3523 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003524
Sean Huntbbd37c62009-11-21 08:43:09 +00003525 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003526 if (D.getIdentifier())
3527 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003528
Reid Spencer5f016e22007-07-11 17:01:13 +00003529 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003530 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003531 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3532 // In such a case, check if we actually have a function declarator; if it
3533 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003534 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3535 // When not in file scope, warn for ambiguous function declarators, just
3536 // in case the author intended it as a variable definition.
3537 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3538 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3539 break;
3540 }
John McCall0b7e6782011-03-24 11:26:52 +00003541 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003542 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00003543 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003544 ParseBracketDeclarator(D);
3545 } else {
3546 break;
3547 }
3548 }
3549}
3550
Chris Lattneref4715c2008-04-06 05:45:57 +00003551/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3552/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003553/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003554/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3555///
3556/// direct-declarator:
3557/// '(' declarator ')'
3558/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003559/// direct-declarator '(' parameter-type-list ')'
3560/// direct-declarator '(' identifier-list[opt] ')'
3561/// [GNU] direct-declarator '(' parameter-forward-declarations
3562/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003563///
3564void Parser::ParseParenDeclarator(Declarator &D) {
3565 SourceLocation StartLoc = ConsumeParen();
3566 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003567
Chris Lattner7399ee02008-10-20 02:05:46 +00003568 // Eat any attributes before we look at whether this is a grouping or function
3569 // declarator paren. If this is a grouping paren, the attribute applies to
3570 // the type being built up, for example:
3571 // int (__attribute__(()) *x)(long y)
3572 // If this ends up not being a grouping paren, the attribute applies to the
3573 // first argument, for example:
3574 // int (__attribute__(()) int x)
3575 // In either case, we need to eat any attributes to be able to determine what
3576 // sort of paren this is.
3577 //
John McCall0b7e6782011-03-24 11:26:52 +00003578 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003579 bool RequiresArg = false;
3580 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003581 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003582
Chris Lattner7399ee02008-10-20 02:05:46 +00003583 // We require that the argument list (if this is a non-grouping paren) be
3584 // present even if the attribute list was empty.
3585 RequiresArg = true;
3586 }
Steve Naroff239f0732008-12-25 14:16:32 +00003587 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003588 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003589 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3590 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall7f040a92010-12-24 02:08:15 +00003591 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003592 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003593 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003594 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003595 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003596
Chris Lattneref4715c2008-04-06 05:45:57 +00003597 // If we haven't past the identifier yet (or where the identifier would be
3598 // stored, if this is an abstract declarator), then this is probably just
3599 // grouping parens. However, if this could be an abstract-declarator, then
3600 // this could also be the start of function arguments (consider 'void()').
3601 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003602
Chris Lattneref4715c2008-04-06 05:45:57 +00003603 if (!D.mayOmitIdentifier()) {
3604 // If this can't be an abstract-declarator, this *must* be a grouping
3605 // paren, because we haven't seen the identifier yet.
3606 isGrouping = true;
3607 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003608 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003609 isDeclarationSpecifier()) { // 'int(int)' is a function.
3610 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3611 // considered to be a type, not a K&R identifier-list.
3612 isGrouping = false;
3613 } else {
3614 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3615 isGrouping = true;
3616 }
Mike Stump1eb44332009-09-09 15:08:12 +00003617
Chris Lattneref4715c2008-04-06 05:45:57 +00003618 // If this is a grouping paren, handle:
3619 // direct-declarator: '(' declarator ')'
3620 // direct-declarator: '(' attributes declarator ')'
3621 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003622 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003623 D.setGroupingParens(true);
3624
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003625 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003626 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003627 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003628 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3629 attrs, EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003630
3631 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003632 return;
3633 }
Mike Stump1eb44332009-09-09 15:08:12 +00003634
Chris Lattneref4715c2008-04-06 05:45:57 +00003635 // Okay, if this wasn't a grouping paren, it must be the start of a function
3636 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003637 // identifier (and remember where it would have been), then call into
3638 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003639 D.SetIdentifier(0, Tok.getLocation());
3640
John McCall7f040a92010-12-24 02:08:15 +00003641 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003642}
3643
3644/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3645/// declarator D up to a paren, which indicates that we are parsing function
3646/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003647///
Chris Lattner7399ee02008-10-20 02:05:46 +00003648/// If AttrList is non-null, then the caller parsed those arguments immediately
3649/// after the open paren - they should be considered to be the first argument of
3650/// a parameter. If RequiresArg is true, then the first argument of the
3651/// function is required to be present and required to not be an identifier
3652/// list.
3653///
Reid Spencer5f016e22007-07-11 17:01:13 +00003654/// This method also handles this portion of the grammar:
3655/// parameter-type-list: [C99 6.7.5]
3656/// parameter-list
3657/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003658/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003659///
3660/// parameter-list: [C99 6.7.5]
3661/// parameter-declaration
3662/// parameter-list ',' parameter-declaration
3663///
3664/// parameter-declaration: [C99 6.7.5]
3665/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003666/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003667/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003668/// declaration-specifiers abstract-declarator[opt]
3669/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003670/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003671/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3672///
Douglas Gregor83f51722011-01-26 03:43:54 +00003673/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3674/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003675///
Sebastian Redl7acafd02011-03-05 14:45:16 +00003676/// [C++0x] exception-specification:
3677/// dynamic-exception-specification
3678/// noexcept-specification
3679///
Chris Lattner7399ee02008-10-20 02:05:46 +00003680void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall7f040a92010-12-24 02:08:15 +00003681 ParsedAttributes &attrs,
Chris Lattner7399ee02008-10-20 02:05:46 +00003682 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003683 // lparen is already consumed!
3684 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003685
Douglas Gregordab60ad2010-10-01 18:44:50 +00003686 ParsedType TrailingReturnType;
3687
Chris Lattner7399ee02008-10-20 02:05:46 +00003688 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003689 if (Tok.is(tok::r_paren)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003690 if (RequiresArg)
Chris Lattner1ab3b962008-11-18 07:48:38 +00003691 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003692
Abramo Bagnara796aa442011-03-12 11:17:06 +00003693 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003694
3695 // cv-qualifier-seq[opt].
John McCall0b7e6782011-03-24 11:26:52 +00003696 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003697 SourceLocation RefQualifierLoc;
3698 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003699 ExceptionSpecificationType ESpecType = EST_None;
3700 SourceRange ESpecRange;
3701 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3702 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3703 ExprResult NoexceptExpr;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003704 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003705 MaybeParseCXX0XAttributes(attrs);
3706
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003707 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003708 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003709 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003710
Douglas Gregor83f51722011-01-26 03:43:54 +00003711 // Parse ref-qualifier[opt]
3712 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3713 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003714 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003715
Douglas Gregor83f51722011-01-26 03:43:54 +00003716 RefQualifierIsLValueRef = Tok.is(tok::amp);
3717 RefQualifierLoc = ConsumeToken();
3718 EndLoc = RefQualifierLoc;
3719 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00003720
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003721 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003722 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3723 DynamicExceptions,
3724 DynamicExceptionRanges,
3725 NoexceptExpr);
3726 if (ESpecType != EST_None)
3727 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003728
3729 // Parse trailing-return-type.
3730 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3731 TrailingReturnType = ParseTrailingReturnType().get();
3732 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003733 }
3734
Chris Lattnerf97409f2008-04-06 06:57:35 +00003735 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003736 // int() -> no prototype, no '...'.
John McCall0b7e6782011-03-24 11:26:52 +00003737 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003738 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003739 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003740 /*arglist*/ 0, 0,
3741 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003742 RefQualifierIsLValueRef,
3743 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003744 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003745 DynamicExceptions.data(),
3746 DynamicExceptionRanges.data(),
3747 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003748 NoexceptExpr.isUsable() ?
3749 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003750 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003751 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00003752 attrs, EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003753 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003754 }
3755
Chris Lattner7399ee02008-10-20 02:05:46 +00003756 // Alternatively, this parameter list may be an identifier list form for a
3757 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003758 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3759 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003760 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003761 // K&R identifier lists can't have typedefs as identifiers, per
3762 // C99 6.7.5.3p11.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003763 if (RequiresArg)
Steve Naroff2d081c42009-01-28 19:16:40 +00003764 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner83a94472010-05-14 17:23:36 +00003765
Steve Naroff2d081c42009-01-28 19:16:40 +00003766 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003767 // normal declarators, not for abstract-declarators. Get the first
3768 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003769 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003770 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003771
3772 // Identifier lists follow a really simple grammar: the identifiers can
3773 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3774 // identifier lists are really rare in the brave new modern world, and it
3775 // is very common for someone to typo a type in a non-k&r style list. If
3776 // we are presented with something like: "void foo(intptr x, float y)",
3777 // we don't want to start parsing the function declarator as though it is
3778 // a K&R style declarator just because intptr is an invalid type.
3779 //
3780 // To handle this, we check to see if the token after the first identifier
3781 // is a "," or ")". Only if so, do we parse it as an identifier list.
3782 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3783 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3784 FirstTok.getIdentifierInfo(),
3785 FirstTok.getLocation(), D);
3786
3787 // If we get here, the code is invalid. Push the first identifier back
3788 // into the token stream and parse the first argument as an (invalid)
3789 // normal argument declarator.
3790 PP.EnterToken(Tok);
3791 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003792 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003793 }
Mike Stump1eb44332009-09-09 15:08:12 +00003794
Chris Lattnerf97409f2008-04-06 06:57:35 +00003795 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003796
Chris Lattnerf97409f2008-04-06 06:57:35 +00003797 // Build up an array of information about the parsed arguments.
3798 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003799
3800 // Enter function-declaration scope, limiting any declarators to the
3801 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003802 ParseScope PrototypeScope(this,
3803 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003804
Chris Lattnerf97409f2008-04-06 06:57:35 +00003805 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003806 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003807 while (1) {
3808 if (Tok.is(tok::ellipsis)) {
3809 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003810 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003811 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003812 }
Mike Stump1eb44332009-09-09 15:08:12 +00003813
Chris Lattnerf97409f2008-04-06 06:57:35 +00003814 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003815 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00003816 DeclSpec DS(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003817
3818 // Skip any Microsoft attributes before a param.
3819 if (getLang().Microsoft && Tok.is(tok::l_square))
3820 ParseMicrosoftAttributes(DS.getAttributes());
3821
3822 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00003823
3824 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00003825 // Take them so that we only apply the attributes to the first parameter.
3826 DS.takeAttributesFrom(attrs);
3827
Chris Lattnere64c5492009-02-27 18:38:20 +00003828 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003829
Chris Lattnerf97409f2008-04-06 06:57:35 +00003830 // Parse the declarator. This is "PrototypeContext", because we must
3831 // accept either 'declarator' or 'abstract-declarator' here.
3832 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3833 ParseDeclarator(ParmDecl);
3834
3835 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00003836 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003837
Chris Lattnerf97409f2008-04-06 06:57:35 +00003838 // Remember this parsed parameter in ParamInfo.
3839 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003840
Douglas Gregor72b505b2008-12-16 21:30:33 +00003841 // DefArgToks is used when the parsing of default arguments needs
3842 // to be delayed.
3843 CachedTokens *DefArgToks = 0;
3844
Chris Lattnerf97409f2008-04-06 06:57:35 +00003845 // If no parameter was specified, verify that *something* was specified,
3846 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003847 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3848 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003849 // Completely missing, emit error.
3850 Diag(DSStart, diag::err_missing_param);
3851 } else {
3852 // Otherwise, we have something. Add it and let semantic analysis try
3853 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003854
Chris Lattnerf97409f2008-04-06 06:57:35 +00003855 // Inform the actions module about the parameter declarator, so it gets
3856 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003857 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003858
3859 // Parse the default argument, if any. We parse the default
3860 // arguments in all dialects; the semantic analysis in
3861 // ActOnParamDefaultArgument will reject the default argument in
3862 // C.
3863 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003864 SourceLocation EqualLoc = Tok.getLocation();
3865
Chris Lattner04421082008-04-08 04:40:51 +00003866 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003867 if (D.getContext() == Declarator::MemberContext) {
3868 // If we're inside a class definition, cache the tokens
3869 // corresponding to the default argument. We'll actually parse
3870 // them when we see the end of the class definition.
3871 // FIXME: Templates will require something similar.
3872 // FIXME: Can we use a smart pointer for Toks?
3873 DefArgToks = new CachedTokens;
3874
Mike Stump1eb44332009-09-09 15:08:12 +00003875 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003876 /*StopAtSemi=*/true,
3877 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003878 delete DefArgToks;
3879 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003880 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003881 } else {
3882 // Mark the end of the default argument so that we know when to
3883 // stop when we parse it later on.
3884 Token DefArgEnd;
3885 DefArgEnd.startToken();
3886 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3887 DefArgEnd.setLocation(Tok.getLocation());
3888 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003889 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003890 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003891 }
Chris Lattner04421082008-04-08 04:40:51 +00003892 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003893 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003894 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003895
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003896 // The argument isn't actually potentially evaluated unless it is
3897 // used.
3898 EnterExpressionEvaluationContext Eval(Actions,
3899 Sema::PotentiallyEvaluatedIfUsed);
3900
John McCall60d7b3a2010-08-24 06:29:42 +00003901 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003902 if (DefArgResult.isInvalid()) {
3903 Actions.ActOnParamDefaultArgumentError(Param);
3904 SkipUntil(tok::comma, tok::r_paren, true, true);
3905 } else {
3906 // Inform the actions module about the default argument
3907 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003908 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003909 }
Chris Lattner04421082008-04-08 04:40:51 +00003910 }
3911 }
Mike Stump1eb44332009-09-09 15:08:12 +00003912
3913 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3914 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003915 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003916 }
3917
3918 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003919 if (Tok.isNot(tok::comma)) {
3920 if (Tok.is(tok::ellipsis)) {
3921 IsVariadic = true;
3922 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3923
3924 if (!getLang().CPlusPlus) {
3925 // We have ellipsis without a preceding ',', which is ill-formed
3926 // in C. Complain and provide the fix.
3927 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003928 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003929 }
3930 }
3931
3932 break;
3933 }
Mike Stump1eb44332009-09-09 15:08:12 +00003934
Chris Lattnerf97409f2008-04-06 06:57:35 +00003935 // Consume the comma.
3936 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003937 }
Mike Stump1eb44332009-09-09 15:08:12 +00003938
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003939 // If we have the closing ')', eat it.
Abramo Bagnara796aa442011-03-12 11:17:06 +00003940 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003941
John McCall0b7e6782011-03-24 11:26:52 +00003942 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003943 SourceLocation RefQualifierLoc;
3944 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003945 ExceptionSpecificationType ESpecType = EST_None;
3946 SourceRange ESpecRange;
3947 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3948 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3949 ExprResult NoexceptExpr;
Sean Huntbbd37c62009-11-21 08:43:09 +00003950
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003951 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003952 MaybeParseCXX0XAttributes(attrs);
3953
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003954 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003955 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003956 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003957 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003958
Douglas Gregor83f51722011-01-26 03:43:54 +00003959 // Parse ref-qualifier[opt]
3960 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3961 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003962 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor83f51722011-01-26 03:43:54 +00003963
3964 RefQualifierIsLValueRef = Tok.is(tok::amp);
3965 RefQualifierLoc = ConsumeToken();
3966 EndLoc = RefQualifierLoc;
3967 }
3968
Sebastian Redl7acafd02011-03-05 14:45:16 +00003969 // FIXME: We should leave the prototype scope before parsing the exception
3970 // specification, and then reenter it when parsing the trailing return type.
3971 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3972
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003973 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003974 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3975 DynamicExceptions,
3976 DynamicExceptionRanges,
3977 NoexceptExpr);
3978 if (ESpecType != EST_None)
3979 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003980
3981 // Parse trailing-return-type.
3982 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3983 TrailingReturnType = ParseTrailingReturnType().get();
3984 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003985 }
3986
Douglas Gregordab60ad2010-10-01 18:44:50 +00003987 // Leave prototype scope.
3988 PrototypeScope.Exit();
3989
Reid Spencer5f016e22007-07-11 17:01:13 +00003990 // Remember that we parsed a function type, and remember the attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003991 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003992 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003993 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003994 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003995 RefQualifierIsLValueRef,
3996 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003997 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003998 DynamicExceptions.data(),
3999 DynamicExceptionRanges.data(),
4000 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00004001 NoexceptExpr.isUsable() ?
4002 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00004003 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00004004 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00004005 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004006}
4007
Chris Lattner66d28652008-04-06 06:34:08 +00004008/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4009/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00004010/// first identifier has already been consumed, and the current token is the
4011/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00004012///
4013/// identifier-list: [C99 6.7.5]
4014/// identifier
4015/// identifier-list ',' identifier
4016///
4017void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00004018 IdentifierInfo *FirstIdent,
4019 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00004020 Declarator &D) {
4021 // Build up an array of information about the parsed arguments.
4022 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
4023 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00004024
Chris Lattner66d28652008-04-06 06:34:08 +00004025 // If there was no identifier specified for the declarator, either we are in
4026 // an abstract-declarator, or we are in a parameter declarator which was found
4027 // to be abstract. In abstract-declarators, identifier lists are not valid:
4028 // diagnose this.
4029 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00004030 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00004031
Chris Lattner83a94472010-05-14 17:23:36 +00004032 // The first identifier was already read, and is known to be the first
4033 // identifier in the list. Remember this identifier in ParamInfo.
4034 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00004035 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00004036
Chris Lattner66d28652008-04-06 06:34:08 +00004037 while (Tok.is(tok::comma)) {
4038 // Eat the comma.
4039 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004040
Chris Lattner50c64772008-04-06 06:39:19 +00004041 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00004042 if (Tok.isNot(tok::identifier)) {
4043 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00004044 SkipUntil(tok::r_paren);
4045 return;
Chris Lattner66d28652008-04-06 06:34:08 +00004046 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00004047
Chris Lattner66d28652008-04-06 06:34:08 +00004048 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00004049
4050 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00004051 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00004052 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00004053
Chris Lattner66d28652008-04-06 06:34:08 +00004054 // Verify that the argument identifier has not already been mentioned.
4055 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004056 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00004057 } else {
4058 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00004059 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00004060 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00004061 0));
Chris Lattner50c64772008-04-06 06:39:19 +00004062 }
Mike Stump1eb44332009-09-09 15:08:12 +00004063
Chris Lattner66d28652008-04-06 06:34:08 +00004064 // Eat the identifier.
4065 ConsumeToken();
4066 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004067
4068 // If we have the closing ')', eat it and we're done.
4069 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4070
Chris Lattner50c64772008-04-06 06:39:19 +00004071 // Remember that we parsed a function type, and remember the attributes. This
4072 // function type is always a K&R style function type, which is not varargs and
4073 // has no prototype.
John McCall0b7e6782011-03-24 11:26:52 +00004074 ParsedAttributes attrs(AttrFactory);
4075 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00004076 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00004077 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00004078 /*TypeQuals*/0,
Douglas Gregor83f51722011-01-26 03:43:54 +00004079 true, SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00004080 EST_None, SourceLocation(), 0, 0,
4081 0, 0, LParenLoc, RLoc, D),
John McCall0b7e6782011-03-24 11:26:52 +00004082 attrs, RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00004083}
Chris Lattneref4715c2008-04-06 05:45:57 +00004084
Reid Spencer5f016e22007-07-11 17:01:13 +00004085/// [C90] direct-declarator '[' constant-expression[opt] ']'
4086/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4087/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4088/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4089/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4090void Parser::ParseBracketDeclarator(Declarator &D) {
4091 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00004092
Chris Lattner378c7e42008-12-18 07:27:21 +00004093 // C array syntax has many features, but by-far the most common is [] and [4].
4094 // This code does a fast path to handle some of the most obvious cases.
4095 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00004096 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004097 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004098 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004099
Chris Lattner378c7e42008-12-18 07:27:21 +00004100 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004101 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004102 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004103 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004104 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004105 return;
4106 } else if (Tok.getKind() == tok::numeric_constant &&
4107 GetLookAheadToken(1).is(tok::r_square)) {
4108 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004109 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004110 ConsumeToken();
4111
Sebastian Redlab197ba2009-02-09 18:23:29 +00004112 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004113 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004114 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004115
Chris Lattner378c7e42008-12-18 07:27:21 +00004116 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004117 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004118 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004119 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004120 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004121 return;
4122 }
Mike Stump1eb44332009-09-09 15:08:12 +00004123
Reid Spencer5f016e22007-07-11 17:01:13 +00004124 // If valid, this location is the position where we read the 'static' keyword.
4125 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004126 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004127 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004128
Reid Spencer5f016e22007-07-11 17:01:13 +00004129 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004130 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004131 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004132 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004133
Reid Spencer5f016e22007-07-11 17:01:13 +00004134 // If we haven't already read 'static', check to see if there is one after the
4135 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004136 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004137 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004138
Reid Spencer5f016e22007-07-11 17:01:13 +00004139 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4140 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004141 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004142
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004143 // Handle the case where we have '[*]' as the array size. However, a leading
4144 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4145 // the the token after the star is a ']'. Since stars in arrays are
4146 // infrequent, use of lookahead is not costly here.
4147 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004148 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004149
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004150 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004151 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004152 StaticLoc = SourceLocation(); // Drop the static.
4153 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004154 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004155 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004156 // Note, in C89, this production uses the constant-expr production instead
4157 // of assignment-expr. The only difference is that assignment-expr allows
4158 // things like '=' and '*='. Sema rejects these in C89 mode because they
4159 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004160
Douglas Gregore0762c92009-06-19 23:52:42 +00004161 // Parse the constant-expression or assignment-expression now (depending
4162 // on dialect).
4163 if (getLang().CPlusPlus)
4164 NumElements = ParseConstantExpression();
4165 else
4166 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004167 }
Mike Stump1eb44332009-09-09 15:08:12 +00004168
Reid Spencer5f016e22007-07-11 17:01:13 +00004169 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004170 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004171 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004172 // If the expression was invalid, skip it.
4173 SkipUntil(tok::r_square);
4174 return;
4175 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004176
4177 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4178
John McCall0b7e6782011-03-24 11:26:52 +00004179 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004180 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004181
Chris Lattner378c7e42008-12-18 07:27:21 +00004182 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004183 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004184 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004185 NumElements.release(),
4186 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004187 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004188}
4189
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004190/// [GNU] typeof-specifier:
4191/// typeof ( expressions )
4192/// typeof ( type-name )
4193/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004194///
4195void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004196 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004197 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004198 SourceLocation StartLoc = ConsumeToken();
4199
John McCallcfb708c2010-01-13 20:03:27 +00004200 const bool hasParens = Tok.is(tok::l_paren);
4201
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004202 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004203 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004204 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004205 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4206 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004207 if (hasParens)
4208 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004209
4210 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004211 // FIXME: Not accurate, the range gets one token more than it should.
4212 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004213 else
4214 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004215
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004216 if (isCastExpr) {
4217 if (!CastTy) {
4218 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004219 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004220 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004221
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004222 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004223 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004224 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4225 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004226 DiagID, CastTy))
4227 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004228 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004229 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004230
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004231 // If we get here, the operand to the typeof was an expresion.
4232 if (Operand.isInvalid()) {
4233 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004234 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004235 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004236
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004237 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004238 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004239 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4240 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004241 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004242 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004243}
Chris Lattner1b492422010-02-28 18:33:55 +00004244
4245
4246/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4247/// from TryAltiVecVectorToken.
4248bool Parser::TryAltiVecVectorTokenOutOfLine() {
4249 Token Next = NextToken();
4250 switch (Next.getKind()) {
4251 default: return false;
4252 case tok::kw_short:
4253 case tok::kw_long:
4254 case tok::kw_signed:
4255 case tok::kw_unsigned:
4256 case tok::kw_void:
4257 case tok::kw_char:
4258 case tok::kw_int:
4259 case tok::kw_float:
4260 case tok::kw_double:
4261 case tok::kw_bool:
4262 case tok::kw___pixel:
4263 Tok.setKind(tok::kw___vector);
4264 return true;
4265 case tok::identifier:
4266 if (Next.getIdentifierInfo() == Ident_pixel) {
4267 Tok.setKind(tok::kw___vector);
4268 return true;
4269 }
4270 return false;
4271 }
4272}
4273
4274bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4275 const char *&PrevSpec, unsigned &DiagID,
4276 bool &isInvalid) {
4277 if (Tok.getIdentifierInfo() == Ident_vector) {
4278 Token Next = NextToken();
4279 switch (Next.getKind()) {
4280 case tok::kw_short:
4281 case tok::kw_long:
4282 case tok::kw_signed:
4283 case tok::kw_unsigned:
4284 case tok::kw_void:
4285 case tok::kw_char:
4286 case tok::kw_int:
4287 case tok::kw_float:
4288 case tok::kw_double:
4289 case tok::kw_bool:
4290 case tok::kw___pixel:
4291 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4292 return true;
4293 case tok::identifier:
4294 if (Next.getIdentifierInfo() == Ident_pixel) {
4295 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4296 return true;
4297 }
4298 break;
4299 default:
4300 break;
4301 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004302 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004303 DS.isTypeAltiVecVector()) {
4304 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4305 return true;
4306 }
4307 return false;
4308}