blob: 99441e0e0e3fd4d0a82cfd8679d1ee179eb03450 [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.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001399 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001400 if ((DSContext == DSC_top_level ||
1401 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1402 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001403 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001404 if (isConstructorDeclarator()) {
1405 // The user meant this to be an out-of-line constructor
1406 // definition, but template arguments are not allowed
1407 // there. Just allow this as a constructor; we'll
1408 // complain about it later.
1409 goto DoneWithDeclSpec;
1410 }
1411
1412 // The user meant this to name a type, but it actually names
1413 // a constructor with some extraneous template
1414 // arguments. Complain, then parse it as a type as the user
1415 // intended.
1416 Diag(TemplateId->TemplateNameLoc,
1417 diag::err_out_of_line_template_id_names_constructor)
1418 << TemplateId->Name;
1419 }
1420
John McCallaa87d332009-12-12 11:40:51 +00001421 DS.getTypeSpecScope() = SS;
1422 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001423 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001424 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001425 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001426 continue;
1427 }
1428
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001429 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001430 DS.getTypeSpecScope() = SS;
1431 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001432 if (Tok.getAnnotationValue()) {
1433 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001434 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1435 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001436 PrevSpec, DiagID, T);
1437 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001438 else
1439 DS.SetTypeSpecError();
1440 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1441 ConsumeToken(); // The typename
1442 }
1443
Douglas Gregor9135c722009-03-25 15:40:00 +00001444 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001445 goto DoneWithDeclSpec;
1446
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001447 // If we're in a context where the identifier could be a class name,
1448 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001449 if ((DSContext == DSC_top_level ||
1450 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001451 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001452 &SS)) {
1453 if (isConstructorDeclarator())
1454 goto DoneWithDeclSpec;
1455
1456 // As noted in C++ [class.qual]p2 (cited above), when the name
1457 // of the class is qualified in a context where it could name
1458 // a constructor, its a constructor name. However, we've
1459 // looked at the declarator, and the user probably meant this
1460 // to be a type. Complain that it isn't supposed to be treated
1461 // as a type, then proceed to parse it as a type.
1462 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1463 << Next.getIdentifierInfo();
1464 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001465
John McCallb3d87482010-08-24 05:47:05 +00001466 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1467 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001468 getCurScope(), &SS,
1469 false, false, ParsedType(),
1470 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001471
Chris Lattnerf4382f52009-04-14 22:17:06 +00001472 // If the referenced identifier is not a type, then this declspec is
1473 // erroneous: We already checked about that it has no type specifier, and
1474 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001475 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001476 if (TypeRep == 0) {
1477 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001478 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001479 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001480 }
Mike Stump1eb44332009-09-09 15:08:12 +00001481
John McCallaa87d332009-12-12 11:40:51 +00001482 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001483 ConsumeToken(); // The C++ scope.
1484
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001485 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001486 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001487 if (isInvalid)
1488 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001489
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001490 DS.SetRangeEnd(Tok.getLocation());
1491 ConsumeToken(); // The typename.
1492
1493 continue;
1494 }
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Chris Lattner80d0c892009-01-21 19:48:37 +00001496 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001497 if (Tok.getAnnotationValue()) {
1498 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001499 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001500 DiagID, T);
1501 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001502 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001503
1504 if (isInvalid)
1505 break;
1506
Chris Lattner80d0c892009-01-21 19:48:37 +00001507 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1508 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Chris Lattner80d0c892009-01-21 19:48:37 +00001510 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1511 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001512 // Objective-C interface.
1513 if (Tok.is(tok::less) && getLang().ObjC1)
1514 ParseObjCProtocolQualifiers(DS);
1515
Chris Lattner80d0c892009-01-21 19:48:37 +00001516 continue;
1517 }
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Douglas Gregorbfad9152011-04-28 15:48:45 +00001519 case tok::kw___is_signed:
1520 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1521 // typically treats it as a trait. If we see __is_signed as it appears
1522 // in libstdc++, e.g.,
1523 //
1524 // static const bool __is_signed;
1525 //
1526 // then treat __is_signed as an identifier rather than as a keyword.
1527 if (DS.getTypeSpecType() == TST_bool &&
1528 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1529 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1530 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1531 Tok.setKind(tok::identifier);
1532 }
1533
1534 // We're done with the declaration-specifiers.
1535 goto DoneWithDeclSpec;
1536
Chris Lattner3bd934a2008-07-26 01:18:38 +00001537 // typedef-name
1538 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001539 // In C++, check to see if this is a scope specifier like foo::bar::, if
1540 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001541 if (getLang().CPlusPlus) {
1542 if (TryAnnotateCXXScopeToken(true)) {
1543 if (!DS.hasTypeSpecifier())
1544 DS.SetTypeSpecError();
1545 goto DoneWithDeclSpec;
1546 }
1547 if (!Tok.is(tok::identifier))
1548 continue;
1549 }
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Chris Lattner3bd934a2008-07-26 01:18:38 +00001551 // This identifier can only be a typedef name if we haven't already seen
1552 // a type-specifier. Without this check we misparse:
1553 // typedef int X; struct Y { short X; }; as 'short int'.
1554 if (DS.hasTypeSpecifier())
1555 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001556
John Thompson82287d12010-02-05 00:12:22 +00001557 // Check for need to substitute AltiVec keyword tokens.
1558 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1559 break;
1560
Chris Lattner3bd934a2008-07-26 01:18:38 +00001561 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001562 ParsedType TypeRep =
1563 Actions.getTypeName(*Tok.getIdentifierInfo(),
1564 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001565
Chris Lattnerc199ab32009-04-12 20:42:31 +00001566 // If this is not a typedef name, don't parse it as part of the declspec,
1567 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001568 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001569 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001570 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001571 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001572
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001573 // If we're in a context where the identifier could be a class name,
1574 // check whether this is a constructor declaration.
1575 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001576 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001577 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001578 goto DoneWithDeclSpec;
1579
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001580 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001581 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001582 if (isInvalid)
1583 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001584
Chris Lattner3bd934a2008-07-26 01:18:38 +00001585 DS.SetRangeEnd(Tok.getLocation());
1586 ConsumeToken(); // The identifier
1587
1588 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1589 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001590 // Objective-C interface.
1591 if (Tok.is(tok::less) && getLang().ObjC1)
1592 ParseObjCProtocolQualifiers(DS);
1593
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001594 // Need to support trailing type qualifiers (e.g. "id<p> const").
1595 // If a type specifier follows, it will be diagnosed elsewhere.
1596 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001597 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001598
1599 // type-name
1600 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001601 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001602 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001603 // This template-id does not refer to a type name, so we're
1604 // done with the type-specifiers.
1605 goto DoneWithDeclSpec;
1606 }
1607
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001608 // If we're in a context where the template-id could be a
1609 // constructor name or specialization, check whether this is a
1610 // constructor declaration.
1611 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001612 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001613 isConstructorDeclarator())
1614 goto DoneWithDeclSpec;
1615
Douglas Gregor39a8de12009-02-25 19:37:18 +00001616 // Turn the template-id annotation token into a type annotation
1617 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001618 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001619 continue;
1620 }
1621
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 // GNU attributes support.
1623 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001624 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001625 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001626
1627 // Microsoft declspec support.
1628 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001629 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001630 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Steve Naroff239f0732008-12-25 14:16:32 +00001632 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001633 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001634 // FIXME: Add handling here!
1635 break;
1636
1637 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001638 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001639 case tok::kw___cdecl:
1640 case tok::kw___stdcall:
1641 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001642 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001643 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001644 continue;
1645
Dawn Perchik52fc3142010-09-03 01:29:35 +00001646 // Borland single token adornments.
1647 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001648 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001649 continue;
1650
Peter Collingbournef315fa82011-02-14 01:42:53 +00001651 // OpenCL single token adornments.
1652 case tok::kw___kernel:
1653 ParseOpenCLAttributes(DS.getAttributes());
1654 continue;
1655
Reid Spencer5f016e22007-07-11 17:01:13 +00001656 // storage-class-specifier
1657 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001658 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001659 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001660 break;
1661 case tok::kw_extern:
1662 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001663 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001664 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001665 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001666 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001667 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001668 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001669 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001670 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001671 case tok::kw_static:
1672 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001673 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001674 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001675 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001676 break;
1677 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001678 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001679 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1680 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1681 DiagID, getLang());
1682 if (!isInvalid)
1683 Diag(Tok, diag::auto_storage_class)
1684 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1685 }
1686 else
1687 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1688 DiagID);
1689 }
Anders Carlssone89d1592009-06-26 18:41:36 +00001690 else
John McCallfec54012009-08-03 20:12:06 +00001691 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001692 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001693 break;
1694 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001695 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001696 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001697 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001698 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001699 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001700 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001701 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001702 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001703 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001705
Reid Spencer5f016e22007-07-11 17:01:13 +00001706 // function-specifier
1707 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001708 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001709 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001710 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001711 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001712 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001713 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001714 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001715 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001716
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001717 // friend
1718 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001719 if (DSContext == DSC_class)
1720 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1721 else {
1722 PrevSpec = ""; // not actually used by the diagnostic
1723 DiagID = diag::err_friend_invalid_in_context;
1724 isInvalid = true;
1725 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001726 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Sebastian Redl2ac67232009-11-05 15:47:02 +00001728 // constexpr
1729 case tok::kw_constexpr:
1730 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1731 break;
1732
Chris Lattner80d0c892009-01-21 19:48:37 +00001733 // type-specifier
1734 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001735 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1736 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001737 break;
1738 case tok::kw_long:
1739 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001740 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1741 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001742 else
John McCallfec54012009-08-03 20:12:06 +00001743 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1744 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001745 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001746 case tok::kw___int64:
1747 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1748 DiagID);
1749 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001750 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001751 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1752 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001753 break;
1754 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001755 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1756 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001757 break;
1758 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001759 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1760 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001761 break;
1762 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001763 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1764 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001765 break;
1766 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001767 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1768 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001769 break;
1770 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001771 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1772 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001773 break;
1774 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1776 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001777 break;
1778 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001779 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1780 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001781 break;
1782 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001783 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1784 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001785 break;
1786 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001787 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1788 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001789 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001790 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001791 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1792 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001793 break;
1794 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001795 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1796 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001797 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001798 case tok::kw_bool:
1799 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001800 if (Tok.is(tok::kw_bool) &&
1801 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1802 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1803 PrevSpec = ""; // Not used by the diagnostic.
1804 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001805 // For better error recovery.
1806 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001807 isInvalid = true;
1808 } else {
1809 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1810 DiagID);
1811 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001812 break;
1813 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001814 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1815 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001816 break;
1817 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001818 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1819 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001820 break;
1821 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001822 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1823 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001824 break;
John Thompson82287d12010-02-05 00:12:22 +00001825 case tok::kw___vector:
1826 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1827 break;
1828 case tok::kw___pixel:
1829 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1830 break;
John McCalla5fc4722011-04-09 22:50:59 +00001831 case tok::kw___unknown_anytype:
1832 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1833 PrevSpec, DiagID);
1834 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001835
1836 // class-specifier:
1837 case tok::kw_class:
1838 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001839 case tok::kw_union: {
1840 tok::TokenKind Kind = Tok.getKind();
1841 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001842 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001843 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001844 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001845
1846 // enum-specifier:
1847 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001848 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001849 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001850 continue;
1851
1852 // cv-qualifier:
1853 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001854 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1855 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001856 break;
1857 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001858 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1859 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001860 break;
1861 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001862 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1863 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001864 break;
1865
Douglas Gregord57959a2009-03-27 23:10:48 +00001866 // C++ typename-specifier:
1867 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001868 if (TryAnnotateTypeOrScopeToken()) {
1869 DS.SetTypeSpecError();
1870 goto DoneWithDeclSpec;
1871 }
1872 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001873 continue;
1874 break;
1875
Chris Lattner80d0c892009-01-21 19:48:37 +00001876 // GNU typeof support.
1877 case tok::kw_typeof:
1878 ParseTypeofSpecifier(DS);
1879 continue;
1880
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001881 case tok::kw_decltype:
1882 ParseDecltypeSpecifier(DS);
1883 continue;
1884
Sean Huntdb5d44b2011-05-19 05:37:45 +00001885 case tok::kw___underlying_type:
1886 ParseUnderlyingTypeSpecifier(DS);
1887
Peter Collingbourne207f4d82011-03-18 22:38:29 +00001888 // OpenCL qualifiers:
1889 case tok::kw_private:
1890 if (!getLang().OpenCL)
1891 goto DoneWithDeclSpec;
1892 case tok::kw___private:
1893 case tok::kw___global:
1894 case tok::kw___local:
1895 case tok::kw___constant:
1896 case tok::kw___read_only:
1897 case tok::kw___write_only:
1898 case tok::kw___read_write:
1899 ParseOpenCLQualifiers(DS);
1900 break;
1901
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001902 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001903 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001904 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1905 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001906 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001907 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Douglas Gregor46f936e2010-11-19 17:10:50 +00001909 if (!ParseObjCProtocolQualifiers(DS))
1910 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1911 << FixItHint::CreateInsertion(Loc, "id")
1912 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001913
1914 // Need to support trailing type qualifiers (e.g. "id<p> const").
1915 // If a type specifier follows, it will be diagnosed elsewhere.
1916 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001917 }
John McCallfec54012009-08-03 20:12:06 +00001918 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001919 if (isInvalid) {
1920 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001921 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001922
1923 if (DiagID == diag::ext_duplicate_declspec)
1924 Diag(Tok, DiagID)
1925 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1926 else
1927 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001928 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001929
Chris Lattner81c018d2008-03-13 06:29:04 +00001930 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001931 if (DiagID != diag::err_bool_redeclaration)
1932 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001933 }
1934}
Douglas Gregoradcac882008-12-01 23:54:00 +00001935
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001936/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001937/// primarily follow the C++ grammar with additions for C99 and GNU,
1938/// which together subsume the C grammar. Note that the C++
1939/// type-specifier also includes the C type-qualifier (for const,
1940/// volatile, and C99 restrict). Returns true if a type-specifier was
1941/// found (and parsed), false otherwise.
1942///
1943/// type-specifier: [C++ 7.1.5]
1944/// simple-type-specifier
1945/// class-specifier
1946/// enum-specifier
1947/// elaborated-type-specifier [TODO]
1948/// cv-qualifier
1949///
1950/// cv-qualifier: [C++ 7.1.5.1]
1951/// 'const'
1952/// 'volatile'
1953/// [C99] 'restrict'
1954///
1955/// simple-type-specifier: [ C++ 7.1.5.2]
1956/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1957/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1958/// 'char'
1959/// 'wchar_t'
1960/// 'bool'
1961/// 'short'
1962/// 'int'
1963/// 'long'
1964/// 'signed'
1965/// 'unsigned'
1966/// 'float'
1967/// 'double'
1968/// 'void'
1969/// [C99] '_Bool'
1970/// [C99] '_Complex'
1971/// [C99] '_Imaginary' // Removed in TC2?
1972/// [GNU] '_Decimal32'
1973/// [GNU] '_Decimal64'
1974/// [GNU] '_Decimal128'
1975/// [GNU] typeof-specifier
1976/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1977/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001978/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001979/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001980bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001981 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001982 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001983 const ParsedTemplateInfo &TemplateInfo,
1984 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001985 SourceLocation Loc = Tok.getLocation();
1986
1987 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001988 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001989 // If we already have a type specifier, this identifier is not a type.
1990 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1991 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1992 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1993 return false;
John Thompson82287d12010-02-05 00:12:22 +00001994 // Check for need to substitute AltiVec keyword tokens.
1995 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1996 break;
1997 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001998 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001999 // Annotate typenames and C++ scope specifiers. If we get one, just
2000 // recurse to handle whatever we get.
2001 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002002 return true;
2003 if (Tok.is(tok::identifier))
2004 return false;
2005 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2006 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002007 case tok::coloncolon: // ::foo::bar
2008 if (NextToken().is(tok::kw_new) || // ::new
2009 NextToken().is(tok::kw_delete)) // ::delete
2010 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Chris Lattner166a8fc2009-01-04 23:41:41 +00002012 // Annotate typenames and C++ scope specifiers. If we get one, just
2013 // recurse to handle whatever we get.
2014 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002015 return true;
2016 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2017 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Douglas Gregor12e083c2008-11-07 15:42:26 +00002019 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002020 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002021 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002022 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2023 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002024 DiagID, T);
2025 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002026 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002027 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2028 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002029
Douglas Gregor12e083c2008-11-07 15:42:26 +00002030 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2031 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2032 // Objective-C interface. If we don't have Objective-C or a '<', this is
2033 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002034 if (Tok.is(tok::less) && getLang().ObjC1)
2035 ParseObjCProtocolQualifiers(DS);
2036
Douglas Gregor12e083c2008-11-07 15:42:26 +00002037 return true;
2038 }
2039
2040 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002041 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002042 break;
2043 case tok::kw_long:
2044 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002045 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2046 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002047 else
John McCallfec54012009-08-03 20:12:06 +00002048 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2049 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002050 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002051 case tok::kw___int64:
2052 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2053 DiagID);
2054 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002055 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002056 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002057 break;
2058 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002059 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2060 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002061 break;
2062 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002063 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2064 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002065 break;
2066 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002067 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2068 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002069 break;
2070 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002071 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002072 break;
2073 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002074 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002075 break;
2076 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002077 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002078 break;
2079 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002080 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002081 break;
2082 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002083 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002084 break;
2085 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002086 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002087 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002088 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002089 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002090 break;
2091 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002092 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002093 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002094 case tok::kw_bool:
2095 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002096 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002097 break;
2098 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002099 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2100 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002101 break;
2102 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002103 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2104 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002105 break;
2106 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002107 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2108 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002109 break;
John Thompson82287d12010-02-05 00:12:22 +00002110 case tok::kw___vector:
2111 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2112 break;
2113 case tok::kw___pixel:
2114 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2115 break;
2116
Douglas Gregor12e083c2008-11-07 15:42:26 +00002117 // class-specifier:
2118 case tok::kw_class:
2119 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002120 case tok::kw_union: {
2121 tok::TokenKind Kind = Tok.getKind();
2122 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002123 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2124 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002125 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002126 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002127
2128 // enum-specifier:
2129 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002130 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002131 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002132 return true;
2133
2134 // cv-qualifier:
2135 case tok::kw_const:
2136 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002137 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002138 break;
2139 case tok::kw_volatile:
2140 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002141 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002142 break;
2143 case tok::kw_restrict:
2144 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002145 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002146 break;
2147
2148 // GNU typeof support.
2149 case tok::kw_typeof:
2150 ParseTypeofSpecifier(DS);
2151 return true;
2152
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002153 // C++0x decltype support.
2154 case tok::kw_decltype:
2155 ParseDecltypeSpecifier(DS);
2156 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002157
Sean Huntdb5d44b2011-05-19 05:37:45 +00002158 // C++0x type traits support.
2159 case tok::kw___underlying_type:
2160 ParseUnderlyingTypeSpecifier(DS);
2161 return true;
2162
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002163 // OpenCL qualifiers:
2164 case tok::kw_private:
2165 if (!getLang().OpenCL)
2166 return false;
2167 case tok::kw___private:
2168 case tok::kw___global:
2169 case tok::kw___local:
2170 case tok::kw___constant:
2171 case tok::kw___read_only:
2172 case tok::kw___write_only:
2173 case tok::kw___read_write:
2174 ParseOpenCLQualifiers(DS);
2175 break;
2176
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002177 // C++0x auto support.
2178 case tok::kw_auto:
2179 if (!getLang().CPlusPlus0x)
2180 return false;
2181
John McCallfec54012009-08-03 20:12:06 +00002182 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002183 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002184
Eli Friedman290eeb02009-06-08 23:27:34 +00002185 case tok::kw___ptr64:
2186 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002187 case tok::kw___cdecl:
2188 case tok::kw___stdcall:
2189 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002190 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00002191 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002192 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002193
Dawn Perchik52fc3142010-09-03 01:29:35 +00002194 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002195 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002196 return true;
2197
Douglas Gregor12e083c2008-11-07 15:42:26 +00002198 default:
2199 // Not a type-specifier; do nothing.
2200 return false;
2201 }
2202
2203 // If the specifier combination wasn't legal, issue a diagnostic.
2204 if (isInvalid) {
2205 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002206 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002207 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002208 }
2209 DS.SetRangeEnd(Tok.getLocation());
2210 ConsumeToken(); // whatever we parsed above.
2211 return true;
2212}
Reid Spencer5f016e22007-07-11 17:01:13 +00002213
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002214/// ParseStructDeclaration - Parse a struct declaration without the terminating
2215/// semicolon.
2216///
Reid Spencer5f016e22007-07-11 17:01:13 +00002217/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002218/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002219/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002220/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002221/// struct-declarator-list:
2222/// struct-declarator
2223/// struct-declarator-list ',' struct-declarator
2224/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2225/// struct-declarator:
2226/// declarator
2227/// [GNU] declarator attributes[opt]
2228/// declarator[opt] ':' constant-expression
2229/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2230///
Chris Lattnere1359422008-04-10 06:46:29 +00002231void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002232ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002233 if (Tok.is(tok::kw___extension__)) {
2234 // __extension__ silences extension warnings in the subexpression.
2235 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002236 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002237 return ParseStructDeclaration(DS, Fields);
2238 }
Mike Stump1eb44332009-09-09 15:08:12 +00002239
Steve Naroff28a7ca82007-08-20 22:28:22 +00002240 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002241 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002242
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002243 // If there are no declarators, this is a free-standing declaration
2244 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002245 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002246 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002247 return;
2248 }
2249
2250 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002251 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002252 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002253 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002254 FieldDeclarator DeclaratorInfo(DS);
2255
2256 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002257 if (!FirstDeclarator)
2258 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002259
Steve Naroff28a7ca82007-08-20 22:28:22 +00002260 /// struct-declarator: declarator
2261 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002262 if (Tok.isNot(tok::colon)) {
2263 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2264 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002265 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002266 }
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Chris Lattner04d66662007-10-09 17:33:22 +00002268 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002269 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002270 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002271 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002272 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002273 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002274 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002275 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002276
Steve Naroff28a7ca82007-08-20 22:28:22 +00002277 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002278 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002279
John McCallbdd563e2009-11-03 02:38:08 +00002280 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002281 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002282 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002283
Steve Naroff28a7ca82007-08-20 22:28:22 +00002284 // If we don't have a comma, it is either the end of the list (a ';')
2285 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002286 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002287 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002288
Steve Naroff28a7ca82007-08-20 22:28:22 +00002289 // Consume the comma.
2290 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002291
John McCallbdd563e2009-11-03 02:38:08 +00002292 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002293 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002294}
2295
2296/// ParseStructUnionBody
2297/// struct-contents:
2298/// struct-declaration-list
2299/// [EXT] empty
2300/// [GNU] "struct-declaration-list" without terminatoring ';'
2301/// struct-declaration-list:
2302/// struct-declaration
2303/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002304/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002305///
Reid Spencer5f016e22007-07-11 17:01:13 +00002306void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002307 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002308 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2309 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002310
Reid Spencer5f016e22007-07-11 17:01:13 +00002311 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002312
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002313 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002314 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002315
Reid Spencer5f016e22007-07-11 17:01:13 +00002316 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2317 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002318 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002319 Diag(Tok, diag::ext_empty_struct_union)
2320 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002321
John McCalld226f652010-08-21 09:40:31 +00002322 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002323
Reid Spencer5f016e22007-07-11 17:01:13 +00002324 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002325 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002326 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002327
Reid Spencer5f016e22007-07-11 17:01:13 +00002328 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002329 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002330 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002331 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002332 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002333 ConsumeToken();
2334 continue;
2335 }
Chris Lattnere1359422008-04-10 06:46:29 +00002336
2337 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002338 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002339
John McCallbdd563e2009-11-03 02:38:08 +00002340 if (!Tok.is(tok::at)) {
2341 struct CFieldCallback : FieldCallback {
2342 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002343 Decl *TagDecl;
2344 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002345
John McCalld226f652010-08-21 09:40:31 +00002346 CFieldCallback(Parser &P, Decl *TagDecl,
2347 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002348 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2349
John McCalld226f652010-08-21 09:40:31 +00002350 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002351 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002352 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002353 FD.D.getDeclSpec().getSourceRange().getBegin(),
2354 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002355 FieldDecls.push_back(Field);
2356 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002357 }
John McCallbdd563e2009-11-03 02:38:08 +00002358 } Callback(*this, TagDecl, FieldDecls);
2359
2360 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002361 } else { // Handle @defs
2362 ConsumeToken();
2363 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2364 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002365 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002366 continue;
2367 }
2368 ConsumeToken();
2369 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2370 if (!Tok.is(tok::identifier)) {
2371 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002372 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002373 continue;
2374 }
John McCalld226f652010-08-21 09:40:31 +00002375 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002376 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002377 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002378 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2379 ConsumeToken();
2380 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002381 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002382
Chris Lattner04d66662007-10-09 17:33:22 +00002383 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002384 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002385 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002386 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002387 break;
2388 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002389 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2390 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002391 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002392 // If we stopped at a ';', eat it.
2393 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002394 }
2395 }
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Steve Naroff60fccee2007-10-29 21:38:07 +00002397 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002398
John McCall0b7e6782011-03-24 11:26:52 +00002399 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002400 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002401 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002402
Douglas Gregor23c94db2010-07-02 17:43:08 +00002403 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00002404 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002405 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00002406 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002407 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002408 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002409}
2410
Reid Spencer5f016e22007-07-11 17:01:13 +00002411/// ParseEnumSpecifier
2412/// enum-specifier: [C99 6.7.2.2]
2413/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002414///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002415/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2416/// '}' attributes[opt]
2417/// 'enum' identifier
2418/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002419///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002420/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2421/// [C++0x] enum-head '{' enumerator-list ',' '}'
2422///
2423/// enum-head: [C++0x]
2424/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2425/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2426///
2427/// enum-key: [C++0x]
2428/// 'enum'
2429/// 'enum' 'class'
2430/// 'enum' 'struct'
2431///
2432/// enum-base: [C++0x]
2433/// ':' type-specifier-seq
2434///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002435/// [C++] elaborated-type-specifier:
2436/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2437///
Chris Lattner4c97d762009-04-12 21:49:30 +00002438void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002439 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002440 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002441 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002442 if (Tok.is(tok::code_completion)) {
2443 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002444 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00002445 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00002446 }
2447
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002448 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002449 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002450 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002451
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002452 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002453 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00002454 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002455 return;
2456
2457 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002458 Diag(Tok, diag::err_expected_ident);
2459 if (Tok.isNot(tok::l_brace)) {
2460 // Has no name and is not a definition.
2461 // Skip the rest of this declarator, up until the comma or semicolon.
2462 SkipUntil(tok::comma, true);
2463 return;
2464 }
2465 }
2466 }
Mike Stump1eb44332009-09-09 15:08:12 +00002467
Douglas Gregor86f208c2011-02-22 20:32:04 +00002468 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002469 bool IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002470 bool IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002471
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002472 if (getLang().CPlusPlus0x &&
2473 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002474 IsScopedEnum = true;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002475 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2476 ConsumeToken();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002477 }
2478
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002479 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002480 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2481 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002482 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002483
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002484 // Skip the rest of this declarator, up until the comma or semicolon.
2485 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002486 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002487 }
Mike Stump1eb44332009-09-09 15:08:12 +00002488
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002489 // If an identifier is present, consume and remember it.
2490 IdentifierInfo *Name = 0;
2491 SourceLocation NameLoc;
2492 if (Tok.is(tok::identifier)) {
2493 Name = Tok.getIdentifierInfo();
2494 NameLoc = ConsumeToken();
2495 }
Mike Stump1eb44332009-09-09 15:08:12 +00002496
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002497 if (!Name && IsScopedEnum) {
2498 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2499 // declaration of a scoped enumeration.
2500 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2501 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002502 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002503 }
2504
2505 TypeResult BaseType;
2506
Douglas Gregora61b3e72010-12-01 17:42:47 +00002507 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002508 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002509 bool PossibleBitfield = false;
2510 if (getCurScope()->getFlags() & Scope::ClassScope) {
2511 // If we're in class scope, this can either be an enum declaration with
2512 // an underlying type, or a declaration of a bitfield member. We try to
2513 // use a simple disambiguation scheme first to catch the common cases
2514 // (integer literal, sizeof); if it's still ambiguous, we then consider
2515 // anything that's a simple-type-specifier followed by '(' as an
2516 // expression. This suffices because function types are not valid
2517 // underlying types anyway.
2518 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2519 // If the next token starts an expression, we know we're parsing a
2520 // bit-field. This is the common case.
2521 if (TPR == TPResult::True())
2522 PossibleBitfield = true;
2523 // If the next token starts a type-specifier-seq, it may be either a
2524 // a fixed underlying type or the start of a function-style cast in C++;
2525 // lookahead one more token to see if it's obvious that we have a
2526 // fixed underlying type.
2527 else if (TPR == TPResult::False() &&
2528 GetLookAheadToken(2).getKind() == tok::semi) {
2529 // Consume the ':'.
2530 ConsumeToken();
2531 } else {
2532 // We have the start of a type-specifier-seq, so we have to perform
2533 // tentative parsing to determine whether we have an expression or a
2534 // type.
2535 TentativeParsingAction TPA(*this);
2536
2537 // Consume the ':'.
2538 ConsumeToken();
2539
Douglas Gregor86f208c2011-02-22 20:32:04 +00002540 if ((getLang().CPlusPlus &&
2541 isCXXDeclarationSpecifier() != TPResult::True()) ||
2542 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002543 // We'll parse this as a bitfield later.
2544 PossibleBitfield = true;
2545 TPA.Revert();
2546 } else {
2547 // We have a type-specifier-seq.
2548 TPA.Commit();
2549 }
2550 }
2551 } else {
2552 // Consume the ':'.
2553 ConsumeToken();
2554 }
2555
2556 if (!PossibleBitfield) {
2557 SourceRange Range;
2558 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002559
2560 if (!getLang().CPlusPlus0x)
2561 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2562 << Range;
Douglas Gregora61b3e72010-12-01 17:42:47 +00002563 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002564 }
2565
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002566 // There are three options here. If we have 'enum foo;', then this is a
2567 // forward declaration. If we have 'enum foo {...' then this is a
2568 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2569 //
2570 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2571 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2572 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2573 //
John McCallf312b1e2010-08-26 23:41:50 +00002574 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002575 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002576 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002577 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002578 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002579 else
John McCallf312b1e2010-08-26 23:41:50 +00002580 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002581
2582 // enums cannot be templates, although they can be referenced from a
2583 // template.
2584 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002585 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002586 Diag(Tok, diag::err_enum_template);
2587
2588 // Skip the rest of this declarator, up until the comma or semicolon.
2589 SkipUntil(tok::comma, true);
2590 return;
2591 }
2592
Douglas Gregorb9075602011-02-22 02:55:24 +00002593 if (!Name && TUK != Sema::TUK_Definition) {
2594 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2595
2596 // Skip the rest of this declarator, up until the comma or semicolon.
2597 SkipUntil(tok::comma, true);
2598 return;
2599 }
2600
Douglas Gregor402abb52009-05-28 23:31:59 +00002601 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002602 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002603 const char *PrevSpec = 0;
2604 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002605 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002606 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002607 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002608 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002609 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002610 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002611
Douglas Gregor48c89f42010-04-24 16:38:41 +00002612 if (IsDependent) {
2613 // This enum has a dependent nested-name-specifier. Handle it as a
2614 // dependent tag.
2615 if (!Name) {
2616 DS.SetTypeSpecError();
2617 Diag(Tok, diag::err_expected_type_name_after_typename);
2618 return;
2619 }
2620
Douglas Gregor23c94db2010-07-02 17:43:08 +00002621 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002622 TUK, SS, Name, StartLoc,
2623 NameLoc);
2624 if (Type.isInvalid()) {
2625 DS.SetTypeSpecError();
2626 return;
2627 }
2628
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002629 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2630 NameLoc.isValid() ? NameLoc : StartLoc,
2631 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002632 Diag(StartLoc, DiagID) << PrevSpec;
2633
2634 return;
2635 }
Mike Stump1eb44332009-09-09 15:08:12 +00002636
John McCalld226f652010-08-21 09:40:31 +00002637 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002638 // The action failed to produce an enumeration tag. If this is a
2639 // definition, consume the entire definition.
2640 if (Tok.is(tok::l_brace)) {
2641 ConsumeBrace();
2642 SkipUntil(tok::r_brace);
2643 }
2644
2645 DS.SetTypeSpecError();
2646 return;
2647 }
2648
Chris Lattner04d66662007-10-09 17:33:22 +00002649 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002650 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002651
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002652 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2653 NameLoc.isValid() ? NameLoc : StartLoc,
2654 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002655 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002656}
2657
2658/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2659/// enumerator-list:
2660/// enumerator
2661/// enumerator-list ',' enumerator
2662/// enumerator:
2663/// enumeration-constant
2664/// enumeration-constant '=' constant-expression
2665/// enumeration-constant:
2666/// identifier
2667///
John McCalld226f652010-08-21 09:40:31 +00002668void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002669 // Enter the scope of the enum body and start the definition.
2670 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002671 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002672
Reid Spencer5f016e22007-07-11 17:01:13 +00002673 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002674
Chris Lattner7946dd32007-08-27 17:24:30 +00002675 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002676 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002677 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002678
John McCalld226f652010-08-21 09:40:31 +00002679 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002680
John McCalld226f652010-08-21 09:40:31 +00002681 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002682
Reid Spencer5f016e22007-07-11 17:01:13 +00002683 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002684 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002685 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2686 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002687
John McCall5b629aa2010-10-22 23:36:17 +00002688 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002689 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002690 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002691
Reid Spencer5f016e22007-07-11 17:01:13 +00002692 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002693 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002694 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002695 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002696 AssignedVal = ParseConstantExpression();
2697 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002698 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002699 }
Mike Stump1eb44332009-09-09 15:08:12 +00002700
Reid Spencer5f016e22007-07-11 17:01:13 +00002701 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002702 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2703 LastEnumConstDecl,
2704 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002705 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002706 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002707 EnumConstantDecls.push_back(EnumConstDecl);
2708 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002709
Douglas Gregor751f6922010-09-07 14:51:08 +00002710 if (Tok.is(tok::identifier)) {
2711 // We're missing a comma between enumerators.
2712 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2713 Diag(Loc, diag::err_enumerator_list_missing_comma)
2714 << FixItHint::CreateInsertion(Loc, ", ");
2715 continue;
2716 }
2717
Chris Lattner04d66662007-10-09 17:33:22 +00002718 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002719 break;
2720 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002721
2722 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002723 !(getLang().C99 || getLang().CPlusPlus0x))
2724 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2725 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002726 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002727 }
Mike Stump1eb44332009-09-09 15:08:12 +00002728
Reid Spencer5f016e22007-07-11 17:01:13 +00002729 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002730 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002731
Reid Spencer5f016e22007-07-11 17:01:13 +00002732 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002733 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002734 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002735
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002736 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2737 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002738 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002739
Douglas Gregor72de6672009-01-08 20:45:30 +00002740 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002741 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002742}
2743
2744/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002745/// start of a type-qualifier-list.
2746bool Parser::isTypeQualifier() const {
2747 switch (Tok.getKind()) {
2748 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002749
2750 // type-qualifier only in OpenCL
2751 case tok::kw_private:
2752 return getLang().OpenCL;
2753
Steve Naroff5f8aa692008-02-11 23:15:56 +00002754 // type-qualifier
2755 case tok::kw_const:
2756 case tok::kw_volatile:
2757 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002758 case tok::kw___private:
2759 case tok::kw___local:
2760 case tok::kw___global:
2761 case tok::kw___constant:
2762 case tok::kw___read_only:
2763 case tok::kw___read_write:
2764 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00002765 return true;
2766 }
2767}
2768
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002769/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2770/// is definitely a type-specifier. Return false if it isn't part of a type
2771/// specifier or if we're not sure.
2772bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2773 switch (Tok.getKind()) {
2774 default: return false;
2775 // type-specifiers
2776 case tok::kw_short:
2777 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002778 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002779 case tok::kw_signed:
2780 case tok::kw_unsigned:
2781 case tok::kw__Complex:
2782 case tok::kw__Imaginary:
2783 case tok::kw_void:
2784 case tok::kw_char:
2785 case tok::kw_wchar_t:
2786 case tok::kw_char16_t:
2787 case tok::kw_char32_t:
2788 case tok::kw_int:
2789 case tok::kw_float:
2790 case tok::kw_double:
2791 case tok::kw_bool:
2792 case tok::kw__Bool:
2793 case tok::kw__Decimal32:
2794 case tok::kw__Decimal64:
2795 case tok::kw__Decimal128:
2796 case tok::kw___vector:
2797
2798 // struct-or-union-specifier (C99) or class-specifier (C++)
2799 case tok::kw_class:
2800 case tok::kw_struct:
2801 case tok::kw_union:
2802 // enum-specifier
2803 case tok::kw_enum:
2804
2805 // typedef-name
2806 case tok::annot_typename:
2807 return true;
2808 }
2809}
2810
Steve Naroff5f8aa692008-02-11 23:15:56 +00002811/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002812/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002813bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002814 switch (Tok.getKind()) {
2815 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002816
Chris Lattner166a8fc2009-01-04 23:41:41 +00002817 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002818 if (TryAltiVecVectorToken())
2819 return true;
2820 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002821 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002822 // Annotate typenames and C++ scope specifiers. If we get one, just
2823 // recurse to handle whatever we get.
2824 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002825 return true;
2826 if (Tok.is(tok::identifier))
2827 return false;
2828 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002829
Chris Lattner166a8fc2009-01-04 23:41:41 +00002830 case tok::coloncolon: // ::foo::bar
2831 if (NextToken().is(tok::kw_new) || // ::new
2832 NextToken().is(tok::kw_delete)) // ::delete
2833 return false;
2834
Chris Lattner166a8fc2009-01-04 23:41:41 +00002835 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002836 return true;
2837 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002838
Reid Spencer5f016e22007-07-11 17:01:13 +00002839 // GNU attributes support.
2840 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002841 // GNU typeof support.
2842 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002843
Reid Spencer5f016e22007-07-11 17:01:13 +00002844 // type-specifiers
2845 case tok::kw_short:
2846 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002847 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002848 case tok::kw_signed:
2849 case tok::kw_unsigned:
2850 case tok::kw__Complex:
2851 case tok::kw__Imaginary:
2852 case tok::kw_void:
2853 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002854 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002855 case tok::kw_char16_t:
2856 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002857 case tok::kw_int:
2858 case tok::kw_float:
2859 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002860 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002861 case tok::kw__Bool:
2862 case tok::kw__Decimal32:
2863 case tok::kw__Decimal64:
2864 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002865 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002866
Chris Lattner99dc9142008-04-13 18:59:07 +00002867 // struct-or-union-specifier (C99) or class-specifier (C++)
2868 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002869 case tok::kw_struct:
2870 case tok::kw_union:
2871 // enum-specifier
2872 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002873
Reid Spencer5f016e22007-07-11 17:01:13 +00002874 // type-qualifier
2875 case tok::kw_const:
2876 case tok::kw_volatile:
2877 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002878
2879 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002880 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002881 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002882
Chris Lattner7c186be2008-10-20 00:25:30 +00002883 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2884 case tok::less:
2885 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002886
Steve Naroff239f0732008-12-25 14:16:32 +00002887 case tok::kw___cdecl:
2888 case tok::kw___stdcall:
2889 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002890 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002891 case tok::kw___w64:
2892 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002893 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002894
2895 case tok::kw___private:
2896 case tok::kw___local:
2897 case tok::kw___global:
2898 case tok::kw___constant:
2899 case tok::kw___read_only:
2900 case tok::kw___read_write:
2901 case tok::kw___write_only:
2902
Eli Friedman290eeb02009-06-08 23:27:34 +00002903 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002904
2905 case tok::kw_private:
2906 return getLang().OpenCL;
Reid Spencer5f016e22007-07-11 17:01:13 +00002907 }
2908}
2909
2910/// isDeclarationSpecifier() - Return true if the current token is part of a
2911/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002912///
2913/// \param DisambiguatingWithExpression True to indicate that the purpose of
2914/// this check is to disambiguate between an expression and a declaration.
2915bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002916 switch (Tok.getKind()) {
2917 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002918
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002919 case tok::kw_private:
2920 return getLang().OpenCL;
2921
Chris Lattner166a8fc2009-01-04 23:41:41 +00002922 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002923 // Unfortunate hack to support "Class.factoryMethod" notation.
2924 if (getLang().ObjC1 && NextToken().is(tok::period))
2925 return false;
John Thompson82287d12010-02-05 00:12:22 +00002926 if (TryAltiVecVectorToken())
2927 return true;
2928 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002929 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002930 // Annotate typenames and C++ scope specifiers. If we get one, just
2931 // recurse to handle whatever we get.
2932 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002933 return true;
2934 if (Tok.is(tok::identifier))
2935 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002936
2937 // If we're in Objective-C and we have an Objective-C class type followed
2938 // by an identifier and then either ':' or ']', in a place where an
2939 // expression is permitted, then this is probably a class message send
2940 // missing the initial '['. In this case, we won't consider this to be
2941 // the start of a declaration.
2942 if (DisambiguatingWithExpression &&
2943 isStartOfObjCClassMessageMissingOpenBracket())
2944 return false;
2945
John McCall9ba61662010-02-26 08:45:28 +00002946 return isDeclarationSpecifier();
2947
Chris Lattner166a8fc2009-01-04 23:41:41 +00002948 case tok::coloncolon: // ::foo::bar
2949 if (NextToken().is(tok::kw_new) || // ::new
2950 NextToken().is(tok::kw_delete)) // ::delete
2951 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002952
Chris Lattner166a8fc2009-01-04 23:41:41 +00002953 // Annotate typenames and C++ scope specifiers. If we get one, just
2954 // recurse to handle whatever we get.
2955 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002956 return true;
2957 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002958
Reid Spencer5f016e22007-07-11 17:01:13 +00002959 // storage-class-specifier
2960 case tok::kw_typedef:
2961 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002962 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002963 case tok::kw_static:
2964 case tok::kw_auto:
2965 case tok::kw_register:
2966 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002967
Reid Spencer5f016e22007-07-11 17:01:13 +00002968 // type-specifiers
2969 case tok::kw_short:
2970 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002971 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002972 case tok::kw_signed:
2973 case tok::kw_unsigned:
2974 case tok::kw__Complex:
2975 case tok::kw__Imaginary:
2976 case tok::kw_void:
2977 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002978 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002979 case tok::kw_char16_t:
2980 case tok::kw_char32_t:
2981
Reid Spencer5f016e22007-07-11 17:01:13 +00002982 case tok::kw_int:
2983 case tok::kw_float:
2984 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002985 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002986 case tok::kw__Bool:
2987 case tok::kw__Decimal32:
2988 case tok::kw__Decimal64:
2989 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002990 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002991
Chris Lattner99dc9142008-04-13 18:59:07 +00002992 // struct-or-union-specifier (C99) or class-specifier (C++)
2993 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002994 case tok::kw_struct:
2995 case tok::kw_union:
2996 // enum-specifier
2997 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002998
Reid Spencer5f016e22007-07-11 17:01:13 +00002999 // type-qualifier
3000 case tok::kw_const:
3001 case tok::kw_volatile:
3002 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003003
Reid Spencer5f016e22007-07-11 17:01:13 +00003004 // function-specifier
3005 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003006 case tok::kw_virtual:
3007 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003008
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003009 // static_assert-declaration
3010 case tok::kw__Static_assert:
3011
Chris Lattner1ef08762007-08-09 17:01:07 +00003012 // GNU typeof support.
3013 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003014
Chris Lattner1ef08762007-08-09 17:01:07 +00003015 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003016 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003017 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003018
Francois Pichete3d49b42011-06-19 08:02:06 +00003019 // C++0x decltype.
3020 case tok::kw_decltype:
3021 return true;
3022
Chris Lattnerf3948c42008-07-26 03:38:44 +00003023 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3024 case tok::less:
3025 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003026
Douglas Gregord9d75e52011-04-27 05:41:15 +00003027 // typedef-name
3028 case tok::annot_typename:
3029 return !DisambiguatingWithExpression ||
3030 !isStartOfObjCClassMessageMissingOpenBracket();
3031
Steve Naroff47f52092009-01-06 19:34:12 +00003032 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003033 case tok::kw___cdecl:
3034 case tok::kw___stdcall:
3035 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003036 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003037 case tok::kw___w64:
3038 case tok::kw___ptr64:
3039 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003040 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003041
3042 case tok::kw___private:
3043 case tok::kw___local:
3044 case tok::kw___global:
3045 case tok::kw___constant:
3046 case tok::kw___read_only:
3047 case tok::kw___read_write:
3048 case tok::kw___write_only:
3049
Eli Friedman290eeb02009-06-08 23:27:34 +00003050 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003051 }
3052}
3053
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003054bool Parser::isConstructorDeclarator() {
3055 TentativeParsingAction TPA(*this);
3056
3057 // Parse the C++ scope specifier.
3058 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003059 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003060 TPA.Revert();
3061 return false;
3062 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003063
3064 // Parse the constructor name.
3065 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3066 // We already know that we have a constructor name; just consume
3067 // the token.
3068 ConsumeToken();
3069 } else {
3070 TPA.Revert();
3071 return false;
3072 }
3073
3074 // Current class name must be followed by a left parentheses.
3075 if (Tok.isNot(tok::l_paren)) {
3076 TPA.Revert();
3077 return false;
3078 }
3079 ConsumeParen();
3080
3081 // A right parentheses or ellipsis signals that we have a constructor.
3082 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3083 TPA.Revert();
3084 return true;
3085 }
3086
3087 // If we need to, enter the specified scope.
3088 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003089 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003090 DeclScopeObj.EnterDeclaratorScope();
3091
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003092 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003093 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003094 MaybeParseMicrosoftAttributes(Attrs);
3095
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003096 // Check whether the next token(s) are part of a declaration
3097 // specifier, in which case we have the start of a parameter and,
3098 // therefore, we know that this is a constructor.
3099 bool IsConstructor = isDeclarationSpecifier();
3100 TPA.Revert();
3101 return IsConstructor;
3102}
Reid Spencer5f016e22007-07-11 17:01:13 +00003103
3104/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003105/// type-qualifier-list: [C99 6.7.5]
3106/// type-qualifier
3107/// [vendor] attributes
3108/// [ only if VendorAttributesAllowed=true ]
3109/// type-qualifier-list type-qualifier
3110/// [vendor] type-qualifier-list attributes
3111/// [ only if VendorAttributesAllowed=true ]
3112/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3113/// [ only if CXX0XAttributesAllowed=true ]
3114/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003115///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003116void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3117 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003118 bool CXX0XAttributesAllowed) {
3119 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3120 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003121 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003122 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003123 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003124 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003125 else
3126 Diag(Loc, diag::err_attributes_not_allowed);
3127 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003128
3129 SourceLocation EndLoc;
3130
Reid Spencer5f016e22007-07-11 17:01:13 +00003131 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003132 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003133 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003134 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003135 SourceLocation Loc = Tok.getLocation();
3136
3137 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003138 case tok::code_completion:
3139 Actions.CodeCompleteTypeQualifiers(DS);
3140 ConsumeCodeCompletionToken();
3141 break;
3142
Reid Spencer5f016e22007-07-11 17:01:13 +00003143 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003144 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3145 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003146 break;
3147 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003148 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3149 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003150 break;
3151 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003152 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3153 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003154 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003155
3156 // OpenCL qualifiers:
3157 case tok::kw_private:
3158 if (!getLang().OpenCL)
3159 goto DoneWithTypeQuals;
3160 case tok::kw___private:
3161 case tok::kw___global:
3162 case tok::kw___local:
3163 case tok::kw___constant:
3164 case tok::kw___read_only:
3165 case tok::kw___write_only:
3166 case tok::kw___read_write:
3167 ParseOpenCLQualifiers(DS);
3168 break;
3169
Eli Friedman290eeb02009-06-08 23:27:34 +00003170 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003171 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00003172 case tok::kw___cdecl:
3173 case tok::kw___stdcall:
3174 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003175 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003176 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003177 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003178 continue;
3179 }
3180 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003181 case tok::kw___pascal:
3182 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003183 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003184 continue;
3185 }
3186 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003187 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003188 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003189 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003190 continue; // do *not* consume the next token!
3191 }
3192 // otherwise, FALL THROUGH!
3193 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003194 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003195 // If this is not a type-qualifier token, we're done reading type
3196 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003197 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003198 if (EndLoc.isValid())
3199 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003200 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003201 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003202
Reid Spencer5f016e22007-07-11 17:01:13 +00003203 // If the specifier combination wasn't legal, issue a diagnostic.
3204 if (isInvalid) {
3205 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003206 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003207 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003208 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003209 }
3210}
3211
3212
3213/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3214///
3215void Parser::ParseDeclarator(Declarator &D) {
3216 /// This implements the 'declarator' production in the C grammar, then checks
3217 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003218 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003219}
3220
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003221/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3222/// is parsed by the function passed to it. Pass null, and the direct-declarator
3223/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003224/// ptr-operator production.
3225///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003226/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3227/// [C] pointer[opt] direct-declarator
3228/// [C++] direct-declarator
3229/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003230///
3231/// pointer: [C99 6.7.5]
3232/// '*' type-qualifier-list[opt]
3233/// '*' type-qualifier-list[opt] pointer
3234///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003235/// ptr-operator:
3236/// '*' cv-qualifier-seq[opt]
3237/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003238/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003239/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003240/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003241/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003242void Parser::ParseDeclaratorInternal(Declarator &D,
3243 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003244 if (Diags.hasAllExtensionsSilenced())
3245 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003246
Sebastian Redlf30208a2009-01-24 21:16:55 +00003247 // C++ member pointers start with a '::' or a nested-name.
3248 // Member pointers get special handling, since there's no place for the
3249 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003250 if (getLang().CPlusPlus &&
3251 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3252 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003253 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003254 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003255
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003256 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003257 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003258 // The scope spec really belongs to the direct-declarator.
3259 D.getCXXScopeSpec() = SS;
3260 if (DirectDeclParser)
3261 (this->*DirectDeclParser)(D);
3262 return;
3263 }
3264
3265 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003266 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003267 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003268 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003269 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003270
3271 // Recurse to parse whatever is left.
3272 ParseDeclaratorInternal(D, DirectDeclParser);
3273
3274 // Sema will have to catch (syntactically invalid) pointers into global
3275 // scope. It has to catch pointers into namespace scope anyway.
3276 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003277 Loc),
3278 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003279 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003280 return;
3281 }
3282 }
3283
3284 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003285 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003286 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003287 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003288 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003289 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003290 if (DirectDeclParser)
3291 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003292 return;
3293 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003294
Sebastian Redl05532f22009-03-15 22:02:01 +00003295 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3296 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003297 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003298 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003299
Chris Lattner9af55002009-03-27 04:18:06 +00003300 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003301 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003302 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003303
Reid Spencer5f016e22007-07-11 17:01:13 +00003304 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003305 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003306
Reid Spencer5f016e22007-07-11 17:01:13 +00003307 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003308 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003309 if (Kind == tok::star)
3310 // Remember that we parsed a pointer type, and remember the type-quals.
3311 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003312 DS.getConstSpecLoc(),
3313 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003314 DS.getRestrictSpecLoc()),
3315 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003316 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003317 else
3318 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003319 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003320 Loc),
3321 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003322 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003323 } else {
3324 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003325 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003326
Sebastian Redl743de1f2009-03-23 00:00:23 +00003327 // Complain about rvalue references in C++03, but then go on and build
3328 // the declarator.
3329 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00003330 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003331
Reid Spencer5f016e22007-07-11 17:01:13 +00003332 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3333 // cv-qualifiers are introduced through the use of a typedef or of a
3334 // template type argument, in which case the cv-qualifiers are ignored.
3335 //
3336 // [GNU] Retricted references are allowed.
3337 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003338 // [C++0x] Attributes on references are not allowed.
3339 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003340 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003341
3342 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3343 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3344 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003345 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003346 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3347 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003348 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003349 }
3350
3351 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003352 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003353
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003354 if (D.getNumTypeObjects() > 0) {
3355 // C++ [dcl.ref]p4: There shall be no references to references.
3356 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3357 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003358 if (const IdentifierInfo *II = D.getIdentifier())
3359 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3360 << II;
3361 else
3362 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3363 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003364
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003365 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003366 // can go ahead and build the (technically ill-formed)
3367 // declarator: reference collapsing will take care of it.
3368 }
3369 }
3370
Reid Spencer5f016e22007-07-11 17:01:13 +00003371 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003372 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003373 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003374 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003375 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003376 }
3377}
3378
3379/// ParseDirectDeclarator
3380/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003381/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003382/// '(' declarator ')'
3383/// [GNU] '(' attributes declarator ')'
3384/// [C90] direct-declarator '[' constant-expression[opt] ']'
3385/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3386/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3387/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3388/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3389/// direct-declarator '(' parameter-type-list ')'
3390/// direct-declarator '(' identifier-list[opt] ')'
3391/// [GNU] direct-declarator '(' parameter-forward-declarations
3392/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003393/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3394/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003395/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003396///
3397/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003398/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003399/// '::'[opt] nested-name-specifier[opt] type-name
3400///
3401/// id-expression: [C++ 5.1]
3402/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003403/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003404///
3405/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003406/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003407/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003408/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003409/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003410/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003411///
Reid Spencer5f016e22007-07-11 17:01:13 +00003412void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003413 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003414
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003415 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3416 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003417 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003418 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003419 }
3420
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003421 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003422 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003423 // Change the declaration context for name lookup, until this function
3424 // is exited (and the declarator has been parsed).
3425 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003426 }
3427
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003428 // C++0x [dcl.fct]p14:
3429 // There is a syntactic ambiguity when an ellipsis occurs at the end
3430 // of a parameter-declaration-clause without a preceding comma. In
3431 // this case, the ellipsis is parsed as part of the
3432 // abstract-declarator if the type of the parameter names a template
3433 // parameter pack that has not been expanded; otherwise, it is parsed
3434 // as part of the parameter-declaration-clause.
3435 if (Tok.is(tok::ellipsis) &&
3436 !((D.getContext() == Declarator::PrototypeContext ||
3437 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003438 NextToken().is(tok::r_paren) &&
3439 !Actions.containsUnexpandedParameterPacks(D)))
3440 D.setEllipsisLoc(ConsumeToken());
3441
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003442 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3443 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3444 // We found something that indicates the start of an unqualified-id.
3445 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003446 bool AllowConstructorName;
3447 if (D.getDeclSpec().hasTypeSpecifier())
3448 AllowConstructorName = false;
3449 else if (D.getCXXScopeSpec().isSet())
3450 AllowConstructorName =
3451 (D.getContext() == Declarator::FileContext ||
3452 (D.getContext() == Declarator::MemberContext &&
3453 D.getDeclSpec().isFriendSpecified()));
3454 else
3455 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3456
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003457 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3458 /*EnteringContext=*/true,
3459 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003460 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003461 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003462 D.getName()) ||
3463 // Once we're past the identifier, if the scope was bad, mark the
3464 // whole declarator bad.
3465 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003466 D.SetIdentifier(0, Tok.getLocation());
3467 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003468 } else {
3469 // Parsed the unqualified-id; update range information and move along.
3470 if (D.getSourceRange().getBegin().isInvalid())
3471 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3472 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003473 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003474 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003475 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003476 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003477 assert(!getLang().CPlusPlus &&
3478 "There's a C++-specific check for tok::identifier above");
3479 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3480 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3481 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003482 goto PastIdentifier;
3483 }
3484
3485 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003486 // direct-declarator: '(' declarator ')'
3487 // direct-declarator: '(' attributes declarator ')'
3488 // Example: 'char (*X)' or 'int (*XX)(void)'
3489 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003490
3491 // If the declarator was parenthesized, we entered the declarator
3492 // scope when parsing the parenthesized declarator, then exited
3493 // the scope already. Re-enter the scope, if we need to.
3494 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003495 // If there was an error parsing parenthesized declarator, declarator
3496 // scope may have been enterred before. Don't do it again.
3497 if (!D.isInvalidType() &&
3498 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003499 // Change the declaration context for name lookup, until this function
3500 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003501 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003502 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003503 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003504 // This could be something simple like "int" (in which case the declarator
3505 // portion is empty), if an abstract-declarator is allowed.
3506 D.SetIdentifier(0, Tok.getLocation());
3507 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003508 if (D.getContext() == Declarator::MemberContext)
3509 Diag(Tok, diag::err_expected_member_name_or_semi)
3510 << D.getDeclSpec().getSourceRange();
3511 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003512 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003513 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003514 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003515 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003516 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003517 }
Mike Stump1eb44332009-09-09 15:08:12 +00003518
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003519 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003520 assert(D.isPastIdentifier() &&
3521 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003522
Sean Huntbbd37c62009-11-21 08:43:09 +00003523 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003524 if (D.getIdentifier())
3525 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003526
Reid Spencer5f016e22007-07-11 17:01:13 +00003527 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003528 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003529 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3530 // In such a case, check if we actually have a function declarator; if it
3531 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003532 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3533 // When not in file scope, warn for ambiguous function declarators, just
3534 // in case the author intended it as a variable definition.
3535 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3536 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3537 break;
3538 }
John McCall0b7e6782011-03-24 11:26:52 +00003539 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003540 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00003541 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003542 ParseBracketDeclarator(D);
3543 } else {
3544 break;
3545 }
3546 }
3547}
3548
Chris Lattneref4715c2008-04-06 05:45:57 +00003549/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3550/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003551/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003552/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3553///
3554/// direct-declarator:
3555/// '(' declarator ')'
3556/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003557/// direct-declarator '(' parameter-type-list ')'
3558/// direct-declarator '(' identifier-list[opt] ')'
3559/// [GNU] direct-declarator '(' parameter-forward-declarations
3560/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003561///
3562void Parser::ParseParenDeclarator(Declarator &D) {
3563 SourceLocation StartLoc = ConsumeParen();
3564 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003565
Chris Lattner7399ee02008-10-20 02:05:46 +00003566 // Eat any attributes before we look at whether this is a grouping or function
3567 // declarator paren. If this is a grouping paren, the attribute applies to
3568 // the type being built up, for example:
3569 // int (__attribute__(()) *x)(long y)
3570 // If this ends up not being a grouping paren, the attribute applies to the
3571 // first argument, for example:
3572 // int (__attribute__(()) int x)
3573 // In either case, we need to eat any attributes to be able to determine what
3574 // sort of paren this is.
3575 //
John McCall0b7e6782011-03-24 11:26:52 +00003576 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003577 bool RequiresArg = false;
3578 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003579 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003580
Chris Lattner7399ee02008-10-20 02:05:46 +00003581 // We require that the argument list (if this is a non-grouping paren) be
3582 // present even if the attribute list was empty.
3583 RequiresArg = true;
3584 }
Steve Naroff239f0732008-12-25 14:16:32 +00003585 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003586 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003587 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3588 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall7f040a92010-12-24 02:08:15 +00003589 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003590 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003591 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003592 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003593 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003594
Chris Lattneref4715c2008-04-06 05:45:57 +00003595 // If we haven't past the identifier yet (or where the identifier would be
3596 // stored, if this is an abstract declarator), then this is probably just
3597 // grouping parens. However, if this could be an abstract-declarator, then
3598 // this could also be the start of function arguments (consider 'void()').
3599 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003600
Chris Lattneref4715c2008-04-06 05:45:57 +00003601 if (!D.mayOmitIdentifier()) {
3602 // If this can't be an abstract-declarator, this *must* be a grouping
3603 // paren, because we haven't seen the identifier yet.
3604 isGrouping = true;
3605 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003606 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003607 isDeclarationSpecifier()) { // 'int(int)' is a function.
3608 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3609 // considered to be a type, not a K&R identifier-list.
3610 isGrouping = false;
3611 } else {
3612 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3613 isGrouping = true;
3614 }
Mike Stump1eb44332009-09-09 15:08:12 +00003615
Chris Lattneref4715c2008-04-06 05:45:57 +00003616 // If this is a grouping paren, handle:
3617 // direct-declarator: '(' declarator ')'
3618 // direct-declarator: '(' attributes declarator ')'
3619 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003620 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003621 D.setGroupingParens(true);
3622
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003623 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003624 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003625 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003626 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3627 attrs, EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003628
3629 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003630 return;
3631 }
Mike Stump1eb44332009-09-09 15:08:12 +00003632
Chris Lattneref4715c2008-04-06 05:45:57 +00003633 // Okay, if this wasn't a grouping paren, it must be the start of a function
3634 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003635 // identifier (and remember where it would have been), then call into
3636 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003637 D.SetIdentifier(0, Tok.getLocation());
3638
John McCall7f040a92010-12-24 02:08:15 +00003639 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003640}
3641
3642/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3643/// declarator D up to a paren, which indicates that we are parsing function
3644/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003645///
Chris Lattner7399ee02008-10-20 02:05:46 +00003646/// If AttrList is non-null, then the caller parsed those arguments immediately
3647/// after the open paren - they should be considered to be the first argument of
3648/// a parameter. If RequiresArg is true, then the first argument of the
3649/// function is required to be present and required to not be an identifier
3650/// list.
3651///
Reid Spencer5f016e22007-07-11 17:01:13 +00003652/// This method also handles this portion of the grammar:
3653/// parameter-type-list: [C99 6.7.5]
3654/// parameter-list
3655/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003656/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003657///
3658/// parameter-list: [C99 6.7.5]
3659/// parameter-declaration
3660/// parameter-list ',' parameter-declaration
3661///
3662/// parameter-declaration: [C99 6.7.5]
3663/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003664/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003665/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003666/// declaration-specifiers abstract-declarator[opt]
3667/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003668/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003669/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3670///
Douglas Gregor83f51722011-01-26 03:43:54 +00003671/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3672/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003673///
Sebastian Redl7acafd02011-03-05 14:45:16 +00003674/// [C++0x] exception-specification:
3675/// dynamic-exception-specification
3676/// noexcept-specification
3677///
Chris Lattner7399ee02008-10-20 02:05:46 +00003678void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall7f040a92010-12-24 02:08:15 +00003679 ParsedAttributes &attrs,
Chris Lattner7399ee02008-10-20 02:05:46 +00003680 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003681 // lparen is already consumed!
3682 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003683
Douglas Gregordab60ad2010-10-01 18:44:50 +00003684 ParsedType TrailingReturnType;
3685
Chris Lattner7399ee02008-10-20 02:05:46 +00003686 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003687 if (Tok.is(tok::r_paren)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003688 if (RequiresArg)
Chris Lattner1ab3b962008-11-18 07:48:38 +00003689 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003690
Abramo Bagnara796aa442011-03-12 11:17:06 +00003691 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003692
3693 // cv-qualifier-seq[opt].
John McCall0b7e6782011-03-24 11:26:52 +00003694 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003695 SourceLocation RefQualifierLoc;
3696 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003697 ExceptionSpecificationType ESpecType = EST_None;
3698 SourceRange ESpecRange;
3699 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3700 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3701 ExprResult NoexceptExpr;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003702 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003703 MaybeParseCXX0XAttributes(attrs);
3704
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003705 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003706 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003707 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003708
Douglas Gregor83f51722011-01-26 03:43:54 +00003709 // Parse ref-qualifier[opt]
3710 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3711 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003712 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003713
Douglas Gregor83f51722011-01-26 03:43:54 +00003714 RefQualifierIsLValueRef = Tok.is(tok::amp);
3715 RefQualifierLoc = ConsumeToken();
3716 EndLoc = RefQualifierLoc;
3717 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00003718
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003719 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003720 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3721 DynamicExceptions,
3722 DynamicExceptionRanges,
3723 NoexceptExpr);
3724 if (ESpecType != EST_None)
3725 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003726
3727 // Parse trailing-return-type.
3728 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3729 TrailingReturnType = ParseTrailingReturnType().get();
3730 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003731 }
3732
Chris Lattnerf97409f2008-04-06 06:57:35 +00003733 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003734 // int() -> no prototype, no '...'.
John McCall0b7e6782011-03-24 11:26:52 +00003735 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003736 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003737 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003738 /*arglist*/ 0, 0,
3739 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003740 RefQualifierIsLValueRef,
3741 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003742 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003743 DynamicExceptions.data(),
3744 DynamicExceptionRanges.data(),
3745 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003746 NoexceptExpr.isUsable() ?
3747 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003748 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003749 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00003750 attrs, EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003751 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003752 }
3753
Chris Lattner7399ee02008-10-20 02:05:46 +00003754 // Alternatively, this parameter list may be an identifier list form for a
3755 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003756 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3757 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003758 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003759 // K&R identifier lists can't have typedefs as identifiers, per
3760 // C99 6.7.5.3p11.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003761 if (RequiresArg)
Steve Naroff2d081c42009-01-28 19:16:40 +00003762 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner83a94472010-05-14 17:23:36 +00003763
Steve Naroff2d081c42009-01-28 19:16:40 +00003764 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003765 // normal declarators, not for abstract-declarators. Get the first
3766 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003767 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003768 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003769
3770 // Identifier lists follow a really simple grammar: the identifiers can
3771 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3772 // identifier lists are really rare in the brave new modern world, and it
3773 // is very common for someone to typo a type in a non-k&r style list. If
3774 // we are presented with something like: "void foo(intptr x, float y)",
3775 // we don't want to start parsing the function declarator as though it is
3776 // a K&R style declarator just because intptr is an invalid type.
3777 //
3778 // To handle this, we check to see if the token after the first identifier
3779 // is a "," or ")". Only if so, do we parse it as an identifier list.
3780 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3781 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3782 FirstTok.getIdentifierInfo(),
3783 FirstTok.getLocation(), D);
3784
3785 // If we get here, the code is invalid. Push the first identifier back
3786 // into the token stream and parse the first argument as an (invalid)
3787 // normal argument declarator.
3788 PP.EnterToken(Tok);
3789 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003790 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003791 }
Mike Stump1eb44332009-09-09 15:08:12 +00003792
Chris Lattnerf97409f2008-04-06 06:57:35 +00003793 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003794
Chris Lattnerf97409f2008-04-06 06:57:35 +00003795 // Build up an array of information about the parsed arguments.
3796 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003797
3798 // Enter function-declaration scope, limiting any declarators to the
3799 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003800 ParseScope PrototypeScope(this,
3801 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003802
Chris Lattnerf97409f2008-04-06 06:57:35 +00003803 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003804 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003805 while (1) {
3806 if (Tok.is(tok::ellipsis)) {
3807 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003808 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003809 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003810 }
Mike Stump1eb44332009-09-09 15:08:12 +00003811
Chris Lattnerf97409f2008-04-06 06:57:35 +00003812 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003813 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00003814 DeclSpec DS(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003815
3816 // Skip any Microsoft attributes before a param.
3817 if (getLang().Microsoft && Tok.is(tok::l_square))
3818 ParseMicrosoftAttributes(DS.getAttributes());
3819
3820 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00003821
3822 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00003823 // Take them so that we only apply the attributes to the first parameter.
3824 DS.takeAttributesFrom(attrs);
3825
Chris Lattnere64c5492009-02-27 18:38:20 +00003826 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003827
Chris Lattnerf97409f2008-04-06 06:57:35 +00003828 // Parse the declarator. This is "PrototypeContext", because we must
3829 // accept either 'declarator' or 'abstract-declarator' here.
3830 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3831 ParseDeclarator(ParmDecl);
3832
3833 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00003834 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003835
Chris Lattnerf97409f2008-04-06 06:57:35 +00003836 // Remember this parsed parameter in ParamInfo.
3837 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003838
Douglas Gregor72b505b2008-12-16 21:30:33 +00003839 // DefArgToks is used when the parsing of default arguments needs
3840 // to be delayed.
3841 CachedTokens *DefArgToks = 0;
3842
Chris Lattnerf97409f2008-04-06 06:57:35 +00003843 // If no parameter was specified, verify that *something* was specified,
3844 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003845 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3846 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003847 // Completely missing, emit error.
3848 Diag(DSStart, diag::err_missing_param);
3849 } else {
3850 // Otherwise, we have something. Add it and let semantic analysis try
3851 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003852
Chris Lattnerf97409f2008-04-06 06:57:35 +00003853 // Inform the actions module about the parameter declarator, so it gets
3854 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003855 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003856
3857 // Parse the default argument, if any. We parse the default
3858 // arguments in all dialects; the semantic analysis in
3859 // ActOnParamDefaultArgument will reject the default argument in
3860 // C.
3861 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003862 SourceLocation EqualLoc = Tok.getLocation();
3863
Chris Lattner04421082008-04-08 04:40:51 +00003864 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003865 if (D.getContext() == Declarator::MemberContext) {
3866 // If we're inside a class definition, cache the tokens
3867 // corresponding to the default argument. We'll actually parse
3868 // them when we see the end of the class definition.
3869 // FIXME: Templates will require something similar.
3870 // FIXME: Can we use a smart pointer for Toks?
3871 DefArgToks = new CachedTokens;
3872
Mike Stump1eb44332009-09-09 15:08:12 +00003873 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003874 /*StopAtSemi=*/true,
3875 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003876 delete DefArgToks;
3877 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003878 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003879 } else {
3880 // Mark the end of the default argument so that we know when to
3881 // stop when we parse it later on.
3882 Token DefArgEnd;
3883 DefArgEnd.startToken();
3884 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3885 DefArgEnd.setLocation(Tok.getLocation());
3886 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003887 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003888 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003889 }
Chris Lattner04421082008-04-08 04:40:51 +00003890 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003891 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003892 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003893
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003894 // The argument isn't actually potentially evaluated unless it is
3895 // used.
3896 EnterExpressionEvaluationContext Eval(Actions,
3897 Sema::PotentiallyEvaluatedIfUsed);
3898
John McCall60d7b3a2010-08-24 06:29:42 +00003899 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003900 if (DefArgResult.isInvalid()) {
3901 Actions.ActOnParamDefaultArgumentError(Param);
3902 SkipUntil(tok::comma, tok::r_paren, true, true);
3903 } else {
3904 // Inform the actions module about the default argument
3905 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003906 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003907 }
Chris Lattner04421082008-04-08 04:40:51 +00003908 }
3909 }
Mike Stump1eb44332009-09-09 15:08:12 +00003910
3911 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3912 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003913 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003914 }
3915
3916 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003917 if (Tok.isNot(tok::comma)) {
3918 if (Tok.is(tok::ellipsis)) {
3919 IsVariadic = true;
3920 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3921
3922 if (!getLang().CPlusPlus) {
3923 // We have ellipsis without a preceding ',', which is ill-formed
3924 // in C. Complain and provide the fix.
3925 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003926 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003927 }
3928 }
3929
3930 break;
3931 }
Mike Stump1eb44332009-09-09 15:08:12 +00003932
Chris Lattnerf97409f2008-04-06 06:57:35 +00003933 // Consume the comma.
3934 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003935 }
Mike Stump1eb44332009-09-09 15:08:12 +00003936
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003937 // If we have the closing ')', eat it.
Abramo Bagnara796aa442011-03-12 11:17:06 +00003938 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003939
John McCall0b7e6782011-03-24 11:26:52 +00003940 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003941 SourceLocation RefQualifierLoc;
3942 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003943 ExceptionSpecificationType ESpecType = EST_None;
3944 SourceRange ESpecRange;
3945 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3946 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3947 ExprResult NoexceptExpr;
Sean Huntbbd37c62009-11-21 08:43:09 +00003948
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003949 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003950 MaybeParseCXX0XAttributes(attrs);
3951
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003952 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003953 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003954 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003955 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003956
Douglas Gregor83f51722011-01-26 03:43:54 +00003957 // Parse ref-qualifier[opt]
3958 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3959 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003960 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor83f51722011-01-26 03:43:54 +00003961
3962 RefQualifierIsLValueRef = Tok.is(tok::amp);
3963 RefQualifierLoc = ConsumeToken();
3964 EndLoc = RefQualifierLoc;
3965 }
3966
Sebastian Redl7acafd02011-03-05 14:45:16 +00003967 // FIXME: We should leave the prototype scope before parsing the exception
3968 // specification, and then reenter it when parsing the trailing return type.
3969 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3970
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003971 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003972 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3973 DynamicExceptions,
3974 DynamicExceptionRanges,
3975 NoexceptExpr);
3976 if (ESpecType != EST_None)
3977 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003978
3979 // Parse trailing-return-type.
3980 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3981 TrailingReturnType = ParseTrailingReturnType().get();
3982 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003983 }
3984
Douglas Gregordab60ad2010-10-01 18:44:50 +00003985 // Leave prototype scope.
3986 PrototypeScope.Exit();
3987
Reid Spencer5f016e22007-07-11 17:01:13 +00003988 // Remember that we parsed a function type, and remember the attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003989 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003990 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003991 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003992 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003993 RefQualifierIsLValueRef,
3994 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003995 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003996 DynamicExceptions.data(),
3997 DynamicExceptionRanges.data(),
3998 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003999 NoexceptExpr.isUsable() ?
4000 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00004001 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00004002 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00004003 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004004}
4005
Chris Lattner66d28652008-04-06 06:34:08 +00004006/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4007/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00004008/// first identifier has already been consumed, and the current token is the
4009/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00004010///
4011/// identifier-list: [C99 6.7.5]
4012/// identifier
4013/// identifier-list ',' identifier
4014///
4015void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00004016 IdentifierInfo *FirstIdent,
4017 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00004018 Declarator &D) {
4019 // Build up an array of information about the parsed arguments.
4020 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
4021 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00004022
Chris Lattner66d28652008-04-06 06:34:08 +00004023 // If there was no identifier specified for the declarator, either we are in
4024 // an abstract-declarator, or we are in a parameter declarator which was found
4025 // to be abstract. In abstract-declarators, identifier lists are not valid:
4026 // diagnose this.
4027 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00004028 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00004029
Chris Lattner83a94472010-05-14 17:23:36 +00004030 // The first identifier was already read, and is known to be the first
4031 // identifier in the list. Remember this identifier in ParamInfo.
4032 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00004033 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00004034
Chris Lattner66d28652008-04-06 06:34:08 +00004035 while (Tok.is(tok::comma)) {
4036 // Eat the comma.
4037 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004038
Chris Lattner50c64772008-04-06 06:39:19 +00004039 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00004040 if (Tok.isNot(tok::identifier)) {
4041 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00004042 SkipUntil(tok::r_paren);
4043 return;
Chris Lattner66d28652008-04-06 06:34:08 +00004044 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00004045
Chris Lattner66d28652008-04-06 06:34:08 +00004046 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00004047
4048 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00004049 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00004050 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00004051
Chris Lattner66d28652008-04-06 06:34:08 +00004052 // Verify that the argument identifier has not already been mentioned.
4053 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004054 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00004055 } else {
4056 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00004057 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00004058 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00004059 0));
Chris Lattner50c64772008-04-06 06:39:19 +00004060 }
Mike Stump1eb44332009-09-09 15:08:12 +00004061
Chris Lattner66d28652008-04-06 06:34:08 +00004062 // Eat the identifier.
4063 ConsumeToken();
4064 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004065
4066 // If we have the closing ')', eat it and we're done.
4067 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4068
Chris Lattner50c64772008-04-06 06:39:19 +00004069 // Remember that we parsed a function type, and remember the attributes. This
4070 // function type is always a K&R style function type, which is not varargs and
4071 // has no prototype.
John McCall0b7e6782011-03-24 11:26:52 +00004072 ParsedAttributes attrs(AttrFactory);
4073 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00004074 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00004075 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00004076 /*TypeQuals*/0,
Douglas Gregor83f51722011-01-26 03:43:54 +00004077 true, SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00004078 EST_None, SourceLocation(), 0, 0,
4079 0, 0, LParenLoc, RLoc, D),
John McCall0b7e6782011-03-24 11:26:52 +00004080 attrs, RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00004081}
Chris Lattneref4715c2008-04-06 05:45:57 +00004082
Reid Spencer5f016e22007-07-11 17:01:13 +00004083/// [C90] direct-declarator '[' constant-expression[opt] ']'
4084/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4085/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4086/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4087/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4088void Parser::ParseBracketDeclarator(Declarator &D) {
4089 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00004090
Chris Lattner378c7e42008-12-18 07:27:21 +00004091 // C array syntax has many features, but by-far the most common is [] and [4].
4092 // This code does a fast path to handle some of the most obvious cases.
4093 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00004094 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004095 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004096 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004097
Chris Lattner378c7e42008-12-18 07:27:21 +00004098 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004099 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004100 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004101 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004102 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004103 return;
4104 } else if (Tok.getKind() == tok::numeric_constant &&
4105 GetLookAheadToken(1).is(tok::r_square)) {
4106 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004107 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004108 ConsumeToken();
4109
Sebastian Redlab197ba2009-02-09 18:23:29 +00004110 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004111 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004112 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004113
Chris Lattner378c7e42008-12-18 07:27:21 +00004114 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004115 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004116 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004117 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004118 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004119 return;
4120 }
Mike Stump1eb44332009-09-09 15:08:12 +00004121
Reid Spencer5f016e22007-07-11 17:01:13 +00004122 // If valid, this location is the position where we read the 'static' keyword.
4123 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004124 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004125 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004126
Reid Spencer5f016e22007-07-11 17:01:13 +00004127 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004128 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004129 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004130 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004131
Reid Spencer5f016e22007-07-11 17:01:13 +00004132 // If we haven't already read 'static', check to see if there is one after the
4133 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004134 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004135 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004136
Reid Spencer5f016e22007-07-11 17:01:13 +00004137 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4138 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004139 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004140
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004141 // Handle the case where we have '[*]' as the array size. However, a leading
4142 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4143 // the the token after the star is a ']'. Since stars in arrays are
4144 // infrequent, use of lookahead is not costly here.
4145 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004146 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004147
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004148 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004149 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004150 StaticLoc = SourceLocation(); // Drop the static.
4151 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004152 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004153 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004154 // Note, in C89, this production uses the constant-expr production instead
4155 // of assignment-expr. The only difference is that assignment-expr allows
4156 // things like '=' and '*='. Sema rejects these in C89 mode because they
4157 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004158
Douglas Gregore0762c92009-06-19 23:52:42 +00004159 // Parse the constant-expression or assignment-expression now (depending
4160 // on dialect).
4161 if (getLang().CPlusPlus)
4162 NumElements = ParseConstantExpression();
4163 else
4164 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004165 }
Mike Stump1eb44332009-09-09 15:08:12 +00004166
Reid Spencer5f016e22007-07-11 17:01:13 +00004167 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004168 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004169 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004170 // If the expression was invalid, skip it.
4171 SkipUntil(tok::r_square);
4172 return;
4173 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004174
4175 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4176
John McCall0b7e6782011-03-24 11:26:52 +00004177 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004178 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004179
Chris Lattner378c7e42008-12-18 07:27:21 +00004180 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004181 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004182 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004183 NumElements.release(),
4184 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004185 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004186}
4187
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004188/// [GNU] typeof-specifier:
4189/// typeof ( expressions )
4190/// typeof ( type-name )
4191/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004192///
4193void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004194 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004195 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004196 SourceLocation StartLoc = ConsumeToken();
4197
John McCallcfb708c2010-01-13 20:03:27 +00004198 const bool hasParens = Tok.is(tok::l_paren);
4199
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004200 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004201 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004202 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004203 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4204 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004205 if (hasParens)
4206 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004207
4208 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004209 // FIXME: Not accurate, the range gets one token more than it should.
4210 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004211 else
4212 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004213
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004214 if (isCastExpr) {
4215 if (!CastTy) {
4216 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004217 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004218 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004219
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004220 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004221 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004222 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4223 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004224 DiagID, CastTy))
4225 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004226 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004227 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004228
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004229 // If we get here, the operand to the typeof was an expresion.
4230 if (Operand.isInvalid()) {
4231 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004232 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004233 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004234
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004235 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004236 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004237 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4238 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004239 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004240 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004241}
Chris Lattner1b492422010-02-28 18:33:55 +00004242
4243
4244/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4245/// from TryAltiVecVectorToken.
4246bool Parser::TryAltiVecVectorTokenOutOfLine() {
4247 Token Next = NextToken();
4248 switch (Next.getKind()) {
4249 default: return false;
4250 case tok::kw_short:
4251 case tok::kw_long:
4252 case tok::kw_signed:
4253 case tok::kw_unsigned:
4254 case tok::kw_void:
4255 case tok::kw_char:
4256 case tok::kw_int:
4257 case tok::kw_float:
4258 case tok::kw_double:
4259 case tok::kw_bool:
4260 case tok::kw___pixel:
4261 Tok.setKind(tok::kw___vector);
4262 return true;
4263 case tok::identifier:
4264 if (Next.getIdentifierInfo() == Ident_pixel) {
4265 Tok.setKind(tok::kw___vector);
4266 return true;
4267 }
4268 return false;
4269 }
4270}
4271
4272bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4273 const char *&PrevSpec, unsigned &DiagID,
4274 bool &isInvalid) {
4275 if (Tok.getIdentifierInfo() == Ident_vector) {
4276 Token Next = NextToken();
4277 switch (Next.getKind()) {
4278 case tok::kw_short:
4279 case tok::kw_long:
4280 case tok::kw_signed:
4281 case tok::kw_unsigned:
4282 case tok::kw_void:
4283 case tok::kw_char:
4284 case tok::kw_int:
4285 case tok::kw_float:
4286 case tok::kw_double:
4287 case tok::kw_bool:
4288 case tok::kw___pixel:
4289 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4290 return true;
4291 case tok::identifier:
4292 if (Next.getIdentifierInfo() == Ident_pixel) {
4293 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4294 return true;
4295 }
4296 break;
4297 default:
4298 break;
4299 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004300 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004301 DS.isTypeAltiVecVector()) {
4302 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4303 return true;
4304 }
4305 return false;
4306}