blob: a4c323979d44df92ea9f0ff56a01053a4b7a6275 [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,
34 Declarator::TheContext Context) {
Reid Spencer5f016e22007-07-11 17:01:13 +000035 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000036 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +000037 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000038
Reid Spencer5f016e22007-07-11 17:01:13 +000039 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000040 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000041 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000042 if (Range)
43 *Range = DeclaratorInfo.getSourceRange();
44
Chris Lattnereaaebc72009-04-25 08:06:05 +000045 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000046 return true;
47
Douglas Gregor23c94db2010-07-02 17:43:08 +000048 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000049}
50
Sean Huntbbd37c62009-11-21 08:43:09 +000051/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000052///
53/// [GNU] attributes:
54/// attribute
55/// attributes attribute
56///
57/// [GNU] attribute:
58/// '__attribute__' '(' '(' attribute-list ')' ')'
59///
60/// [GNU] attribute-list:
61/// attrib
62/// attribute_list ',' attrib
63///
64/// [GNU] attrib:
65/// empty
66/// attrib-name
67/// attrib-name '(' identifier ')'
68/// attrib-name '(' identifier ',' nonempty-expr-list ')'
69/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
70///
71/// [GNU] attrib-name:
72/// identifier
73/// typespec
74/// typequal
75/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000076///
Reid Spencer5f016e22007-07-11 17:01:13 +000077/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000078/// token lookahead. Comment from gcc: "If they start with an identifier
79/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000080/// start with that identifier; otherwise they are an expression list."
81///
82/// At the moment, I am not doing 2 token lookahead. I am also unaware of
83/// any attributes that don't work (based on my limited testing). Most
84/// attributes are very simple in practice. Until we find a bug, I don't see
85/// a pressing need to implement the 2 token lookahead.
86
John McCall7f040a92010-12-24 02:08:15 +000087void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
88 SourceLocation *endLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +000089 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000090
Chris Lattner04d66662007-10-09 17:33:22 +000091 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000092 ConsumeToken();
93 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
94 "attribute")) {
95 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +000096 return;
Reid Spencer5f016e22007-07-11 17:01:13 +000097 }
98 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
99 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000100 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 }
102 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000103 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
104 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000105
106 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
108 ConsumeToken();
109 continue;
110 }
111 // we have an identifier or declaration specifier (const, int, etc.)
112 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
113 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000115 // Availability attributes have their own grammar.
116 if (AttrName->isStr("availability"))
117 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, attrs, endLoc);
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000118 // check if we have a "parameterized" attribute
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000119 else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Chris Lattner04d66662007-10-09 17:33:22 +0000122 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
124 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000125
126 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 // __attribute__(( mode(byte) ))
128 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000129 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
130 ParmName, ParmLoc, 0, 0);
Chris Lattner04d66662007-10-09 17:33:22 +0000131 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 ConsumeToken();
133 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000134 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 // now parse the non-empty comma separated list of expressions
138 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000139 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000140 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 ArgExprsOk = false;
142 SkipUntil(tok::r_paren);
143 break;
144 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000145 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 }
Chris Lattner04d66662007-10-09 17:33:22 +0000147 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 break;
149 ConsumeToken(); // Eat the comma, move to the next argument
150 }
Chris Lattner04d66662007-10-09 17:33:22 +0000151 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000153 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
154 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 }
156 }
157 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000158 switch (Tok.getKind()) {
159 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 // __attribute__(( nonnull() ))
162 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000163 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
164 0, SourceLocation(), 0, 0);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000165 break;
166 case tok::kw_char:
167 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000168 case tok::kw_char16_t:
169 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000170 case tok::kw_bool:
171 case tok::kw_short:
172 case tok::kw_int:
173 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +0000174 case tok::kw___int64:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000175 case tok::kw_signed:
176 case tok::kw_unsigned:
177 case tok::kw_float:
178 case tok::kw_double:
179 case tok::kw_void:
John McCall7f040a92010-12-24 02:08:15 +0000180 case tok::kw_typeof: {
181 AttributeList *attr
John McCall0b7e6782011-03-24 11:26:52 +0000182 = attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
183 0, SourceLocation(), 0, 0);
John McCall7f040a92010-12-24 02:08:15 +0000184 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian1b72fa72010-08-17 23:19:16 +0000185 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000186 // If it's a builtin type name, eat it and expect a rparen
187 // __attribute__(( vec_type_hint(char) ))
188 ConsumeToken();
Nate Begeman6f3d8382009-06-26 06:32:41 +0000189 if (Tok.is(tok::r_paren))
190 ConsumeParen();
191 break;
John McCall7f040a92010-12-24 02:08:15 +0000192 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000193 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000195 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 // now parse the list of expressions
199 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000200 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000201 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 ArgExprsOk = false;
203 SkipUntil(tok::r_paren);
204 break;
205 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000206 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 }
Chris Lattner04d66662007-10-09 17:33:22 +0000208 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 break;
210 ConsumeToken(); // Eat the comma, move to the next argument
211 }
212 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000213 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 ConsumeParen(); // ignore the right paren loc for now
John McCall0b7e6782011-03-24 11:26:52 +0000215 attrs.addNew(AttrName, AttrNameLoc, 0,
216 AttrNameLoc, 0, SourceLocation(),
217 ArgExprs.take(), ArgExprs.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000219 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 }
221 }
222 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000223 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
224 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 }
226 }
227 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000229 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000230 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
231 SkipUntil(tok::r_paren, false);
232 }
John McCall7f040a92010-12-24 02:08:15 +0000233 if (endLoc)
234 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000236}
237
Eli Friedmana23b4852009-06-08 07:21:15 +0000238/// ParseMicrosoftDeclSpec - Parse an __declspec construct
239///
240/// [MS] decl-specifier:
241/// __declspec ( extended-decl-modifier-seq )
242///
243/// [MS] extended-decl-modifier-seq:
244/// extended-decl-modifier[opt]
245/// extended-decl-modifier extended-decl-modifier-seq
246
John McCall7f040a92010-12-24 02:08:15 +0000247void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000248 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000249
Steve Narofff59e17e2008-12-24 20:59:21 +0000250 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000251 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
252 "declspec")) {
253 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000254 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000255 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000256 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000257 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
258 SourceLocation AttrNameLoc = ConsumeToken();
259 if (Tok.is(tok::l_paren)) {
260 ConsumeParen();
261 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
262 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000263 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000264 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000265 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000266 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
267 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000268 }
269 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
270 SkipUntil(tok::r_paren, false);
271 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000272 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
273 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000274 }
275 }
276 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
277 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000278 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000279}
280
John McCall7f040a92010-12-24 02:08:15 +0000281void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000282 // Treat these like attributes
283 // FIXME: Allow Sema to distinguish between these and real attributes!
284 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000285 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
286 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000287 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
288 SourceLocation AttrNameLoc = ConsumeToken();
289 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
290 // FIXME: Support these properly!
291 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000292 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
293 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000294 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000295}
296
John McCall7f040a92010-12-24 02:08:15 +0000297void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000298 // Treat these like attributes
299 while (Tok.is(tok::kw___pascal)) {
300 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
301 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000302 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
303 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000304 }
John McCall7f040a92010-12-24 02:08:15 +0000305}
306
Peter Collingbournef315fa82011-02-14 01:42:53 +0000307void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
308 // Treat these like attributes
309 while (Tok.is(tok::kw___kernel)) {
310 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000311 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
312 AttrNameLoc, 0, AttrNameLoc, 0,
313 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000314 }
315}
316
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000317void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
318 SourceLocation Loc = Tok.getLocation();
319 switch(Tok.getKind()) {
320 // OpenCL qualifiers:
321 case tok::kw___private:
322 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000323 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000324 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000325 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000326 break;
327
328 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000329 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000330 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000331 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000332 break;
333
334 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000335 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000336 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000337 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000338 break;
339
340 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000341 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000342 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000343 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000344 break;
345
346 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000347 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000348 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000349 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000350 break;
351
352 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000353 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000354 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000355 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000356 break;
357
358 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000359 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000360 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000361 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000362 break;
363 default: break;
364 }
365}
366
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000367/// \brief Parse a version number.
368///
369/// version:
370/// simple-integer
371/// simple-integer ',' simple-integer
372/// simple-integer ',' simple-integer ',' simple-integer
373VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
374 Range = Tok.getLocation();
375
376 if (!Tok.is(tok::numeric_constant)) {
377 Diag(Tok, diag::err_expected_version);
378 SkipUntil(tok::comma, tok::r_paren, true, true, true);
379 return VersionTuple();
380 }
381
382 // Parse the major (and possibly minor and subminor) versions, which
383 // are stored in the numeric constant. We utilize a quirk of the
384 // lexer, which is that it handles something like 1.2.3 as a single
385 // numeric constant, rather than two separate tokens.
386 llvm::SmallString<512> Buffer;
387 Buffer.resize(Tok.getLength()+1);
388 const char *ThisTokBegin = &Buffer[0];
389
390 // Get the spelling of the token, which eliminates trigraphs, etc.
391 bool Invalid = false;
392 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
393 if (Invalid)
394 return VersionTuple();
395
396 // Parse the major version.
397 unsigned AfterMajor = 0;
398 unsigned Major = 0;
399 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
400 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
401 ++AfterMajor;
402 }
403
404 if (AfterMajor == 0) {
405 Diag(Tok, diag::err_expected_version);
406 SkipUntil(tok::comma, tok::r_paren, true, true, true);
407 return VersionTuple();
408 }
409
410 if (AfterMajor == ActualLength) {
411 ConsumeToken();
412
413 // We only had a single version component.
414 if (Major == 0) {
415 Diag(Tok, diag::err_zero_version);
416 return VersionTuple();
417 }
418
419 return VersionTuple(Major);
420 }
421
422 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
423 Diag(Tok, diag::err_expected_version);
424 SkipUntil(tok::comma, tok::r_paren, true, true, true);
425 return VersionTuple();
426 }
427
428 // Parse the minor version.
429 unsigned AfterMinor = AfterMajor + 1;
430 unsigned Minor = 0;
431 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
432 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
433 ++AfterMinor;
434 }
435
436 if (AfterMinor == ActualLength) {
437 ConsumeToken();
438
439 // We had major.minor.
440 if (Major == 0 && Minor == 0) {
441 Diag(Tok, diag::err_zero_version);
442 return VersionTuple();
443 }
444
445 return VersionTuple(Major, Minor);
446 }
447
448 // If what follows is not a '.', we have a problem.
449 if (ThisTokBegin[AfterMinor] != '.') {
450 Diag(Tok, diag::err_expected_version);
451 SkipUntil(tok::comma, tok::r_paren, true, true, true);
452 return VersionTuple();
453 }
454
455 // Parse the subminor version.
456 unsigned AfterSubminor = AfterMinor + 1;
457 unsigned Subminor = 0;
458 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
459 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
460 ++AfterSubminor;
461 }
462
463 if (AfterSubminor != ActualLength) {
464 Diag(Tok, diag::err_expected_version);
465 SkipUntil(tok::comma, tok::r_paren, true, true, true);
466 return VersionTuple();
467 }
468 ConsumeToken();
469 return VersionTuple(Major, Minor, Subminor);
470}
471
472/// \brief Parse the contents of the "availability" attribute.
473///
474/// availability-attribute:
475/// 'availability' '(' platform ',' version-arg-list ')'
476///
477/// platform:
478/// identifier
479///
480/// version-arg-list:
481/// version-arg
482/// version-arg ',' version-arg-list
483///
484/// version-arg:
485/// 'introduced' '=' version
486/// 'deprecated' '=' version
487/// 'removed' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000488/// 'unavailable'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000489void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
490 SourceLocation AvailabilityLoc,
491 ParsedAttributes &attrs,
492 SourceLocation *endLoc) {
493 SourceLocation PlatformLoc;
494 IdentifierInfo *Platform = 0;
495
496 enum { Introduced, Deprecated, Obsoleted, Unknown };
497 AvailabilityChange Changes[Unknown];
498
499 // Opening '('.
500 SourceLocation LParenLoc;
501 if (!Tok.is(tok::l_paren)) {
502 Diag(Tok, diag::err_expected_lparen);
503 return;
504 }
505 LParenLoc = ConsumeParen();
506
507 // Parse the platform name,
508 if (Tok.isNot(tok::identifier)) {
509 Diag(Tok, diag::err_availability_expected_platform);
510 SkipUntil(tok::r_paren);
511 return;
512 }
513 Platform = Tok.getIdentifierInfo();
514 PlatformLoc = ConsumeToken();
515
516 // Parse the ',' following the platform name.
517 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
518 return;
519
520 // If we haven't grabbed the pointers for the identifiers
521 // "introduced", "deprecated", and "obsoleted", do so now.
522 if (!Ident_introduced) {
523 Ident_introduced = PP.getIdentifierInfo("introduced");
524 Ident_deprecated = PP.getIdentifierInfo("deprecated");
525 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000526 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000527 }
528
529 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000530 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000531 do {
532 if (Tok.isNot(tok::identifier)) {
533 Diag(Tok, diag::err_availability_expected_change);
534 SkipUntil(tok::r_paren);
535 return;
536 }
537 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
538 SourceLocation KeywordLoc = ConsumeToken();
539
Douglas Gregorb53e4172011-03-26 03:35:55 +0000540 if (Keyword == Ident_unavailable) {
541 if (UnavailableLoc.isValid()) {
542 Diag(KeywordLoc, diag::err_availability_redundant)
543 << Keyword << SourceRange(UnavailableLoc);
544 }
545 UnavailableLoc = KeywordLoc;
546
547 if (Tok.isNot(tok::comma))
548 break;
549
550 ConsumeToken();
551 continue;
552 }
553
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000554 if (Tok.isNot(tok::equal)) {
555 Diag(Tok, diag::err_expected_equal_after)
556 << Keyword;
557 SkipUntil(tok::r_paren);
558 return;
559 }
560 ConsumeToken();
561
562 SourceRange VersionRange;
563 VersionTuple Version = ParseVersionTuple(VersionRange);
564
565 if (Version.empty()) {
566 SkipUntil(tok::r_paren);
567 return;
568 }
569
570 unsigned Index;
571 if (Keyword == Ident_introduced)
572 Index = Introduced;
573 else if (Keyword == Ident_deprecated)
574 Index = Deprecated;
575 else if (Keyword == Ident_obsoleted)
576 Index = Obsoleted;
577 else
578 Index = Unknown;
579
580 if (Index < Unknown) {
581 if (!Changes[Index].KeywordLoc.isInvalid()) {
582 Diag(KeywordLoc, diag::err_availability_redundant)
583 << Keyword
584 << SourceRange(Changes[Index].KeywordLoc,
585 Changes[Index].VersionRange.getEnd());
586 }
587
588 Changes[Index].KeywordLoc = KeywordLoc;
589 Changes[Index].Version = Version;
590 Changes[Index].VersionRange = VersionRange;
591 } else {
592 Diag(KeywordLoc, diag::err_availability_unknown_change)
593 << Keyword << VersionRange;
594 }
595
596 if (Tok.isNot(tok::comma))
597 break;
598
599 ConsumeToken();
600 } while (true);
601
602 // Closing ')'.
603 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
604 if (RParenLoc.isInvalid())
605 return;
606
607 if (endLoc)
608 *endLoc = RParenLoc;
609
Douglas Gregorb53e4172011-03-26 03:35:55 +0000610 // The 'unavailable' availability cannot be combined with any other
611 // availability changes. Make sure that hasn't happened.
612 if (UnavailableLoc.isValid()) {
613 bool Complained = false;
614 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
615 if (Changes[Index].KeywordLoc.isValid()) {
616 if (!Complained) {
617 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
618 << SourceRange(Changes[Index].KeywordLoc,
619 Changes[Index].VersionRange.getEnd());
620 Complained = true;
621 }
622
623 // Clear out the availability.
624 Changes[Index] = AvailabilityChange();
625 }
626 }
627 }
628
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000629 // Record this attribute
Douglas Gregorb53e4172011-03-26 03:35:55 +0000630 attrs.addNew(&Availability, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000631 0, SourceLocation(),
632 Platform, PlatformLoc,
633 Changes[Introduced],
634 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000635 Changes[Obsoleted],
636 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000637}
638
John McCall7f040a92010-12-24 02:08:15 +0000639void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
640 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
641 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000642}
643
Reid Spencer5f016e22007-07-11 17:01:13 +0000644/// ParseDeclaration - Parse a full 'declaration', which consists of
645/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000646/// 'Context' should be a Declarator::TheContext value. This returns the
647/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000648///
649/// declaration: [C99 6.7]
650/// block-declaration ->
651/// simple-declaration
652/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000653/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000654/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000655/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000656/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000657/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000658/// others... [FIXME]
659///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000660Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
661 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000662 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000663 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000664 ParenBraceBracketBalancer BalancerRAIIObj(*this);
665
John McCalld226f652010-08-21 09:40:31 +0000666 Decl *SingleDecl = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000667 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000668 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000669 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000670 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000671 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000672 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000673 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000674 // Could be the start of an inline namespace. Allowed as an ext in C++03.
675 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000676 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000677 SourceLocation InlineLoc = ConsumeToken();
678 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
679 break;
680 }
John McCall7f040a92010-12-24 02:08:15 +0000681 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000682 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000683 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000684 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000685 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000686 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000687 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000688 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
John McCall7f040a92010-12-24 02:08:15 +0000689 DeclEnd, attrs);
Chris Lattner682bf922009-03-29 16:50:03 +0000690 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000691 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000692 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000693 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000694 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000695 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000696 default:
John McCall7f040a92010-12-24 02:08:15 +0000697 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000698 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000699
Chris Lattner682bf922009-03-29 16:50:03 +0000700 // This routine returns a DeclGroup, if the thing we parsed only contains a
701 // single decl, convert it now.
702 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000703}
704
705/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
706/// declaration-specifiers init-declarator-list[opt] ';'
707///[C90/C++]init-declarator-list ';' [TODO]
708/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000709///
Richard Smithad762fc2011-04-14 22:09:26 +0000710/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
711/// attribute-specifier-seq[opt] type-specifier-seq declarator
712///
Chris Lattnercd147752009-03-29 17:27:48 +0000713/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000714/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000715///
716/// If FRI is non-null, we might be parsing a for-range-declaration instead
717/// of a simple-declaration. If we find that we are, we also parse the
718/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000719Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
720 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000721 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000722 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000723 bool RequireSemi,
724 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000726 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000727 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000728
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000729 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000730 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000731 StmtResult R = Actions.ActOnVlaStmt(DS);
732 if (R.isUsable())
733 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000734
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
736 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000737 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000738 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000739 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000740 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000741 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000742 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000744
745 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000746}
Mike Stump1eb44332009-09-09 15:08:12 +0000747
John McCalld8ac0572009-11-03 19:26:08 +0000748/// ParseDeclGroup - Having concluded that this is either a function
749/// definition or a group of object declarations, actually parse the
750/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000751Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
752 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000753 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +0000754 SourceLocation *DeclEnd,
755 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +0000756 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000757 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000758 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000759
John McCalld8ac0572009-11-03 19:26:08 +0000760 // Bail out if the first declarator didn't seem well-formed.
761 if (!D.hasName() && !D.mayOmitIdentifier()) {
762 // Skip until ; or }.
763 SkipUntil(tok::r_brace, true, true);
764 if (Tok.is(tok::semi))
765 ConsumeToken();
766 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000767 }
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Chris Lattnerc82daef2010-07-11 22:24:20 +0000769 // Check to see if we have a function *definition* which must have a body.
770 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
771 // Look at the next token to make sure that this isn't a function
772 // declaration. We have to check this because __attribute__ might be the
773 // start of a function definition in GCC-extended K&R C.
774 !isDeclarationAfterDeclarator()) {
775
Chris Lattner004659a2010-07-11 22:42:07 +0000776 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000777 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
778 Diag(Tok, diag::err_function_declared_typedef);
779
780 // Recover by treating the 'typedef' as spurious.
781 DS.ClearStorageClassSpecs();
782 }
783
John McCalld226f652010-08-21 09:40:31 +0000784 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000785 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000786 }
787
788 if (isDeclarationSpecifier()) {
789 // If there is an invalid declaration specifier right after the function
790 // prototype, then we must be in a missing semicolon case where this isn't
791 // actually a body. Just fall through into the code that handles it as a
792 // prototype, and let the top-level code handle the erroneous declspec
793 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000794 } else {
795 Diag(Tok, diag::err_expected_fn_body);
796 SkipUntil(tok::semi);
797 return DeclGroupPtrTy();
798 }
799 }
800
Richard Smithad762fc2011-04-14 22:09:26 +0000801 if (ParseAttributesAfterDeclarator(D))
802 return DeclGroupPtrTy();
803
804 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
805 // must parse and analyze the for-range-initializer before the declaration is
806 // analyzed.
807 if (FRI && Tok.is(tok::colon)) {
808 FRI->ColonLoc = ConsumeToken();
809 // FIXME: handle braced-init-list here.
810 FRI->RangeExpr = ParseExpression();
811 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
812 Actions.ActOnCXXForRangeDecl(ThisDecl);
813 Actions.FinalizeDeclaration(ThisDecl);
814 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
815 }
816
John McCalld226f652010-08-21 09:40:31 +0000817 llvm::SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +0000818 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +0000819 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000820 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000821 DeclsInGroup.push_back(FirstDecl);
822
823 // If we don't have a comma, it is either the end of the list (a ';') or an
824 // error, bail out.
825 while (Tok.is(tok::comma)) {
826 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000827 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000828
829 // Parse the next declarator.
830 D.clear();
831
832 // Accept attributes in an init-declarator. In the first declarator in a
833 // declaration, these would be part of the declspec. In subsequent
834 // declarators, they become part of the declarator itself, so that they
835 // don't apply to declarators after *this* one. Examples:
836 // short __attribute__((common)) var; -> declspec
837 // short var __attribute__((common)); -> declarator
838 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +0000839 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +0000840
841 ParseDeclarator(D);
842
John McCalld226f652010-08-21 09:40:31 +0000843 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000844 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000845 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000846 DeclsInGroup.push_back(ThisDecl);
847 }
848
849 if (DeclEnd)
850 *DeclEnd = Tok.getLocation();
851
852 if (Context != Declarator::ForContext &&
853 ExpectAndConsume(tok::semi,
854 Context == Declarator::FileContext
855 ? diag::err_invalid_token_after_toplevel_declarator
856 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000857 // Okay, there was no semicolon and one was expected. If we see a
858 // declaration specifier, just assume it was missing and continue parsing.
859 // Otherwise things are very confused and we skip to recover.
860 if (!isDeclarationSpecifier()) {
861 SkipUntil(tok::r_brace, true, true);
862 if (Tok.is(tok::semi))
863 ConsumeToken();
864 }
John McCalld8ac0572009-11-03 19:26:08 +0000865 }
866
Douglas Gregor23c94db2010-07-02 17:43:08 +0000867 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000868 DeclsInGroup.data(),
869 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000870}
871
Richard Smithad762fc2011-04-14 22:09:26 +0000872/// Parse an optional simple-asm-expr and attributes, and attach them to a
873/// declarator. Returns true on an error.
874bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
875 // If a simple-asm-expr is present, parse it.
876 if (Tok.is(tok::kw_asm)) {
877 SourceLocation Loc;
878 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
879 if (AsmLabel.isInvalid()) {
880 SkipUntil(tok::semi, true, true);
881 return true;
882 }
883
884 D.setAsmLabel(AsmLabel.release());
885 D.SetRangeEnd(Loc);
886 }
887
888 MaybeParseGNUAttributes(D);
889 return false;
890}
891
Douglas Gregor1426e532009-05-12 21:31:51 +0000892/// \brief Parse 'declaration' after parsing 'declaration-specifiers
893/// declarator'. This method parses the remainder of the declaration
894/// (including any attributes or initializer, among other things) and
895/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000896///
Reid Spencer5f016e22007-07-11 17:01:13 +0000897/// init-declarator: [C99 6.7]
898/// declarator
899/// declarator '=' initializer
900/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
901/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000902/// [C++] declarator initializer[opt]
903///
904/// [C++] initializer:
905/// [C++] '=' initializer-clause
906/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000907/// [C++0x] '=' 'default' [TODO]
908/// [C++0x] '=' 'delete'
909///
910/// According to the standard grammar, =default and =delete are function
911/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000912///
John McCalld226f652010-08-21 09:40:31 +0000913Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000914 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +0000915 if (ParseAttributesAfterDeclarator(D))
916 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Richard Smithad762fc2011-04-14 22:09:26 +0000918 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
919}
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Richard Smithad762fc2011-04-14 22:09:26 +0000921Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
922 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000923 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000924 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000925 switch (TemplateInfo.Kind) {
926 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000927 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000928 break;
929
930 case ParsedTemplateInfo::Template:
931 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000932 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000933 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +0000934 TemplateInfo.TemplateParams->data(),
935 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000936 D);
937 break;
938
939 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000940 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000941 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000942 TemplateInfo.ExternLoc,
943 TemplateInfo.TemplateLoc,
944 D);
945 if (ThisRes.isInvalid()) {
946 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000947 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000948 }
949
950 ThisDecl = ThisRes.get();
951 break;
952 }
953 }
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Richard Smith34b41d92011-02-20 03:19:35 +0000955 bool TypeContainsAuto =
956 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
957
Douglas Gregor1426e532009-05-12 21:31:51 +0000958 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000959 if (isTokenEqualOrMistypedEqualEqual(
960 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000961 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000962 if (Tok.is(tok::kw_delete)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000963 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000964
965 if (!getLang().CPlusPlus0x)
966 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
967
Douglas Gregor1426e532009-05-12 21:31:51 +0000968 Actions.SetDeclDeleted(ThisDecl, DelLoc);
Sean Huntfe2695e2011-05-06 01:42:00 +0000969 } else if (Tok.is(tok::kw_default)) {
970 SourceLocation DefLoc = ConsumeToken();
971
972 Diag(DefLoc, diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +0000973 } else {
John McCall731ad842009-12-19 09:28:58 +0000974 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
975 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000976 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000977 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000978
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000979 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000980 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000981 ConsumeCodeCompletionToken();
982 SkipUntil(tok::comma, true, true);
983 return ThisDecl;
984 }
985
John McCall60d7b3a2010-08-24 06:29:42 +0000986 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000987
John McCall731ad842009-12-19 09:28:58 +0000988 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000989 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000990 ExitScope();
991 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000992
Douglas Gregor1426e532009-05-12 21:31:51 +0000993 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000994 SkipUntil(tok::comma, true, true);
995 Actions.ActOnInitializerError(ThisDecl);
996 } else
Richard Smith34b41d92011-02-20 03:19:35 +0000997 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
998 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000999 }
1000 } else if (Tok.is(tok::l_paren)) {
1001 // Parse C++ direct initializer: '(' expression-list ')'
1002 SourceLocation LParenLoc = ConsumeParen();
1003 ExprVector Exprs(Actions);
1004 CommaLocsTy CommaLocs;
1005
Douglas Gregorb4debae2009-12-22 17:47:17 +00001006 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1007 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001008 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001009 }
1010
Douglas Gregor1426e532009-05-12 21:31:51 +00001011 if (ParseExpressionList(Exprs, CommaLocs)) {
1012 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001013
1014 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001015 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001016 ExitScope();
1017 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001018 } else {
1019 // Match the ')'.
1020 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1021
1022 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1023 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001024
1025 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001026 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001027 ExitScope();
1028 }
1029
Douglas Gregor1426e532009-05-12 21:31:51 +00001030 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1031 move_arg(Exprs),
Richard Smith34b41d92011-02-20 03:19:35 +00001032 RParenLoc,
1033 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001034 }
1035 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001036 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001037 }
1038
Richard Smith483b9f32011-02-21 20:05:19 +00001039 Actions.FinalizeDeclaration(ThisDecl);
1040
Douglas Gregor1426e532009-05-12 21:31:51 +00001041 return ThisDecl;
1042}
1043
Reid Spencer5f016e22007-07-11 17:01:13 +00001044/// ParseSpecifierQualifierList
1045/// specifier-qualifier-list:
1046/// type-specifier specifier-qualifier-list[opt]
1047/// type-qualifier specifier-qualifier-list[opt]
1048/// [GNU] attributes specifier-qualifier-list[opt]
1049///
1050void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
1051 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1052 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 // Validate declspec for type-name.
1056 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001057 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001058 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Reid Spencer5f016e22007-07-11 17:01:13 +00001061 // Issue diagnostic and remove storage class if present.
1062 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1063 if (DS.getStorageClassSpecLoc().isValid())
1064 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1065 else
1066 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1067 DS.ClearStorageClassSpecs();
1068 }
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 // Issue diagnostic and remove function specfier if present.
1071 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001072 if (DS.isInlineSpecified())
1073 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1074 if (DS.isVirtualSpecified())
1075 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1076 if (DS.isExplicitSpecified())
1077 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001078 DS.ClearFunctionSpecs();
1079 }
1080}
1081
Chris Lattnerc199ab32009-04-12 20:42:31 +00001082/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1083/// specified token is valid after the identifier in a declarator which
1084/// immediately follows the declspec. For example, these things are valid:
1085///
1086/// int x [ 4]; // direct-declarator
1087/// int x ( int y); // direct-declarator
1088/// int(int x ) // direct-declarator
1089/// int x ; // simple-declaration
1090/// int x = 17; // init-declarator-list
1091/// int x , y; // init-declarator-list
1092/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001093/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001094/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001095///
1096/// This is not, because 'x' does not immediately follow the declspec (though
1097/// ')' happens to be valid anyway).
1098/// int (x)
1099///
1100static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1101 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1102 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001103 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001104}
1105
Chris Lattnere40c2952009-04-14 21:34:55 +00001106
1107/// ParseImplicitInt - This method is called when we have an non-typename
1108/// identifier in a declspec (which normally terminates the decl spec) when
1109/// the declspec has no type specifier. In this case, the declspec is either
1110/// malformed or is "implicit int" (in K&R and C89).
1111///
1112/// This method handles diagnosing this prettily and returns false if the
1113/// declspec is done being processed. If it recovers and thinks there may be
1114/// other pieces of declspec after it, it returns true.
1115///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001116bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001117 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001118 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001119 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Chris Lattnere40c2952009-04-14 21:34:55 +00001121 SourceLocation Loc = Tok.getLocation();
1122 // If we see an identifier that is not a type name, we normally would
1123 // parse it as the identifer being declared. However, when a typename
1124 // is typo'd or the definition is not included, this will incorrectly
1125 // parse the typename as the identifier name and fall over misparsing
1126 // later parts of the diagnostic.
1127 //
1128 // As such, we try to do some look-ahead in cases where this would
1129 // otherwise be an "implicit-int" case to see if this is invalid. For
1130 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1131 // an identifier with implicit int, we'd get a parse error because the
1132 // next token is obviously invalid for a type. Parse these as a case
1133 // with an invalid type specifier.
1134 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Chris Lattnere40c2952009-04-14 21:34:55 +00001136 // Since we know that this either implicit int (which is rare) or an
1137 // error, we'd do lookahead to try to do better recovery.
1138 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1139 // If this token is valid for implicit int, e.g. "static x = 4", then
1140 // we just avoid eating the identifier, so it will be parsed as the
1141 // identifier in the declarator.
1142 return false;
1143 }
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Chris Lattnere40c2952009-04-14 21:34:55 +00001145 // Otherwise, if we don't consume this token, we are going to emit an
1146 // error anyway. Try to recover from various common problems. Check
1147 // to see if this was a reference to a tag name without a tag specified.
1148 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001149 //
1150 // C++ doesn't need this, and isTagName doesn't take SS.
1151 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001152 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001153 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Douglas Gregor23c94db2010-07-02 17:43:08 +00001155 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001156 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001157 case DeclSpec::TST_enum:
1158 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1159 case DeclSpec::TST_union:
1160 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1161 case DeclSpec::TST_struct:
1162 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1163 case DeclSpec::TST_class:
1164 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001165 }
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Chris Lattnerf4382f52009-04-14 22:17:06 +00001167 if (TagName) {
1168 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001169 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001170 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Chris Lattnerf4382f52009-04-14 22:17:06 +00001172 // Parse this as a tag as if the missing tag were present.
1173 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001174 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001175 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001176 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001177 return true;
1178 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001179 }
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Douglas Gregora786fdb2009-10-13 23:27:22 +00001181 // This is almost certainly an invalid type name. Let the action emit a
1182 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001183 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001184 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001185 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001186 // The action emitted a diagnostic, so we don't have to.
1187 if (T) {
1188 // The action has suggested that the type T could be used. Set that as
1189 // the type in the declaration specifiers, consume the would-be type
1190 // name token, and we're done.
1191 const char *PrevSpec;
1192 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001193 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001194 DS.SetRangeEnd(Tok.getLocation());
1195 ConsumeToken();
1196
1197 // There may be other declaration specifiers after this.
1198 return true;
1199 }
1200
1201 // Fall through; the action had no suggestion for us.
1202 } else {
1203 // The action did not emit a diagnostic, so emit one now.
1204 SourceRange R;
1205 if (SS) R = SS->getRange();
1206 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1207 }
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Douglas Gregora786fdb2009-10-13 23:27:22 +00001209 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001210 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001211 unsigned DiagID;
1212 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001213 DS.SetRangeEnd(Tok.getLocation());
1214 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Chris Lattnere40c2952009-04-14 21:34:55 +00001216 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1217 // avoid rippling error messages on subsequent uses of the same type,
1218 // could be useful if #include was forgotten.
1219 return false;
1220}
1221
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001222/// \brief Determine the declaration specifier context from the declarator
1223/// context.
1224///
1225/// \param Context the declarator context, which is one of the
1226/// Declarator::TheContext enumerator values.
1227Parser::DeclSpecContext
1228Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1229 if (Context == Declarator::MemberContext)
1230 return DSC_class;
1231 if (Context == Declarator::FileContext)
1232 return DSC_top_level;
1233 return DSC_normal;
1234}
1235
Reid Spencer5f016e22007-07-11 17:01:13 +00001236/// ParseDeclarationSpecifiers
1237/// declaration-specifiers: [C99 6.7]
1238/// storage-class-specifier declaration-specifiers[opt]
1239/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001240/// [C99] function-specifier declaration-specifiers[opt]
1241/// [GNU] attributes declaration-specifiers[opt]
1242///
1243/// storage-class-specifier: [C99 6.7.1]
1244/// 'typedef'
1245/// 'extern'
1246/// 'static'
1247/// 'auto'
1248/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001249/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001250/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001251/// function-specifier: [C99 6.7.4]
1252/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001253/// [C++] 'virtual'
1254/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001255/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001256/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001257/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001258
Reid Spencer5f016e22007-07-11 17:01:13 +00001259///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001260void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001261 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001262 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001263 DeclSpecContext DSContext) {
1264 if (DS.getSourceRange().isInvalid()) {
1265 DS.SetRangeStart(Tok.getLocation());
1266 DS.SetRangeEnd(Tok.getLocation());
1267 }
1268
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001270 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001272 unsigned DiagID = 0;
1273
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001275
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001277 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001278 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 // If this is not a declaration specifier token, we're done reading decl
1280 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001281 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001282 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001284 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001285 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001286 if (DS.hasTypeSpecifier()) {
1287 bool AllowNonIdentifiers
1288 = (getCurScope()->getFlags() & (Scope::ControlScope |
1289 Scope::BlockScope |
1290 Scope::TemplateParamScope |
1291 Scope::FunctionPrototypeScope |
1292 Scope::AtCatchScope)) == 0;
1293 bool AllowNestedNameSpecifiers
1294 = DSContext == DSC_top_level ||
1295 (DSContext == DSC_class && DS.isFriendSpecified());
1296
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001297 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1298 AllowNonIdentifiers,
1299 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001300 ConsumeCodeCompletionToken();
1301 return;
1302 }
1303
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001304 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1305 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1306 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001307 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1308 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001309 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001310 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001311 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001312 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001313
1314 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1315 ConsumeCodeCompletionToken();
1316 return;
1317 }
1318
Chris Lattner5e02c472009-01-05 00:07:25 +00001319 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001320 // C++ scope specifier. Annotate and loop, or bail out on error.
1321 if (TryAnnotateCXXScopeToken(true)) {
1322 if (!DS.hasTypeSpecifier())
1323 DS.SetTypeSpecError();
1324 goto DoneWithDeclSpec;
1325 }
John McCall2e0a7152010-03-01 18:20:46 +00001326 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1327 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001328 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001329
1330 case tok::annot_cxxscope: {
1331 if (DS.hasTypeSpecifier())
1332 goto DoneWithDeclSpec;
1333
John McCallaa87d332009-12-12 11:40:51 +00001334 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001335 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1336 Tok.getAnnotationRange(),
1337 SS);
John McCallaa87d332009-12-12 11:40:51 +00001338
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001339 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001340 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001341 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001342 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001343 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001344 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001345
1346 // C++ [class.qual]p2:
1347 // In a lookup in which the constructor is an acceptable lookup
1348 // result and the nested-name-specifier nominates a class C:
1349 //
1350 // - if the name specified after the
1351 // nested-name-specifier, when looked up in C, is the
1352 // injected-class-name of C (Clause 9), or
1353 //
1354 // - if the name specified after the nested-name-specifier
1355 // is the same as the identifier or the
1356 // simple-template-id's template-name in the last
1357 // component of the nested-name-specifier,
1358 //
1359 // the name is instead considered to name the constructor of
1360 // class C.
1361 //
1362 // Thus, if the template-name is actually the constructor
1363 // name, then the code is ill-formed; this interpretation is
1364 // reinforced by the NAD status of core issue 635.
1365 TemplateIdAnnotation *TemplateId
1366 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +00001367 if ((DSContext == DSC_top_level ||
1368 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1369 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001370 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001371 if (isConstructorDeclarator()) {
1372 // The user meant this to be an out-of-line constructor
1373 // definition, but template arguments are not allowed
1374 // there. Just allow this as a constructor; we'll
1375 // complain about it later.
1376 goto DoneWithDeclSpec;
1377 }
1378
1379 // The user meant this to name a type, but it actually names
1380 // a constructor with some extraneous template
1381 // arguments. Complain, then parse it as a type as the user
1382 // intended.
1383 Diag(TemplateId->TemplateNameLoc,
1384 diag::err_out_of_line_template_id_names_constructor)
1385 << TemplateId->Name;
1386 }
1387
John McCallaa87d332009-12-12 11:40:51 +00001388 DS.getTypeSpecScope() = SS;
1389 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001390 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001391 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001392 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001393 continue;
1394 }
1395
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001396 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001397 DS.getTypeSpecScope() = SS;
1398 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001399 if (Tok.getAnnotationValue()) {
1400 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001401 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1402 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001403 PrevSpec, DiagID, T);
1404 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001405 else
1406 DS.SetTypeSpecError();
1407 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1408 ConsumeToken(); // The typename
1409 }
1410
Douglas Gregor9135c722009-03-25 15:40:00 +00001411 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001412 goto DoneWithDeclSpec;
1413
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001414 // If we're in a context where the identifier could be a class name,
1415 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001416 if ((DSContext == DSC_top_level ||
1417 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001418 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001419 &SS)) {
1420 if (isConstructorDeclarator())
1421 goto DoneWithDeclSpec;
1422
1423 // As noted in C++ [class.qual]p2 (cited above), when the name
1424 // of the class is qualified in a context where it could name
1425 // a constructor, its a constructor name. However, we've
1426 // looked at the declarator, and the user probably meant this
1427 // to be a type. Complain that it isn't supposed to be treated
1428 // as a type, then proceed to parse it as a type.
1429 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1430 << Next.getIdentifierInfo();
1431 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001432
John McCallb3d87482010-08-24 05:47:05 +00001433 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1434 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001435 getCurScope(), &SS,
1436 false, false, ParsedType(),
1437 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001438
Chris Lattnerf4382f52009-04-14 22:17:06 +00001439 // If the referenced identifier is not a type, then this declspec is
1440 // erroneous: We already checked about that it has no type specifier, and
1441 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001442 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001443 if (TypeRep == 0) {
1444 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001445 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001446 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001447 }
Mike Stump1eb44332009-09-09 15:08:12 +00001448
John McCallaa87d332009-12-12 11:40:51 +00001449 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001450 ConsumeToken(); // The C++ scope.
1451
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001452 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001453 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001454 if (isInvalid)
1455 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001457 DS.SetRangeEnd(Tok.getLocation());
1458 ConsumeToken(); // The typename.
1459
1460 continue;
1461 }
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Chris Lattner80d0c892009-01-21 19:48:37 +00001463 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001464 if (Tok.getAnnotationValue()) {
1465 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001466 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001467 DiagID, T);
1468 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001469 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001470
1471 if (isInvalid)
1472 break;
1473
Chris Lattner80d0c892009-01-21 19:48:37 +00001474 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1475 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Chris Lattner80d0c892009-01-21 19:48:37 +00001477 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1478 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001479 // Objective-C interface.
1480 if (Tok.is(tok::less) && getLang().ObjC1)
1481 ParseObjCProtocolQualifiers(DS);
1482
Chris Lattner80d0c892009-01-21 19:48:37 +00001483 continue;
1484 }
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Douglas Gregorbfad9152011-04-28 15:48:45 +00001486 case tok::kw___is_signed:
1487 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1488 // typically treats it as a trait. If we see __is_signed as it appears
1489 // in libstdc++, e.g.,
1490 //
1491 // static const bool __is_signed;
1492 //
1493 // then treat __is_signed as an identifier rather than as a keyword.
1494 if (DS.getTypeSpecType() == TST_bool &&
1495 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1496 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1497 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1498 Tok.setKind(tok::identifier);
1499 }
1500
1501 // We're done with the declaration-specifiers.
1502 goto DoneWithDeclSpec;
1503
Chris Lattner3bd934a2008-07-26 01:18:38 +00001504 // typedef-name
1505 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001506 // In C++, check to see if this is a scope specifier like foo::bar::, if
1507 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001508 if (getLang().CPlusPlus) {
1509 if (TryAnnotateCXXScopeToken(true)) {
1510 if (!DS.hasTypeSpecifier())
1511 DS.SetTypeSpecError();
1512 goto DoneWithDeclSpec;
1513 }
1514 if (!Tok.is(tok::identifier))
1515 continue;
1516 }
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Chris Lattner3bd934a2008-07-26 01:18:38 +00001518 // This identifier can only be a typedef name if we haven't already seen
1519 // a type-specifier. Without this check we misparse:
1520 // typedef int X; struct Y { short X; }; as 'short int'.
1521 if (DS.hasTypeSpecifier())
1522 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001523
John Thompson82287d12010-02-05 00:12:22 +00001524 // Check for need to substitute AltiVec keyword tokens.
1525 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1526 break;
1527
Chris Lattner3bd934a2008-07-26 01:18:38 +00001528 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001529 ParsedType TypeRep =
1530 Actions.getTypeName(*Tok.getIdentifierInfo(),
1531 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001532
Chris Lattnerc199ab32009-04-12 20:42:31 +00001533 // If this is not a typedef name, don't parse it as part of the declspec,
1534 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001535 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001536 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001537 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001538 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001539
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001540 // If we're in a context where the identifier could be a class name,
1541 // check whether this is a constructor declaration.
1542 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001543 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001544 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001545 goto DoneWithDeclSpec;
1546
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001547 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001548 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001549 if (isInvalid)
1550 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Chris Lattner3bd934a2008-07-26 01:18:38 +00001552 DS.SetRangeEnd(Tok.getLocation());
1553 ConsumeToken(); // The identifier
1554
1555 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1556 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001557 // Objective-C interface.
1558 if (Tok.is(tok::less) && getLang().ObjC1)
1559 ParseObjCProtocolQualifiers(DS);
1560
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001561 // Need to support trailing type qualifiers (e.g. "id<p> const").
1562 // If a type specifier follows, it will be diagnosed elsewhere.
1563 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001564 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001565
1566 // type-name
1567 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001568 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001569 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001570 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001571 // This template-id does not refer to a type name, so we're
1572 // done with the type-specifiers.
1573 goto DoneWithDeclSpec;
1574 }
1575
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001576 // If we're in a context where the template-id could be a
1577 // constructor name or specialization, check whether this is a
1578 // constructor declaration.
1579 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001580 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001581 isConstructorDeclarator())
1582 goto DoneWithDeclSpec;
1583
Douglas Gregor39a8de12009-02-25 19:37:18 +00001584 // Turn the template-id annotation token into a type annotation
1585 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001586 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001587 continue;
1588 }
1589
Reid Spencer5f016e22007-07-11 17:01:13 +00001590 // GNU attributes support.
1591 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001592 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001593 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001594
1595 // Microsoft declspec support.
1596 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001597 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001598 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Steve Naroff239f0732008-12-25 14:16:32 +00001600 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001601 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001602 // FIXME: Add handling here!
1603 break;
1604
1605 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001606 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001607 case tok::kw___cdecl:
1608 case tok::kw___stdcall:
1609 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001610 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001611 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001612 continue;
1613
Dawn Perchik52fc3142010-09-03 01:29:35 +00001614 // Borland single token adornments.
1615 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001616 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001617 continue;
1618
Peter Collingbournef315fa82011-02-14 01:42:53 +00001619 // OpenCL single token adornments.
1620 case tok::kw___kernel:
1621 ParseOpenCLAttributes(DS.getAttributes());
1622 continue;
1623
Reid Spencer5f016e22007-07-11 17:01:13 +00001624 // storage-class-specifier
1625 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001626 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001627 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001628 break;
1629 case tok::kw_extern:
1630 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001631 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001632 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001633 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001634 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001635 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001636 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001637 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001638 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001639 case tok::kw_static:
1640 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001641 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001642 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001643 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001644 break;
1645 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001646 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001647 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1648 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1649 DiagID, getLang());
1650 if (!isInvalid)
1651 Diag(Tok, diag::auto_storage_class)
1652 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1653 }
1654 else
1655 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1656 DiagID);
1657 }
Anders Carlssone89d1592009-06-26 18:41:36 +00001658 else
John McCallfec54012009-08-03 20:12:06 +00001659 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001660 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001661 break;
1662 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001663 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001664 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001665 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001666 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001667 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001668 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001669 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001670 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001671 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 // function-specifier
1675 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001676 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001677 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001678 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001679 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001680 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001681 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001682 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001683 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001684
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001685 // friend
1686 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001687 if (DSContext == DSC_class)
1688 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1689 else {
1690 PrevSpec = ""; // not actually used by the diagnostic
1691 DiagID = diag::err_friend_invalid_in_context;
1692 isInvalid = true;
1693 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001694 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001695
Sebastian Redl2ac67232009-11-05 15:47:02 +00001696 // constexpr
1697 case tok::kw_constexpr:
1698 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1699 break;
1700
Chris Lattner80d0c892009-01-21 19:48:37 +00001701 // type-specifier
1702 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001703 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1704 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001705 break;
1706 case tok::kw_long:
1707 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001708 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1709 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001710 else
John McCallfec54012009-08-03 20:12:06 +00001711 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1712 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001713 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001714 case tok::kw___int64:
1715 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1716 DiagID);
1717 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001718 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001719 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1720 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001721 break;
1722 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001723 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1724 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001725 break;
1726 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001727 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1728 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001729 break;
1730 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001731 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1732 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001733 break;
1734 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001735 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1736 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001737 break;
1738 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001739 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1740 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001741 break;
1742 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001743 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1744 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001745 break;
1746 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001747 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1748 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001749 break;
1750 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1752 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001753 break;
1754 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001755 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1756 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001757 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001758 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001759 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1760 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001761 break;
1762 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001763 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1764 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001765 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001766 case tok::kw_bool:
1767 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001768 if (Tok.is(tok::kw_bool) &&
1769 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1770 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1771 PrevSpec = ""; // Not used by the diagnostic.
1772 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001773 // For better error recovery.
1774 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001775 isInvalid = true;
1776 } else {
1777 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1778 DiagID);
1779 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001780 break;
1781 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001782 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1783 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001784 break;
1785 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001786 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1787 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001788 break;
1789 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001790 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1791 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001792 break;
John Thompson82287d12010-02-05 00:12:22 +00001793 case tok::kw___vector:
1794 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1795 break;
1796 case tok::kw___pixel:
1797 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1798 break;
John McCalla5fc4722011-04-09 22:50:59 +00001799 case tok::kw___unknown_anytype:
1800 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1801 PrevSpec, DiagID);
1802 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001803
1804 // class-specifier:
1805 case tok::kw_class:
1806 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001807 case tok::kw_union: {
1808 tok::TokenKind Kind = Tok.getKind();
1809 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001810 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001811 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001812 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001813
1814 // enum-specifier:
1815 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001816 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001817 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001818 continue;
1819
1820 // cv-qualifier:
1821 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001822 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1823 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001824 break;
1825 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001826 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1827 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001828 break;
1829 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001830 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1831 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001832 break;
1833
Douglas Gregord57959a2009-03-27 23:10:48 +00001834 // C++ typename-specifier:
1835 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001836 if (TryAnnotateTypeOrScopeToken()) {
1837 DS.SetTypeSpecError();
1838 goto DoneWithDeclSpec;
1839 }
1840 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001841 continue;
1842 break;
1843
Chris Lattner80d0c892009-01-21 19:48:37 +00001844 // GNU typeof support.
1845 case tok::kw_typeof:
1846 ParseTypeofSpecifier(DS);
1847 continue;
1848
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001849 case tok::kw_decltype:
1850 ParseDecltypeSpecifier(DS);
1851 continue;
1852
Peter Collingbourne207f4d82011-03-18 22:38:29 +00001853 // OpenCL qualifiers:
1854 case tok::kw_private:
1855 if (!getLang().OpenCL)
1856 goto DoneWithDeclSpec;
1857 case tok::kw___private:
1858 case tok::kw___global:
1859 case tok::kw___local:
1860 case tok::kw___constant:
1861 case tok::kw___read_only:
1862 case tok::kw___write_only:
1863 case tok::kw___read_write:
1864 ParseOpenCLQualifiers(DS);
1865 break;
1866
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001867 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001868 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001869 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1870 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001871 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001872 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Douglas Gregor46f936e2010-11-19 17:10:50 +00001874 if (!ParseObjCProtocolQualifiers(DS))
1875 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1876 << FixItHint::CreateInsertion(Loc, "id")
1877 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001878
1879 // Need to support trailing type qualifiers (e.g. "id<p> const").
1880 // If a type specifier follows, it will be diagnosed elsewhere.
1881 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001882 }
John McCallfec54012009-08-03 20:12:06 +00001883 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001884 if (isInvalid) {
1885 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001886 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001887
1888 if (DiagID == diag::ext_duplicate_declspec)
1889 Diag(Tok, DiagID)
1890 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1891 else
1892 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001893 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001894
Chris Lattner81c018d2008-03-13 06:29:04 +00001895 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001896 if (DiagID != diag::err_bool_redeclaration)
1897 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001898 }
1899}
Douglas Gregoradcac882008-12-01 23:54:00 +00001900
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001901/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001902/// primarily follow the C++ grammar with additions for C99 and GNU,
1903/// which together subsume the C grammar. Note that the C++
1904/// type-specifier also includes the C type-qualifier (for const,
1905/// volatile, and C99 restrict). Returns true if a type-specifier was
1906/// found (and parsed), false otherwise.
1907///
1908/// type-specifier: [C++ 7.1.5]
1909/// simple-type-specifier
1910/// class-specifier
1911/// enum-specifier
1912/// elaborated-type-specifier [TODO]
1913/// cv-qualifier
1914///
1915/// cv-qualifier: [C++ 7.1.5.1]
1916/// 'const'
1917/// 'volatile'
1918/// [C99] 'restrict'
1919///
1920/// simple-type-specifier: [ C++ 7.1.5.2]
1921/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1922/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1923/// 'char'
1924/// 'wchar_t'
1925/// 'bool'
1926/// 'short'
1927/// 'int'
1928/// 'long'
1929/// 'signed'
1930/// 'unsigned'
1931/// 'float'
1932/// 'double'
1933/// 'void'
1934/// [C99] '_Bool'
1935/// [C99] '_Complex'
1936/// [C99] '_Imaginary' // Removed in TC2?
1937/// [GNU] '_Decimal32'
1938/// [GNU] '_Decimal64'
1939/// [GNU] '_Decimal128'
1940/// [GNU] typeof-specifier
1941/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1942/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001943/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001944/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001945bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001946 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001947 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001948 const ParsedTemplateInfo &TemplateInfo,
1949 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001950 SourceLocation Loc = Tok.getLocation();
1951
1952 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001953 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001954 // If we already have a type specifier, this identifier is not a type.
1955 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1956 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1957 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1958 return false;
John Thompson82287d12010-02-05 00:12:22 +00001959 // Check for need to substitute AltiVec keyword tokens.
1960 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1961 break;
1962 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001963 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001964 // Annotate typenames and C++ scope specifiers. If we get one, just
1965 // recurse to handle whatever we get.
1966 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001967 return true;
1968 if (Tok.is(tok::identifier))
1969 return false;
1970 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1971 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001972 case tok::coloncolon: // ::foo::bar
1973 if (NextToken().is(tok::kw_new) || // ::new
1974 NextToken().is(tok::kw_delete)) // ::delete
1975 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001976
Chris Lattner166a8fc2009-01-04 23:41:41 +00001977 // Annotate typenames and C++ scope specifiers. If we get one, just
1978 // recurse to handle whatever we get.
1979 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001980 return true;
1981 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1982 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001983
Douglas Gregor12e083c2008-11-07 15:42:26 +00001984 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001985 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001986 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00001987 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1988 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001989 DiagID, T);
1990 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001991 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001992 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1993 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001994
Douglas Gregor12e083c2008-11-07 15:42:26 +00001995 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1996 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1997 // Objective-C interface. If we don't have Objective-C or a '<', this is
1998 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001999 if (Tok.is(tok::less) && getLang().ObjC1)
2000 ParseObjCProtocolQualifiers(DS);
2001
Douglas Gregor12e083c2008-11-07 15:42:26 +00002002 return true;
2003 }
2004
2005 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002006 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002007 break;
2008 case tok::kw_long:
2009 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002010 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2011 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002012 else
John McCallfec54012009-08-03 20:12:06 +00002013 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2014 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002015 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002016 case tok::kw___int64:
2017 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2018 DiagID);
2019 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002020 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002021 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002022 break;
2023 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002024 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2025 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002026 break;
2027 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002028 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2029 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002030 break;
2031 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002032 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2033 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002034 break;
2035 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002036 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002037 break;
2038 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002039 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002040 break;
2041 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002042 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002043 break;
2044 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002045 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002046 break;
2047 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002048 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002049 break;
2050 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002051 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002052 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002053 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002054 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002055 break;
2056 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002057 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002058 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002059 case tok::kw_bool:
2060 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002061 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002062 break;
2063 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002064 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2065 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002066 break;
2067 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002068 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2069 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002070 break;
2071 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002072 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2073 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002074 break;
John Thompson82287d12010-02-05 00:12:22 +00002075 case tok::kw___vector:
2076 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2077 break;
2078 case tok::kw___pixel:
2079 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2080 break;
2081
Douglas Gregor12e083c2008-11-07 15:42:26 +00002082 // class-specifier:
2083 case tok::kw_class:
2084 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002085 case tok::kw_union: {
2086 tok::TokenKind Kind = Tok.getKind();
2087 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002088 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2089 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002090 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002091 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002092
2093 // enum-specifier:
2094 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002095 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002096 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002097 return true;
2098
2099 // cv-qualifier:
2100 case tok::kw_const:
2101 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002102 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002103 break;
2104 case tok::kw_volatile:
2105 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002106 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002107 break;
2108 case tok::kw_restrict:
2109 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002110 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002111 break;
2112
2113 // GNU typeof support.
2114 case tok::kw_typeof:
2115 ParseTypeofSpecifier(DS);
2116 return true;
2117
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002118 // C++0x decltype support.
2119 case tok::kw_decltype:
2120 ParseDecltypeSpecifier(DS);
2121 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002122
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002123 // OpenCL qualifiers:
2124 case tok::kw_private:
2125 if (!getLang().OpenCL)
2126 return false;
2127 case tok::kw___private:
2128 case tok::kw___global:
2129 case tok::kw___local:
2130 case tok::kw___constant:
2131 case tok::kw___read_only:
2132 case tok::kw___write_only:
2133 case tok::kw___read_write:
2134 ParseOpenCLQualifiers(DS);
2135 break;
2136
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002137 // C++0x auto support.
2138 case tok::kw_auto:
2139 if (!getLang().CPlusPlus0x)
2140 return false;
2141
John McCallfec54012009-08-03 20:12:06 +00002142 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002143 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002144
Eli Friedman290eeb02009-06-08 23:27:34 +00002145 case tok::kw___ptr64:
2146 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002147 case tok::kw___cdecl:
2148 case tok::kw___stdcall:
2149 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002150 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00002151 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002152 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002153
Dawn Perchik52fc3142010-09-03 01:29:35 +00002154 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002155 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002156 return true;
2157
Douglas Gregor12e083c2008-11-07 15:42:26 +00002158 default:
2159 // Not a type-specifier; do nothing.
2160 return false;
2161 }
2162
2163 // If the specifier combination wasn't legal, issue a diagnostic.
2164 if (isInvalid) {
2165 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002166 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002167 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002168 }
2169 DS.SetRangeEnd(Tok.getLocation());
2170 ConsumeToken(); // whatever we parsed above.
2171 return true;
2172}
Reid Spencer5f016e22007-07-11 17:01:13 +00002173
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002174/// ParseStructDeclaration - Parse a struct declaration without the terminating
2175/// semicolon.
2176///
Reid Spencer5f016e22007-07-11 17:01:13 +00002177/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002178/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002179/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002180/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002181/// struct-declarator-list:
2182/// struct-declarator
2183/// struct-declarator-list ',' struct-declarator
2184/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2185/// struct-declarator:
2186/// declarator
2187/// [GNU] declarator attributes[opt]
2188/// declarator[opt] ':' constant-expression
2189/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2190///
Chris Lattnere1359422008-04-10 06:46:29 +00002191void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002192ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002193 if (Tok.is(tok::kw___extension__)) {
2194 // __extension__ silences extension warnings in the subexpression.
2195 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002196 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002197 return ParseStructDeclaration(DS, Fields);
2198 }
Mike Stump1eb44332009-09-09 15:08:12 +00002199
Steve Naroff28a7ca82007-08-20 22:28:22 +00002200 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002201 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002202
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002203 // If there are no declarators, this is a free-standing declaration
2204 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002205 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002206 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002207 return;
2208 }
2209
2210 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002211 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002212 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002213 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002214 FieldDeclarator DeclaratorInfo(DS);
2215
2216 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002217 if (!FirstDeclarator)
2218 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002219
Steve Naroff28a7ca82007-08-20 22:28:22 +00002220 /// struct-declarator: declarator
2221 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002222 if (Tok.isNot(tok::colon)) {
2223 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2224 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002225 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002226 }
Mike Stump1eb44332009-09-09 15:08:12 +00002227
Chris Lattner04d66662007-10-09 17:33:22 +00002228 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002229 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002230 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002231 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002232 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002233 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002234 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002235 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002236
Steve Naroff28a7ca82007-08-20 22:28:22 +00002237 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002238 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002239
John McCallbdd563e2009-11-03 02:38:08 +00002240 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002241 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002242 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002243
Steve Naroff28a7ca82007-08-20 22:28:22 +00002244 // If we don't have a comma, it is either the end of the list (a ';')
2245 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002246 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002247 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002248
Steve Naroff28a7ca82007-08-20 22:28:22 +00002249 // Consume the comma.
2250 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002251
John McCallbdd563e2009-11-03 02:38:08 +00002252 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002253 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002254}
2255
2256/// ParseStructUnionBody
2257/// struct-contents:
2258/// struct-declaration-list
2259/// [EXT] empty
2260/// [GNU] "struct-declaration-list" without terminatoring ';'
2261/// struct-declaration-list:
2262/// struct-declaration
2263/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002264/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002265///
Reid Spencer5f016e22007-07-11 17:01:13 +00002266void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002267 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002268 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2269 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002270
Reid Spencer5f016e22007-07-11 17:01:13 +00002271 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002272
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002273 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002274 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002275
Reid Spencer5f016e22007-07-11 17:01:13 +00002276 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2277 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002278 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002279 Diag(Tok, diag::ext_empty_struct_union)
2280 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002281
John McCalld226f652010-08-21 09:40:31 +00002282 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002283
Reid Spencer5f016e22007-07-11 17:01:13 +00002284 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002285 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002286 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002287
Reid Spencer5f016e22007-07-11 17:01:13 +00002288 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002289 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002290 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002291 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002292 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002293 ConsumeToken();
2294 continue;
2295 }
Chris Lattnere1359422008-04-10 06:46:29 +00002296
2297 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002298 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002299
John McCallbdd563e2009-11-03 02:38:08 +00002300 if (!Tok.is(tok::at)) {
2301 struct CFieldCallback : FieldCallback {
2302 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002303 Decl *TagDecl;
2304 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002305
John McCalld226f652010-08-21 09:40:31 +00002306 CFieldCallback(Parser &P, Decl *TagDecl,
2307 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002308 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2309
John McCalld226f652010-08-21 09:40:31 +00002310 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002311 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002312 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002313 FD.D.getDeclSpec().getSourceRange().getBegin(),
2314 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002315 FieldDecls.push_back(Field);
2316 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002317 }
John McCallbdd563e2009-11-03 02:38:08 +00002318 } Callback(*this, TagDecl, FieldDecls);
2319
2320 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002321 } else { // Handle @defs
2322 ConsumeToken();
2323 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2324 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002325 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002326 continue;
2327 }
2328 ConsumeToken();
2329 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2330 if (!Tok.is(tok::identifier)) {
2331 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002332 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002333 continue;
2334 }
John McCalld226f652010-08-21 09:40:31 +00002335 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002336 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002337 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002338 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2339 ConsumeToken();
2340 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002341 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002342
Chris Lattner04d66662007-10-09 17:33:22 +00002343 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002344 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002345 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002346 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002347 break;
2348 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002349 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2350 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002351 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002352 // If we stopped at a ';', eat it.
2353 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002354 }
2355 }
Mike Stump1eb44332009-09-09 15:08:12 +00002356
Steve Naroff60fccee2007-10-29 21:38:07 +00002357 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002358
John McCall0b7e6782011-03-24 11:26:52 +00002359 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002360 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002361 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002362
Douglas Gregor23c94db2010-07-02 17:43:08 +00002363 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00002364 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002365 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00002366 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002367 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002368 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002369}
2370
Reid Spencer5f016e22007-07-11 17:01:13 +00002371/// ParseEnumSpecifier
2372/// enum-specifier: [C99 6.7.2.2]
2373/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002374///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002375/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2376/// '}' attributes[opt]
2377/// 'enum' identifier
2378/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002379///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002380/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2381/// [C++0x] enum-head '{' enumerator-list ',' '}'
2382///
2383/// enum-head: [C++0x]
2384/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2385/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2386///
2387/// enum-key: [C++0x]
2388/// 'enum'
2389/// 'enum' 'class'
2390/// 'enum' 'struct'
2391///
2392/// enum-base: [C++0x]
2393/// ':' type-specifier-seq
2394///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002395/// [C++] elaborated-type-specifier:
2396/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2397///
Chris Lattner4c97d762009-04-12 21:49:30 +00002398void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002399 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002400 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002401 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002402 if (Tok.is(tok::code_completion)) {
2403 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002404 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00002405 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00002406 }
2407
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002408 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002409 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002410 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002411
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002412 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002413 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00002414 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002415 return;
2416
2417 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002418 Diag(Tok, diag::err_expected_ident);
2419 if (Tok.isNot(tok::l_brace)) {
2420 // Has no name and is not a definition.
2421 // Skip the rest of this declarator, up until the comma or semicolon.
2422 SkipUntil(tok::comma, true);
2423 return;
2424 }
2425 }
2426 }
Mike Stump1eb44332009-09-09 15:08:12 +00002427
Douglas Gregor86f208c2011-02-22 20:32:04 +00002428 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002429 bool IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002430 bool IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002431
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002432 if (getLang().CPlusPlus0x &&
2433 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002434 IsScopedEnum = true;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002435 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2436 ConsumeToken();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002437 }
2438
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002439 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002440 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2441 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002442 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002443
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002444 // Skip the rest of this declarator, up until the comma or semicolon.
2445 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002446 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002447 }
Mike Stump1eb44332009-09-09 15:08:12 +00002448
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002449 // If an identifier is present, consume and remember it.
2450 IdentifierInfo *Name = 0;
2451 SourceLocation NameLoc;
2452 if (Tok.is(tok::identifier)) {
2453 Name = Tok.getIdentifierInfo();
2454 NameLoc = ConsumeToken();
2455 }
Mike Stump1eb44332009-09-09 15:08:12 +00002456
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002457 if (!Name && IsScopedEnum) {
2458 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2459 // declaration of a scoped enumeration.
2460 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2461 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002462 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002463 }
2464
2465 TypeResult BaseType;
2466
Douglas Gregora61b3e72010-12-01 17:42:47 +00002467 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002468 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002469 bool PossibleBitfield = false;
2470 if (getCurScope()->getFlags() & Scope::ClassScope) {
2471 // If we're in class scope, this can either be an enum declaration with
2472 // an underlying type, or a declaration of a bitfield member. We try to
2473 // use a simple disambiguation scheme first to catch the common cases
2474 // (integer literal, sizeof); if it's still ambiguous, we then consider
2475 // anything that's a simple-type-specifier followed by '(' as an
2476 // expression. This suffices because function types are not valid
2477 // underlying types anyway.
2478 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2479 // If the next token starts an expression, we know we're parsing a
2480 // bit-field. This is the common case.
2481 if (TPR == TPResult::True())
2482 PossibleBitfield = true;
2483 // If the next token starts a type-specifier-seq, it may be either a
2484 // a fixed underlying type or the start of a function-style cast in C++;
2485 // lookahead one more token to see if it's obvious that we have a
2486 // fixed underlying type.
2487 else if (TPR == TPResult::False() &&
2488 GetLookAheadToken(2).getKind() == tok::semi) {
2489 // Consume the ':'.
2490 ConsumeToken();
2491 } else {
2492 // We have the start of a type-specifier-seq, so we have to perform
2493 // tentative parsing to determine whether we have an expression or a
2494 // type.
2495 TentativeParsingAction TPA(*this);
2496
2497 // Consume the ':'.
2498 ConsumeToken();
2499
Douglas Gregor86f208c2011-02-22 20:32:04 +00002500 if ((getLang().CPlusPlus &&
2501 isCXXDeclarationSpecifier() != TPResult::True()) ||
2502 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002503 // We'll parse this as a bitfield later.
2504 PossibleBitfield = true;
2505 TPA.Revert();
2506 } else {
2507 // We have a type-specifier-seq.
2508 TPA.Commit();
2509 }
2510 }
2511 } else {
2512 // Consume the ':'.
2513 ConsumeToken();
2514 }
2515
2516 if (!PossibleBitfield) {
2517 SourceRange Range;
2518 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002519
2520 if (!getLang().CPlusPlus0x)
2521 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2522 << Range;
Douglas Gregora61b3e72010-12-01 17:42:47 +00002523 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002524 }
2525
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002526 // There are three options here. If we have 'enum foo;', then this is a
2527 // forward declaration. If we have 'enum foo {...' then this is a
2528 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2529 //
2530 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2531 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2532 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2533 //
John McCallf312b1e2010-08-26 23:41:50 +00002534 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002535 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002536 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002537 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002538 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002539 else
John McCallf312b1e2010-08-26 23:41:50 +00002540 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002541
2542 // enums cannot be templates, although they can be referenced from a
2543 // template.
2544 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002545 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002546 Diag(Tok, diag::err_enum_template);
2547
2548 // Skip the rest of this declarator, up until the comma or semicolon.
2549 SkipUntil(tok::comma, true);
2550 return;
2551 }
2552
Douglas Gregorb9075602011-02-22 02:55:24 +00002553 if (!Name && TUK != Sema::TUK_Definition) {
2554 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2555
2556 // Skip the rest of this declarator, up until the comma or semicolon.
2557 SkipUntil(tok::comma, true);
2558 return;
2559 }
2560
Douglas Gregor402abb52009-05-28 23:31:59 +00002561 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002562 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002563 const char *PrevSpec = 0;
2564 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002565 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002566 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002567 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002568 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002569 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002570 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002571
Douglas Gregor48c89f42010-04-24 16:38:41 +00002572 if (IsDependent) {
2573 // This enum has a dependent nested-name-specifier. Handle it as a
2574 // dependent tag.
2575 if (!Name) {
2576 DS.SetTypeSpecError();
2577 Diag(Tok, diag::err_expected_type_name_after_typename);
2578 return;
2579 }
2580
Douglas Gregor23c94db2010-07-02 17:43:08 +00002581 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002582 TUK, SS, Name, StartLoc,
2583 NameLoc);
2584 if (Type.isInvalid()) {
2585 DS.SetTypeSpecError();
2586 return;
2587 }
2588
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002589 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2590 NameLoc.isValid() ? NameLoc : StartLoc,
2591 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002592 Diag(StartLoc, DiagID) << PrevSpec;
2593
2594 return;
2595 }
Mike Stump1eb44332009-09-09 15:08:12 +00002596
John McCalld226f652010-08-21 09:40:31 +00002597 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002598 // The action failed to produce an enumeration tag. If this is a
2599 // definition, consume the entire definition.
2600 if (Tok.is(tok::l_brace)) {
2601 ConsumeBrace();
2602 SkipUntil(tok::r_brace);
2603 }
2604
2605 DS.SetTypeSpecError();
2606 return;
2607 }
2608
Chris Lattner04d66662007-10-09 17:33:22 +00002609 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002610 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002611
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002612 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2613 NameLoc.isValid() ? NameLoc : StartLoc,
2614 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002615 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002616}
2617
2618/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2619/// enumerator-list:
2620/// enumerator
2621/// enumerator-list ',' enumerator
2622/// enumerator:
2623/// enumeration-constant
2624/// enumeration-constant '=' constant-expression
2625/// enumeration-constant:
2626/// identifier
2627///
John McCalld226f652010-08-21 09:40:31 +00002628void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002629 // Enter the scope of the enum body and start the definition.
2630 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002631 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002632
Reid Spencer5f016e22007-07-11 17:01:13 +00002633 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002634
Chris Lattner7946dd32007-08-27 17:24:30 +00002635 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002636 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002637 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002638
John McCalld226f652010-08-21 09:40:31 +00002639 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002640
John McCalld226f652010-08-21 09:40:31 +00002641 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002642
Reid Spencer5f016e22007-07-11 17:01:13 +00002643 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002644 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002645 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2646 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002647
John McCall5b629aa2010-10-22 23:36:17 +00002648 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002649 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002650 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002651
Reid Spencer5f016e22007-07-11 17:01:13 +00002652 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002653 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002654 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002655 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002656 AssignedVal = ParseConstantExpression();
2657 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002658 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002659 }
Mike Stump1eb44332009-09-09 15:08:12 +00002660
Reid Spencer5f016e22007-07-11 17:01:13 +00002661 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002662 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2663 LastEnumConstDecl,
2664 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002665 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002666 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002667 EnumConstantDecls.push_back(EnumConstDecl);
2668 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002669
Douglas Gregor751f6922010-09-07 14:51:08 +00002670 if (Tok.is(tok::identifier)) {
2671 // We're missing a comma between enumerators.
2672 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2673 Diag(Loc, diag::err_enumerator_list_missing_comma)
2674 << FixItHint::CreateInsertion(Loc, ", ");
2675 continue;
2676 }
2677
Chris Lattner04d66662007-10-09 17:33:22 +00002678 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002679 break;
2680 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002681
2682 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002683 !(getLang().C99 || getLang().CPlusPlus0x))
2684 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2685 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002686 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002687 }
Mike Stump1eb44332009-09-09 15:08:12 +00002688
Reid Spencer5f016e22007-07-11 17:01:13 +00002689 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002690 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002691
Reid Spencer5f016e22007-07-11 17:01:13 +00002692 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002693 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002694 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002695
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002696 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2697 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002698 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002699
Douglas Gregor72de6672009-01-08 20:45:30 +00002700 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002701 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002702}
2703
2704/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002705/// start of a type-qualifier-list.
2706bool Parser::isTypeQualifier() const {
2707 switch (Tok.getKind()) {
2708 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002709
2710 // type-qualifier only in OpenCL
2711 case tok::kw_private:
2712 return getLang().OpenCL;
2713
Steve Naroff5f8aa692008-02-11 23:15:56 +00002714 // type-qualifier
2715 case tok::kw_const:
2716 case tok::kw_volatile:
2717 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002718 case tok::kw___private:
2719 case tok::kw___local:
2720 case tok::kw___global:
2721 case tok::kw___constant:
2722 case tok::kw___read_only:
2723 case tok::kw___read_write:
2724 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00002725 return true;
2726 }
2727}
2728
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002729/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2730/// is definitely a type-specifier. Return false if it isn't part of a type
2731/// specifier or if we're not sure.
2732bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2733 switch (Tok.getKind()) {
2734 default: return false;
2735 // type-specifiers
2736 case tok::kw_short:
2737 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002738 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002739 case tok::kw_signed:
2740 case tok::kw_unsigned:
2741 case tok::kw__Complex:
2742 case tok::kw__Imaginary:
2743 case tok::kw_void:
2744 case tok::kw_char:
2745 case tok::kw_wchar_t:
2746 case tok::kw_char16_t:
2747 case tok::kw_char32_t:
2748 case tok::kw_int:
2749 case tok::kw_float:
2750 case tok::kw_double:
2751 case tok::kw_bool:
2752 case tok::kw__Bool:
2753 case tok::kw__Decimal32:
2754 case tok::kw__Decimal64:
2755 case tok::kw__Decimal128:
2756 case tok::kw___vector:
2757
2758 // struct-or-union-specifier (C99) or class-specifier (C++)
2759 case tok::kw_class:
2760 case tok::kw_struct:
2761 case tok::kw_union:
2762 // enum-specifier
2763 case tok::kw_enum:
2764
2765 // typedef-name
2766 case tok::annot_typename:
2767 return true;
2768 }
2769}
2770
Steve Naroff5f8aa692008-02-11 23:15:56 +00002771/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002772/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002773bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002774 switch (Tok.getKind()) {
2775 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002776
Chris Lattner166a8fc2009-01-04 23:41:41 +00002777 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002778 if (TryAltiVecVectorToken())
2779 return true;
2780 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002781 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002782 // Annotate typenames and C++ scope specifiers. If we get one, just
2783 // recurse to handle whatever we get.
2784 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002785 return true;
2786 if (Tok.is(tok::identifier))
2787 return false;
2788 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002789
Chris Lattner166a8fc2009-01-04 23:41:41 +00002790 case tok::coloncolon: // ::foo::bar
2791 if (NextToken().is(tok::kw_new) || // ::new
2792 NextToken().is(tok::kw_delete)) // ::delete
2793 return false;
2794
Chris Lattner166a8fc2009-01-04 23:41:41 +00002795 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002796 return true;
2797 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002798
Reid Spencer5f016e22007-07-11 17:01:13 +00002799 // GNU attributes support.
2800 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002801 // GNU typeof support.
2802 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002803
Reid Spencer5f016e22007-07-11 17:01:13 +00002804 // type-specifiers
2805 case tok::kw_short:
2806 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002807 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002808 case tok::kw_signed:
2809 case tok::kw_unsigned:
2810 case tok::kw__Complex:
2811 case tok::kw__Imaginary:
2812 case tok::kw_void:
2813 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002814 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002815 case tok::kw_char16_t:
2816 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002817 case tok::kw_int:
2818 case tok::kw_float:
2819 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002820 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002821 case tok::kw__Bool:
2822 case tok::kw__Decimal32:
2823 case tok::kw__Decimal64:
2824 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002825 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002826
Chris Lattner99dc9142008-04-13 18:59:07 +00002827 // struct-or-union-specifier (C99) or class-specifier (C++)
2828 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002829 case tok::kw_struct:
2830 case tok::kw_union:
2831 // enum-specifier
2832 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002833
Reid Spencer5f016e22007-07-11 17:01:13 +00002834 // type-qualifier
2835 case tok::kw_const:
2836 case tok::kw_volatile:
2837 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002838
2839 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002840 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002841 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002842
Chris Lattner7c186be2008-10-20 00:25:30 +00002843 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2844 case tok::less:
2845 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002846
Steve Naroff239f0732008-12-25 14:16:32 +00002847 case tok::kw___cdecl:
2848 case tok::kw___stdcall:
2849 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002850 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002851 case tok::kw___w64:
2852 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002853 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002854
2855 case tok::kw___private:
2856 case tok::kw___local:
2857 case tok::kw___global:
2858 case tok::kw___constant:
2859 case tok::kw___read_only:
2860 case tok::kw___read_write:
2861 case tok::kw___write_only:
2862
Eli Friedman290eeb02009-06-08 23:27:34 +00002863 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002864
2865 case tok::kw_private:
2866 return getLang().OpenCL;
Reid Spencer5f016e22007-07-11 17:01:13 +00002867 }
2868}
2869
2870/// isDeclarationSpecifier() - Return true if the current token is part of a
2871/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002872///
2873/// \param DisambiguatingWithExpression True to indicate that the purpose of
2874/// this check is to disambiguate between an expression and a declaration.
2875bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002876 switch (Tok.getKind()) {
2877 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002878
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002879 case tok::kw_private:
2880 return getLang().OpenCL;
2881
Chris Lattner166a8fc2009-01-04 23:41:41 +00002882 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002883 // Unfortunate hack to support "Class.factoryMethod" notation.
2884 if (getLang().ObjC1 && NextToken().is(tok::period))
2885 return false;
John Thompson82287d12010-02-05 00:12:22 +00002886 if (TryAltiVecVectorToken())
2887 return true;
2888 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002889 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002890 // Annotate typenames and C++ scope specifiers. If we get one, just
2891 // recurse to handle whatever we get.
2892 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002893 return true;
2894 if (Tok.is(tok::identifier))
2895 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002896
2897 // If we're in Objective-C and we have an Objective-C class type followed
2898 // by an identifier and then either ':' or ']', in a place where an
2899 // expression is permitted, then this is probably a class message send
2900 // missing the initial '['. In this case, we won't consider this to be
2901 // the start of a declaration.
2902 if (DisambiguatingWithExpression &&
2903 isStartOfObjCClassMessageMissingOpenBracket())
2904 return false;
2905
John McCall9ba61662010-02-26 08:45:28 +00002906 return isDeclarationSpecifier();
2907
Chris Lattner166a8fc2009-01-04 23:41:41 +00002908 case tok::coloncolon: // ::foo::bar
2909 if (NextToken().is(tok::kw_new) || // ::new
2910 NextToken().is(tok::kw_delete)) // ::delete
2911 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002912
Chris Lattner166a8fc2009-01-04 23:41:41 +00002913 // Annotate typenames and C++ scope specifiers. If we get one, just
2914 // recurse to handle whatever we get.
2915 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002916 return true;
2917 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002918
Reid Spencer5f016e22007-07-11 17:01:13 +00002919 // storage-class-specifier
2920 case tok::kw_typedef:
2921 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002922 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002923 case tok::kw_static:
2924 case tok::kw_auto:
2925 case tok::kw_register:
2926 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002927
Reid Spencer5f016e22007-07-11 17:01:13 +00002928 // type-specifiers
2929 case tok::kw_short:
2930 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002931 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002932 case tok::kw_signed:
2933 case tok::kw_unsigned:
2934 case tok::kw__Complex:
2935 case tok::kw__Imaginary:
2936 case tok::kw_void:
2937 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002938 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002939 case tok::kw_char16_t:
2940 case tok::kw_char32_t:
2941
Reid Spencer5f016e22007-07-11 17:01:13 +00002942 case tok::kw_int:
2943 case tok::kw_float:
2944 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002945 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002946 case tok::kw__Bool:
2947 case tok::kw__Decimal32:
2948 case tok::kw__Decimal64:
2949 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002950 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002951
Chris Lattner99dc9142008-04-13 18:59:07 +00002952 // struct-or-union-specifier (C99) or class-specifier (C++)
2953 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002954 case tok::kw_struct:
2955 case tok::kw_union:
2956 // enum-specifier
2957 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002958
Reid Spencer5f016e22007-07-11 17:01:13 +00002959 // type-qualifier
2960 case tok::kw_const:
2961 case tok::kw_volatile:
2962 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002963
Reid Spencer5f016e22007-07-11 17:01:13 +00002964 // function-specifier
2965 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002966 case tok::kw_virtual:
2967 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002968
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00002969 // static_assert-declaration
2970 case tok::kw__Static_assert:
2971
Chris Lattner1ef08762007-08-09 17:01:07 +00002972 // GNU typeof support.
2973 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002974
Chris Lattner1ef08762007-08-09 17:01:07 +00002975 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002976 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002977 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002978
Chris Lattnerf3948c42008-07-26 03:38:44 +00002979 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2980 case tok::less:
2981 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002982
Douglas Gregord9d75e52011-04-27 05:41:15 +00002983 // typedef-name
2984 case tok::annot_typename:
2985 return !DisambiguatingWithExpression ||
2986 !isStartOfObjCClassMessageMissingOpenBracket();
2987
Steve Naroff47f52092009-01-06 19:34:12 +00002988 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002989 case tok::kw___cdecl:
2990 case tok::kw___stdcall:
2991 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002992 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002993 case tok::kw___w64:
2994 case tok::kw___ptr64:
2995 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002996 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002997
2998 case tok::kw___private:
2999 case tok::kw___local:
3000 case tok::kw___global:
3001 case tok::kw___constant:
3002 case tok::kw___read_only:
3003 case tok::kw___read_write:
3004 case tok::kw___write_only:
3005
Eli Friedman290eeb02009-06-08 23:27:34 +00003006 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003007 }
3008}
3009
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003010bool Parser::isConstructorDeclarator() {
3011 TentativeParsingAction TPA(*this);
3012
3013 // Parse the C++ scope specifier.
3014 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003015 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003016 TPA.Revert();
3017 return false;
3018 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003019
3020 // Parse the constructor name.
3021 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3022 // We already know that we have a constructor name; just consume
3023 // the token.
3024 ConsumeToken();
3025 } else {
3026 TPA.Revert();
3027 return false;
3028 }
3029
3030 // Current class name must be followed by a left parentheses.
3031 if (Tok.isNot(tok::l_paren)) {
3032 TPA.Revert();
3033 return false;
3034 }
3035 ConsumeParen();
3036
3037 // A right parentheses or ellipsis signals that we have a constructor.
3038 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3039 TPA.Revert();
3040 return true;
3041 }
3042
3043 // If we need to, enter the specified scope.
3044 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003045 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003046 DeclScopeObj.EnterDeclaratorScope();
3047
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003048 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003049 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003050 MaybeParseMicrosoftAttributes(Attrs);
3051
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003052 // Check whether the next token(s) are part of a declaration
3053 // specifier, in which case we have the start of a parameter and,
3054 // therefore, we know that this is a constructor.
3055 bool IsConstructor = isDeclarationSpecifier();
3056 TPA.Revert();
3057 return IsConstructor;
3058}
Reid Spencer5f016e22007-07-11 17:01:13 +00003059
3060/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003061/// type-qualifier-list: [C99 6.7.5]
3062/// type-qualifier
3063/// [vendor] attributes
3064/// [ only if VendorAttributesAllowed=true ]
3065/// type-qualifier-list type-qualifier
3066/// [vendor] type-qualifier-list attributes
3067/// [ only if VendorAttributesAllowed=true ]
3068/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3069/// [ only if CXX0XAttributesAllowed=true ]
3070/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003071///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003072void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3073 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003074 bool CXX0XAttributesAllowed) {
3075 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3076 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003077 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003078 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003079 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003080 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003081 else
3082 Diag(Loc, diag::err_attributes_not_allowed);
3083 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003084
3085 SourceLocation EndLoc;
3086
Reid Spencer5f016e22007-07-11 17:01:13 +00003087 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003088 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003089 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003090 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003091 SourceLocation Loc = Tok.getLocation();
3092
3093 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003094 case tok::code_completion:
3095 Actions.CodeCompleteTypeQualifiers(DS);
3096 ConsumeCodeCompletionToken();
3097 break;
3098
Reid Spencer5f016e22007-07-11 17:01:13 +00003099 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003100 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3101 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003102 break;
3103 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003104 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3105 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003106 break;
3107 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003108 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3109 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003110 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003111
3112 // OpenCL qualifiers:
3113 case tok::kw_private:
3114 if (!getLang().OpenCL)
3115 goto DoneWithTypeQuals;
3116 case tok::kw___private:
3117 case tok::kw___global:
3118 case tok::kw___local:
3119 case tok::kw___constant:
3120 case tok::kw___read_only:
3121 case tok::kw___write_only:
3122 case tok::kw___read_write:
3123 ParseOpenCLQualifiers(DS);
3124 break;
3125
Eli Friedman290eeb02009-06-08 23:27:34 +00003126 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003127 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00003128 case tok::kw___cdecl:
3129 case tok::kw___stdcall:
3130 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003131 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003132 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003133 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003134 continue;
3135 }
3136 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003137 case tok::kw___pascal:
3138 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003139 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003140 continue;
3141 }
3142 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003143 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003144 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003145 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003146 continue; // do *not* consume the next token!
3147 }
3148 // otherwise, FALL THROUGH!
3149 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003150 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003151 // If this is not a type-qualifier token, we're done reading type
3152 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003153 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003154 if (EndLoc.isValid())
3155 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003156 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003157 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003158
Reid Spencer5f016e22007-07-11 17:01:13 +00003159 // If the specifier combination wasn't legal, issue a diagnostic.
3160 if (isInvalid) {
3161 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003162 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003163 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003164 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003165 }
3166}
3167
3168
3169/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3170///
3171void Parser::ParseDeclarator(Declarator &D) {
3172 /// This implements the 'declarator' production in the C grammar, then checks
3173 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003174 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003175}
3176
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003177/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3178/// is parsed by the function passed to it. Pass null, and the direct-declarator
3179/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003180/// ptr-operator production.
3181///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003182/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3183/// [C] pointer[opt] direct-declarator
3184/// [C++] direct-declarator
3185/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003186///
3187/// pointer: [C99 6.7.5]
3188/// '*' type-qualifier-list[opt]
3189/// '*' type-qualifier-list[opt] pointer
3190///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003191/// ptr-operator:
3192/// '*' cv-qualifier-seq[opt]
3193/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003194/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003195/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003196/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003197/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003198void Parser::ParseDeclaratorInternal(Declarator &D,
3199 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003200 if (Diags.hasAllExtensionsSilenced())
3201 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003202
Sebastian Redlf30208a2009-01-24 21:16:55 +00003203 // C++ member pointers start with a '::' or a nested-name.
3204 // Member pointers get special handling, since there's no place for the
3205 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003206 if (getLang().CPlusPlus &&
3207 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3208 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003209 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003210 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003211
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003212 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003213 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003214 // The scope spec really belongs to the direct-declarator.
3215 D.getCXXScopeSpec() = SS;
3216 if (DirectDeclParser)
3217 (this->*DirectDeclParser)(D);
3218 return;
3219 }
3220
3221 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003222 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003223 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003224 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003225 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003226
3227 // Recurse to parse whatever is left.
3228 ParseDeclaratorInternal(D, DirectDeclParser);
3229
3230 // Sema will have to catch (syntactically invalid) pointers into global
3231 // scope. It has to catch pointers into namespace scope anyway.
3232 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003233 Loc),
3234 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003235 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003236 return;
3237 }
3238 }
3239
3240 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003241 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003242 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003243 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003244 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003245 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003246 if (DirectDeclParser)
3247 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003248 return;
3249 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003250
Sebastian Redl05532f22009-03-15 22:02:01 +00003251 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3252 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003253 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003254 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003255
Chris Lattner9af55002009-03-27 04:18:06 +00003256 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003257 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003258 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003259
Reid Spencer5f016e22007-07-11 17:01:13 +00003260 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003261 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003262
Reid Spencer5f016e22007-07-11 17:01:13 +00003263 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003264 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003265 if (Kind == tok::star)
3266 // Remember that we parsed a pointer type, and remember the type-quals.
3267 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003268 DS.getConstSpecLoc(),
3269 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003270 DS.getRestrictSpecLoc()),
3271 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003272 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003273 else
3274 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003275 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003276 Loc),
3277 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003278 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003279 } else {
3280 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003281 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003282
Sebastian Redl743de1f2009-03-23 00:00:23 +00003283 // Complain about rvalue references in C++03, but then go on and build
3284 // the declarator.
3285 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00003286 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003287
Reid Spencer5f016e22007-07-11 17:01:13 +00003288 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3289 // cv-qualifiers are introduced through the use of a typedef or of a
3290 // template type argument, in which case the cv-qualifiers are ignored.
3291 //
3292 // [GNU] Retricted references are allowed.
3293 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003294 // [C++0x] Attributes on references are not allowed.
3295 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003296 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003297
3298 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3299 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3300 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003301 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003302 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3303 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003304 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003305 }
3306
3307 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003308 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003309
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003310 if (D.getNumTypeObjects() > 0) {
3311 // C++ [dcl.ref]p4: There shall be no references to references.
3312 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3313 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003314 if (const IdentifierInfo *II = D.getIdentifier())
3315 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3316 << II;
3317 else
3318 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3319 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003320
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003321 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003322 // can go ahead and build the (technically ill-formed)
3323 // declarator: reference collapsing will take care of it.
3324 }
3325 }
3326
Reid Spencer5f016e22007-07-11 17:01:13 +00003327 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003328 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003329 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003330 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003331 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003332 }
3333}
3334
3335/// ParseDirectDeclarator
3336/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003337/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003338/// '(' declarator ')'
3339/// [GNU] '(' attributes declarator ')'
3340/// [C90] direct-declarator '[' constant-expression[opt] ']'
3341/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3342/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3343/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3344/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3345/// direct-declarator '(' parameter-type-list ')'
3346/// direct-declarator '(' identifier-list[opt] ')'
3347/// [GNU] direct-declarator '(' parameter-forward-declarations
3348/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003349/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3350/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003351/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003352///
3353/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003354/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003355/// '::'[opt] nested-name-specifier[opt] type-name
3356///
3357/// id-expression: [C++ 5.1]
3358/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003359/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003360///
3361/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003362/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003363/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003364/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003365/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003366/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003367///
Reid Spencer5f016e22007-07-11 17:01:13 +00003368void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003369 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003370
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003371 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3372 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003373 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003374 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003375 }
3376
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003377 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003378 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003379 // Change the declaration context for name lookup, until this function
3380 // is exited (and the declarator has been parsed).
3381 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003382 }
3383
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003384 // C++0x [dcl.fct]p14:
3385 // There is a syntactic ambiguity when an ellipsis occurs at the end
3386 // of a parameter-declaration-clause without a preceding comma. In
3387 // this case, the ellipsis is parsed as part of the
3388 // abstract-declarator if the type of the parameter names a template
3389 // parameter pack that has not been expanded; otherwise, it is parsed
3390 // as part of the parameter-declaration-clause.
3391 if (Tok.is(tok::ellipsis) &&
3392 !((D.getContext() == Declarator::PrototypeContext ||
3393 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003394 NextToken().is(tok::r_paren) &&
3395 !Actions.containsUnexpandedParameterPacks(D)))
3396 D.setEllipsisLoc(ConsumeToken());
3397
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003398 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3399 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3400 // We found something that indicates the start of an unqualified-id.
3401 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003402 bool AllowConstructorName;
3403 if (D.getDeclSpec().hasTypeSpecifier())
3404 AllowConstructorName = false;
3405 else if (D.getCXXScopeSpec().isSet())
3406 AllowConstructorName =
3407 (D.getContext() == Declarator::FileContext ||
3408 (D.getContext() == Declarator::MemberContext &&
3409 D.getDeclSpec().isFriendSpecified()));
3410 else
3411 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3412
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003413 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3414 /*EnteringContext=*/true,
3415 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003416 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003417 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003418 D.getName()) ||
3419 // Once we're past the identifier, if the scope was bad, mark the
3420 // whole declarator bad.
3421 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003422 D.SetIdentifier(0, Tok.getLocation());
3423 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003424 } else {
3425 // Parsed the unqualified-id; update range information and move along.
3426 if (D.getSourceRange().getBegin().isInvalid())
3427 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3428 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003429 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003430 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003431 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003432 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003433 assert(!getLang().CPlusPlus &&
3434 "There's a C++-specific check for tok::identifier above");
3435 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3436 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3437 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003438 goto PastIdentifier;
3439 }
3440
3441 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003442 // direct-declarator: '(' declarator ')'
3443 // direct-declarator: '(' attributes declarator ')'
3444 // Example: 'char (*X)' or 'int (*XX)(void)'
3445 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003446
3447 // If the declarator was parenthesized, we entered the declarator
3448 // scope when parsing the parenthesized declarator, then exited
3449 // the scope already. Re-enter the scope, if we need to.
3450 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003451 // If there was an error parsing parenthesized declarator, declarator
3452 // scope may have been enterred before. Don't do it again.
3453 if (!D.isInvalidType() &&
3454 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003455 // Change the declaration context for name lookup, until this function
3456 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003457 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003458 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003459 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003460 // This could be something simple like "int" (in which case the declarator
3461 // portion is empty), if an abstract-declarator is allowed.
3462 D.SetIdentifier(0, Tok.getLocation());
3463 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003464 if (D.getContext() == Declarator::MemberContext)
3465 Diag(Tok, diag::err_expected_member_name_or_semi)
3466 << D.getDeclSpec().getSourceRange();
3467 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003468 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003469 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003470 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003471 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003472 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003473 }
Mike Stump1eb44332009-09-09 15:08:12 +00003474
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003475 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003476 assert(D.isPastIdentifier() &&
3477 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003478
Sean Huntbbd37c62009-11-21 08:43:09 +00003479 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003480 if (D.getIdentifier())
3481 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003482
Reid Spencer5f016e22007-07-11 17:01:13 +00003483 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003484 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003485 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3486 // In such a case, check if we actually have a function declarator; if it
3487 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003488 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3489 // When not in file scope, warn for ambiguous function declarators, just
3490 // in case the author intended it as a variable definition.
3491 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3492 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3493 break;
3494 }
John McCall0b7e6782011-03-24 11:26:52 +00003495 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003496 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00003497 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003498 ParseBracketDeclarator(D);
3499 } else {
3500 break;
3501 }
3502 }
3503}
3504
Chris Lattneref4715c2008-04-06 05:45:57 +00003505/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3506/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003507/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003508/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3509///
3510/// direct-declarator:
3511/// '(' declarator ')'
3512/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003513/// direct-declarator '(' parameter-type-list ')'
3514/// direct-declarator '(' identifier-list[opt] ')'
3515/// [GNU] direct-declarator '(' parameter-forward-declarations
3516/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003517///
3518void Parser::ParseParenDeclarator(Declarator &D) {
3519 SourceLocation StartLoc = ConsumeParen();
3520 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003521
Chris Lattner7399ee02008-10-20 02:05:46 +00003522 // Eat any attributes before we look at whether this is a grouping or function
3523 // declarator paren. If this is a grouping paren, the attribute applies to
3524 // the type being built up, for example:
3525 // int (__attribute__(()) *x)(long y)
3526 // If this ends up not being a grouping paren, the attribute applies to the
3527 // first argument, for example:
3528 // int (__attribute__(()) int x)
3529 // In either case, we need to eat any attributes to be able to determine what
3530 // sort of paren this is.
3531 //
John McCall0b7e6782011-03-24 11:26:52 +00003532 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003533 bool RequiresArg = false;
3534 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003535 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003536
Chris Lattner7399ee02008-10-20 02:05:46 +00003537 // We require that the argument list (if this is a non-grouping paren) be
3538 // present even if the attribute list was empty.
3539 RequiresArg = true;
3540 }
Steve Naroff239f0732008-12-25 14:16:32 +00003541 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003542 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003543 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3544 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall7f040a92010-12-24 02:08:15 +00003545 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003546 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003547 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003548 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003549 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003550
Chris Lattneref4715c2008-04-06 05:45:57 +00003551 // If we haven't past the identifier yet (or where the identifier would be
3552 // stored, if this is an abstract declarator), then this is probably just
3553 // grouping parens. However, if this could be an abstract-declarator, then
3554 // this could also be the start of function arguments (consider 'void()').
3555 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003556
Chris Lattneref4715c2008-04-06 05:45:57 +00003557 if (!D.mayOmitIdentifier()) {
3558 // If this can't be an abstract-declarator, this *must* be a grouping
3559 // paren, because we haven't seen the identifier yet.
3560 isGrouping = true;
3561 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003562 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003563 isDeclarationSpecifier()) { // 'int(int)' is a function.
3564 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3565 // considered to be a type, not a K&R identifier-list.
3566 isGrouping = false;
3567 } else {
3568 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3569 isGrouping = true;
3570 }
Mike Stump1eb44332009-09-09 15:08:12 +00003571
Chris Lattneref4715c2008-04-06 05:45:57 +00003572 // If this is a grouping paren, handle:
3573 // direct-declarator: '(' declarator ')'
3574 // direct-declarator: '(' attributes declarator ')'
3575 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003576 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003577 D.setGroupingParens(true);
3578
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003579 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003580 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003581 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003582 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3583 attrs, EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003584
3585 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003586 return;
3587 }
Mike Stump1eb44332009-09-09 15:08:12 +00003588
Chris Lattneref4715c2008-04-06 05:45:57 +00003589 // Okay, if this wasn't a grouping paren, it must be the start of a function
3590 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003591 // identifier (and remember where it would have been), then call into
3592 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003593 D.SetIdentifier(0, Tok.getLocation());
3594
John McCall7f040a92010-12-24 02:08:15 +00003595 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003596}
3597
3598/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3599/// declarator D up to a paren, which indicates that we are parsing function
3600/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003601///
Chris Lattner7399ee02008-10-20 02:05:46 +00003602/// If AttrList is non-null, then the caller parsed those arguments immediately
3603/// after the open paren - they should be considered to be the first argument of
3604/// a parameter. If RequiresArg is true, then the first argument of the
3605/// function is required to be present and required to not be an identifier
3606/// list.
3607///
Reid Spencer5f016e22007-07-11 17:01:13 +00003608/// This method also handles this portion of the grammar:
3609/// parameter-type-list: [C99 6.7.5]
3610/// parameter-list
3611/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003612/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003613///
3614/// parameter-list: [C99 6.7.5]
3615/// parameter-declaration
3616/// parameter-list ',' parameter-declaration
3617///
3618/// parameter-declaration: [C99 6.7.5]
3619/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003620/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003621/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003622/// declaration-specifiers abstract-declarator[opt]
3623/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003624/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003625/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3626///
Douglas Gregor83f51722011-01-26 03:43:54 +00003627/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3628/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003629///
Sebastian Redl7acafd02011-03-05 14:45:16 +00003630/// [C++0x] exception-specification:
3631/// dynamic-exception-specification
3632/// noexcept-specification
3633///
Chris Lattner7399ee02008-10-20 02:05:46 +00003634void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall7f040a92010-12-24 02:08:15 +00003635 ParsedAttributes &attrs,
Chris Lattner7399ee02008-10-20 02:05:46 +00003636 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003637 // lparen is already consumed!
3638 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003639
Douglas Gregordab60ad2010-10-01 18:44:50 +00003640 ParsedType TrailingReturnType;
3641
Chris Lattner7399ee02008-10-20 02:05:46 +00003642 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003643 if (Tok.is(tok::r_paren)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003644 if (RequiresArg)
Chris Lattner1ab3b962008-11-18 07:48:38 +00003645 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003646
Abramo Bagnara796aa442011-03-12 11:17:06 +00003647 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003648
3649 // cv-qualifier-seq[opt].
John McCall0b7e6782011-03-24 11:26:52 +00003650 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003651 SourceLocation RefQualifierLoc;
3652 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003653 ExceptionSpecificationType ESpecType = EST_None;
3654 SourceRange ESpecRange;
3655 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3656 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3657 ExprResult NoexceptExpr;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003658 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003659 MaybeParseCXX0XAttributes(attrs);
3660
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003661 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003662 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003663 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003664
Douglas Gregor83f51722011-01-26 03:43:54 +00003665 // Parse ref-qualifier[opt]
3666 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3667 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003668 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003669
Douglas Gregor83f51722011-01-26 03:43:54 +00003670 RefQualifierIsLValueRef = Tok.is(tok::amp);
3671 RefQualifierLoc = ConsumeToken();
3672 EndLoc = RefQualifierLoc;
3673 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00003674
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003675 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003676 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3677 DynamicExceptions,
3678 DynamicExceptionRanges,
3679 NoexceptExpr);
3680 if (ESpecType != EST_None)
3681 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003682
3683 // Parse trailing-return-type.
3684 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3685 TrailingReturnType = ParseTrailingReturnType().get();
3686 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003687 }
3688
Chris Lattnerf97409f2008-04-06 06:57:35 +00003689 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003690 // int() -> no prototype, no '...'.
John McCall0b7e6782011-03-24 11:26:52 +00003691 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003692 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003693 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003694 /*arglist*/ 0, 0,
3695 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003696 RefQualifierIsLValueRef,
3697 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003698 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003699 DynamicExceptions.data(),
3700 DynamicExceptionRanges.data(),
3701 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003702 NoexceptExpr.isUsable() ?
3703 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003704 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003705 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00003706 attrs, EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003707 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003708 }
3709
Chris Lattner7399ee02008-10-20 02:05:46 +00003710 // Alternatively, this parameter list may be an identifier list form for a
3711 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003712 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3713 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003714 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003715 // K&R identifier lists can't have typedefs as identifiers, per
3716 // C99 6.7.5.3p11.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003717 if (RequiresArg)
Steve Naroff2d081c42009-01-28 19:16:40 +00003718 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner83a94472010-05-14 17:23:36 +00003719
Steve Naroff2d081c42009-01-28 19:16:40 +00003720 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003721 // normal declarators, not for abstract-declarators. Get the first
3722 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003723 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003724 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003725
3726 // Identifier lists follow a really simple grammar: the identifiers can
3727 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3728 // identifier lists are really rare in the brave new modern world, and it
3729 // is very common for someone to typo a type in a non-k&r style list. If
3730 // we are presented with something like: "void foo(intptr x, float y)",
3731 // we don't want to start parsing the function declarator as though it is
3732 // a K&R style declarator just because intptr is an invalid type.
3733 //
3734 // To handle this, we check to see if the token after the first identifier
3735 // is a "," or ")". Only if so, do we parse it as an identifier list.
3736 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3737 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3738 FirstTok.getIdentifierInfo(),
3739 FirstTok.getLocation(), D);
3740
3741 // If we get here, the code is invalid. Push the first identifier back
3742 // into the token stream and parse the first argument as an (invalid)
3743 // normal argument declarator.
3744 PP.EnterToken(Tok);
3745 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003746 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003747 }
Mike Stump1eb44332009-09-09 15:08:12 +00003748
Chris Lattnerf97409f2008-04-06 06:57:35 +00003749 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003750
Chris Lattnerf97409f2008-04-06 06:57:35 +00003751 // Build up an array of information about the parsed arguments.
3752 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003753
3754 // Enter function-declaration scope, limiting any declarators to the
3755 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003756 ParseScope PrototypeScope(this,
3757 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003758
Chris Lattnerf97409f2008-04-06 06:57:35 +00003759 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003760 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003761 while (1) {
3762 if (Tok.is(tok::ellipsis)) {
3763 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003764 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003765 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003766 }
Mike Stump1eb44332009-09-09 15:08:12 +00003767
Chris Lattnerf97409f2008-04-06 06:57:35 +00003768 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003769 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00003770 DeclSpec DS(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003771
3772 // Skip any Microsoft attributes before a param.
3773 if (getLang().Microsoft && Tok.is(tok::l_square))
3774 ParseMicrosoftAttributes(DS.getAttributes());
3775
3776 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00003777
3778 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00003779 // Take them so that we only apply the attributes to the first parameter.
3780 DS.takeAttributesFrom(attrs);
3781
Chris Lattnere64c5492009-02-27 18:38:20 +00003782 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003783
Chris Lattnerf97409f2008-04-06 06:57:35 +00003784 // Parse the declarator. This is "PrototypeContext", because we must
3785 // accept either 'declarator' or 'abstract-declarator' here.
3786 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3787 ParseDeclarator(ParmDecl);
3788
3789 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00003790 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003791
Chris Lattnerf97409f2008-04-06 06:57:35 +00003792 // Remember this parsed parameter in ParamInfo.
3793 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003794
Douglas Gregor72b505b2008-12-16 21:30:33 +00003795 // DefArgToks is used when the parsing of default arguments needs
3796 // to be delayed.
3797 CachedTokens *DefArgToks = 0;
3798
Chris Lattnerf97409f2008-04-06 06:57:35 +00003799 // If no parameter was specified, verify that *something* was specified,
3800 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003801 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3802 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003803 // Completely missing, emit error.
3804 Diag(DSStart, diag::err_missing_param);
3805 } else {
3806 // Otherwise, we have something. Add it and let semantic analysis try
3807 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003808
Chris Lattnerf97409f2008-04-06 06:57:35 +00003809 // Inform the actions module about the parameter declarator, so it gets
3810 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003811 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003812
3813 // Parse the default argument, if any. We parse the default
3814 // arguments in all dialects; the semantic analysis in
3815 // ActOnParamDefaultArgument will reject the default argument in
3816 // C.
3817 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003818 SourceLocation EqualLoc = Tok.getLocation();
3819
Chris Lattner04421082008-04-08 04:40:51 +00003820 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003821 if (D.getContext() == Declarator::MemberContext) {
3822 // If we're inside a class definition, cache the tokens
3823 // corresponding to the default argument. We'll actually parse
3824 // them when we see the end of the class definition.
3825 // FIXME: Templates will require something similar.
3826 // FIXME: Can we use a smart pointer for Toks?
3827 DefArgToks = new CachedTokens;
3828
Mike Stump1eb44332009-09-09 15:08:12 +00003829 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003830 /*StopAtSemi=*/true,
3831 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003832 delete DefArgToks;
3833 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003834 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003835 } else {
3836 // Mark the end of the default argument so that we know when to
3837 // stop when we parse it later on.
3838 Token DefArgEnd;
3839 DefArgEnd.startToken();
3840 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3841 DefArgEnd.setLocation(Tok.getLocation());
3842 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003843 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003844 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003845 }
Chris Lattner04421082008-04-08 04:40:51 +00003846 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003847 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003848 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003849
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003850 // The argument isn't actually potentially evaluated unless it is
3851 // used.
3852 EnterExpressionEvaluationContext Eval(Actions,
3853 Sema::PotentiallyEvaluatedIfUsed);
3854
John McCall60d7b3a2010-08-24 06:29:42 +00003855 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003856 if (DefArgResult.isInvalid()) {
3857 Actions.ActOnParamDefaultArgumentError(Param);
3858 SkipUntil(tok::comma, tok::r_paren, true, true);
3859 } else {
3860 // Inform the actions module about the default argument
3861 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003862 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003863 }
Chris Lattner04421082008-04-08 04:40:51 +00003864 }
3865 }
Mike Stump1eb44332009-09-09 15:08:12 +00003866
3867 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3868 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003869 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003870 }
3871
3872 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003873 if (Tok.isNot(tok::comma)) {
3874 if (Tok.is(tok::ellipsis)) {
3875 IsVariadic = true;
3876 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3877
3878 if (!getLang().CPlusPlus) {
3879 // We have ellipsis without a preceding ',', which is ill-formed
3880 // in C. Complain and provide the fix.
3881 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003882 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003883 }
3884 }
3885
3886 break;
3887 }
Mike Stump1eb44332009-09-09 15:08:12 +00003888
Chris Lattnerf97409f2008-04-06 06:57:35 +00003889 // Consume the comma.
3890 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003891 }
Mike Stump1eb44332009-09-09 15:08:12 +00003892
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003893 // If we have the closing ')', eat it.
Abramo Bagnara796aa442011-03-12 11:17:06 +00003894 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003895
John McCall0b7e6782011-03-24 11:26:52 +00003896 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003897 SourceLocation RefQualifierLoc;
3898 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003899 ExceptionSpecificationType ESpecType = EST_None;
3900 SourceRange ESpecRange;
3901 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3902 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3903 ExprResult NoexceptExpr;
Sean Huntbbd37c62009-11-21 08:43:09 +00003904
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003905 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003906 MaybeParseCXX0XAttributes(attrs);
3907
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003908 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003909 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003910 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003911 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003912
Douglas Gregor83f51722011-01-26 03:43:54 +00003913 // Parse ref-qualifier[opt]
3914 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3915 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003916 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor83f51722011-01-26 03:43:54 +00003917
3918 RefQualifierIsLValueRef = Tok.is(tok::amp);
3919 RefQualifierLoc = ConsumeToken();
3920 EndLoc = RefQualifierLoc;
3921 }
3922
Sebastian Redl7acafd02011-03-05 14:45:16 +00003923 // FIXME: We should leave the prototype scope before parsing the exception
3924 // specification, and then reenter it when parsing the trailing return type.
3925 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3926
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003927 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003928 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3929 DynamicExceptions,
3930 DynamicExceptionRanges,
3931 NoexceptExpr);
3932 if (ESpecType != EST_None)
3933 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003934
3935 // Parse trailing-return-type.
3936 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3937 TrailingReturnType = ParseTrailingReturnType().get();
3938 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003939 }
3940
Douglas Gregordab60ad2010-10-01 18:44:50 +00003941 // Leave prototype scope.
3942 PrototypeScope.Exit();
3943
Reid Spencer5f016e22007-07-11 17:01:13 +00003944 // Remember that we parsed a function type, and remember the attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003945 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003946 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003947 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003948 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003949 RefQualifierIsLValueRef,
3950 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003951 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003952 DynamicExceptions.data(),
3953 DynamicExceptionRanges.data(),
3954 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003955 NoexceptExpr.isUsable() ?
3956 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003957 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003958 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00003959 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003960}
3961
Chris Lattner66d28652008-04-06 06:34:08 +00003962/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3963/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003964/// first identifier has already been consumed, and the current token is the
3965/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003966///
3967/// identifier-list: [C99 6.7.5]
3968/// identifier
3969/// identifier-list ',' identifier
3970///
3971void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003972 IdentifierInfo *FirstIdent,
3973 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003974 Declarator &D) {
3975 // Build up an array of information about the parsed arguments.
3976 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3977 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003978
Chris Lattner66d28652008-04-06 06:34:08 +00003979 // If there was no identifier specified for the declarator, either we are in
3980 // an abstract-declarator, or we are in a parameter declarator which was found
3981 // to be abstract. In abstract-declarators, identifier lists are not valid:
3982 // diagnose this.
3983 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003984 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003985
Chris Lattner83a94472010-05-14 17:23:36 +00003986 // The first identifier was already read, and is known to be the first
3987 // identifier in the list. Remember this identifier in ParamInfo.
3988 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00003989 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00003990
Chris Lattner66d28652008-04-06 06:34:08 +00003991 while (Tok.is(tok::comma)) {
3992 // Eat the comma.
3993 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003994
Chris Lattner50c64772008-04-06 06:39:19 +00003995 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003996 if (Tok.isNot(tok::identifier)) {
3997 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003998 SkipUntil(tok::r_paren);
3999 return;
Chris Lattner66d28652008-04-06 06:34:08 +00004000 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00004001
Chris Lattner66d28652008-04-06 06:34:08 +00004002 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00004003
4004 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00004005 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00004006 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00004007
Chris Lattner66d28652008-04-06 06:34:08 +00004008 // Verify that the argument identifier has not already been mentioned.
4009 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004010 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00004011 } else {
4012 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00004013 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00004014 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00004015 0));
Chris Lattner50c64772008-04-06 06:39:19 +00004016 }
Mike Stump1eb44332009-09-09 15:08:12 +00004017
Chris Lattner66d28652008-04-06 06:34:08 +00004018 // Eat the identifier.
4019 ConsumeToken();
4020 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004021
4022 // If we have the closing ')', eat it and we're done.
4023 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4024
Chris Lattner50c64772008-04-06 06:39:19 +00004025 // Remember that we parsed a function type, and remember the attributes. This
4026 // function type is always a K&R style function type, which is not varargs and
4027 // has no prototype.
John McCall0b7e6782011-03-24 11:26:52 +00004028 ParsedAttributes attrs(AttrFactory);
4029 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00004030 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00004031 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00004032 /*TypeQuals*/0,
Douglas Gregor83f51722011-01-26 03:43:54 +00004033 true, SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00004034 EST_None, SourceLocation(), 0, 0,
4035 0, 0, LParenLoc, RLoc, D),
John McCall0b7e6782011-03-24 11:26:52 +00004036 attrs, RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00004037}
Chris Lattneref4715c2008-04-06 05:45:57 +00004038
Reid Spencer5f016e22007-07-11 17:01:13 +00004039/// [C90] direct-declarator '[' constant-expression[opt] ']'
4040/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4041/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4042/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4043/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4044void Parser::ParseBracketDeclarator(Declarator &D) {
4045 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00004046
Chris Lattner378c7e42008-12-18 07:27:21 +00004047 // C array syntax has many features, but by-far the most common is [] and [4].
4048 // This code does a fast path to handle some of the most obvious cases.
4049 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00004050 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004051 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004052 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004053
Chris Lattner378c7e42008-12-18 07:27:21 +00004054 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004055 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004056 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004057 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004058 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004059 return;
4060 } else if (Tok.getKind() == tok::numeric_constant &&
4061 GetLookAheadToken(1).is(tok::r_square)) {
4062 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004063 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004064 ConsumeToken();
4065
Sebastian Redlab197ba2009-02-09 18:23:29 +00004066 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004067 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004068 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004069
Chris Lattner378c7e42008-12-18 07:27:21 +00004070 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004071 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004072 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004073 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004074 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004075 return;
4076 }
Mike Stump1eb44332009-09-09 15:08:12 +00004077
Reid Spencer5f016e22007-07-11 17:01:13 +00004078 // If valid, this location is the position where we read the 'static' keyword.
4079 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004080 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004081 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004082
Reid Spencer5f016e22007-07-11 17:01:13 +00004083 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004084 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004085 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004086 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004087
Reid Spencer5f016e22007-07-11 17:01:13 +00004088 // If we haven't already read 'static', check to see if there is one after the
4089 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004090 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004091 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004092
Reid Spencer5f016e22007-07-11 17:01:13 +00004093 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4094 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004095 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004096
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004097 // Handle the case where we have '[*]' as the array size. However, a leading
4098 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4099 // the the token after the star is a ']'. Since stars in arrays are
4100 // infrequent, use of lookahead is not costly here.
4101 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004102 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004103
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004104 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004105 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004106 StaticLoc = SourceLocation(); // Drop the static.
4107 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004108 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004109 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004110 // Note, in C89, this production uses the constant-expr production instead
4111 // of assignment-expr. The only difference is that assignment-expr allows
4112 // things like '=' and '*='. Sema rejects these in C89 mode because they
4113 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004114
Douglas Gregore0762c92009-06-19 23:52:42 +00004115 // Parse the constant-expression or assignment-expression now (depending
4116 // on dialect).
4117 if (getLang().CPlusPlus)
4118 NumElements = ParseConstantExpression();
4119 else
4120 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004121 }
Mike Stump1eb44332009-09-09 15:08:12 +00004122
Reid Spencer5f016e22007-07-11 17:01:13 +00004123 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004124 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004125 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004126 // If the expression was invalid, skip it.
4127 SkipUntil(tok::r_square);
4128 return;
4129 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004130
4131 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4132
John McCall0b7e6782011-03-24 11:26:52 +00004133 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004134 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004135
Chris Lattner378c7e42008-12-18 07:27:21 +00004136 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004137 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004138 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004139 NumElements.release(),
4140 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004141 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004142}
4143
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004144/// [GNU] typeof-specifier:
4145/// typeof ( expressions )
4146/// typeof ( type-name )
4147/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004148///
4149void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004150 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004151 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004152 SourceLocation StartLoc = ConsumeToken();
4153
John McCallcfb708c2010-01-13 20:03:27 +00004154 const bool hasParens = Tok.is(tok::l_paren);
4155
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004156 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004157 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004158 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004159 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4160 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004161 if (hasParens)
4162 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004163
4164 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004165 // FIXME: Not accurate, the range gets one token more than it should.
4166 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004167 else
4168 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004169
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004170 if (isCastExpr) {
4171 if (!CastTy) {
4172 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004173 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004174 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004175
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004176 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004177 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004178 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4179 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004180 DiagID, CastTy))
4181 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004182 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004183 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004184
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004185 // If we get here, the operand to the typeof was an expresion.
4186 if (Operand.isInvalid()) {
4187 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004188 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004189 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004190
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004191 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004192 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004193 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4194 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004195 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004196 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004197}
Chris Lattner1b492422010-02-28 18:33:55 +00004198
4199
4200/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4201/// from TryAltiVecVectorToken.
4202bool Parser::TryAltiVecVectorTokenOutOfLine() {
4203 Token Next = NextToken();
4204 switch (Next.getKind()) {
4205 default: return false;
4206 case tok::kw_short:
4207 case tok::kw_long:
4208 case tok::kw_signed:
4209 case tok::kw_unsigned:
4210 case tok::kw_void:
4211 case tok::kw_char:
4212 case tok::kw_int:
4213 case tok::kw_float:
4214 case tok::kw_double:
4215 case tok::kw_bool:
4216 case tok::kw___pixel:
4217 Tok.setKind(tok::kw___vector);
4218 return true;
4219 case tok::identifier:
4220 if (Next.getIdentifierInfo() == Ident_pixel) {
4221 Tok.setKind(tok::kw___vector);
4222 return true;
4223 }
4224 return false;
4225 }
4226}
4227
4228bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4229 const char *&PrevSpec, unsigned &DiagID,
4230 bool &isInvalid) {
4231 if (Tok.getIdentifierInfo() == Ident_vector) {
4232 Token Next = NextToken();
4233 switch (Next.getKind()) {
4234 case tok::kw_short:
4235 case tok::kw_long:
4236 case tok::kw_signed:
4237 case tok::kw_unsigned:
4238 case tok::kw_void:
4239 case tok::kw_char:
4240 case tok::kw_int:
4241 case tok::kw_float:
4242 case tok::kw_double:
4243 case tok::kw_bool:
4244 case tok::kw___pixel:
4245 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4246 return true;
4247 case tok::identifier:
4248 if (Next.getIdentifierInfo() == Ident_pixel) {
4249 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4250 return true;
4251 }
4252 break;
4253 default:
4254 break;
4255 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004256 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004257 DS.isTypeAltiVecVector()) {
4258 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4259 return true;
4260 }
4261 return false;
4262}