blob: bde650ba3fda9a55e046ddc2e31e29ea47fea0bf [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)) {
Sean Huntbb85f8e2011-05-06 21:24:28 +0000970 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +0000971 } else {
John McCall731ad842009-12-19 09:28:58 +0000972 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
973 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000974 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000975 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000976
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000977 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000978 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000979 ConsumeCodeCompletionToken();
980 SkipUntil(tok::comma, true, true);
981 return ThisDecl;
982 }
983
John McCall60d7b3a2010-08-24 06:29:42 +0000984 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000985
John McCall731ad842009-12-19 09:28:58 +0000986 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000987 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000988 ExitScope();
989 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000990
Douglas Gregor1426e532009-05-12 21:31:51 +0000991 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000992 SkipUntil(tok::comma, true, true);
993 Actions.ActOnInitializerError(ThisDecl);
994 } else
Richard Smith34b41d92011-02-20 03:19:35 +0000995 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
996 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000997 }
998 } else if (Tok.is(tok::l_paren)) {
999 // Parse C++ direct initializer: '(' expression-list ')'
1000 SourceLocation LParenLoc = ConsumeParen();
1001 ExprVector Exprs(Actions);
1002 CommaLocsTy CommaLocs;
1003
Douglas Gregorb4debae2009-12-22 17:47:17 +00001004 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1005 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001006 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001007 }
1008
Douglas Gregor1426e532009-05-12 21:31:51 +00001009 if (ParseExpressionList(Exprs, CommaLocs)) {
1010 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001011
1012 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001013 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001014 ExitScope();
1015 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001016 } else {
1017 // Match the ')'.
1018 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1019
1020 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1021 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001022
1023 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001024 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001025 ExitScope();
1026 }
1027
Douglas Gregor1426e532009-05-12 21:31:51 +00001028 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1029 move_arg(Exprs),
Richard Smith34b41d92011-02-20 03:19:35 +00001030 RParenLoc,
1031 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001032 }
1033 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001034 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001035 }
1036
Richard Smith483b9f32011-02-21 20:05:19 +00001037 Actions.FinalizeDeclaration(ThisDecl);
1038
Douglas Gregor1426e532009-05-12 21:31:51 +00001039 return ThisDecl;
1040}
1041
Reid Spencer5f016e22007-07-11 17:01:13 +00001042/// ParseSpecifierQualifierList
1043/// specifier-qualifier-list:
1044/// type-specifier specifier-qualifier-list[opt]
1045/// type-qualifier specifier-qualifier-list[opt]
1046/// [GNU] attributes specifier-qualifier-list[opt]
1047///
1048void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
1049 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1050 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 // Validate declspec for type-name.
1054 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001055 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001056 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 // Issue diagnostic and remove storage class if present.
1060 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1061 if (DS.getStorageClassSpecLoc().isValid())
1062 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1063 else
1064 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1065 DS.ClearStorageClassSpecs();
1066 }
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 // Issue diagnostic and remove function specfier if present.
1069 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001070 if (DS.isInlineSpecified())
1071 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1072 if (DS.isVirtualSpecified())
1073 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1074 if (DS.isExplicitSpecified())
1075 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 DS.ClearFunctionSpecs();
1077 }
1078}
1079
Chris Lattnerc199ab32009-04-12 20:42:31 +00001080/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1081/// specified token is valid after the identifier in a declarator which
1082/// immediately follows the declspec. For example, these things are valid:
1083///
1084/// int x [ 4]; // direct-declarator
1085/// int x ( int y); // direct-declarator
1086/// int(int x ) // direct-declarator
1087/// int x ; // simple-declaration
1088/// int x = 17; // init-declarator-list
1089/// int x , y; // init-declarator-list
1090/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001091/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001092/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001093///
1094/// This is not, because 'x' does not immediately follow the declspec (though
1095/// ')' happens to be valid anyway).
1096/// int (x)
1097///
1098static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1099 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1100 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001101 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001102}
1103
Chris Lattnere40c2952009-04-14 21:34:55 +00001104
1105/// ParseImplicitInt - This method is called when we have an non-typename
1106/// identifier in a declspec (which normally terminates the decl spec) when
1107/// the declspec has no type specifier. In this case, the declspec is either
1108/// malformed or is "implicit int" (in K&R and C89).
1109///
1110/// This method handles diagnosing this prettily and returns false if the
1111/// declspec is done being processed. If it recovers and thinks there may be
1112/// other pieces of declspec after it, it returns true.
1113///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001114bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001115 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001116 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001117 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Chris Lattnere40c2952009-04-14 21:34:55 +00001119 SourceLocation Loc = Tok.getLocation();
1120 // If we see an identifier that is not a type name, we normally would
1121 // parse it as the identifer being declared. However, when a typename
1122 // is typo'd or the definition is not included, this will incorrectly
1123 // parse the typename as the identifier name and fall over misparsing
1124 // later parts of the diagnostic.
1125 //
1126 // As such, we try to do some look-ahead in cases where this would
1127 // otherwise be an "implicit-int" case to see if this is invalid. For
1128 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1129 // an identifier with implicit int, we'd get a parse error because the
1130 // next token is obviously invalid for a type. Parse these as a case
1131 // with an invalid type specifier.
1132 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Chris Lattnere40c2952009-04-14 21:34:55 +00001134 // Since we know that this either implicit int (which is rare) or an
1135 // error, we'd do lookahead to try to do better recovery.
1136 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1137 // If this token is valid for implicit int, e.g. "static x = 4", then
1138 // we just avoid eating the identifier, so it will be parsed as the
1139 // identifier in the declarator.
1140 return false;
1141 }
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Chris Lattnere40c2952009-04-14 21:34:55 +00001143 // Otherwise, if we don't consume this token, we are going to emit an
1144 // error anyway. Try to recover from various common problems. Check
1145 // to see if this was a reference to a tag name without a tag specified.
1146 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001147 //
1148 // C++ doesn't need this, and isTagName doesn't take SS.
1149 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001150 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001151 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Douglas Gregor23c94db2010-07-02 17:43:08 +00001153 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001154 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001155 case DeclSpec::TST_enum:
1156 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1157 case DeclSpec::TST_union:
1158 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1159 case DeclSpec::TST_struct:
1160 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1161 case DeclSpec::TST_class:
1162 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001163 }
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Chris Lattnerf4382f52009-04-14 22:17:06 +00001165 if (TagName) {
1166 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001167 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001168 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Chris Lattnerf4382f52009-04-14 22:17:06 +00001170 // Parse this as a tag as if the missing tag were present.
1171 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001172 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001173 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001174 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001175 return true;
1176 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001177 }
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Douglas Gregora786fdb2009-10-13 23:27:22 +00001179 // This is almost certainly an invalid type name. Let the action emit a
1180 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001181 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001182 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001183 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001184 // The action emitted a diagnostic, so we don't have to.
1185 if (T) {
1186 // The action has suggested that the type T could be used. Set that as
1187 // the type in the declaration specifiers, consume the would-be type
1188 // name token, and we're done.
1189 const char *PrevSpec;
1190 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001191 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001192 DS.SetRangeEnd(Tok.getLocation());
1193 ConsumeToken();
1194
1195 // There may be other declaration specifiers after this.
1196 return true;
1197 }
1198
1199 // Fall through; the action had no suggestion for us.
1200 } else {
1201 // The action did not emit a diagnostic, so emit one now.
1202 SourceRange R;
1203 if (SS) R = SS->getRange();
1204 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1205 }
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Douglas Gregora786fdb2009-10-13 23:27:22 +00001207 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001208 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001209 unsigned DiagID;
1210 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001211 DS.SetRangeEnd(Tok.getLocation());
1212 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Chris Lattnere40c2952009-04-14 21:34:55 +00001214 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1215 // avoid rippling error messages on subsequent uses of the same type,
1216 // could be useful if #include was forgotten.
1217 return false;
1218}
1219
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001220/// \brief Determine the declaration specifier context from the declarator
1221/// context.
1222///
1223/// \param Context the declarator context, which is one of the
1224/// Declarator::TheContext enumerator values.
1225Parser::DeclSpecContext
1226Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1227 if (Context == Declarator::MemberContext)
1228 return DSC_class;
1229 if (Context == Declarator::FileContext)
1230 return DSC_top_level;
1231 return DSC_normal;
1232}
1233
Reid Spencer5f016e22007-07-11 17:01:13 +00001234/// ParseDeclarationSpecifiers
1235/// declaration-specifiers: [C99 6.7]
1236/// storage-class-specifier declaration-specifiers[opt]
1237/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001238/// [C99] function-specifier declaration-specifiers[opt]
1239/// [GNU] attributes declaration-specifiers[opt]
1240///
1241/// storage-class-specifier: [C99 6.7.1]
1242/// 'typedef'
1243/// 'extern'
1244/// 'static'
1245/// 'auto'
1246/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001247/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001248/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001249/// function-specifier: [C99 6.7.4]
1250/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001251/// [C++] 'virtual'
1252/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001253/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001254/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001255/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001256
Reid Spencer5f016e22007-07-11 17:01:13 +00001257///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001258void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001259 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001260 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001261 DeclSpecContext DSContext) {
1262 if (DS.getSourceRange().isInvalid()) {
1263 DS.SetRangeStart(Tok.getLocation());
1264 DS.SetRangeEnd(Tok.getLocation());
1265 }
1266
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001268 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001270 unsigned DiagID = 0;
1271
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001273
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001275 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001276 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 // If this is not a declaration specifier token, we're done reading decl
1278 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001279 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001282 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001283 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001284 if (DS.hasTypeSpecifier()) {
1285 bool AllowNonIdentifiers
1286 = (getCurScope()->getFlags() & (Scope::ControlScope |
1287 Scope::BlockScope |
1288 Scope::TemplateParamScope |
1289 Scope::FunctionPrototypeScope |
1290 Scope::AtCatchScope)) == 0;
1291 bool AllowNestedNameSpecifiers
1292 = DSContext == DSC_top_level ||
1293 (DSContext == DSC_class && DS.isFriendSpecified());
1294
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001295 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1296 AllowNonIdentifiers,
1297 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001298 ConsumeCodeCompletionToken();
1299 return;
1300 }
1301
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001302 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1303 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1304 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001305 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1306 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001307 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001308 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001309 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001310 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001311
1312 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1313 ConsumeCodeCompletionToken();
1314 return;
1315 }
1316
Chris Lattner5e02c472009-01-05 00:07:25 +00001317 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001318 // C++ scope specifier. Annotate and loop, or bail out on error.
1319 if (TryAnnotateCXXScopeToken(true)) {
1320 if (!DS.hasTypeSpecifier())
1321 DS.SetTypeSpecError();
1322 goto DoneWithDeclSpec;
1323 }
John McCall2e0a7152010-03-01 18:20:46 +00001324 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1325 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001326 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001327
1328 case tok::annot_cxxscope: {
1329 if (DS.hasTypeSpecifier())
1330 goto DoneWithDeclSpec;
1331
John McCallaa87d332009-12-12 11:40:51 +00001332 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001333 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1334 Tok.getAnnotationRange(),
1335 SS);
John McCallaa87d332009-12-12 11:40:51 +00001336
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001337 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001338 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001339 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001340 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001341 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001342 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001343
1344 // C++ [class.qual]p2:
1345 // In a lookup in which the constructor is an acceptable lookup
1346 // result and the nested-name-specifier nominates a class C:
1347 //
1348 // - if the name specified after the
1349 // nested-name-specifier, when looked up in C, is the
1350 // injected-class-name of C (Clause 9), or
1351 //
1352 // - if the name specified after the nested-name-specifier
1353 // is the same as the identifier or the
1354 // simple-template-id's template-name in the last
1355 // component of the nested-name-specifier,
1356 //
1357 // the name is instead considered to name the constructor of
1358 // class C.
1359 //
1360 // Thus, if the template-name is actually the constructor
1361 // name, then the code is ill-formed; this interpretation is
1362 // reinforced by the NAD status of core issue 635.
1363 TemplateIdAnnotation *TemplateId
1364 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +00001365 if ((DSContext == DSC_top_level ||
1366 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1367 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001368 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001369 if (isConstructorDeclarator()) {
1370 // The user meant this to be an out-of-line constructor
1371 // definition, but template arguments are not allowed
1372 // there. Just allow this as a constructor; we'll
1373 // complain about it later.
1374 goto DoneWithDeclSpec;
1375 }
1376
1377 // The user meant this to name a type, but it actually names
1378 // a constructor with some extraneous template
1379 // arguments. Complain, then parse it as a type as the user
1380 // intended.
1381 Diag(TemplateId->TemplateNameLoc,
1382 diag::err_out_of_line_template_id_names_constructor)
1383 << TemplateId->Name;
1384 }
1385
John McCallaa87d332009-12-12 11:40:51 +00001386 DS.getTypeSpecScope() = SS;
1387 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001388 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001389 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001390 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001391 continue;
1392 }
1393
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001394 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001395 DS.getTypeSpecScope() = SS;
1396 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001397 if (Tok.getAnnotationValue()) {
1398 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001399 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1400 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001401 PrevSpec, DiagID, T);
1402 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001403 else
1404 DS.SetTypeSpecError();
1405 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1406 ConsumeToken(); // The typename
1407 }
1408
Douglas Gregor9135c722009-03-25 15:40:00 +00001409 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001410 goto DoneWithDeclSpec;
1411
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001412 // If we're in a context where the identifier could be a class name,
1413 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001414 if ((DSContext == DSC_top_level ||
1415 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001416 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001417 &SS)) {
1418 if (isConstructorDeclarator())
1419 goto DoneWithDeclSpec;
1420
1421 // As noted in C++ [class.qual]p2 (cited above), when the name
1422 // of the class is qualified in a context where it could name
1423 // a constructor, its a constructor name. However, we've
1424 // looked at the declarator, and the user probably meant this
1425 // to be a type. Complain that it isn't supposed to be treated
1426 // as a type, then proceed to parse it as a type.
1427 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1428 << Next.getIdentifierInfo();
1429 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001430
John McCallb3d87482010-08-24 05:47:05 +00001431 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1432 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001433 getCurScope(), &SS,
1434 false, false, ParsedType(),
1435 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001436
Chris Lattnerf4382f52009-04-14 22:17:06 +00001437 // If the referenced identifier is not a type, then this declspec is
1438 // erroneous: We already checked about that it has no type specifier, and
1439 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001440 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001441 if (TypeRep == 0) {
1442 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001443 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001444 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001445 }
Mike Stump1eb44332009-09-09 15:08:12 +00001446
John McCallaa87d332009-12-12 11:40:51 +00001447 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001448 ConsumeToken(); // The C++ scope.
1449
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001450 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001451 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001452 if (isInvalid)
1453 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001454
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001455 DS.SetRangeEnd(Tok.getLocation());
1456 ConsumeToken(); // The typename.
1457
1458 continue;
1459 }
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Chris Lattner80d0c892009-01-21 19:48:37 +00001461 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001462 if (Tok.getAnnotationValue()) {
1463 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001464 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001465 DiagID, T);
1466 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001467 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001468
1469 if (isInvalid)
1470 break;
1471
Chris Lattner80d0c892009-01-21 19:48:37 +00001472 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1473 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001474
Chris Lattner80d0c892009-01-21 19:48:37 +00001475 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1476 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001477 // Objective-C interface.
1478 if (Tok.is(tok::less) && getLang().ObjC1)
1479 ParseObjCProtocolQualifiers(DS);
1480
Chris Lattner80d0c892009-01-21 19:48:37 +00001481 continue;
1482 }
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Douglas Gregorbfad9152011-04-28 15:48:45 +00001484 case tok::kw___is_signed:
1485 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1486 // typically treats it as a trait. If we see __is_signed as it appears
1487 // in libstdc++, e.g.,
1488 //
1489 // static const bool __is_signed;
1490 //
1491 // then treat __is_signed as an identifier rather than as a keyword.
1492 if (DS.getTypeSpecType() == TST_bool &&
1493 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1494 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1495 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1496 Tok.setKind(tok::identifier);
1497 }
1498
1499 // We're done with the declaration-specifiers.
1500 goto DoneWithDeclSpec;
1501
Chris Lattner3bd934a2008-07-26 01:18:38 +00001502 // typedef-name
1503 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001504 // In C++, check to see if this is a scope specifier like foo::bar::, if
1505 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001506 if (getLang().CPlusPlus) {
1507 if (TryAnnotateCXXScopeToken(true)) {
1508 if (!DS.hasTypeSpecifier())
1509 DS.SetTypeSpecError();
1510 goto DoneWithDeclSpec;
1511 }
1512 if (!Tok.is(tok::identifier))
1513 continue;
1514 }
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Chris Lattner3bd934a2008-07-26 01:18:38 +00001516 // This identifier can only be a typedef name if we haven't already seen
1517 // a type-specifier. Without this check we misparse:
1518 // typedef int X; struct Y { short X; }; as 'short int'.
1519 if (DS.hasTypeSpecifier())
1520 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001521
John Thompson82287d12010-02-05 00:12:22 +00001522 // Check for need to substitute AltiVec keyword tokens.
1523 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1524 break;
1525
Chris Lattner3bd934a2008-07-26 01:18:38 +00001526 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001527 ParsedType TypeRep =
1528 Actions.getTypeName(*Tok.getIdentifierInfo(),
1529 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001530
Chris Lattnerc199ab32009-04-12 20:42:31 +00001531 // If this is not a typedef name, don't parse it as part of the declspec,
1532 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001533 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001534 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001535 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001536 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001537
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001538 // If we're in a context where the identifier could be a class name,
1539 // check whether this is a constructor declaration.
1540 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001541 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001542 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001543 goto DoneWithDeclSpec;
1544
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001545 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001546 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001547 if (isInvalid)
1548 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Chris Lattner3bd934a2008-07-26 01:18:38 +00001550 DS.SetRangeEnd(Tok.getLocation());
1551 ConsumeToken(); // The identifier
1552
1553 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1554 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001555 // Objective-C interface.
1556 if (Tok.is(tok::less) && getLang().ObjC1)
1557 ParseObjCProtocolQualifiers(DS);
1558
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001559 // Need to support trailing type qualifiers (e.g. "id<p> const").
1560 // If a type specifier follows, it will be diagnosed elsewhere.
1561 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001562 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001563
1564 // type-name
1565 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001566 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001567 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001568 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001569 // This template-id does not refer to a type name, so we're
1570 // done with the type-specifiers.
1571 goto DoneWithDeclSpec;
1572 }
1573
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001574 // If we're in a context where the template-id could be a
1575 // constructor name or specialization, check whether this is a
1576 // constructor declaration.
1577 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001578 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001579 isConstructorDeclarator())
1580 goto DoneWithDeclSpec;
1581
Douglas Gregor39a8de12009-02-25 19:37:18 +00001582 // Turn the template-id annotation token into a type annotation
1583 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001584 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001585 continue;
1586 }
1587
Reid Spencer5f016e22007-07-11 17:01:13 +00001588 // GNU attributes support.
1589 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001590 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001591 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001592
1593 // Microsoft declspec support.
1594 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001595 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001596 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Steve Naroff239f0732008-12-25 14:16:32 +00001598 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001599 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001600 // FIXME: Add handling here!
1601 break;
1602
1603 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001604 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001605 case tok::kw___cdecl:
1606 case tok::kw___stdcall:
1607 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001608 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001609 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001610 continue;
1611
Dawn Perchik52fc3142010-09-03 01:29:35 +00001612 // Borland single token adornments.
1613 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001614 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001615 continue;
1616
Peter Collingbournef315fa82011-02-14 01:42:53 +00001617 // OpenCL single token adornments.
1618 case tok::kw___kernel:
1619 ParseOpenCLAttributes(DS.getAttributes());
1620 continue;
1621
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 // storage-class-specifier
1623 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001624 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001625 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001626 break;
1627 case tok::kw_extern:
1628 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001629 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001630 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001631 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001632 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001633 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001634 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001635 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001636 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001637 case tok::kw_static:
1638 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001639 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001640 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001641 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001642 break;
1643 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001644 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001645 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1646 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1647 DiagID, getLang());
1648 if (!isInvalid)
1649 Diag(Tok, diag::auto_storage_class)
1650 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1651 }
1652 else
1653 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1654 DiagID);
1655 }
Anders Carlssone89d1592009-06-26 18:41:36 +00001656 else
John McCallfec54012009-08-03 20:12:06 +00001657 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001658 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001659 break;
1660 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001661 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001662 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001663 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001664 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001665 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001666 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001667 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001668 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001669 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001670 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001671
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 // function-specifier
1673 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001674 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001675 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001676 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001677 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001678 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001679 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001680 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001681 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001682
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001683 // friend
1684 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001685 if (DSContext == DSC_class)
1686 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1687 else {
1688 PrevSpec = ""; // not actually used by the diagnostic
1689 DiagID = diag::err_friend_invalid_in_context;
1690 isInvalid = true;
1691 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001692 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Sebastian Redl2ac67232009-11-05 15:47:02 +00001694 // constexpr
1695 case tok::kw_constexpr:
1696 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1697 break;
1698
Chris Lattner80d0c892009-01-21 19:48:37 +00001699 // type-specifier
1700 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001701 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1702 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001703 break;
1704 case tok::kw_long:
1705 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001706 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1707 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001708 else
John McCallfec54012009-08-03 20:12:06 +00001709 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1710 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001711 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00001712 case tok::kw___int64:
1713 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1714 DiagID);
1715 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001716 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001717 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1718 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001719 break;
1720 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001721 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1722 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001723 break;
1724 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001725 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1726 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001727 break;
1728 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001729 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1730 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001731 break;
1732 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001733 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1734 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001735 break;
1736 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001737 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1738 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001739 break;
1740 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001741 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1742 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001743 break;
1744 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001745 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1746 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001747 break;
1748 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001749 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1750 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001751 break;
1752 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001753 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1754 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001755 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001756 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001757 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1758 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001759 break;
1760 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001761 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1762 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001763 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001764 case tok::kw_bool:
1765 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001766 if (Tok.is(tok::kw_bool) &&
1767 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1768 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1769 PrevSpec = ""; // Not used by the diagnostic.
1770 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001771 // For better error recovery.
1772 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001773 isInvalid = true;
1774 } else {
1775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1776 DiagID);
1777 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001778 break;
1779 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001780 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1781 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001782 break;
1783 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001784 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1785 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001786 break;
1787 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001788 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1789 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001790 break;
John Thompson82287d12010-02-05 00:12:22 +00001791 case tok::kw___vector:
1792 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1793 break;
1794 case tok::kw___pixel:
1795 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1796 break;
John McCalla5fc4722011-04-09 22:50:59 +00001797 case tok::kw___unknown_anytype:
1798 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1799 PrevSpec, DiagID);
1800 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001801
1802 // class-specifier:
1803 case tok::kw_class:
1804 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001805 case tok::kw_union: {
1806 tok::TokenKind Kind = Tok.getKind();
1807 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001808 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001809 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001810 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001811
1812 // enum-specifier:
1813 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001814 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001815 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001816 continue;
1817
1818 // cv-qualifier:
1819 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001820 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1821 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001822 break;
1823 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001824 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1825 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001826 break;
1827 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001828 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1829 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001830 break;
1831
Douglas Gregord57959a2009-03-27 23:10:48 +00001832 // C++ typename-specifier:
1833 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001834 if (TryAnnotateTypeOrScopeToken()) {
1835 DS.SetTypeSpecError();
1836 goto DoneWithDeclSpec;
1837 }
1838 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001839 continue;
1840 break;
1841
Chris Lattner80d0c892009-01-21 19:48:37 +00001842 // GNU typeof support.
1843 case tok::kw_typeof:
1844 ParseTypeofSpecifier(DS);
1845 continue;
1846
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001847 case tok::kw_decltype:
1848 ParseDecltypeSpecifier(DS);
1849 continue;
1850
Peter Collingbourne207f4d82011-03-18 22:38:29 +00001851 // OpenCL qualifiers:
1852 case tok::kw_private:
1853 if (!getLang().OpenCL)
1854 goto DoneWithDeclSpec;
1855 case tok::kw___private:
1856 case tok::kw___global:
1857 case tok::kw___local:
1858 case tok::kw___constant:
1859 case tok::kw___read_only:
1860 case tok::kw___write_only:
1861 case tok::kw___read_write:
1862 ParseOpenCLQualifiers(DS);
1863 break;
1864
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001865 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001866 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001867 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1868 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001869 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001870 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Douglas Gregor46f936e2010-11-19 17:10:50 +00001872 if (!ParseObjCProtocolQualifiers(DS))
1873 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1874 << FixItHint::CreateInsertion(Loc, "id")
1875 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001876
1877 // Need to support trailing type qualifiers (e.g. "id<p> const").
1878 // If a type specifier follows, it will be diagnosed elsewhere.
1879 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001880 }
John McCallfec54012009-08-03 20:12:06 +00001881 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001882 if (isInvalid) {
1883 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001884 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001885
1886 if (DiagID == diag::ext_duplicate_declspec)
1887 Diag(Tok, DiagID)
1888 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1889 else
1890 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001891 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001892
Chris Lattner81c018d2008-03-13 06:29:04 +00001893 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00001894 if (DiagID != diag::err_bool_redeclaration)
1895 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001896 }
1897}
Douglas Gregoradcac882008-12-01 23:54:00 +00001898
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001899/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001900/// primarily follow the C++ grammar with additions for C99 and GNU,
1901/// which together subsume the C grammar. Note that the C++
1902/// type-specifier also includes the C type-qualifier (for const,
1903/// volatile, and C99 restrict). Returns true if a type-specifier was
1904/// found (and parsed), false otherwise.
1905///
1906/// type-specifier: [C++ 7.1.5]
1907/// simple-type-specifier
1908/// class-specifier
1909/// enum-specifier
1910/// elaborated-type-specifier [TODO]
1911/// cv-qualifier
1912///
1913/// cv-qualifier: [C++ 7.1.5.1]
1914/// 'const'
1915/// 'volatile'
1916/// [C99] 'restrict'
1917///
1918/// simple-type-specifier: [ C++ 7.1.5.2]
1919/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1920/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1921/// 'char'
1922/// 'wchar_t'
1923/// 'bool'
1924/// 'short'
1925/// 'int'
1926/// 'long'
1927/// 'signed'
1928/// 'unsigned'
1929/// 'float'
1930/// 'double'
1931/// 'void'
1932/// [C99] '_Bool'
1933/// [C99] '_Complex'
1934/// [C99] '_Imaginary' // Removed in TC2?
1935/// [GNU] '_Decimal32'
1936/// [GNU] '_Decimal64'
1937/// [GNU] '_Decimal128'
1938/// [GNU] typeof-specifier
1939/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1940/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001941/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001942/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001943bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001944 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001945 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001946 const ParsedTemplateInfo &TemplateInfo,
1947 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001948 SourceLocation Loc = Tok.getLocation();
1949
1950 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001951 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001952 // If we already have a type specifier, this identifier is not a type.
1953 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1954 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1955 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1956 return false;
John Thompson82287d12010-02-05 00:12:22 +00001957 // Check for need to substitute AltiVec keyword tokens.
1958 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1959 break;
1960 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001961 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001962 // Annotate typenames and C++ scope specifiers. If we get one, just
1963 // recurse to handle whatever we get.
1964 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001965 return true;
1966 if (Tok.is(tok::identifier))
1967 return false;
1968 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1969 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001970 case tok::coloncolon: // ::foo::bar
1971 if (NextToken().is(tok::kw_new) || // ::new
1972 NextToken().is(tok::kw_delete)) // ::delete
1973 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Chris Lattner166a8fc2009-01-04 23:41:41 +00001975 // Annotate typenames and C++ scope specifiers. If we get one, just
1976 // recurse to handle whatever we get.
1977 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001978 return true;
1979 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1980 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Douglas Gregor12e083c2008-11-07 15:42:26 +00001982 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001983 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001984 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00001985 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1986 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001987 DiagID, T);
1988 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001989 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001990 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1991 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001992
Douglas Gregor12e083c2008-11-07 15:42:26 +00001993 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1994 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1995 // Objective-C interface. If we don't have Objective-C or a '<', this is
1996 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001997 if (Tok.is(tok::less) && getLang().ObjC1)
1998 ParseObjCProtocolQualifiers(DS);
1999
Douglas Gregor12e083c2008-11-07 15:42:26 +00002000 return true;
2001 }
2002
2003 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002004 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002005 break;
2006 case tok::kw_long:
2007 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002008 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2009 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002010 else
John McCallfec54012009-08-03 20:12:06 +00002011 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2012 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002013 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002014 case tok::kw___int64:
2015 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2016 DiagID);
2017 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002018 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002019 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002020 break;
2021 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002022 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2023 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002024 break;
2025 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002026 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2027 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002028 break;
2029 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002030 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2031 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002032 break;
2033 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002034 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002035 break;
2036 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002037 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002038 break;
2039 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002040 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002041 break;
2042 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002043 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002044 break;
2045 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002046 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002047 break;
2048 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002049 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002050 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002051 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002052 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002053 break;
2054 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002055 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002056 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002057 case tok::kw_bool:
2058 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002059 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002060 break;
2061 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002062 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2063 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002064 break;
2065 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002066 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2067 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002068 break;
2069 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002070 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2071 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002072 break;
John Thompson82287d12010-02-05 00:12:22 +00002073 case tok::kw___vector:
2074 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2075 break;
2076 case tok::kw___pixel:
2077 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2078 break;
2079
Douglas Gregor12e083c2008-11-07 15:42:26 +00002080 // class-specifier:
2081 case tok::kw_class:
2082 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002083 case tok::kw_union: {
2084 tok::TokenKind Kind = Tok.getKind();
2085 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002086 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2087 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002088 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002089 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002090
2091 // enum-specifier:
2092 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002093 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002094 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002095 return true;
2096
2097 // cv-qualifier:
2098 case tok::kw_const:
2099 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002100 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002101 break;
2102 case tok::kw_volatile:
2103 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002104 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002105 break;
2106 case tok::kw_restrict:
2107 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002108 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002109 break;
2110
2111 // GNU typeof support.
2112 case tok::kw_typeof:
2113 ParseTypeofSpecifier(DS);
2114 return true;
2115
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002116 // C++0x decltype support.
2117 case tok::kw_decltype:
2118 ParseDecltypeSpecifier(DS);
2119 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002120
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002121 // OpenCL qualifiers:
2122 case tok::kw_private:
2123 if (!getLang().OpenCL)
2124 return false;
2125 case tok::kw___private:
2126 case tok::kw___global:
2127 case tok::kw___local:
2128 case tok::kw___constant:
2129 case tok::kw___read_only:
2130 case tok::kw___write_only:
2131 case tok::kw___read_write:
2132 ParseOpenCLQualifiers(DS);
2133 break;
2134
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002135 // C++0x auto support.
2136 case tok::kw_auto:
2137 if (!getLang().CPlusPlus0x)
2138 return false;
2139
John McCallfec54012009-08-03 20:12:06 +00002140 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002141 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002142
Eli Friedman290eeb02009-06-08 23:27:34 +00002143 case tok::kw___ptr64:
2144 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002145 case tok::kw___cdecl:
2146 case tok::kw___stdcall:
2147 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002148 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00002149 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002150 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002151
Dawn Perchik52fc3142010-09-03 01:29:35 +00002152 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002153 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002154 return true;
2155
Douglas Gregor12e083c2008-11-07 15:42:26 +00002156 default:
2157 // Not a type-specifier; do nothing.
2158 return false;
2159 }
2160
2161 // If the specifier combination wasn't legal, issue a diagnostic.
2162 if (isInvalid) {
2163 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002164 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002165 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002166 }
2167 DS.SetRangeEnd(Tok.getLocation());
2168 ConsumeToken(); // whatever we parsed above.
2169 return true;
2170}
Reid Spencer5f016e22007-07-11 17:01:13 +00002171
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002172/// ParseStructDeclaration - Parse a struct declaration without the terminating
2173/// semicolon.
2174///
Reid Spencer5f016e22007-07-11 17:01:13 +00002175/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002176/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002177/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002178/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002179/// struct-declarator-list:
2180/// struct-declarator
2181/// struct-declarator-list ',' struct-declarator
2182/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2183/// struct-declarator:
2184/// declarator
2185/// [GNU] declarator attributes[opt]
2186/// declarator[opt] ':' constant-expression
2187/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2188///
Chris Lattnere1359422008-04-10 06:46:29 +00002189void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002190ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002191 if (Tok.is(tok::kw___extension__)) {
2192 // __extension__ silences extension warnings in the subexpression.
2193 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002194 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002195 return ParseStructDeclaration(DS, Fields);
2196 }
Mike Stump1eb44332009-09-09 15:08:12 +00002197
Steve Naroff28a7ca82007-08-20 22:28:22 +00002198 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002199 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002200
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002201 // If there are no declarators, this is a free-standing declaration
2202 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002203 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002204 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002205 return;
2206 }
2207
2208 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002209 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002210 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002211 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002212 FieldDeclarator DeclaratorInfo(DS);
2213
2214 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002215 if (!FirstDeclarator)
2216 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002217
Steve Naroff28a7ca82007-08-20 22:28:22 +00002218 /// struct-declarator: declarator
2219 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002220 if (Tok.isNot(tok::colon)) {
2221 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2222 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002223 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002224 }
Mike Stump1eb44332009-09-09 15:08:12 +00002225
Chris Lattner04d66662007-10-09 17:33:22 +00002226 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002227 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002228 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002229 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002230 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002231 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002232 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002233 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002234
Steve Naroff28a7ca82007-08-20 22:28:22 +00002235 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002236 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002237
John McCallbdd563e2009-11-03 02:38:08 +00002238 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002239 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002240 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002241
Steve Naroff28a7ca82007-08-20 22:28:22 +00002242 // If we don't have a comma, it is either the end of the list (a ';')
2243 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002244 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002245 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002246
Steve Naroff28a7ca82007-08-20 22:28:22 +00002247 // Consume the comma.
2248 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002249
John McCallbdd563e2009-11-03 02:38:08 +00002250 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002251 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002252}
2253
2254/// ParseStructUnionBody
2255/// struct-contents:
2256/// struct-declaration-list
2257/// [EXT] empty
2258/// [GNU] "struct-declaration-list" without terminatoring ';'
2259/// struct-declaration-list:
2260/// struct-declaration
2261/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002262/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002263///
Reid Spencer5f016e22007-07-11 17:01:13 +00002264void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002265 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002266 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2267 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002268
Reid Spencer5f016e22007-07-11 17:01:13 +00002269 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002270
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002271 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002272 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002273
Reid Spencer5f016e22007-07-11 17:01:13 +00002274 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2275 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002276 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002277 Diag(Tok, diag::ext_empty_struct_union)
2278 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002279
John McCalld226f652010-08-21 09:40:31 +00002280 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002281
Reid Spencer5f016e22007-07-11 17:01:13 +00002282 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002283 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002284 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002285
Reid Spencer5f016e22007-07-11 17:01:13 +00002286 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002287 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002288 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002289 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002290 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002291 ConsumeToken();
2292 continue;
2293 }
Chris Lattnere1359422008-04-10 06:46:29 +00002294
2295 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002296 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002297
John McCallbdd563e2009-11-03 02:38:08 +00002298 if (!Tok.is(tok::at)) {
2299 struct CFieldCallback : FieldCallback {
2300 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002301 Decl *TagDecl;
2302 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002303
John McCalld226f652010-08-21 09:40:31 +00002304 CFieldCallback(Parser &P, Decl *TagDecl,
2305 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002306 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2307
John McCalld226f652010-08-21 09:40:31 +00002308 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002309 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002310 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002311 FD.D.getDeclSpec().getSourceRange().getBegin(),
2312 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002313 FieldDecls.push_back(Field);
2314 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002315 }
John McCallbdd563e2009-11-03 02:38:08 +00002316 } Callback(*this, TagDecl, FieldDecls);
2317
2318 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002319 } else { // Handle @defs
2320 ConsumeToken();
2321 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2322 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002323 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002324 continue;
2325 }
2326 ConsumeToken();
2327 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2328 if (!Tok.is(tok::identifier)) {
2329 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002330 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002331 continue;
2332 }
John McCalld226f652010-08-21 09:40:31 +00002333 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002334 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002335 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002336 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2337 ConsumeToken();
2338 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002339 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002340
Chris Lattner04d66662007-10-09 17:33:22 +00002341 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002342 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002343 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002344 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002345 break;
2346 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002347 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2348 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002349 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002350 // If we stopped at a ';', eat it.
2351 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002352 }
2353 }
Mike Stump1eb44332009-09-09 15:08:12 +00002354
Steve Naroff60fccee2007-10-29 21:38:07 +00002355 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002356
John McCall0b7e6782011-03-24 11:26:52 +00002357 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002358 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002359 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002360
Douglas Gregor23c94db2010-07-02 17:43:08 +00002361 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00002362 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002363 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00002364 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002365 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002366 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002367}
2368
Reid Spencer5f016e22007-07-11 17:01:13 +00002369/// ParseEnumSpecifier
2370/// enum-specifier: [C99 6.7.2.2]
2371/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002372///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002373/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2374/// '}' attributes[opt]
2375/// 'enum' identifier
2376/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002377///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002378/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2379/// [C++0x] enum-head '{' enumerator-list ',' '}'
2380///
2381/// enum-head: [C++0x]
2382/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2383/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2384///
2385/// enum-key: [C++0x]
2386/// 'enum'
2387/// 'enum' 'class'
2388/// 'enum' 'struct'
2389///
2390/// enum-base: [C++0x]
2391/// ':' type-specifier-seq
2392///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002393/// [C++] elaborated-type-specifier:
2394/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2395///
Chris Lattner4c97d762009-04-12 21:49:30 +00002396void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002397 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002398 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002399 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002400 if (Tok.is(tok::code_completion)) {
2401 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002402 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00002403 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00002404 }
2405
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002406 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002407 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002408 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002409
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002410 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002411 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00002412 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002413 return;
2414
2415 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002416 Diag(Tok, diag::err_expected_ident);
2417 if (Tok.isNot(tok::l_brace)) {
2418 // Has no name and is not a definition.
2419 // Skip the rest of this declarator, up until the comma or semicolon.
2420 SkipUntil(tok::comma, true);
2421 return;
2422 }
2423 }
2424 }
Mike Stump1eb44332009-09-09 15:08:12 +00002425
Douglas Gregor86f208c2011-02-22 20:32:04 +00002426 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002427 bool IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002428 bool IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002429
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002430 if (getLang().CPlusPlus0x &&
2431 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002432 IsScopedEnum = true;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002433 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2434 ConsumeToken();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002435 }
2436
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002437 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002438 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2439 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002440 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002441
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002442 // Skip the rest of this declarator, up until the comma or semicolon.
2443 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002444 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002445 }
Mike Stump1eb44332009-09-09 15:08:12 +00002446
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002447 // If an identifier is present, consume and remember it.
2448 IdentifierInfo *Name = 0;
2449 SourceLocation NameLoc;
2450 if (Tok.is(tok::identifier)) {
2451 Name = Tok.getIdentifierInfo();
2452 NameLoc = ConsumeToken();
2453 }
Mike Stump1eb44332009-09-09 15:08:12 +00002454
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002455 if (!Name && IsScopedEnum) {
2456 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2457 // declaration of a scoped enumeration.
2458 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2459 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002460 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002461 }
2462
2463 TypeResult BaseType;
2464
Douglas Gregora61b3e72010-12-01 17:42:47 +00002465 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002466 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002467 bool PossibleBitfield = false;
2468 if (getCurScope()->getFlags() & Scope::ClassScope) {
2469 // If we're in class scope, this can either be an enum declaration with
2470 // an underlying type, or a declaration of a bitfield member. We try to
2471 // use a simple disambiguation scheme first to catch the common cases
2472 // (integer literal, sizeof); if it's still ambiguous, we then consider
2473 // anything that's a simple-type-specifier followed by '(' as an
2474 // expression. This suffices because function types are not valid
2475 // underlying types anyway.
2476 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2477 // If the next token starts an expression, we know we're parsing a
2478 // bit-field. This is the common case.
2479 if (TPR == TPResult::True())
2480 PossibleBitfield = true;
2481 // If the next token starts a type-specifier-seq, it may be either a
2482 // a fixed underlying type or the start of a function-style cast in C++;
2483 // lookahead one more token to see if it's obvious that we have a
2484 // fixed underlying type.
2485 else if (TPR == TPResult::False() &&
2486 GetLookAheadToken(2).getKind() == tok::semi) {
2487 // Consume the ':'.
2488 ConsumeToken();
2489 } else {
2490 // We have the start of a type-specifier-seq, so we have to perform
2491 // tentative parsing to determine whether we have an expression or a
2492 // type.
2493 TentativeParsingAction TPA(*this);
2494
2495 // Consume the ':'.
2496 ConsumeToken();
2497
Douglas Gregor86f208c2011-02-22 20:32:04 +00002498 if ((getLang().CPlusPlus &&
2499 isCXXDeclarationSpecifier() != TPResult::True()) ||
2500 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002501 // We'll parse this as a bitfield later.
2502 PossibleBitfield = true;
2503 TPA.Revert();
2504 } else {
2505 // We have a type-specifier-seq.
2506 TPA.Commit();
2507 }
2508 }
2509 } else {
2510 // Consume the ':'.
2511 ConsumeToken();
2512 }
2513
2514 if (!PossibleBitfield) {
2515 SourceRange Range;
2516 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002517
2518 if (!getLang().CPlusPlus0x)
2519 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2520 << Range;
Douglas Gregora61b3e72010-12-01 17:42:47 +00002521 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002522 }
2523
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002524 // There are three options here. If we have 'enum foo;', then this is a
2525 // forward declaration. If we have 'enum foo {...' then this is a
2526 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2527 //
2528 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2529 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2530 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2531 //
John McCallf312b1e2010-08-26 23:41:50 +00002532 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002533 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002534 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002535 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002536 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002537 else
John McCallf312b1e2010-08-26 23:41:50 +00002538 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002539
2540 // enums cannot be templates, although they can be referenced from a
2541 // template.
2542 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002543 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002544 Diag(Tok, diag::err_enum_template);
2545
2546 // Skip the rest of this declarator, up until the comma or semicolon.
2547 SkipUntil(tok::comma, true);
2548 return;
2549 }
2550
Douglas Gregorb9075602011-02-22 02:55:24 +00002551 if (!Name && TUK != Sema::TUK_Definition) {
2552 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2553
2554 // Skip the rest of this declarator, up until the comma or semicolon.
2555 SkipUntil(tok::comma, true);
2556 return;
2557 }
2558
Douglas Gregor402abb52009-05-28 23:31:59 +00002559 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002560 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002561 const char *PrevSpec = 0;
2562 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002563 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002564 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002565 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002566 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002567 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002568 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002569
Douglas Gregor48c89f42010-04-24 16:38:41 +00002570 if (IsDependent) {
2571 // This enum has a dependent nested-name-specifier. Handle it as a
2572 // dependent tag.
2573 if (!Name) {
2574 DS.SetTypeSpecError();
2575 Diag(Tok, diag::err_expected_type_name_after_typename);
2576 return;
2577 }
2578
Douglas Gregor23c94db2010-07-02 17:43:08 +00002579 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002580 TUK, SS, Name, StartLoc,
2581 NameLoc);
2582 if (Type.isInvalid()) {
2583 DS.SetTypeSpecError();
2584 return;
2585 }
2586
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002587 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2588 NameLoc.isValid() ? NameLoc : StartLoc,
2589 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002590 Diag(StartLoc, DiagID) << PrevSpec;
2591
2592 return;
2593 }
Mike Stump1eb44332009-09-09 15:08:12 +00002594
John McCalld226f652010-08-21 09:40:31 +00002595 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002596 // The action failed to produce an enumeration tag. If this is a
2597 // definition, consume the entire definition.
2598 if (Tok.is(tok::l_brace)) {
2599 ConsumeBrace();
2600 SkipUntil(tok::r_brace);
2601 }
2602
2603 DS.SetTypeSpecError();
2604 return;
2605 }
2606
Chris Lattner04d66662007-10-09 17:33:22 +00002607 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002608 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002610 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2611 NameLoc.isValid() ? NameLoc : StartLoc,
2612 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002613 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002614}
2615
2616/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2617/// enumerator-list:
2618/// enumerator
2619/// enumerator-list ',' enumerator
2620/// enumerator:
2621/// enumeration-constant
2622/// enumeration-constant '=' constant-expression
2623/// enumeration-constant:
2624/// identifier
2625///
John McCalld226f652010-08-21 09:40:31 +00002626void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002627 // Enter the scope of the enum body and start the definition.
2628 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002629 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002630
Reid Spencer5f016e22007-07-11 17:01:13 +00002631 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002632
Chris Lattner7946dd32007-08-27 17:24:30 +00002633 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002634 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002635 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002636
John McCalld226f652010-08-21 09:40:31 +00002637 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002638
John McCalld226f652010-08-21 09:40:31 +00002639 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002640
Reid Spencer5f016e22007-07-11 17:01:13 +00002641 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002642 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002643 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2644 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002645
John McCall5b629aa2010-10-22 23:36:17 +00002646 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002647 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002648 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002649
Reid Spencer5f016e22007-07-11 17:01:13 +00002650 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002651 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002652 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002653 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002654 AssignedVal = ParseConstantExpression();
2655 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002656 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002657 }
Mike Stump1eb44332009-09-09 15:08:12 +00002658
Reid Spencer5f016e22007-07-11 17:01:13 +00002659 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002660 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2661 LastEnumConstDecl,
2662 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002663 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002664 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002665 EnumConstantDecls.push_back(EnumConstDecl);
2666 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002667
Douglas Gregor751f6922010-09-07 14:51:08 +00002668 if (Tok.is(tok::identifier)) {
2669 // We're missing a comma between enumerators.
2670 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2671 Diag(Loc, diag::err_enumerator_list_missing_comma)
2672 << FixItHint::CreateInsertion(Loc, ", ");
2673 continue;
2674 }
2675
Chris Lattner04d66662007-10-09 17:33:22 +00002676 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002677 break;
2678 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002679
2680 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002681 !(getLang().C99 || getLang().CPlusPlus0x))
2682 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2683 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002684 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002685 }
Mike Stump1eb44332009-09-09 15:08:12 +00002686
Reid Spencer5f016e22007-07-11 17:01:13 +00002687 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002688 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002689
Reid Spencer5f016e22007-07-11 17:01:13 +00002690 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002691 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002692 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002693
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002694 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2695 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002696 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002697
Douglas Gregor72de6672009-01-08 20:45:30 +00002698 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002699 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002700}
2701
2702/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002703/// start of a type-qualifier-list.
2704bool Parser::isTypeQualifier() const {
2705 switch (Tok.getKind()) {
2706 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002707
2708 // type-qualifier only in OpenCL
2709 case tok::kw_private:
2710 return getLang().OpenCL;
2711
Steve Naroff5f8aa692008-02-11 23:15:56 +00002712 // type-qualifier
2713 case tok::kw_const:
2714 case tok::kw_volatile:
2715 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002716 case tok::kw___private:
2717 case tok::kw___local:
2718 case tok::kw___global:
2719 case tok::kw___constant:
2720 case tok::kw___read_only:
2721 case tok::kw___read_write:
2722 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00002723 return true;
2724 }
2725}
2726
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002727/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2728/// is definitely a type-specifier. Return false if it isn't part of a type
2729/// specifier or if we're not sure.
2730bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2731 switch (Tok.getKind()) {
2732 default: return false;
2733 // type-specifiers
2734 case tok::kw_short:
2735 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002736 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002737 case tok::kw_signed:
2738 case tok::kw_unsigned:
2739 case tok::kw__Complex:
2740 case tok::kw__Imaginary:
2741 case tok::kw_void:
2742 case tok::kw_char:
2743 case tok::kw_wchar_t:
2744 case tok::kw_char16_t:
2745 case tok::kw_char32_t:
2746 case tok::kw_int:
2747 case tok::kw_float:
2748 case tok::kw_double:
2749 case tok::kw_bool:
2750 case tok::kw__Bool:
2751 case tok::kw__Decimal32:
2752 case tok::kw__Decimal64:
2753 case tok::kw__Decimal128:
2754 case tok::kw___vector:
2755
2756 // struct-or-union-specifier (C99) or class-specifier (C++)
2757 case tok::kw_class:
2758 case tok::kw_struct:
2759 case tok::kw_union:
2760 // enum-specifier
2761 case tok::kw_enum:
2762
2763 // typedef-name
2764 case tok::annot_typename:
2765 return true;
2766 }
2767}
2768
Steve Naroff5f8aa692008-02-11 23:15:56 +00002769/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002770/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002771bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002772 switch (Tok.getKind()) {
2773 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002774
Chris Lattner166a8fc2009-01-04 23:41:41 +00002775 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002776 if (TryAltiVecVectorToken())
2777 return true;
2778 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002779 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002780 // Annotate typenames and C++ scope specifiers. If we get one, just
2781 // recurse to handle whatever we get.
2782 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002783 return true;
2784 if (Tok.is(tok::identifier))
2785 return false;
2786 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002787
Chris Lattner166a8fc2009-01-04 23:41:41 +00002788 case tok::coloncolon: // ::foo::bar
2789 if (NextToken().is(tok::kw_new) || // ::new
2790 NextToken().is(tok::kw_delete)) // ::delete
2791 return false;
2792
Chris Lattner166a8fc2009-01-04 23:41:41 +00002793 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002794 return true;
2795 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002796
Reid Spencer5f016e22007-07-11 17:01:13 +00002797 // GNU attributes support.
2798 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002799 // GNU typeof support.
2800 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002801
Reid Spencer5f016e22007-07-11 17:01:13 +00002802 // type-specifiers
2803 case tok::kw_short:
2804 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002805 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002806 case tok::kw_signed:
2807 case tok::kw_unsigned:
2808 case tok::kw__Complex:
2809 case tok::kw__Imaginary:
2810 case tok::kw_void:
2811 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002812 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002813 case tok::kw_char16_t:
2814 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002815 case tok::kw_int:
2816 case tok::kw_float:
2817 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002818 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002819 case tok::kw__Bool:
2820 case tok::kw__Decimal32:
2821 case tok::kw__Decimal64:
2822 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002823 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002824
Chris Lattner99dc9142008-04-13 18:59:07 +00002825 // struct-or-union-specifier (C99) or class-specifier (C++)
2826 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002827 case tok::kw_struct:
2828 case tok::kw_union:
2829 // enum-specifier
2830 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002831
Reid Spencer5f016e22007-07-11 17:01:13 +00002832 // type-qualifier
2833 case tok::kw_const:
2834 case tok::kw_volatile:
2835 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002836
2837 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002838 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002839 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002840
Chris Lattner7c186be2008-10-20 00:25:30 +00002841 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2842 case tok::less:
2843 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002844
Steve Naroff239f0732008-12-25 14:16:32 +00002845 case tok::kw___cdecl:
2846 case tok::kw___stdcall:
2847 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002848 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002849 case tok::kw___w64:
2850 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002851 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002852
2853 case tok::kw___private:
2854 case tok::kw___local:
2855 case tok::kw___global:
2856 case tok::kw___constant:
2857 case tok::kw___read_only:
2858 case tok::kw___read_write:
2859 case tok::kw___write_only:
2860
Eli Friedman290eeb02009-06-08 23:27:34 +00002861 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002862
2863 case tok::kw_private:
2864 return getLang().OpenCL;
Reid Spencer5f016e22007-07-11 17:01:13 +00002865 }
2866}
2867
2868/// isDeclarationSpecifier() - Return true if the current token is part of a
2869/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002870///
2871/// \param DisambiguatingWithExpression True to indicate that the purpose of
2872/// this check is to disambiguate between an expression and a declaration.
2873bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002874 switch (Tok.getKind()) {
2875 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002876
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002877 case tok::kw_private:
2878 return getLang().OpenCL;
2879
Chris Lattner166a8fc2009-01-04 23:41:41 +00002880 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002881 // Unfortunate hack to support "Class.factoryMethod" notation.
2882 if (getLang().ObjC1 && NextToken().is(tok::period))
2883 return false;
John Thompson82287d12010-02-05 00:12:22 +00002884 if (TryAltiVecVectorToken())
2885 return true;
2886 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002887 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002888 // Annotate typenames and C++ scope specifiers. If we get one, just
2889 // recurse to handle whatever we get.
2890 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002891 return true;
2892 if (Tok.is(tok::identifier))
2893 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002894
2895 // If we're in Objective-C and we have an Objective-C class type followed
2896 // by an identifier and then either ':' or ']', in a place where an
2897 // expression is permitted, then this is probably a class message send
2898 // missing the initial '['. In this case, we won't consider this to be
2899 // the start of a declaration.
2900 if (DisambiguatingWithExpression &&
2901 isStartOfObjCClassMessageMissingOpenBracket())
2902 return false;
2903
John McCall9ba61662010-02-26 08:45:28 +00002904 return isDeclarationSpecifier();
2905
Chris Lattner166a8fc2009-01-04 23:41:41 +00002906 case tok::coloncolon: // ::foo::bar
2907 if (NextToken().is(tok::kw_new) || // ::new
2908 NextToken().is(tok::kw_delete)) // ::delete
2909 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002910
Chris Lattner166a8fc2009-01-04 23:41:41 +00002911 // Annotate typenames and C++ scope specifiers. If we get one, just
2912 // recurse to handle whatever we get.
2913 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002914 return true;
2915 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002916
Reid Spencer5f016e22007-07-11 17:01:13 +00002917 // storage-class-specifier
2918 case tok::kw_typedef:
2919 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002920 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002921 case tok::kw_static:
2922 case tok::kw_auto:
2923 case tok::kw_register:
2924 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002925
Reid Spencer5f016e22007-07-11 17:01:13 +00002926 // type-specifiers
2927 case tok::kw_short:
2928 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00002929 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00002930 case tok::kw_signed:
2931 case tok::kw_unsigned:
2932 case tok::kw__Complex:
2933 case tok::kw__Imaginary:
2934 case tok::kw_void:
2935 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002936 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002937 case tok::kw_char16_t:
2938 case tok::kw_char32_t:
2939
Reid Spencer5f016e22007-07-11 17:01:13 +00002940 case tok::kw_int:
2941 case tok::kw_float:
2942 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002943 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002944 case tok::kw__Bool:
2945 case tok::kw__Decimal32:
2946 case tok::kw__Decimal64:
2947 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002948 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002949
Chris Lattner99dc9142008-04-13 18:59:07 +00002950 // struct-or-union-specifier (C99) or class-specifier (C++)
2951 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002952 case tok::kw_struct:
2953 case tok::kw_union:
2954 // enum-specifier
2955 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002956
Reid Spencer5f016e22007-07-11 17:01:13 +00002957 // type-qualifier
2958 case tok::kw_const:
2959 case tok::kw_volatile:
2960 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002961
Reid Spencer5f016e22007-07-11 17:01:13 +00002962 // function-specifier
2963 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002964 case tok::kw_virtual:
2965 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002966
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00002967 // static_assert-declaration
2968 case tok::kw__Static_assert:
2969
Chris Lattner1ef08762007-08-09 17:01:07 +00002970 // GNU typeof support.
2971 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002972
Chris Lattner1ef08762007-08-09 17:01:07 +00002973 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002974 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002975 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002976
Chris Lattnerf3948c42008-07-26 03:38:44 +00002977 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2978 case tok::less:
2979 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002980
Douglas Gregord9d75e52011-04-27 05:41:15 +00002981 // typedef-name
2982 case tok::annot_typename:
2983 return !DisambiguatingWithExpression ||
2984 !isStartOfObjCClassMessageMissingOpenBracket();
2985
Steve Naroff47f52092009-01-06 19:34:12 +00002986 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002987 case tok::kw___cdecl:
2988 case tok::kw___stdcall:
2989 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002990 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002991 case tok::kw___w64:
2992 case tok::kw___ptr64:
2993 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002994 case tok::kw___pascal:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002995
2996 case tok::kw___private:
2997 case tok::kw___local:
2998 case tok::kw___global:
2999 case tok::kw___constant:
3000 case tok::kw___read_only:
3001 case tok::kw___read_write:
3002 case tok::kw___write_only:
3003
Eli Friedman290eeb02009-06-08 23:27:34 +00003004 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003005 }
3006}
3007
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003008bool Parser::isConstructorDeclarator() {
3009 TentativeParsingAction TPA(*this);
3010
3011 // Parse the C++ scope specifier.
3012 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003013 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003014 TPA.Revert();
3015 return false;
3016 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003017
3018 // Parse the constructor name.
3019 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3020 // We already know that we have a constructor name; just consume
3021 // the token.
3022 ConsumeToken();
3023 } else {
3024 TPA.Revert();
3025 return false;
3026 }
3027
3028 // Current class name must be followed by a left parentheses.
3029 if (Tok.isNot(tok::l_paren)) {
3030 TPA.Revert();
3031 return false;
3032 }
3033 ConsumeParen();
3034
3035 // A right parentheses or ellipsis signals that we have a constructor.
3036 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3037 TPA.Revert();
3038 return true;
3039 }
3040
3041 // If we need to, enter the specified scope.
3042 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003043 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003044 DeclScopeObj.EnterDeclaratorScope();
3045
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003046 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003047 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003048 MaybeParseMicrosoftAttributes(Attrs);
3049
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003050 // Check whether the next token(s) are part of a declaration
3051 // specifier, in which case we have the start of a parameter and,
3052 // therefore, we know that this is a constructor.
3053 bool IsConstructor = isDeclarationSpecifier();
3054 TPA.Revert();
3055 return IsConstructor;
3056}
Reid Spencer5f016e22007-07-11 17:01:13 +00003057
3058/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003059/// type-qualifier-list: [C99 6.7.5]
3060/// type-qualifier
3061/// [vendor] attributes
3062/// [ only if VendorAttributesAllowed=true ]
3063/// type-qualifier-list type-qualifier
3064/// [vendor] type-qualifier-list attributes
3065/// [ only if VendorAttributesAllowed=true ]
3066/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3067/// [ only if CXX0XAttributesAllowed=true ]
3068/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003069///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003070void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3071 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003072 bool CXX0XAttributesAllowed) {
3073 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3074 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003075 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003076 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003077 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003078 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003079 else
3080 Diag(Loc, diag::err_attributes_not_allowed);
3081 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003082
3083 SourceLocation EndLoc;
3084
Reid Spencer5f016e22007-07-11 17:01:13 +00003085 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003086 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003087 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003088 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003089 SourceLocation Loc = Tok.getLocation();
3090
3091 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003092 case tok::code_completion:
3093 Actions.CodeCompleteTypeQualifiers(DS);
3094 ConsumeCodeCompletionToken();
3095 break;
3096
Reid Spencer5f016e22007-07-11 17:01:13 +00003097 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003098 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3099 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003100 break;
3101 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003102 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3103 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003104 break;
3105 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003106 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3107 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003108 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003109
3110 // OpenCL qualifiers:
3111 case tok::kw_private:
3112 if (!getLang().OpenCL)
3113 goto DoneWithTypeQuals;
3114 case tok::kw___private:
3115 case tok::kw___global:
3116 case tok::kw___local:
3117 case tok::kw___constant:
3118 case tok::kw___read_only:
3119 case tok::kw___write_only:
3120 case tok::kw___read_write:
3121 ParseOpenCLQualifiers(DS);
3122 break;
3123
Eli Friedman290eeb02009-06-08 23:27:34 +00003124 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003125 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00003126 case tok::kw___cdecl:
3127 case tok::kw___stdcall:
3128 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003129 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003130 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003131 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003132 continue;
3133 }
3134 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003135 case tok::kw___pascal:
3136 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003137 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003138 continue;
3139 }
3140 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003141 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003142 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003143 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003144 continue; // do *not* consume the next token!
3145 }
3146 // otherwise, FALL THROUGH!
3147 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003148 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003149 // If this is not a type-qualifier token, we're done reading type
3150 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003151 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003152 if (EndLoc.isValid())
3153 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003154 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003155 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003156
Reid Spencer5f016e22007-07-11 17:01:13 +00003157 // If the specifier combination wasn't legal, issue a diagnostic.
3158 if (isInvalid) {
3159 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003160 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003161 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003162 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003163 }
3164}
3165
3166
3167/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3168///
3169void Parser::ParseDeclarator(Declarator &D) {
3170 /// This implements the 'declarator' production in the C grammar, then checks
3171 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003172 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003173}
3174
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003175/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3176/// is parsed by the function passed to it. Pass null, and the direct-declarator
3177/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003178/// ptr-operator production.
3179///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003180/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3181/// [C] pointer[opt] direct-declarator
3182/// [C++] direct-declarator
3183/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003184///
3185/// pointer: [C99 6.7.5]
3186/// '*' type-qualifier-list[opt]
3187/// '*' type-qualifier-list[opt] pointer
3188///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003189/// ptr-operator:
3190/// '*' cv-qualifier-seq[opt]
3191/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003192/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003193/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003194/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003195/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003196void Parser::ParseDeclaratorInternal(Declarator &D,
3197 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003198 if (Diags.hasAllExtensionsSilenced())
3199 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003200
Sebastian Redlf30208a2009-01-24 21:16:55 +00003201 // C++ member pointers start with a '::' or a nested-name.
3202 // Member pointers get special handling, since there's no place for the
3203 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003204 if (getLang().CPlusPlus &&
3205 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3206 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003207 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003208 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003209
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003210 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003211 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003212 // The scope spec really belongs to the direct-declarator.
3213 D.getCXXScopeSpec() = SS;
3214 if (DirectDeclParser)
3215 (this->*DirectDeclParser)(D);
3216 return;
3217 }
3218
3219 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003220 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003221 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003222 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003223 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003224
3225 // Recurse to parse whatever is left.
3226 ParseDeclaratorInternal(D, DirectDeclParser);
3227
3228 // Sema will have to catch (syntactically invalid) pointers into global
3229 // scope. It has to catch pointers into namespace scope anyway.
3230 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003231 Loc),
3232 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003233 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003234 return;
3235 }
3236 }
3237
3238 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003239 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003240 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003241 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003242 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003243 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003244 if (DirectDeclParser)
3245 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003246 return;
3247 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003248
Sebastian Redl05532f22009-03-15 22:02:01 +00003249 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3250 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003251 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003252 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003253
Chris Lattner9af55002009-03-27 04:18:06 +00003254 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003255 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003256 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003257
Reid Spencer5f016e22007-07-11 17:01:13 +00003258 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003259 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003260
Reid Spencer5f016e22007-07-11 17:01:13 +00003261 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003262 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003263 if (Kind == tok::star)
3264 // Remember that we parsed a pointer type, and remember the type-quals.
3265 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003266 DS.getConstSpecLoc(),
3267 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003268 DS.getRestrictSpecLoc()),
3269 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003270 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003271 else
3272 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003273 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003274 Loc),
3275 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003276 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003277 } else {
3278 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003279 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003280
Sebastian Redl743de1f2009-03-23 00:00:23 +00003281 // Complain about rvalue references in C++03, but then go on and build
3282 // the declarator.
3283 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00003284 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003285
Reid Spencer5f016e22007-07-11 17:01:13 +00003286 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3287 // cv-qualifiers are introduced through the use of a typedef or of a
3288 // template type argument, in which case the cv-qualifiers are ignored.
3289 //
3290 // [GNU] Retricted references are allowed.
3291 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003292 // [C++0x] Attributes on references are not allowed.
3293 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003294 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003295
3296 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3297 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3298 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003299 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003300 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3301 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003302 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003303 }
3304
3305 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003306 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003307
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003308 if (D.getNumTypeObjects() > 0) {
3309 // C++ [dcl.ref]p4: There shall be no references to references.
3310 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3311 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003312 if (const IdentifierInfo *II = D.getIdentifier())
3313 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3314 << II;
3315 else
3316 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3317 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003318
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003319 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003320 // can go ahead and build the (technically ill-formed)
3321 // declarator: reference collapsing will take care of it.
3322 }
3323 }
3324
Reid Spencer5f016e22007-07-11 17:01:13 +00003325 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003326 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003327 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003328 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003329 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003330 }
3331}
3332
3333/// ParseDirectDeclarator
3334/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003335/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003336/// '(' declarator ')'
3337/// [GNU] '(' attributes declarator ')'
3338/// [C90] direct-declarator '[' constant-expression[opt] ']'
3339/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3340/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3341/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3342/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3343/// direct-declarator '(' parameter-type-list ')'
3344/// direct-declarator '(' identifier-list[opt] ')'
3345/// [GNU] direct-declarator '(' parameter-forward-declarations
3346/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003347/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3348/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003349/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003350///
3351/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003352/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003353/// '::'[opt] nested-name-specifier[opt] type-name
3354///
3355/// id-expression: [C++ 5.1]
3356/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003357/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003358///
3359/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003360/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003361/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003362/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003363/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003364/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003365///
Reid Spencer5f016e22007-07-11 17:01:13 +00003366void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003367 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003368
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003369 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3370 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003371 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003372 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003373 }
3374
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003375 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003376 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003377 // Change the declaration context for name lookup, until this function
3378 // is exited (and the declarator has been parsed).
3379 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003380 }
3381
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003382 // C++0x [dcl.fct]p14:
3383 // There is a syntactic ambiguity when an ellipsis occurs at the end
3384 // of a parameter-declaration-clause without a preceding comma. In
3385 // this case, the ellipsis is parsed as part of the
3386 // abstract-declarator if the type of the parameter names a template
3387 // parameter pack that has not been expanded; otherwise, it is parsed
3388 // as part of the parameter-declaration-clause.
3389 if (Tok.is(tok::ellipsis) &&
3390 !((D.getContext() == Declarator::PrototypeContext ||
3391 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003392 NextToken().is(tok::r_paren) &&
3393 !Actions.containsUnexpandedParameterPacks(D)))
3394 D.setEllipsisLoc(ConsumeToken());
3395
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003396 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3397 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3398 // We found something that indicates the start of an unqualified-id.
3399 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003400 bool AllowConstructorName;
3401 if (D.getDeclSpec().hasTypeSpecifier())
3402 AllowConstructorName = false;
3403 else if (D.getCXXScopeSpec().isSet())
3404 AllowConstructorName =
3405 (D.getContext() == Declarator::FileContext ||
3406 (D.getContext() == Declarator::MemberContext &&
3407 D.getDeclSpec().isFriendSpecified()));
3408 else
3409 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3410
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003411 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3412 /*EnteringContext=*/true,
3413 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003414 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003415 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003416 D.getName()) ||
3417 // Once we're past the identifier, if the scope was bad, mark the
3418 // whole declarator bad.
3419 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003420 D.SetIdentifier(0, Tok.getLocation());
3421 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003422 } else {
3423 // Parsed the unqualified-id; update range information and move along.
3424 if (D.getSourceRange().getBegin().isInvalid())
3425 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3426 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003427 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003428 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003429 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003430 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003431 assert(!getLang().CPlusPlus &&
3432 "There's a C++-specific check for tok::identifier above");
3433 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3434 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3435 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003436 goto PastIdentifier;
3437 }
3438
3439 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003440 // direct-declarator: '(' declarator ')'
3441 // direct-declarator: '(' attributes declarator ')'
3442 // Example: 'char (*X)' or 'int (*XX)(void)'
3443 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003444
3445 // If the declarator was parenthesized, we entered the declarator
3446 // scope when parsing the parenthesized declarator, then exited
3447 // the scope already. Re-enter the scope, if we need to.
3448 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003449 // If there was an error parsing parenthesized declarator, declarator
3450 // scope may have been enterred before. Don't do it again.
3451 if (!D.isInvalidType() &&
3452 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003453 // Change the declaration context for name lookup, until this function
3454 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003455 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003456 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003457 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003458 // This could be something simple like "int" (in which case the declarator
3459 // portion is empty), if an abstract-declarator is allowed.
3460 D.SetIdentifier(0, Tok.getLocation());
3461 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003462 if (D.getContext() == Declarator::MemberContext)
3463 Diag(Tok, diag::err_expected_member_name_or_semi)
3464 << D.getDeclSpec().getSourceRange();
3465 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003466 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003467 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003468 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003469 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003470 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003471 }
Mike Stump1eb44332009-09-09 15:08:12 +00003472
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003473 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003474 assert(D.isPastIdentifier() &&
3475 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003476
Sean Huntbbd37c62009-11-21 08:43:09 +00003477 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003478 if (D.getIdentifier())
3479 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003480
Reid Spencer5f016e22007-07-11 17:01:13 +00003481 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003482 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003483 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3484 // In such a case, check if we actually have a function declarator; if it
3485 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003486 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3487 // When not in file scope, warn for ambiguous function declarators, just
3488 // in case the author intended it as a variable definition.
3489 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3490 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3491 break;
3492 }
John McCall0b7e6782011-03-24 11:26:52 +00003493 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003494 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00003495 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003496 ParseBracketDeclarator(D);
3497 } else {
3498 break;
3499 }
3500 }
3501}
3502
Chris Lattneref4715c2008-04-06 05:45:57 +00003503/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3504/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003505/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003506/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3507///
3508/// direct-declarator:
3509/// '(' declarator ')'
3510/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003511/// direct-declarator '(' parameter-type-list ')'
3512/// direct-declarator '(' identifier-list[opt] ')'
3513/// [GNU] direct-declarator '(' parameter-forward-declarations
3514/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003515///
3516void Parser::ParseParenDeclarator(Declarator &D) {
3517 SourceLocation StartLoc = ConsumeParen();
3518 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003519
Chris Lattner7399ee02008-10-20 02:05:46 +00003520 // Eat any attributes before we look at whether this is a grouping or function
3521 // declarator paren. If this is a grouping paren, the attribute applies to
3522 // the type being built up, for example:
3523 // int (__attribute__(()) *x)(long y)
3524 // If this ends up not being a grouping paren, the attribute applies to the
3525 // first argument, for example:
3526 // int (__attribute__(()) int x)
3527 // In either case, we need to eat any attributes to be able to determine what
3528 // sort of paren this is.
3529 //
John McCall0b7e6782011-03-24 11:26:52 +00003530 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003531 bool RequiresArg = false;
3532 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003533 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003534
Chris Lattner7399ee02008-10-20 02:05:46 +00003535 // We require that the argument list (if this is a non-grouping paren) be
3536 // present even if the attribute list was empty.
3537 RequiresArg = true;
3538 }
Steve Naroff239f0732008-12-25 14:16:32 +00003539 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003540 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003541 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3542 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall7f040a92010-12-24 02:08:15 +00003543 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003544 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003545 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003546 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003547 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003548
Chris Lattneref4715c2008-04-06 05:45:57 +00003549 // If we haven't past the identifier yet (or where the identifier would be
3550 // stored, if this is an abstract declarator), then this is probably just
3551 // grouping parens. However, if this could be an abstract-declarator, then
3552 // this could also be the start of function arguments (consider 'void()').
3553 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003554
Chris Lattneref4715c2008-04-06 05:45:57 +00003555 if (!D.mayOmitIdentifier()) {
3556 // If this can't be an abstract-declarator, this *must* be a grouping
3557 // paren, because we haven't seen the identifier yet.
3558 isGrouping = true;
3559 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003560 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003561 isDeclarationSpecifier()) { // 'int(int)' is a function.
3562 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3563 // considered to be a type, not a K&R identifier-list.
3564 isGrouping = false;
3565 } else {
3566 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3567 isGrouping = true;
3568 }
Mike Stump1eb44332009-09-09 15:08:12 +00003569
Chris Lattneref4715c2008-04-06 05:45:57 +00003570 // If this is a grouping paren, handle:
3571 // direct-declarator: '(' declarator ')'
3572 // direct-declarator: '(' attributes declarator ')'
3573 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003574 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003575 D.setGroupingParens(true);
3576
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003577 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003578 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003579 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003580 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3581 attrs, EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003582
3583 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003584 return;
3585 }
Mike Stump1eb44332009-09-09 15:08:12 +00003586
Chris Lattneref4715c2008-04-06 05:45:57 +00003587 // Okay, if this wasn't a grouping paren, it must be the start of a function
3588 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003589 // identifier (and remember where it would have been), then call into
3590 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003591 D.SetIdentifier(0, Tok.getLocation());
3592
John McCall7f040a92010-12-24 02:08:15 +00003593 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003594}
3595
3596/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3597/// declarator D up to a paren, which indicates that we are parsing function
3598/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003599///
Chris Lattner7399ee02008-10-20 02:05:46 +00003600/// If AttrList is non-null, then the caller parsed those arguments immediately
3601/// after the open paren - they should be considered to be the first argument of
3602/// a parameter. If RequiresArg is true, then the first argument of the
3603/// function is required to be present and required to not be an identifier
3604/// list.
3605///
Reid Spencer5f016e22007-07-11 17:01:13 +00003606/// This method also handles this portion of the grammar:
3607/// parameter-type-list: [C99 6.7.5]
3608/// parameter-list
3609/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003610/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003611///
3612/// parameter-list: [C99 6.7.5]
3613/// parameter-declaration
3614/// parameter-list ',' parameter-declaration
3615///
3616/// parameter-declaration: [C99 6.7.5]
3617/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003618/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003619/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003620/// declaration-specifiers abstract-declarator[opt]
3621/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003622/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003623/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3624///
Douglas Gregor83f51722011-01-26 03:43:54 +00003625/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3626/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003627///
Sebastian Redl7acafd02011-03-05 14:45:16 +00003628/// [C++0x] exception-specification:
3629/// dynamic-exception-specification
3630/// noexcept-specification
3631///
Chris Lattner7399ee02008-10-20 02:05:46 +00003632void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall7f040a92010-12-24 02:08:15 +00003633 ParsedAttributes &attrs,
Chris Lattner7399ee02008-10-20 02:05:46 +00003634 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003635 // lparen is already consumed!
3636 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003637
Douglas Gregordab60ad2010-10-01 18:44:50 +00003638 ParsedType TrailingReturnType;
3639
Chris Lattner7399ee02008-10-20 02:05:46 +00003640 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003641 if (Tok.is(tok::r_paren)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003642 if (RequiresArg)
Chris Lattner1ab3b962008-11-18 07:48:38 +00003643 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003644
Abramo Bagnara796aa442011-03-12 11:17:06 +00003645 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003646
3647 // cv-qualifier-seq[opt].
John McCall0b7e6782011-03-24 11:26:52 +00003648 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003649 SourceLocation RefQualifierLoc;
3650 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003651 ExceptionSpecificationType ESpecType = EST_None;
3652 SourceRange ESpecRange;
3653 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3654 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3655 ExprResult NoexceptExpr;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003656 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003657 MaybeParseCXX0XAttributes(attrs);
3658
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003659 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003660 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003661 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003662
Douglas Gregor83f51722011-01-26 03:43:54 +00003663 // Parse ref-qualifier[opt]
3664 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3665 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003666 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003667
Douglas Gregor83f51722011-01-26 03:43:54 +00003668 RefQualifierIsLValueRef = Tok.is(tok::amp);
3669 RefQualifierLoc = ConsumeToken();
3670 EndLoc = RefQualifierLoc;
3671 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00003672
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003673 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003674 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3675 DynamicExceptions,
3676 DynamicExceptionRanges,
3677 NoexceptExpr);
3678 if (ESpecType != EST_None)
3679 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003680
3681 // Parse trailing-return-type.
3682 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3683 TrailingReturnType = ParseTrailingReturnType().get();
3684 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003685 }
3686
Chris Lattnerf97409f2008-04-06 06:57:35 +00003687 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003688 // int() -> no prototype, no '...'.
John McCall0b7e6782011-03-24 11:26:52 +00003689 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003690 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003691 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003692 /*arglist*/ 0, 0,
3693 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003694 RefQualifierIsLValueRef,
3695 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003696 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003697 DynamicExceptions.data(),
3698 DynamicExceptionRanges.data(),
3699 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003700 NoexceptExpr.isUsable() ?
3701 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003702 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003703 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00003704 attrs, EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003705 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003706 }
3707
Chris Lattner7399ee02008-10-20 02:05:46 +00003708 // Alternatively, this parameter list may be an identifier list form for a
3709 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003710 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3711 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003712 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003713 // K&R identifier lists can't have typedefs as identifiers, per
3714 // C99 6.7.5.3p11.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003715 if (RequiresArg)
Steve Naroff2d081c42009-01-28 19:16:40 +00003716 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner83a94472010-05-14 17:23:36 +00003717
Steve Naroff2d081c42009-01-28 19:16:40 +00003718 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003719 // normal declarators, not for abstract-declarators. Get the first
3720 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003721 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003722 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003723
3724 // Identifier lists follow a really simple grammar: the identifiers can
3725 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3726 // identifier lists are really rare in the brave new modern world, and it
3727 // is very common for someone to typo a type in a non-k&r style list. If
3728 // we are presented with something like: "void foo(intptr x, float y)",
3729 // we don't want to start parsing the function declarator as though it is
3730 // a K&R style declarator just because intptr is an invalid type.
3731 //
3732 // To handle this, we check to see if the token after the first identifier
3733 // is a "," or ")". Only if so, do we parse it as an identifier list.
3734 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3735 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3736 FirstTok.getIdentifierInfo(),
3737 FirstTok.getLocation(), D);
3738
3739 // If we get here, the code is invalid. Push the first identifier back
3740 // into the token stream and parse the first argument as an (invalid)
3741 // normal argument declarator.
3742 PP.EnterToken(Tok);
3743 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003744 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003745 }
Mike Stump1eb44332009-09-09 15:08:12 +00003746
Chris Lattnerf97409f2008-04-06 06:57:35 +00003747 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003748
Chris Lattnerf97409f2008-04-06 06:57:35 +00003749 // Build up an array of information about the parsed arguments.
3750 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003751
3752 // Enter function-declaration scope, limiting any declarators to the
3753 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003754 ParseScope PrototypeScope(this,
3755 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003756
Chris Lattnerf97409f2008-04-06 06:57:35 +00003757 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003758 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003759 while (1) {
3760 if (Tok.is(tok::ellipsis)) {
3761 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003762 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003763 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003764 }
Mike Stump1eb44332009-09-09 15:08:12 +00003765
Chris Lattnerf97409f2008-04-06 06:57:35 +00003766 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003767 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00003768 DeclSpec DS(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003769
3770 // Skip any Microsoft attributes before a param.
3771 if (getLang().Microsoft && Tok.is(tok::l_square))
3772 ParseMicrosoftAttributes(DS.getAttributes());
3773
3774 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00003775
3776 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00003777 // Take them so that we only apply the attributes to the first parameter.
3778 DS.takeAttributesFrom(attrs);
3779
Chris Lattnere64c5492009-02-27 18:38:20 +00003780 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003781
Chris Lattnerf97409f2008-04-06 06:57:35 +00003782 // Parse the declarator. This is "PrototypeContext", because we must
3783 // accept either 'declarator' or 'abstract-declarator' here.
3784 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3785 ParseDeclarator(ParmDecl);
3786
3787 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00003788 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003789
Chris Lattnerf97409f2008-04-06 06:57:35 +00003790 // Remember this parsed parameter in ParamInfo.
3791 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003792
Douglas Gregor72b505b2008-12-16 21:30:33 +00003793 // DefArgToks is used when the parsing of default arguments needs
3794 // to be delayed.
3795 CachedTokens *DefArgToks = 0;
3796
Chris Lattnerf97409f2008-04-06 06:57:35 +00003797 // If no parameter was specified, verify that *something* was specified,
3798 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003799 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3800 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003801 // Completely missing, emit error.
3802 Diag(DSStart, diag::err_missing_param);
3803 } else {
3804 // Otherwise, we have something. Add it and let semantic analysis try
3805 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003806
Chris Lattnerf97409f2008-04-06 06:57:35 +00003807 // Inform the actions module about the parameter declarator, so it gets
3808 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003809 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003810
3811 // Parse the default argument, if any. We parse the default
3812 // arguments in all dialects; the semantic analysis in
3813 // ActOnParamDefaultArgument will reject the default argument in
3814 // C.
3815 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003816 SourceLocation EqualLoc = Tok.getLocation();
3817
Chris Lattner04421082008-04-08 04:40:51 +00003818 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003819 if (D.getContext() == Declarator::MemberContext) {
3820 // If we're inside a class definition, cache the tokens
3821 // corresponding to the default argument. We'll actually parse
3822 // them when we see the end of the class definition.
3823 // FIXME: Templates will require something similar.
3824 // FIXME: Can we use a smart pointer for Toks?
3825 DefArgToks = new CachedTokens;
3826
Mike Stump1eb44332009-09-09 15:08:12 +00003827 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003828 /*StopAtSemi=*/true,
3829 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003830 delete DefArgToks;
3831 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003832 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003833 } else {
3834 // Mark the end of the default argument so that we know when to
3835 // stop when we parse it later on.
3836 Token DefArgEnd;
3837 DefArgEnd.startToken();
3838 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3839 DefArgEnd.setLocation(Tok.getLocation());
3840 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003841 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003842 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003843 }
Chris Lattner04421082008-04-08 04:40:51 +00003844 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003845 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003846 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003847
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003848 // The argument isn't actually potentially evaluated unless it is
3849 // used.
3850 EnterExpressionEvaluationContext Eval(Actions,
3851 Sema::PotentiallyEvaluatedIfUsed);
3852
John McCall60d7b3a2010-08-24 06:29:42 +00003853 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003854 if (DefArgResult.isInvalid()) {
3855 Actions.ActOnParamDefaultArgumentError(Param);
3856 SkipUntil(tok::comma, tok::r_paren, true, true);
3857 } else {
3858 // Inform the actions module about the default argument
3859 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003860 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003861 }
Chris Lattner04421082008-04-08 04:40:51 +00003862 }
3863 }
Mike Stump1eb44332009-09-09 15:08:12 +00003864
3865 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3866 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003867 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003868 }
3869
3870 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003871 if (Tok.isNot(tok::comma)) {
3872 if (Tok.is(tok::ellipsis)) {
3873 IsVariadic = true;
3874 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3875
3876 if (!getLang().CPlusPlus) {
3877 // We have ellipsis without a preceding ',', which is ill-formed
3878 // in C. Complain and provide the fix.
3879 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003880 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003881 }
3882 }
3883
3884 break;
3885 }
Mike Stump1eb44332009-09-09 15:08:12 +00003886
Chris Lattnerf97409f2008-04-06 06:57:35 +00003887 // Consume the comma.
3888 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003889 }
Mike Stump1eb44332009-09-09 15:08:12 +00003890
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003891 // If we have the closing ')', eat it.
Abramo Bagnara796aa442011-03-12 11:17:06 +00003892 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003893
John McCall0b7e6782011-03-24 11:26:52 +00003894 DeclSpec DS(AttrFactory);
Douglas Gregor83f51722011-01-26 03:43:54 +00003895 SourceLocation RefQualifierLoc;
3896 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003897 ExceptionSpecificationType ESpecType = EST_None;
3898 SourceRange ESpecRange;
3899 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3900 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3901 ExprResult NoexceptExpr;
Sean Huntbbd37c62009-11-21 08:43:09 +00003902
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003903 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003904 MaybeParseCXX0XAttributes(attrs);
3905
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003906 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003907 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003908 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003909 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003910
Douglas Gregor83f51722011-01-26 03:43:54 +00003911 // Parse ref-qualifier[opt]
3912 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3913 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003914 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor83f51722011-01-26 03:43:54 +00003915
3916 RefQualifierIsLValueRef = Tok.is(tok::amp);
3917 RefQualifierLoc = ConsumeToken();
3918 EndLoc = RefQualifierLoc;
3919 }
3920
Sebastian Redl7acafd02011-03-05 14:45:16 +00003921 // FIXME: We should leave the prototype scope before parsing the exception
3922 // specification, and then reenter it when parsing the trailing return type.
3923 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3924
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003925 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003926 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3927 DynamicExceptions,
3928 DynamicExceptionRanges,
3929 NoexceptExpr);
3930 if (ESpecType != EST_None)
3931 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003932
3933 // Parse trailing-return-type.
3934 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3935 TrailingReturnType = ParseTrailingReturnType().get();
3936 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003937 }
3938
Douglas Gregordab60ad2010-10-01 18:44:50 +00003939 // Leave prototype scope.
3940 PrototypeScope.Exit();
3941
Reid Spencer5f016e22007-07-11 17:01:13 +00003942 // Remember that we parsed a function type, and remember the attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003943 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003944 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003945 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003946 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003947 RefQualifierIsLValueRef,
3948 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003949 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003950 DynamicExceptions.data(),
3951 DynamicExceptionRanges.data(),
3952 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003953 NoexceptExpr.isUsable() ?
3954 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003955 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003956 TrailingReturnType),
John McCall0b7e6782011-03-24 11:26:52 +00003957 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003958}
3959
Chris Lattner66d28652008-04-06 06:34:08 +00003960/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3961/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003962/// first identifier has already been consumed, and the current token is the
3963/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003964///
3965/// identifier-list: [C99 6.7.5]
3966/// identifier
3967/// identifier-list ',' identifier
3968///
3969void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003970 IdentifierInfo *FirstIdent,
3971 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003972 Declarator &D) {
3973 // Build up an array of information about the parsed arguments.
3974 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3975 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003976
Chris Lattner66d28652008-04-06 06:34:08 +00003977 // If there was no identifier specified for the declarator, either we are in
3978 // an abstract-declarator, or we are in a parameter declarator which was found
3979 // to be abstract. In abstract-declarators, identifier lists are not valid:
3980 // diagnose this.
3981 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003982 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003983
Chris Lattner83a94472010-05-14 17:23:36 +00003984 // The first identifier was already read, and is known to be the first
3985 // identifier in the list. Remember this identifier in ParamInfo.
3986 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00003987 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00003988
Chris Lattner66d28652008-04-06 06:34:08 +00003989 while (Tok.is(tok::comma)) {
3990 // Eat the comma.
3991 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003992
Chris Lattner50c64772008-04-06 06:39:19 +00003993 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003994 if (Tok.isNot(tok::identifier)) {
3995 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003996 SkipUntil(tok::r_paren);
3997 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003998 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003999
Chris Lattner66d28652008-04-06 06:34:08 +00004000 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00004001
4002 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00004003 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00004004 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00004005
Chris Lattner66d28652008-04-06 06:34:08 +00004006 // Verify that the argument identifier has not already been mentioned.
4007 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004008 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00004009 } else {
4010 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00004011 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00004012 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00004013 0));
Chris Lattner50c64772008-04-06 06:39:19 +00004014 }
Mike Stump1eb44332009-09-09 15:08:12 +00004015
Chris Lattner66d28652008-04-06 06:34:08 +00004016 // Eat the identifier.
4017 ConsumeToken();
4018 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004019
4020 // If we have the closing ')', eat it and we're done.
4021 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4022
Chris Lattner50c64772008-04-06 06:39:19 +00004023 // Remember that we parsed a function type, and remember the attributes. This
4024 // function type is always a K&R style function type, which is not varargs and
4025 // has no prototype.
John McCall0b7e6782011-03-24 11:26:52 +00004026 ParsedAttributes attrs(AttrFactory);
4027 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00004028 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00004029 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00004030 /*TypeQuals*/0,
Douglas Gregor83f51722011-01-26 03:43:54 +00004031 true, SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00004032 EST_None, SourceLocation(), 0, 0,
4033 0, 0, LParenLoc, RLoc, D),
John McCall0b7e6782011-03-24 11:26:52 +00004034 attrs, RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00004035}
Chris Lattneref4715c2008-04-06 05:45:57 +00004036
Reid Spencer5f016e22007-07-11 17:01:13 +00004037/// [C90] direct-declarator '[' constant-expression[opt] ']'
4038/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4039/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4040/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4041/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4042void Parser::ParseBracketDeclarator(Declarator &D) {
4043 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00004044
Chris Lattner378c7e42008-12-18 07:27:21 +00004045 // C array syntax has many features, but by-far the most common is [] and [4].
4046 // This code does a fast path to handle some of the most obvious cases.
4047 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00004048 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004049 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004050 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004051
Chris Lattner378c7e42008-12-18 07:27:21 +00004052 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004053 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004054 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004055 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004056 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004057 return;
4058 } else if (Tok.getKind() == tok::numeric_constant &&
4059 GetLookAheadToken(1).is(tok::r_square)) {
4060 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004061 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004062 ConsumeToken();
4063
Sebastian Redlab197ba2009-02-09 18:23:29 +00004064 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004065 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004066 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004067
Chris Lattner378c7e42008-12-18 07:27:21 +00004068 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004069 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004070 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004071 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004072 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004073 return;
4074 }
Mike Stump1eb44332009-09-09 15:08:12 +00004075
Reid Spencer5f016e22007-07-11 17:01:13 +00004076 // If valid, this location is the position where we read the 'static' keyword.
4077 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004078 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004079 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004080
Reid Spencer5f016e22007-07-11 17:01:13 +00004081 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004082 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004083 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004084 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004085
Reid Spencer5f016e22007-07-11 17:01:13 +00004086 // If we haven't already read 'static', check to see if there is one after the
4087 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004088 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004089 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004090
Reid Spencer5f016e22007-07-11 17:01:13 +00004091 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4092 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004093 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004094
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004095 // Handle the case where we have '[*]' as the array size. However, a leading
4096 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4097 // the the token after the star is a ']'. Since stars in arrays are
4098 // infrequent, use of lookahead is not costly here.
4099 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004100 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004101
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004102 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004103 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004104 StaticLoc = SourceLocation(); // Drop the static.
4105 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004106 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004107 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004108 // Note, in C89, this production uses the constant-expr production instead
4109 // of assignment-expr. The only difference is that assignment-expr allows
4110 // things like '=' and '*='. Sema rejects these in C89 mode because they
4111 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004112
Douglas Gregore0762c92009-06-19 23:52:42 +00004113 // Parse the constant-expression or assignment-expression now (depending
4114 // on dialect).
4115 if (getLang().CPlusPlus)
4116 NumElements = ParseConstantExpression();
4117 else
4118 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004119 }
Mike Stump1eb44332009-09-09 15:08:12 +00004120
Reid Spencer5f016e22007-07-11 17:01:13 +00004121 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004122 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004123 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004124 // If the expression was invalid, skip it.
4125 SkipUntil(tok::r_square);
4126 return;
4127 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004128
4129 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4130
John McCall0b7e6782011-03-24 11:26:52 +00004131 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004132 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004133
Chris Lattner378c7e42008-12-18 07:27:21 +00004134 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004135 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004136 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004137 NumElements.release(),
4138 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004139 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004140}
4141
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004142/// [GNU] typeof-specifier:
4143/// typeof ( expressions )
4144/// typeof ( type-name )
4145/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004146///
4147void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004148 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004149 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004150 SourceLocation StartLoc = ConsumeToken();
4151
John McCallcfb708c2010-01-13 20:03:27 +00004152 const bool hasParens = Tok.is(tok::l_paren);
4153
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004154 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004155 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004156 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004157 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4158 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004159 if (hasParens)
4160 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004161
4162 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004163 // FIXME: Not accurate, the range gets one token more than it should.
4164 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004165 else
4166 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004167
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004168 if (isCastExpr) {
4169 if (!CastTy) {
4170 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004171 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004172 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004173
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004174 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004175 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004176 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4177 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004178 DiagID, CastTy))
4179 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004180 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004181 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004182
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004183 // If we get here, the operand to the typeof was an expresion.
4184 if (Operand.isInvalid()) {
4185 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004186 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004187 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004188
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004189 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004190 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004191 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4192 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004193 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004194 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004195}
Chris Lattner1b492422010-02-28 18:33:55 +00004196
4197
4198/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4199/// from TryAltiVecVectorToken.
4200bool Parser::TryAltiVecVectorTokenOutOfLine() {
4201 Token Next = NextToken();
4202 switch (Next.getKind()) {
4203 default: return false;
4204 case tok::kw_short:
4205 case tok::kw_long:
4206 case tok::kw_signed:
4207 case tok::kw_unsigned:
4208 case tok::kw_void:
4209 case tok::kw_char:
4210 case tok::kw_int:
4211 case tok::kw_float:
4212 case tok::kw_double:
4213 case tok::kw_bool:
4214 case tok::kw___pixel:
4215 Tok.setKind(tok::kw___vector);
4216 return true;
4217 case tok::identifier:
4218 if (Next.getIdentifierInfo() == Ident_pixel) {
4219 Tok.setKind(tok::kw___vector);
4220 return true;
4221 }
4222 return false;
4223 }
4224}
4225
4226bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4227 const char *&PrevSpec, unsigned &DiagID,
4228 bool &isInvalid) {
4229 if (Tok.getIdentifierInfo() == Ident_vector) {
4230 Token Next = NextToken();
4231 switch (Next.getKind()) {
4232 case tok::kw_short:
4233 case tok::kw_long:
4234 case tok::kw_signed:
4235 case tok::kw_unsigned:
4236 case tok::kw_void:
4237 case tok::kw_char:
4238 case tok::kw_int:
4239 case tok::kw_float:
4240 case tok::kw_double:
4241 case tok::kw_bool:
4242 case tok::kw___pixel:
4243 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4244 return true;
4245 case tok::identifier:
4246 if (Next.getIdentifierInfo() == Ident_pixel) {
4247 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4248 return true;
4249 }
4250 break;
4251 default:
4252 break;
4253 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004254 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004255 DS.isTypeAltiVecVector()) {
4256 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4257 return true;
4258 }
4259 return false;
4260}