blob: 3c692cc57710b485427bd278ea236a2ea2ee3d0a [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000021#include "llvm/ADT/SmallSet.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000022#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.7: Declarations.
27//===----------------------------------------------------------------------===//
28
Chris Lattnerf5fbd792006-08-10 23:56:11 +000029/// ParseTypeName
30/// type-name: [C99 6.7.6]
31/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000032///
33/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000034TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000035 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000036 ObjCDeclSpec *objcQuals,
37 AccessSpecifier AS,
38 Decl **OwnedType) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000039 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000040 DeclSpec DS(AttrFactory);
John McCall31168b02011-06-15 23:02:42 +000041 DS.setObjCQualifiers(objcQuals);
Richard Smithcd1c0552011-07-01 19:46:12 +000042 ParseSpecifierQualifierList(DS, AS);
43 if (OwnedType)
44 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000045
Chris Lattnerf5fbd792006-08-10 23:56:11 +000046 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000047 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000048 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000049 if (Range)
50 *Range = DeclaratorInfo.getSourceRange();
51
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000052 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000053 return true;
54
Douglas Gregor0be31a22010-07-02 17:43:08 +000055 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000056}
57
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000058
59/// isAttributeLateParsed - Return true if the attribute has arguments that
60/// require late parsing.
61static bool isAttributeLateParsed(const IdentifierInfo &II) {
62 return llvm::StringSwitch<bool>(II.getName())
63#include "clang/Parse/AttrLateParsed.inc"
64 .Default(false);
65}
66
67
Alexis Hunt96d5c762009-11-21 08:43:09 +000068/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000069///
70/// [GNU] attributes:
71/// attribute
72/// attributes attribute
73///
74/// [GNU] attribute:
75/// '__attribute__' '(' '(' attribute-list ')' ')'
76///
77/// [GNU] attribute-list:
78/// attrib
79/// attribute_list ',' attrib
80///
81/// [GNU] attrib:
82/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000083/// attrib-name
84/// attrib-name '(' identifier ')'
85/// attrib-name '(' identifier ',' nonempty-expr-list ')'
86/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000087///
Steve Naroff0f2fe172007-06-01 17:11:19 +000088/// [GNU] attrib-name:
89/// identifier
90/// typespec
91/// typequal
92/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000093///
Steve Naroff0f2fe172007-06-01 17:11:19 +000094/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000095/// token lookahead. Comment from gcc: "If they start with an identifier
96/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +000097/// start with that identifier; otherwise they are an expression list."
98///
99/// At the moment, I am not doing 2 token lookahead. I am also unaware of
100/// any attributes that don't work (based on my limited testing). Most
101/// attributes are very simple in practice. Until we find a bug, I don't see
102/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000103
John McCall53fa7142010-12-24 02:08:15 +0000104void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000105 SourceLocation *endLoc,
106 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000107 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000108
Chris Lattner76c72282007-10-09 17:33:22 +0000109 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000110 ConsumeToken();
111 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
112 "attribute")) {
113 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000114 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000115 }
116 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
117 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000118 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000119 }
120 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000121 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
122 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000123 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000124 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
125 ConsumeToken();
126 continue;
127 }
128 // we have an identifier or declaration specifier (const, int, etc.)
129 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
130 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000131
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000132 if (Tok.is(tok::l_paren)) {
133 // handle "parameterized" attributes
134 if (LateAttrs && !ClassStack.empty() &&
135 isAttributeLateParsed(*AttrName)) {
136 // Delayed parsing is only available for attributes that occur
137 // in certain locations within a class scope.
138 LateParsedAttribute *LA =
139 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
140 LateAttrs->push_back(LA);
141 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000142
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000143 // consume everything up to and including the matching right parens
144 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000145
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000146 Token Eof;
147 Eof.startToken();
148 Eof.setLocation(Tok.getLocation());
149 LA->Toks.push_back(Eof);
150 } else {
151 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000152 }
153 } else {
John McCall084e83d2011-03-24 11:26:52 +0000154 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
155 0, SourceLocation(), 0, 0);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000156 }
157 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000158 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000159 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000160 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000161 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
162 SkipUntil(tok::r_paren, false);
163 }
John McCall53fa7142010-12-24 02:08:15 +0000164 if (endLoc)
165 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000166 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000167}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000168
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000169
170/// Parse the arguments to a parameterized GNU attribute
171void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
172 SourceLocation AttrNameLoc,
173 ParsedAttributes &Attrs,
174 SourceLocation *EndLoc) {
175
176 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
177
178 // Availability attributes have their own grammar.
179 if (AttrName->isStr("availability")) {
180 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
181 return;
182 }
183 // Thread safety attributes fit into the FIXME case above, so we
184 // just parse the arguments as a list of expressions
185 if (IsThreadSafetyAttribute(AttrName->getName())) {
186 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
187 return;
188 }
189
190 ConsumeParen(); // ignore the left paren loc for now
191
192 if (Tok.is(tok::identifier)) {
193 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
194 SourceLocation ParmLoc = ConsumeToken();
195
196 if (Tok.is(tok::r_paren)) {
197 // __attribute__(( mode(byte) ))
198 ConsumeParen(); // ignore the right paren loc for now
199 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
200 ParmName, ParmLoc, 0, 0);
201 } else if (Tok.is(tok::comma)) {
202 ConsumeToken();
203 // __attribute__(( format(printf, 1, 2) ))
204 ExprVector ArgExprs(Actions);
205 bool ArgExprsOk = true;
206
207 // now parse the non-empty comma separated list of expressions
208 while (1) {
209 ExprResult ArgExpr(ParseAssignmentExpression());
210 if (ArgExpr.isInvalid()) {
211 ArgExprsOk = false;
212 SkipUntil(tok::r_paren);
213 break;
214 } else {
215 ArgExprs.push_back(ArgExpr.release());
216 }
217 if (Tok.isNot(tok::comma))
218 break;
219 ConsumeToken(); // Eat the comma, move to the next argument
220 }
221 if (ArgExprsOk && Tok.is(tok::r_paren)) {
222 ConsumeParen(); // ignore the right paren loc for now
223 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
224 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
225 }
226 }
227 } else { // not an identifier
228 switch (Tok.getKind()) {
229 case tok::r_paren:
230 // parse a possibly empty comma separated list of expressions
231 // __attribute__(( nonnull() ))
232 ConsumeParen(); // ignore the right paren loc for now
233 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
234 0, SourceLocation(), 0, 0);
235 break;
236 case tok::kw_char:
237 case tok::kw_wchar_t:
238 case tok::kw_char16_t:
239 case tok::kw_char32_t:
240 case tok::kw_bool:
241 case tok::kw_short:
242 case tok::kw_int:
243 case tok::kw_long:
244 case tok::kw___int64:
245 case tok::kw_signed:
246 case tok::kw_unsigned:
247 case tok::kw_float:
248 case tok::kw_double:
249 case tok::kw_void:
250 case tok::kw_typeof: {
251 AttributeList *attr
252 = Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
253 0, SourceLocation(), 0, 0);
254 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
255 Diag(Tok, diag::err_iboutletcollection_builtintype);
256 // If it's a builtin type name, eat it and expect a rparen
257 // __attribute__(( vec_type_hint(char) ))
258 ConsumeToken();
259 if (Tok.is(tok::r_paren))
260 ConsumeParen();
261 break;
262 }
263 default:
264 // __attribute__(( aligned(16) ))
265 ExprVector ArgExprs(Actions);
266 bool ArgExprsOk = true;
267
268 // now parse the list of expressions
269 while (1) {
270 ExprResult ArgExpr(ParseAssignmentExpression());
271 if (ArgExpr.isInvalid()) {
272 ArgExprsOk = false;
273 SkipUntil(tok::r_paren);
274 break;
275 } else {
276 ArgExprs.push_back(ArgExpr.release());
277 }
278 if (Tok.isNot(tok::comma))
279 break;
280 ConsumeToken(); // Eat the comma, move to the next argument
281 }
282 // Match the ')'.
283 if (ArgExprsOk && Tok.is(tok::r_paren)) {
284 ConsumeParen(); // ignore the right paren loc for now
285 Attrs.addNew(AttrName, AttrNameLoc, 0,
286 AttrNameLoc, 0, SourceLocation(),
287 ArgExprs.take(), ArgExprs.size());
288 }
289 break;
290 }
291 }
292}
293
294
Eli Friedman06de2b52009-06-08 07:21:15 +0000295/// ParseMicrosoftDeclSpec - Parse an __declspec construct
296///
297/// [MS] decl-specifier:
298/// __declspec ( extended-decl-modifier-seq )
299///
300/// [MS] extended-decl-modifier-seq:
301/// extended-decl-modifier[opt]
302/// extended-decl-modifier extended-decl-modifier-seq
303
John McCall53fa7142010-12-24 02:08:15 +0000304void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000305 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000306
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000307 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000308 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
309 "declspec")) {
310 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000311 return;
Eli Friedman06de2b52009-06-08 07:21:15 +0000312 }
Francois Pichetdcf88932011-05-07 19:04:49 +0000313
Eli Friedman53339e02009-06-08 23:27:34 +0000314 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000315 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
316 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichetdcf88932011-05-07 19:04:49 +0000317
318 // FIXME: Remove this when we have proper __declspec(property()) support.
319 // Just skip everything inside property().
320 if (AttrName->getName() == "property") {
321 ConsumeParen();
322 SkipUntil(tok::r_paren);
323 }
Eli Friedman06de2b52009-06-08 07:21:15 +0000324 if (Tok.is(tok::l_paren)) {
325 ConsumeParen();
326 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
327 // correctly.
John McCalldadc5752010-08-24 06:29:42 +0000328 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedman06de2b52009-06-08 07:21:15 +0000329 if (!ArgExpr.isInvalid()) {
John McCall37ad5512010-08-23 06:44:23 +0000330 Expr *ExprList = ArgExpr.take();
John McCall084e83d2011-03-24 11:26:52 +0000331 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
332 SourceLocation(), &ExprList, 1, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000333 }
334 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
335 SkipUntil(tok::r_paren, false);
336 } else {
John McCall084e83d2011-03-24 11:26:52 +0000337 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
338 0, SourceLocation(), 0, 0, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000339 }
340 }
341 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
342 SkipUntil(tok::r_paren, false);
John McCall53fa7142010-12-24 02:08:15 +0000343 return;
Eli Friedman53339e02009-06-08 23:27:34 +0000344}
345
John McCall53fa7142010-12-24 02:08:15 +0000346void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000347 // Treat these like attributes
348 // FIXME: Allow Sema to distinguish between these and real attributes!
349 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000350 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000351 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +0000352 Tok.is(tok::kw___ptr32) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000353 Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000354 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
355 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichetf2fb4112011-08-25 00:36:46 +0000356 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
357 Tok.is(tok::kw___ptr32))
Eli Friedman53339e02009-06-08 23:27:34 +0000358 // FIXME: Support these properly!
359 continue;
John McCall084e83d2011-03-24 11:26:52 +0000360 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
361 SourceLocation(), 0, 0, true);
Eli Friedman53339e02009-06-08 23:27:34 +0000362 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000363}
364
John McCall53fa7142010-12-24 02:08:15 +0000365void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000366 // Treat these like attributes
367 while (Tok.is(tok::kw___pascal)) {
368 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
369 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000370 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
371 SourceLocation(), 0, 0, true);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000372 }
John McCall53fa7142010-12-24 02:08:15 +0000373}
374
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000375void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
376 // Treat these like attributes
377 while (Tok.is(tok::kw___kernel)) {
378 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000379 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
380 AttrNameLoc, 0, AttrNameLoc, 0,
381 SourceLocation(), 0, 0, false);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000382 }
383}
384
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000385void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
386 SourceLocation Loc = Tok.getLocation();
387 switch(Tok.getKind()) {
388 // OpenCL qualifiers:
389 case tok::kw___private:
390 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000391 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000392 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000393 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000394 break;
395
396 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000397 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000398 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000399 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000400 break;
401
402 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000403 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000404 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000405 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000406 break;
407
408 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000409 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000410 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000411 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000412 break;
413
414 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000415 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000416 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000417 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000418 break;
419
420 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000421 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000422 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000423 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000424 break;
425
426 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000427 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000428 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000429 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000430 break;
431 default: break;
432 }
433}
434
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000435/// \brief Parse a version number.
436///
437/// version:
438/// simple-integer
439/// simple-integer ',' simple-integer
440/// simple-integer ',' simple-integer ',' simple-integer
441VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
442 Range = Tok.getLocation();
443
444 if (!Tok.is(tok::numeric_constant)) {
445 Diag(Tok, diag::err_expected_version);
446 SkipUntil(tok::comma, tok::r_paren, true, true, true);
447 return VersionTuple();
448 }
449
450 // Parse the major (and possibly minor and subminor) versions, which
451 // are stored in the numeric constant. We utilize a quirk of the
452 // lexer, which is that it handles something like 1.2.3 as a single
453 // numeric constant, rather than two separate tokens.
454 llvm::SmallString<512> Buffer;
455 Buffer.resize(Tok.getLength()+1);
456 const char *ThisTokBegin = &Buffer[0];
457
458 // Get the spelling of the token, which eliminates trigraphs, etc.
459 bool Invalid = false;
460 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
461 if (Invalid)
462 return VersionTuple();
463
464 // Parse the major version.
465 unsigned AfterMajor = 0;
466 unsigned Major = 0;
467 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
468 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
469 ++AfterMajor;
470 }
471
472 if (AfterMajor == 0) {
473 Diag(Tok, diag::err_expected_version);
474 SkipUntil(tok::comma, tok::r_paren, true, true, true);
475 return VersionTuple();
476 }
477
478 if (AfterMajor == ActualLength) {
479 ConsumeToken();
480
481 // We only had a single version component.
482 if (Major == 0) {
483 Diag(Tok, diag::err_zero_version);
484 return VersionTuple();
485 }
486
487 return VersionTuple(Major);
488 }
489
490 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
491 Diag(Tok, diag::err_expected_version);
492 SkipUntil(tok::comma, tok::r_paren, true, true, true);
493 return VersionTuple();
494 }
495
496 // Parse the minor version.
497 unsigned AfterMinor = AfterMajor + 1;
498 unsigned Minor = 0;
499 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
500 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
501 ++AfterMinor;
502 }
503
504 if (AfterMinor == ActualLength) {
505 ConsumeToken();
506
507 // We had major.minor.
508 if (Major == 0 && Minor == 0) {
509 Diag(Tok, diag::err_zero_version);
510 return VersionTuple();
511 }
512
513 return VersionTuple(Major, Minor);
514 }
515
516 // If what follows is not a '.', we have a problem.
517 if (ThisTokBegin[AfterMinor] != '.') {
518 Diag(Tok, diag::err_expected_version);
519 SkipUntil(tok::comma, tok::r_paren, true, true, true);
520 return VersionTuple();
521 }
522
523 // Parse the subminor version.
524 unsigned AfterSubminor = AfterMinor + 1;
525 unsigned Subminor = 0;
526 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
527 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
528 ++AfterSubminor;
529 }
530
531 if (AfterSubminor != ActualLength) {
532 Diag(Tok, diag::err_expected_version);
533 SkipUntil(tok::comma, tok::r_paren, true, true, true);
534 return VersionTuple();
535 }
536 ConsumeToken();
537 return VersionTuple(Major, Minor, Subminor);
538}
539
540/// \brief Parse the contents of the "availability" attribute.
541///
542/// availability-attribute:
543/// 'availability' '(' platform ',' version-arg-list ')'
544///
545/// platform:
546/// identifier
547///
548/// version-arg-list:
549/// version-arg
550/// version-arg ',' version-arg-list
551///
552/// version-arg:
553/// 'introduced' '=' version
554/// 'deprecated' '=' version
555/// 'removed' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000556/// 'unavailable'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000557void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
558 SourceLocation AvailabilityLoc,
559 ParsedAttributes &attrs,
560 SourceLocation *endLoc) {
561 SourceLocation PlatformLoc;
562 IdentifierInfo *Platform = 0;
563
564 enum { Introduced, Deprecated, Obsoleted, Unknown };
565 AvailabilityChange Changes[Unknown];
566
567 // Opening '('.
568 SourceLocation LParenLoc;
569 if (!Tok.is(tok::l_paren)) {
570 Diag(Tok, diag::err_expected_lparen);
571 return;
572 }
573 LParenLoc = ConsumeParen();
574
575 // Parse the platform name,
576 if (Tok.isNot(tok::identifier)) {
577 Diag(Tok, diag::err_availability_expected_platform);
578 SkipUntil(tok::r_paren);
579 return;
580 }
581 Platform = Tok.getIdentifierInfo();
582 PlatformLoc = ConsumeToken();
583
584 // Parse the ',' following the platform name.
585 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
586 return;
587
588 // If we haven't grabbed the pointers for the identifiers
589 // "introduced", "deprecated", and "obsoleted", do so now.
590 if (!Ident_introduced) {
591 Ident_introduced = PP.getIdentifierInfo("introduced");
592 Ident_deprecated = PP.getIdentifierInfo("deprecated");
593 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000594 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000595 }
596
597 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000598 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000599 do {
600 if (Tok.isNot(tok::identifier)) {
601 Diag(Tok, diag::err_availability_expected_change);
602 SkipUntil(tok::r_paren);
603 return;
604 }
605 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
606 SourceLocation KeywordLoc = ConsumeToken();
607
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000608 if (Keyword == Ident_unavailable) {
609 if (UnavailableLoc.isValid()) {
610 Diag(KeywordLoc, diag::err_availability_redundant)
611 << Keyword << SourceRange(UnavailableLoc);
612 }
613 UnavailableLoc = KeywordLoc;
614
615 if (Tok.isNot(tok::comma))
616 break;
617
618 ConsumeToken();
619 continue;
620 }
621
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000622 if (Tok.isNot(tok::equal)) {
623 Diag(Tok, diag::err_expected_equal_after)
624 << Keyword;
625 SkipUntil(tok::r_paren);
626 return;
627 }
628 ConsumeToken();
629
630 SourceRange VersionRange;
631 VersionTuple Version = ParseVersionTuple(VersionRange);
632
633 if (Version.empty()) {
634 SkipUntil(tok::r_paren);
635 return;
636 }
637
638 unsigned Index;
639 if (Keyword == Ident_introduced)
640 Index = Introduced;
641 else if (Keyword == Ident_deprecated)
642 Index = Deprecated;
643 else if (Keyword == Ident_obsoleted)
644 Index = Obsoleted;
645 else
646 Index = Unknown;
647
648 if (Index < Unknown) {
649 if (!Changes[Index].KeywordLoc.isInvalid()) {
650 Diag(KeywordLoc, diag::err_availability_redundant)
651 << Keyword
652 << SourceRange(Changes[Index].KeywordLoc,
653 Changes[Index].VersionRange.getEnd());
654 }
655
656 Changes[Index].KeywordLoc = KeywordLoc;
657 Changes[Index].Version = Version;
658 Changes[Index].VersionRange = VersionRange;
659 } else {
660 Diag(KeywordLoc, diag::err_availability_unknown_change)
661 << Keyword << VersionRange;
662 }
663
664 if (Tok.isNot(tok::comma))
665 break;
666
667 ConsumeToken();
668 } while (true);
669
670 // Closing ')'.
671 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
672 if (RParenLoc.isInvalid())
673 return;
674
675 if (endLoc)
676 *endLoc = RParenLoc;
677
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000678 // The 'unavailable' availability cannot be combined with any other
679 // availability changes. Make sure that hasn't happened.
680 if (UnavailableLoc.isValid()) {
681 bool Complained = false;
682 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
683 if (Changes[Index].KeywordLoc.isValid()) {
684 if (!Complained) {
685 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
686 << SourceRange(Changes[Index].KeywordLoc,
687 Changes[Index].VersionRange.getEnd());
688 Complained = true;
689 }
690
691 // Clear out the availability.
692 Changes[Index] = AvailabilityChange();
693 }
694 }
695 }
696
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000697 // Record this attribute
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000698 attrs.addNew(&Availability, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000699 0, SourceLocation(),
700 Platform, PlatformLoc,
701 Changes[Introduced],
702 Changes[Deprecated],
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000703 Changes[Obsoleted],
704 UnavailableLoc, false, false);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000705}
706
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000707
708// Late Parsed Attributes:
709// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
710
711void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
712
713void Parser::LateParsedClass::ParseLexedAttributes() {
714 Self->ParseLexedAttributes(*Class);
715}
716
717void Parser::LateParsedAttribute::ParseLexedAttributes() {
718 Self->ParseLexedAttribute(*this);
719}
720
721/// Wrapper class which calls ParseLexedAttribute, after setting up the
722/// scope appropriately.
723void Parser::ParseLexedAttributes(ParsingClass &Class) {
724 // Deal with templates
725 // FIXME: Test cases to make sure this does the right thing for templates.
726 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
727 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
728 HasTemplateScope);
729 if (HasTemplateScope)
730 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
731
732 // Set or update the scope flags to include Scope::ThisScope.
733 bool AlreadyHasClassScope = Class.TopLevelClass;
734 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope;
735 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
736 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
737
738 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
739 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
740 }
741}
742
743/// \brief Finish parsing an attribute for which parsing was delayed.
744/// This will be called at the end of parsing a class declaration
745/// for each LateParsedAttribute. We consume the saved tokens and
746/// create an attribute with the arguments filled in. We add this
747/// to the Attribute list for the decl.
748void Parser::ParseLexedAttribute(LateParsedAttribute &LA) {
749 // Save the current token position.
750 SourceLocation OrigLoc = Tok.getLocation();
751
752 // Append the current token at the end of the new token stream so that it
753 // doesn't get lost.
754 LA.Toks.push_back(Tok);
755 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
756 // Consume the previously pushed token.
757 ConsumeAnyToken();
758
759 ParsedAttributes Attrs(AttrFactory);
760 SourceLocation endLoc;
761
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000762 // If the Decl is templatized, add template parameters to scope.
763 bool HasTemplateScope = LA.D && LA.D->isTemplateDecl();
764 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
765 if (HasTemplateScope)
766 Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D);
767
768 // If the Decl is on a function, add function parameters to the scope.
769 bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate();
770 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
771 if (HasFunctionScope)
772 Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D);
773
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000774 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
775
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000776 if (HasFunctionScope) {
777 Actions.ActOnExitFunctionContext();
778 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
779 }
780 if (HasTemplateScope) {
781 TempScope.Exit();
782 }
783
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000784 // Late parsed attributes must be attached to Decls by hand. If the
785 // LA.D is not set, then this was not done properly.
786 assert(LA.D && "No decl attached to late parsed attribute");
787 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs);
788
789 if (Tok.getLocation() != OrigLoc) {
790 // Due to a parsing error, we either went over the cached tokens or
791 // there are still cached tokens left, so we skip the leftover tokens.
792 // Since this is an uncommon situation that should be avoided, use the
793 // expensive isBeforeInTranslationUnit call.
794 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
795 OrigLoc))
796 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
797 ConsumeAnyToken();
798 }
799}
800
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000801/// \brief Wrapper around a case statement checking if AttrName is
802/// one of the thread safety attributes
803bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
804 return llvm::StringSwitch<bool>(AttrName)
805 .Case("guarded_by", true)
806 .Case("guarded_var", true)
807 .Case("pt_guarded_by", true)
808 .Case("pt_guarded_var", true)
809 .Case("lockable", true)
810 .Case("scoped_lockable", true)
811 .Case("no_thread_safety_analysis", true)
812 .Case("acquired_after", true)
813 .Case("acquired_before", true)
814 .Case("exclusive_lock_function", true)
815 .Case("shared_lock_function", true)
816 .Case("exclusive_trylock_function", true)
817 .Case("shared_trylock_function", true)
818 .Case("unlock_function", true)
819 .Case("lock_returned", true)
820 .Case("locks_excluded", true)
821 .Case("exclusive_locks_required", true)
822 .Case("shared_locks_required", true)
823 .Default(false);
824}
825
826/// \brief Parse the contents of thread safety attributes. These
827/// should always be parsed as an expression list.
828///
829/// We need to special case the parsing due to the fact that if the first token
830/// of the first argument is an identifier, the main parse loop will store
831/// that token as a "parameter" and the rest of
832/// the arguments will be added to a list of "arguments". However,
833/// subsequent tokens in the first argument are lost. We instead parse each
834/// argument as an expression and add all arguments to the list of "arguments".
835/// In future, we will take advantage of this special case to also
836/// deal with some argument scoping issues here (for example, referring to a
837/// function parameter in the attribute on that function).
838void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
839 SourceLocation AttrNameLoc,
840 ParsedAttributes &Attrs,
841 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000842 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000843
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000844 SourceLocation LeftParenLoc = Tok.getLocation();
845 ConsumeParen();
846
847 ExprVector ArgExprs(Actions);
848 bool ArgExprsOk = true;
849
850 // now parse the list of expressions
851 while (1) {
852 ExprResult ArgExpr(ParseAssignmentExpression());
853 if (ArgExpr.isInvalid()) {
854 ArgExprsOk = false;
855 MatchRHSPunctuation(tok::r_paren, LeftParenLoc);
856 break;
857 } else {
858 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000859 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000860 if (Tok.isNot(tok::comma))
861 break;
862 ConsumeToken(); // Eat the comma, move to the next argument
863 }
864 // Match the ')'.
865 if (ArgExprsOk && Tok.is(tok::r_paren)) {
866 if (EndLoc)
867 *EndLoc = Tok.getLocation();
868 ConsumeParen();
869 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
870 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000871 }
872}
873
John McCall53fa7142010-12-24 02:08:15 +0000874void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
875 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
876 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000877}
878
Chris Lattner53361ac2006-08-10 05:19:57 +0000879/// ParseDeclaration - Parse a full 'declaration', which consists of
880/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000881/// 'Context' should be a Declarator::TheContext value. This returns the
882/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000883///
884/// declaration: [C99 6.7]
885/// block-declaration ->
886/// simple-declaration
887/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000888/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000889/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000890/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000891/// [C++] using-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000892/// [C++0x/C1X] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000893/// others... [FIXME]
894///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000895Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
896 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000897 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000898 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000899 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +0000900 // Must temporarily exit the objective-c container scope for
901 // parsing c none objective-c decls.
902 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000903
John McCall48871652010-08-21 09:40:31 +0000904 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +0000905 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000906 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000907 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000908 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +0000909 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000910 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000911 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000912 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000913 // Could be the start of an inline namespace. Allowed as an ext in C++03.
914 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +0000915 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +0000916 SourceLocation InlineLoc = ConsumeToken();
917 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
918 break;
919 }
John McCall53fa7142010-12-24 02:08:15 +0000920 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000921 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000922 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +0000923 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000924 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000925 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000926 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +0000927 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +0000928 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000929 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000930 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000931 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +0000932 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000933 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000934 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000935 default:
John McCall53fa7142010-12-24 02:08:15 +0000936 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000937 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000938
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000939 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +0000940 // single decl, convert it now. Alias declarations can also declare a type;
941 // include that too if it is present.
942 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +0000943}
944
945/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
946/// declaration-specifiers init-declarator-list[opt] ';'
947///[C90/C++]init-declarator-list ';' [TODO]
948/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000949///
Richard Smith02e85f32011-04-14 22:09:26 +0000950/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
951/// attribute-specifier-seq[opt] type-specifier-seq declarator
952///
Chris Lattner32dc41c2009-03-29 17:27:48 +0000953/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000954/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +0000955///
956/// If FRI is non-null, we might be parsing a for-range-declaration instead
957/// of a simple-declaration. If we find that we are, we also parse the
958/// for-range-initializer, and place it here.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000959Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
960 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000961 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000962 ParsedAttributes &attrs,
Richard Smith02e85f32011-04-14 22:09:26 +0000963 bool RequireSemi,
964 ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000965 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000966 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000967 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000968
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000969 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +0000970 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000971 StmtResult R = Actions.ActOnVlaStmt(DS);
972 if (R.isUsable())
973 Stmts.push_back(R.release());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000974
Chris Lattner0e894622006-08-13 19:58:17 +0000975 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
976 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000977 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000978 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000979 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000980 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000981 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000982 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000983 }
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000984
985 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +0000986}
Mike Stump11289f42009-09-09 15:08:12 +0000987
John McCalld5a36322009-11-03 19:26:08 +0000988/// ParseDeclGroup - Having concluded that this is either a function
989/// definition or a group of object declarations, actually parse the
990/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000991Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
992 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000993 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +0000994 SourceLocation *DeclEnd,
995 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +0000996 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000997 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000998 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000999
John McCalld5a36322009-11-03 19:26:08 +00001000 // Bail out if the first declarator didn't seem well-formed.
1001 if (!D.hasName() && !D.mayOmitIdentifier()) {
1002 // Skip until ; or }.
1003 SkipUntil(tok::r_brace, true, true);
1004 if (Tok.is(tok::semi))
1005 ConsumeToken();
1006 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001007 }
Mike Stump11289f42009-09-09 15:08:12 +00001008
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001009 // Check to see if we have a function *definition* which must have a body.
1010 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1011 // Look at the next token to make sure that this isn't a function
1012 // declaration. We have to check this because __attribute__ might be the
1013 // start of a function definition in GCC-extended K&R C.
1014 !isDeclarationAfterDeclarator()) {
1015
Chris Lattner13901342010-07-11 22:42:07 +00001016 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +00001017 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1018 Diag(Tok, diag::err_function_declared_typedef);
1019
1020 // Recover by treating the 'typedef' as spurious.
1021 DS.ClearStorageClassSpecs();
1022 }
1023
John McCall48871652010-08-21 09:40:31 +00001024 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +00001025 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +00001026 }
1027
1028 if (isDeclarationSpecifier()) {
1029 // If there is an invalid declaration specifier right after the function
1030 // prototype, then we must be in a missing semicolon case where this isn't
1031 // actually a body. Just fall through into the code that handles it as a
1032 // prototype, and let the top-level code handle the erroneous declspec
1033 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +00001034 } else {
1035 Diag(Tok, diag::err_expected_fn_body);
1036 SkipUntil(tok::semi);
1037 return DeclGroupPtrTy();
1038 }
1039 }
1040
Richard Smith02e85f32011-04-14 22:09:26 +00001041 if (ParseAttributesAfterDeclarator(D))
1042 return DeclGroupPtrTy();
1043
1044 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1045 // must parse and analyze the for-range-initializer before the declaration is
1046 // analyzed.
1047 if (FRI && Tok.is(tok::colon)) {
1048 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +00001049 if (Tok.is(tok::l_brace))
1050 FRI->RangeExpr = ParseBraceInitializer();
1051 else
1052 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001053 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1054 Actions.ActOnCXXForRangeDecl(ThisDecl);
1055 Actions.FinalizeDeclaration(ThisDecl);
1056 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1057 }
1058
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001059 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001060 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall28a6aea2009-11-04 02:18:39 +00001061 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001062 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001063 DeclsInGroup.push_back(FirstDecl);
1064
1065 // If we don't have a comma, it is either the end of the list (a ';') or an
1066 // error, bail out.
1067 while (Tok.is(tok::comma)) {
1068 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +00001069 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +00001070
1071 // Parse the next declarator.
1072 D.clear();
1073
1074 // Accept attributes in an init-declarator. In the first declarator in a
1075 // declaration, these would be part of the declspec. In subsequent
1076 // declarators, they become part of the declarator itself, so that they
1077 // don't apply to declarators after *this* one. Examples:
1078 // short __attribute__((common)) var; -> declspec
1079 // short var __attribute__((common)); -> declarator
1080 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001081 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001082
1083 ParseDeclarator(D);
1084
John McCall48871652010-08-21 09:40:31 +00001085 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +00001086 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +00001087 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +00001088 DeclsInGroup.push_back(ThisDecl);
1089 }
1090
1091 if (DeclEnd)
1092 *DeclEnd = Tok.getLocation();
1093
1094 if (Context != Declarator::ForContext &&
1095 ExpectAndConsume(tok::semi,
1096 Context == Declarator::FileContext
1097 ? diag::err_invalid_token_after_toplevel_declarator
1098 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001099 // Okay, there was no semicolon and one was expected. If we see a
1100 // declaration specifier, just assume it was missing and continue parsing.
1101 // Otherwise things are very confused and we skip to recover.
1102 if (!isDeclarationSpecifier()) {
1103 SkipUntil(tok::r_brace, true, true);
1104 if (Tok.is(tok::semi))
1105 ConsumeToken();
1106 }
John McCalld5a36322009-11-03 19:26:08 +00001107 }
1108
Douglas Gregor0be31a22010-07-02 17:43:08 +00001109 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001110 DeclsInGroup.data(),
1111 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001112}
1113
Richard Smith02e85f32011-04-14 22:09:26 +00001114/// Parse an optional simple-asm-expr and attributes, and attach them to a
1115/// declarator. Returns true on an error.
1116bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
1117 // If a simple-asm-expr is present, parse it.
1118 if (Tok.is(tok::kw_asm)) {
1119 SourceLocation Loc;
1120 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1121 if (AsmLabel.isInvalid()) {
1122 SkipUntil(tok::semi, true, true);
1123 return true;
1124 }
1125
1126 D.setAsmLabel(AsmLabel.release());
1127 D.SetRangeEnd(Loc);
1128 }
1129
1130 MaybeParseGNUAttributes(D);
1131 return false;
1132}
1133
Douglas Gregor23996282009-05-12 21:31:51 +00001134/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1135/// declarator'. This method parses the remainder of the declaration
1136/// (including any attributes or initializer, among other things) and
1137/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001138///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001139/// init-declarator: [C99 6.7]
1140/// declarator
1141/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001142/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1143/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001144/// [C++] declarator initializer[opt]
1145///
1146/// [C++] initializer:
1147/// [C++] '=' initializer-clause
1148/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001149/// [C++0x] '=' 'default' [TODO]
1150/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001151/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001152///
1153/// According to the standard grammar, =default and =delete are function
1154/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001155///
John McCall48871652010-08-21 09:40:31 +00001156Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001157 const ParsedTemplateInfo &TemplateInfo) {
Richard Smith02e85f32011-04-14 22:09:26 +00001158 if (ParseAttributesAfterDeclarator(D))
1159 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001160
Richard Smith02e85f32011-04-14 22:09:26 +00001161 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1162}
Mike Stump11289f42009-09-09 15:08:12 +00001163
Richard Smith02e85f32011-04-14 22:09:26 +00001164Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1165 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001166 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001167 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001168 switch (TemplateInfo.Kind) {
1169 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001170 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001171 break;
1172
1173 case ParsedTemplateInfo::Template:
1174 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001175 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001176 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001177 TemplateInfo.TemplateParams->data(),
1178 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +00001179 D);
1180 break;
1181
1182 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +00001183 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001184 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001185 TemplateInfo.ExternLoc,
1186 TemplateInfo.TemplateLoc,
1187 D);
1188 if (ThisRes.isInvalid()) {
1189 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001190 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001191 }
1192
1193 ThisDecl = ThisRes.get();
1194 break;
1195 }
1196 }
Mike Stump11289f42009-09-09 15:08:12 +00001197
Richard Smith30482bc2011-02-20 03:19:35 +00001198 bool TypeContainsAuto =
1199 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1200
Douglas Gregor23996282009-05-12 21:31:51 +00001201 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001202 if (isTokenEqualOrMistypedEqualEqual(
1203 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +00001204 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001205 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001206 if (D.isFunctionDeclarator())
1207 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1208 << 1 /* delete */;
1209 else
1210 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001211 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001212 if (D.isFunctionDeclarator())
1213 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1214 << 1 /* delete */;
1215 else
1216 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001217 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +00001218 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1219 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001220 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001221 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001222
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001223 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001224 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001225 cutOffParsing();
1226 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001227 }
1228
John McCalldadc5752010-08-24 06:29:42 +00001229 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001230
John McCall1f4ee7b2009-12-19 09:28:58 +00001231 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001232 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001233 ExitScope();
1234 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001235
Douglas Gregor23996282009-05-12 21:31:51 +00001236 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001237 SkipUntil(tok::comma, true, true);
1238 Actions.ActOnInitializerError(ThisDecl);
1239 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001240 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1241 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001242 }
1243 } else if (Tok.is(tok::l_paren)) {
1244 // Parse C++ direct initializer: '(' expression-list ')'
1245 SourceLocation LParenLoc = ConsumeParen();
1246 ExprVector Exprs(Actions);
1247 CommaLocsTy CommaLocs;
1248
Douglas Gregor613bf102009-12-22 17:47:17 +00001249 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1250 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001251 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001252 }
1253
Douglas Gregor23996282009-05-12 21:31:51 +00001254 if (ParseExpressionList(Exprs, CommaLocs)) {
1255 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001256
1257 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001258 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001259 ExitScope();
1260 }
Douglas Gregor23996282009-05-12 21:31:51 +00001261 } else {
1262 // Match the ')'.
1263 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1264
1265 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1266 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001267
1268 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001269 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001270 ExitScope();
1271 }
1272
Douglas Gregor23996282009-05-12 21:31:51 +00001273 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1274 move_arg(Exprs),
Richard Smith30482bc2011-02-20 03:19:35 +00001275 RParenLoc,
1276 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001277 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001278 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1279 // Parse C++0x braced-init-list.
1280 if (D.getCXXScopeSpec().isSet()) {
1281 EnterScope(0);
1282 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1283 }
1284
1285 ExprResult Init(ParseBraceInitializer());
1286
1287 if (D.getCXXScopeSpec().isSet()) {
1288 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1289 ExitScope();
1290 }
1291
1292 if (Init.isInvalid()) {
1293 Actions.ActOnInitializerError(ThisDecl);
1294 } else
1295 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1296 /*DirectInit=*/true, TypeContainsAuto);
1297
Douglas Gregor23996282009-05-12 21:31:51 +00001298 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001299 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001300 }
1301
Richard Smithb2bc2e62011-02-21 20:05:19 +00001302 Actions.FinalizeDeclaration(ThisDecl);
1303
Douglas Gregor23996282009-05-12 21:31:51 +00001304 return ThisDecl;
1305}
1306
Chris Lattner1890ac82006-08-13 01:16:23 +00001307/// ParseSpecifierQualifierList
1308/// specifier-qualifier-list:
1309/// type-specifier specifier-qualifier-list[opt]
1310/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001311/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001312///
Richard Smithcd1c0552011-07-01 19:46:12 +00001313void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001314 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1315 /// parse declaration-specifiers and complain about extra stuff.
Richard Smithcd1c0552011-07-01 19:46:12 +00001316 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump11289f42009-09-09 15:08:12 +00001317
Chris Lattner1890ac82006-08-13 01:16:23 +00001318 // Validate declspec for type-name.
1319 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +00001320 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +00001321 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +00001322 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +00001323
Chris Lattner1b22eed2006-11-28 05:12:07 +00001324 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001325 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001326 if (DS.getStorageClassSpecLoc().isValid())
1327 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1328 else
1329 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001330 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001331 }
Mike Stump11289f42009-09-09 15:08:12 +00001332
Chris Lattner1b22eed2006-11-28 05:12:07 +00001333 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001334 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001335 if (DS.isInlineSpecified())
1336 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1337 if (DS.isVirtualSpecified())
1338 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1339 if (DS.isExplicitSpecified())
1340 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001341 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001342 }
1343}
Chris Lattner53361ac2006-08-10 05:19:57 +00001344
Chris Lattner6cc055a2009-04-12 20:42:31 +00001345/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1346/// specified token is valid after the identifier in a declarator which
1347/// immediately follows the declspec. For example, these things are valid:
1348///
1349/// int x [ 4]; // direct-declarator
1350/// int x ( int y); // direct-declarator
1351/// int(int x ) // direct-declarator
1352/// int x ; // simple-declaration
1353/// int x = 17; // init-declarator-list
1354/// int x , y; // init-declarator-list
1355/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001356/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001357/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001358///
1359/// This is not, because 'x' does not immediately follow the declspec (though
1360/// ')' happens to be valid anyway).
1361/// int (x)
1362///
1363static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1364 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1365 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001366 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001367}
1368
Chris Lattner20a0c612009-04-14 21:34:55 +00001369
1370/// ParseImplicitInt - This method is called when we have an non-typename
1371/// identifier in a declspec (which normally terminates the decl spec) when
1372/// the declspec has no type specifier. In this case, the declspec is either
1373/// malformed or is "implicit int" (in K&R and C89).
1374///
1375/// This method handles diagnosing this prettily and returns false if the
1376/// declspec is done being processed. If it recovers and thinks there may be
1377/// other pieces of declspec after it, it returns true.
1378///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001379bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001380 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +00001381 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001382 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001383
Chris Lattner20a0c612009-04-14 21:34:55 +00001384 SourceLocation Loc = Tok.getLocation();
1385 // If we see an identifier that is not a type name, we normally would
1386 // parse it as the identifer being declared. However, when a typename
1387 // is typo'd or the definition is not included, this will incorrectly
1388 // parse the typename as the identifier name and fall over misparsing
1389 // later parts of the diagnostic.
1390 //
1391 // As such, we try to do some look-ahead in cases where this would
1392 // otherwise be an "implicit-int" case to see if this is invalid. For
1393 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1394 // an identifier with implicit int, we'd get a parse error because the
1395 // next token is obviously invalid for a type. Parse these as a case
1396 // with an invalid type specifier.
1397 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001398
Chris Lattner20a0c612009-04-14 21:34:55 +00001399 // Since we know that this either implicit int (which is rare) or an
1400 // error, we'd do lookahead to try to do better recovery.
1401 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1402 // If this token is valid for implicit int, e.g. "static x = 4", then
1403 // we just avoid eating the identifier, so it will be parsed as the
1404 // identifier in the declarator.
1405 return false;
1406 }
Mike Stump11289f42009-09-09 15:08:12 +00001407
Chris Lattner20a0c612009-04-14 21:34:55 +00001408 // Otherwise, if we don't consume this token, we are going to emit an
1409 // error anyway. Try to recover from various common problems. Check
1410 // to see if this was a reference to a tag name without a tag specified.
1411 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001412 //
1413 // C++ doesn't need this, and isTagName doesn't take SS.
1414 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001415 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001416 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001417
Douglas Gregor0be31a22010-07-02 17:43:08 +00001418 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001419 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001420 case DeclSpec::TST_enum:
1421 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1422 case DeclSpec::TST_union:
1423 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1424 case DeclSpec::TST_struct:
1425 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1426 case DeclSpec::TST_class:
1427 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001428 }
Mike Stump11289f42009-09-09 15:08:12 +00001429
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001430 if (TagName) {
1431 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +00001432 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001433 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump11289f42009-09-09 15:08:12 +00001434
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001435 // Parse this as a tag as if the missing tag were present.
1436 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001437 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001438 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001439 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001440 return true;
1441 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001442 }
Mike Stump11289f42009-09-09 15:08:12 +00001443
Douglas Gregor15e56022009-10-13 23:27:22 +00001444 // This is almost certainly an invalid type name. Let the action emit a
1445 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001446 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +00001447 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001448 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001449 // The action emitted a diagnostic, so we don't have to.
1450 if (T) {
1451 // The action has suggested that the type T could be used. Set that as
1452 // the type in the declaration specifiers, consume the would-be type
1453 // name token, and we're done.
1454 const char *PrevSpec;
1455 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001456 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001457 DS.SetRangeEnd(Tok.getLocation());
1458 ConsumeToken();
1459
1460 // There may be other declaration specifiers after this.
1461 return true;
1462 }
1463
1464 // Fall through; the action had no suggestion for us.
1465 } else {
1466 // The action did not emit a diagnostic, so emit one now.
1467 SourceRange R;
1468 if (SS) R = SS->getRange();
1469 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1470 }
Mike Stump11289f42009-09-09 15:08:12 +00001471
Douglas Gregor15e56022009-10-13 23:27:22 +00001472 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +00001473 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001474 unsigned DiagID;
1475 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +00001476 DS.SetRangeEnd(Tok.getLocation());
1477 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001478
Chris Lattner20a0c612009-04-14 21:34:55 +00001479 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1480 // avoid rippling error messages on subsequent uses of the same type,
1481 // could be useful if #include was forgotten.
1482 return false;
1483}
1484
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001485/// \brief Determine the declaration specifier context from the declarator
1486/// context.
1487///
1488/// \param Context the declarator context, which is one of the
1489/// Declarator::TheContext enumerator values.
1490Parser::DeclSpecContext
1491Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1492 if (Context == Declarator::MemberContext)
1493 return DSC_class;
1494 if (Context == Declarator::FileContext)
1495 return DSC_top_level;
1496 return DSC_normal;
1497}
1498
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001499/// ParseDeclarationSpecifiers
1500/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00001501/// storage-class-specifier declaration-specifiers[opt]
1502/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001503/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001504/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00001505/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001506///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001507/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001508/// 'typedef'
1509/// 'extern'
1510/// 'static'
1511/// 'auto'
1512/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001513/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001514/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001515/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00001516/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00001517/// [C++] 'virtual'
1518/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001519/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00001520/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001521/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00001522
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001523///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00001524void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001525 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00001526 AccessSpecifier AS,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001527 DeclSpecContext DSContext) {
1528 if (DS.getSourceRange().isInvalid()) {
1529 DS.SetRangeStart(Tok.getLocation());
1530 DS.SetRangeEnd(Tok.getLocation());
1531 }
1532
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001533 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001534 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001535 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001536 unsigned DiagID = 0;
1537
Chris Lattner4d8f8732006-11-28 05:05:08 +00001538 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00001539
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001540 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00001541 default:
Chris Lattner0974b232008-07-26 00:20:22 +00001542 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001543 // If this is not a declaration specifier token, we're done reading decl
1544 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001545 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001546 return;
Mike Stump11289f42009-09-09 15:08:12 +00001547
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001548 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001549 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001550 if (DS.hasTypeSpecifier()) {
1551 bool AllowNonIdentifiers
1552 = (getCurScope()->getFlags() & (Scope::ControlScope |
1553 Scope::BlockScope |
1554 Scope::TemplateParamScope |
1555 Scope::FunctionPrototypeScope |
1556 Scope::AtCatchScope)) == 0;
1557 bool AllowNestedNameSpecifiers
1558 = DSContext == DSC_top_level ||
1559 (DSContext == DSC_class && DS.isFriendSpecified());
1560
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00001561 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1562 AllowNonIdentifiers,
1563 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001564 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001565 }
1566
Douglas Gregor80039242011-02-15 20:33:25 +00001567 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1568 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1569 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +00001570 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1571 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001572 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00001573 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001574 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +00001575 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001576
1577 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001578 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001579 }
1580
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001581 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00001582 // C++ scope specifier. Annotate and loop, or bail out on error.
1583 if (TryAnnotateCXXScopeToken(true)) {
1584 if (!DS.hasTypeSpecifier())
1585 DS.SetTypeSpecError();
1586 goto DoneWithDeclSpec;
1587 }
John McCall8bc2a702010-03-01 18:20:46 +00001588 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1589 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00001590 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001591
1592 case tok::annot_cxxscope: {
1593 if (DS.hasTypeSpecifier())
1594 goto DoneWithDeclSpec;
1595
John McCall9dab4e62009-12-12 11:40:51 +00001596 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00001597 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1598 Tok.getAnnotationRange(),
1599 SS);
John McCall9dab4e62009-12-12 11:40:51 +00001600
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001601 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00001602 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00001603 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001604 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00001605 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00001606 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001607
1608 // C++ [class.qual]p2:
1609 // In a lookup in which the constructor is an acceptable lookup
1610 // result and the nested-name-specifier nominates a class C:
1611 //
1612 // - if the name specified after the
1613 // nested-name-specifier, when looked up in C, is the
1614 // injected-class-name of C (Clause 9), or
1615 //
1616 // - if the name specified after the nested-name-specifier
1617 // is the same as the identifier or the
1618 // simple-template-id's template-name in the last
1619 // component of the nested-name-specifier,
1620 //
1621 // the name is instead considered to name the constructor of
1622 // class C.
1623 //
1624 // Thus, if the template-name is actually the constructor
1625 // name, then the code is ill-formed; this interpretation is
1626 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001627 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00001628 if ((DSContext == DSC_top_level ||
1629 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1630 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001631 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001632 if (isConstructorDeclarator()) {
1633 // The user meant this to be an out-of-line constructor
1634 // definition, but template arguments are not allowed
1635 // there. Just allow this as a constructor; we'll
1636 // complain about it later.
1637 goto DoneWithDeclSpec;
1638 }
1639
1640 // The user meant this to name a type, but it actually names
1641 // a constructor with some extraneous template
1642 // arguments. Complain, then parse it as a type as the user
1643 // intended.
1644 Diag(TemplateId->TemplateNameLoc,
1645 diag::err_out_of_line_template_id_names_constructor)
1646 << TemplateId->Name;
1647 }
1648
John McCall9dab4e62009-12-12 11:40:51 +00001649 DS.getTypeSpecScope() = SS;
1650 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001651 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001652 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00001653 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00001654 continue;
1655 }
1656
Douglas Gregorc5790df2009-09-28 07:26:33 +00001657 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001658 DS.getTypeSpecScope() = SS;
1659 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001660 if (Tok.getAnnotationValue()) {
1661 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001662 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1663 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001664 PrevSpec, DiagID, T);
1665 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001666 else
1667 DS.SetTypeSpecError();
1668 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1669 ConsumeToken(); // The typename
1670 }
1671
Douglas Gregor167fa622009-03-25 15:40:00 +00001672 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001673 goto DoneWithDeclSpec;
1674
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001675 // If we're in a context where the identifier could be a class name,
1676 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001677 if ((DSContext == DSC_top_level ||
1678 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001679 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001680 &SS)) {
1681 if (isConstructorDeclarator())
1682 goto DoneWithDeclSpec;
1683
1684 // As noted in C++ [class.qual]p2 (cited above), when the name
1685 // of the class is qualified in a context where it could name
1686 // a constructor, its a constructor name. However, we've
1687 // looked at the declarator, and the user probably meant this
1688 // to be a type. Complain that it isn't supposed to be treated
1689 // as a type, then proceed to parse it as a type.
1690 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1691 << Next.getIdentifierInfo();
1692 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001693
John McCallba7bf592010-08-24 05:47:05 +00001694 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1695 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001696 getCurScope(), &SS,
1697 false, false, ParsedType(),
1698 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001699
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001700 // If the referenced identifier is not a type, then this declspec is
1701 // erroneous: We already checked about that it has no type specifier, and
1702 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001703 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001704 if (TypeRep == 0) {
1705 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001706 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001707 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001708 }
Mike Stump11289f42009-09-09 15:08:12 +00001709
John McCall9dab4e62009-12-12 11:40:51 +00001710 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001711 ConsumeToken(); // The C++ scope.
1712
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001713 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001714 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001715 if (isInvalid)
1716 break;
Mike Stump11289f42009-09-09 15:08:12 +00001717
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001718 DS.SetRangeEnd(Tok.getLocation());
1719 ConsumeToken(); // The typename.
1720
1721 continue;
1722 }
Mike Stump11289f42009-09-09 15:08:12 +00001723
Chris Lattnere387d9e2009-01-21 19:48:37 +00001724 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001725 if (Tok.getAnnotationValue()) {
1726 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001727 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001728 DiagID, T);
1729 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001730 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001731
1732 if (isInvalid)
1733 break;
1734
Chris Lattnere387d9e2009-01-21 19:48:37 +00001735 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1736 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001737
Chris Lattnere387d9e2009-01-21 19:48:37 +00001738 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1739 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001740 // Objective-C interface.
1741 if (Tok.is(tok::less) && getLang().ObjC1)
1742 ParseObjCProtocolQualifiers(DS);
1743
Chris Lattnere387d9e2009-01-21 19:48:37 +00001744 continue;
1745 }
Mike Stump11289f42009-09-09 15:08:12 +00001746
Douglas Gregor06873092011-04-28 15:48:45 +00001747 case tok::kw___is_signed:
1748 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1749 // typically treats it as a trait. If we see __is_signed as it appears
1750 // in libstdc++, e.g.,
1751 //
1752 // static const bool __is_signed;
1753 //
1754 // then treat __is_signed as an identifier rather than as a keyword.
1755 if (DS.getTypeSpecType() == TST_bool &&
1756 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1757 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1758 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1759 Tok.setKind(tok::identifier);
1760 }
1761
1762 // We're done with the declaration-specifiers.
1763 goto DoneWithDeclSpec;
1764
Chris Lattner16fac4f2008-07-26 01:18:38 +00001765 // typedef-name
1766 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001767 // In C++, check to see if this is a scope specifier like foo::bar::, if
1768 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001769 if (getLang().CPlusPlus) {
1770 if (TryAnnotateCXXScopeToken(true)) {
1771 if (!DS.hasTypeSpecifier())
1772 DS.SetTypeSpecError();
1773 goto DoneWithDeclSpec;
1774 }
1775 if (!Tok.is(tok::identifier))
1776 continue;
1777 }
Mike Stump11289f42009-09-09 15:08:12 +00001778
Chris Lattner16fac4f2008-07-26 01:18:38 +00001779 // This identifier can only be a typedef name if we haven't already seen
1780 // a type-specifier. Without this check we misparse:
1781 // typedef int X; struct Y { short X; }; as 'short int'.
1782 if (DS.hasTypeSpecifier())
1783 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001784
John Thompson22334602010-02-05 00:12:22 +00001785 // Check for need to substitute AltiVec keyword tokens.
1786 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1787 break;
1788
Chris Lattner16fac4f2008-07-26 01:18:38 +00001789 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001790 ParsedType TypeRep =
1791 Actions.getTypeName(*Tok.getIdentifierInfo(),
1792 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001793
Chris Lattner6cc055a2009-04-12 20:42:31 +00001794 // If this is not a typedef name, don't parse it as part of the declspec,
1795 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001796 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001797 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001798 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001799 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001800
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001801 // If we're in a context where the identifier could be a class name,
1802 // check whether this is a constructor declaration.
1803 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001804 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001805 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001806 goto DoneWithDeclSpec;
1807
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001808 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001809 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001810 if (isInvalid)
1811 break;
Mike Stump11289f42009-09-09 15:08:12 +00001812
Chris Lattner16fac4f2008-07-26 01:18:38 +00001813 DS.SetRangeEnd(Tok.getLocation());
1814 ConsumeToken(); // The identifier
1815
1816 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1817 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001818 // Objective-C interface.
1819 if (Tok.is(tok::less) && getLang().ObjC1)
1820 ParseObjCProtocolQualifiers(DS);
1821
Steve Naroffcd5e7822008-09-22 10:28:57 +00001822 // Need to support trailing type qualifiers (e.g. "id<p> const").
1823 // If a type specifier follows, it will be diagnosed elsewhere.
1824 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001825 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001826
1827 // type-name
1828 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001829 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00001830 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001831 // This template-id does not refer to a type name, so we're
1832 // done with the type-specifiers.
1833 goto DoneWithDeclSpec;
1834 }
1835
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001836 // If we're in a context where the template-id could be a
1837 // constructor name or specialization, check whether this is a
1838 // constructor declaration.
1839 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001840 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001841 isConstructorDeclarator())
1842 goto DoneWithDeclSpec;
1843
Douglas Gregor7f741122009-02-25 19:37:18 +00001844 // Turn the template-id annotation token into a type annotation
1845 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001846 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001847 continue;
1848 }
1849
Chris Lattnere37e2332006-08-15 04:50:22 +00001850 // GNU attributes support.
1851 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001852 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001853 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001854
1855 // Microsoft declspec support.
1856 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001857 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001858 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001859
Steve Naroff44ac7772008-12-25 14:16:32 +00001860 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001861 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001862 // FIXME: Add handling here!
1863 break;
1864
1865 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00001866 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00001867 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001868 case tok::kw___cdecl:
1869 case tok::kw___stdcall:
1870 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001871 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00001872 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00001873 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001874 continue;
1875
Dawn Perchik335e16b2010-09-03 01:29:35 +00001876 // Borland single token adornments.
1877 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001878 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001879 continue;
1880
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001881 // OpenCL single token adornments.
1882 case tok::kw___kernel:
1883 ParseOpenCLAttributes(DS.getAttributes());
1884 continue;
1885
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001886 // storage-class-specifier
1887 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001888 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001889 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001890 break;
1891 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001892 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001893 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001894 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001895 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001896 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001897 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001898 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001899 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001900 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001901 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001902 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001903 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001904 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001905 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001906 break;
1907 case tok::kw_auto:
Douglas Gregor1e989862011-03-14 21:43:30 +00001908 if (getLang().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001909 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1910 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Richard Smith58c74332011-09-04 19:54:14 +00001911 DiagID, getLang());
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001912 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00001913 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001914 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00001915 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001916 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1917 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00001918 } else
John McCall49bfce42009-08-03 20:12:06 +00001919 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001920 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001921 break;
1922 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001923 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001924 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001925 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001926 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001927 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001928 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001929 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001930 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001931 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001932 break;
Mike Stump11289f42009-09-09 15:08:12 +00001933
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001934 // function-specifier
1935 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001936 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001937 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001938 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001939 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001940 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001941 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001942 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001943 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001944
Anders Carlssoncd8db412009-05-06 04:46:28 +00001945 // friend
1946 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001947 if (DSContext == DSC_class)
1948 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1949 else {
1950 PrevSpec = ""; // not actually used by the diagnostic
1951 DiagID = diag::err_friend_invalid_in_context;
1952 isInvalid = true;
1953 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001954 break;
Mike Stump11289f42009-09-09 15:08:12 +00001955
Douglas Gregor26701a42011-09-09 02:06:17 +00001956 // Modules
1957 case tok::kw___module_private__:
1958 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
1959 break;
1960
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001961 // constexpr
1962 case tok::kw_constexpr:
1963 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1964 break;
1965
Chris Lattnere387d9e2009-01-21 19:48:37 +00001966 // type-specifier
1967 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001968 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1969 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001970 break;
1971 case tok::kw_long:
1972 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001973 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1974 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001975 else
John McCall49bfce42009-08-03 20:12:06 +00001976 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1977 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001978 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001979 case tok::kw___int64:
1980 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1981 DiagID);
1982 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001983 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001984 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1985 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001986 break;
1987 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001988 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1989 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001990 break;
1991 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001992 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1993 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001994 break;
1995 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001996 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1997 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001998 break;
1999 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002000 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2001 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002002 break;
2003 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002004 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2005 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002006 break;
2007 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002008 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2009 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002010 break;
2011 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002012 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2013 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002014 break;
2015 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002016 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2017 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002018 break;
2019 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002020 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2021 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002022 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002023 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002024 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2025 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002026 break;
2027 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002028 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2029 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002030 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002031 case tok::kw_bool:
2032 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002033 if (Tok.is(tok::kw_bool) &&
2034 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2035 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2036 PrevSpec = ""; // Not used by the diagnostic.
2037 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002038 // For better error recovery.
2039 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002040 isInvalid = true;
2041 } else {
2042 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2043 DiagID);
2044 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002045 break;
2046 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002047 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2048 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002049 break;
2050 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002051 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2052 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002053 break;
2054 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002055 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2056 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002057 break;
John Thompson22334602010-02-05 00:12:22 +00002058 case tok::kw___vector:
2059 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2060 break;
2061 case tok::kw___pixel:
2062 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2063 break;
John McCall39439732011-04-09 22:50:59 +00002064 case tok::kw___unknown_anytype:
2065 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2066 PrevSpec, DiagID);
2067 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002068
2069 // class-specifier:
2070 case tok::kw_class:
2071 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002072 case tok::kw_union: {
2073 tok::TokenKind Kind = Tok.getKind();
2074 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002075 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002076 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002077 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002078
2079 // enum-specifier:
2080 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002081 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002082 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002083 continue;
2084
2085 // cv-qualifier:
2086 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002087 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2088 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002089 break;
2090 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002091 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2092 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002093 break;
2094 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002095 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2096 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002097 break;
2098
Douglas Gregor333489b2009-03-27 23:10:48 +00002099 // C++ typename-specifier:
2100 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00002101 if (TryAnnotateTypeOrScopeToken()) {
2102 DS.SetTypeSpecError();
2103 goto DoneWithDeclSpec;
2104 }
2105 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00002106 continue;
2107 break;
2108
Chris Lattnere387d9e2009-01-21 19:48:37 +00002109 // GNU typeof support.
2110 case tok::kw_typeof:
2111 ParseTypeofSpecifier(DS);
2112 continue;
2113
Anders Carlsson74948d02009-06-24 17:47:40 +00002114 case tok::kw_decltype:
2115 ParseDecltypeSpecifier(DS);
2116 continue;
2117
Alexis Hunt4a257072011-05-19 05:37:45 +00002118 case tok::kw___underlying_type:
2119 ParseUnderlyingTypeSpecifier(DS);
2120
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002121 // OpenCL qualifiers:
2122 case tok::kw_private:
2123 if (!getLang().OpenCL)
2124 goto DoneWithDeclSpec;
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
Steve Naroffcfdf6162008-06-05 00:02:44 +00002135 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002136 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00002137 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2138 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00002139 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002140 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002141
Douglas Gregor3a001f42010-11-19 17:10:50 +00002142 if (!ParseObjCProtocolQualifiers(DS))
2143 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2144 << FixItHint::CreateInsertion(Loc, "id")
2145 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002146
2147 // Need to support trailing type qualifiers (e.g. "id<p> const").
2148 // If a type specifier follows, it will be diagnosed elsewhere.
2149 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002150 }
John McCall49bfce42009-08-03 20:12:06 +00002151 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002152 if (isInvalid) {
2153 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002154 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002155
2156 if (DiagID == diag::ext_duplicate_declspec)
2157 Diag(Tok, DiagID)
2158 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2159 else
2160 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002161 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002162
Chris Lattner2e232092008-03-13 06:29:04 +00002163 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002164 if (DiagID != diag::err_bool_redeclaration)
2165 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002166 }
2167}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002168
Chris Lattnera448d752009-01-06 06:59:53 +00002169/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00002170/// primarily follow the C++ grammar with additions for C99 and GNU,
2171/// which together subsume the C grammar. Note that the C++
2172/// type-specifier also includes the C type-qualifier (for const,
2173/// volatile, and C99 restrict). Returns true if a type-specifier was
2174/// found (and parsed), false otherwise.
2175///
2176/// type-specifier: [C++ 7.1.5]
2177/// simple-type-specifier
2178/// class-specifier
2179/// enum-specifier
2180/// elaborated-type-specifier [TODO]
2181/// cv-qualifier
2182///
2183/// cv-qualifier: [C++ 7.1.5.1]
2184/// 'const'
2185/// 'volatile'
2186/// [C99] 'restrict'
2187///
2188/// simple-type-specifier: [ C++ 7.1.5.2]
2189/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2190/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2191/// 'char'
2192/// 'wchar_t'
2193/// 'bool'
2194/// 'short'
2195/// 'int'
2196/// 'long'
2197/// 'signed'
2198/// 'unsigned'
2199/// 'float'
2200/// 'double'
2201/// 'void'
2202/// [C99] '_Bool'
2203/// [C99] '_Complex'
2204/// [C99] '_Imaginary' // Removed in TC2?
2205/// [GNU] '_Decimal32'
2206/// [GNU] '_Decimal64'
2207/// [GNU] '_Decimal128'
2208/// [GNU] typeof-specifier
2209/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2210/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00002211/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00002212/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00002213bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00002214 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002215 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00002216 const ParsedTemplateInfo &TemplateInfo,
2217 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00002218 SourceLocation Loc = Tok.getLocation();
2219
2220 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00002221 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00002222 // If we already have a type specifier, this identifier is not a type.
2223 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2224 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2225 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2226 return false;
John Thompson22334602010-02-05 00:12:22 +00002227 // Check for need to substitute AltiVec keyword tokens.
2228 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2229 break;
2230 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002231 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00002232 // Annotate typenames and C++ scope specifiers. If we get one, just
2233 // recurse to handle whatever we get.
2234 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002235 return true;
2236 if (Tok.is(tok::identifier))
2237 return false;
2238 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2239 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00002240 case tok::coloncolon: // ::foo::bar
2241 if (NextToken().is(tok::kw_new) || // ::new
2242 NextToken().is(tok::kw_delete)) // ::delete
2243 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002244
Chris Lattner020bab92009-01-04 23:41:41 +00002245 // Annotate typenames and C++ scope specifiers. If we get one, just
2246 // recurse to handle whatever we get.
2247 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002248 return true;
2249 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2250 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00002251
Douglas Gregor450c75a2008-11-07 15:42:26 +00002252 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00002253 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002254 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00002255 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2256 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002257 DiagID, T);
2258 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002259 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002260 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2261 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002262
Douglas Gregor450c75a2008-11-07 15:42:26 +00002263 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2264 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2265 // Objective-C interface. If we don't have Objective-C or a '<', this is
2266 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002267 if (Tok.is(tok::less) && getLang().ObjC1)
2268 ParseObjCProtocolQualifiers(DS);
2269
Douglas Gregor450c75a2008-11-07 15:42:26 +00002270 return true;
2271 }
2272
2273 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002274 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002275 break;
2276 case tok::kw_long:
2277 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002278 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2279 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002280 else
John McCall49bfce42009-08-03 20:12:06 +00002281 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2282 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002283 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002284 case tok::kw___int64:
2285 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2286 DiagID);
2287 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002288 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002289 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002290 break;
2291 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002292 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2293 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002294 break;
2295 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002296 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2297 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002298 break;
2299 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002300 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2301 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002302 break;
2303 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002304 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002305 break;
2306 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002307 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002308 break;
2309 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002310 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002311 break;
2312 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002313 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002314 break;
2315 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002316 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002317 break;
2318 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002319 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002320 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002321 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002322 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002323 break;
2324 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002325 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002326 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002327 case tok::kw_bool:
2328 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00002329 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002330 break;
2331 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2333 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002334 break;
2335 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002336 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2337 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002338 break;
2339 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002340 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2341 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002342 break;
John Thompson22334602010-02-05 00:12:22 +00002343 case tok::kw___vector:
2344 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2345 break;
2346 case tok::kw___pixel:
2347 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2348 break;
2349
Douglas Gregor450c75a2008-11-07 15:42:26 +00002350 // class-specifier:
2351 case tok::kw_class:
2352 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002353 case tok::kw_union: {
2354 tok::TokenKind Kind = Tok.getKind();
2355 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00002356 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2357 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002358 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002359 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00002360
2361 // enum-specifier:
2362 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002363 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002364 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002365 return true;
2366
2367 // cv-qualifier:
2368 case tok::kw_const:
2369 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002370 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002371 break;
2372 case tok::kw_volatile:
2373 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002374 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002375 break;
2376 case tok::kw_restrict:
2377 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002378 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002379 break;
2380
2381 // GNU typeof support.
2382 case tok::kw_typeof:
2383 ParseTypeofSpecifier(DS);
2384 return true;
2385
Anders Carlsson74948d02009-06-24 17:47:40 +00002386 // C++0x decltype support.
2387 case tok::kw_decltype:
2388 ParseDecltypeSpecifier(DS);
2389 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002390
Alexis Hunt4a257072011-05-19 05:37:45 +00002391 // C++0x type traits support.
2392 case tok::kw___underlying_type:
2393 ParseUnderlyingTypeSpecifier(DS);
2394 return true;
2395
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002396 // OpenCL qualifiers:
2397 case tok::kw_private:
2398 if (!getLang().OpenCL)
2399 return false;
2400 case tok::kw___private:
2401 case tok::kw___global:
2402 case tok::kw___local:
2403 case tok::kw___constant:
2404 case tok::kw___read_only:
2405 case tok::kw___write_only:
2406 case tok::kw___read_write:
2407 ParseOpenCLQualifiers(DS);
2408 break;
2409
Anders Carlssonbae27372009-06-26 23:44:14 +00002410 // C++0x auto support.
2411 case tok::kw_auto:
Richard Smith50658642011-09-04 20:24:20 +00002412 // This is only called in situations where a storage-class specifier is
2413 // illegal, so we can assume an auto type specifier was intended even in
2414 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2415 // extension diagnostic.
2416 if (!getLang().CPlusPlus)
Anders Carlssonbae27372009-06-26 23:44:14 +00002417 return false;
2418
John McCall49bfce42009-08-03 20:12:06 +00002419 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00002420 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002421
Eli Friedman53339e02009-06-08 23:27:34 +00002422 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002423 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00002424 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002425 case tok::kw___cdecl:
2426 case tok::kw___stdcall:
2427 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002428 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002429 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002430 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00002431 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00002432
Dawn Perchik335e16b2010-09-03 01:29:35 +00002433 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002434 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002435 return true;
2436
Douglas Gregor450c75a2008-11-07 15:42:26 +00002437 default:
2438 // Not a type-specifier; do nothing.
2439 return false;
2440 }
2441
2442 // If the specifier combination wasn't legal, issue a diagnostic.
2443 if (isInvalid) {
2444 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002445 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00002446 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002447 }
2448 DS.SetRangeEnd(Tok.getLocation());
2449 ConsumeToken(); // whatever we parsed above.
2450 return true;
2451}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002452
Chris Lattner70ae4912007-10-29 04:42:53 +00002453/// ParseStructDeclaration - Parse a struct declaration without the terminating
2454/// semicolon.
2455///
Chris Lattner90a26b02007-01-23 04:38:16 +00002456/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002457/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002458/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002459/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002460/// struct-declarator-list:
2461/// struct-declarator
2462/// struct-declarator-list ',' struct-declarator
2463/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2464/// struct-declarator:
2465/// declarator
2466/// [GNU] declarator attributes[opt]
2467/// declarator[opt] ':' constant-expression
2468/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2469///
Chris Lattnera12405b2008-04-10 06:46:29 +00002470void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00002471ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002472
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002473 if (Tok.is(tok::kw___extension__)) {
2474 // __extension__ silences extension warnings in the subexpression.
2475 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002476 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002477 return ParseStructDeclaration(DS, Fields);
2478 }
Mike Stump11289f42009-09-09 15:08:12 +00002479
Steve Naroff97170802007-08-20 22:28:22 +00002480 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002481 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002482
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002483 // If there are no declarators, this is a free-standing declaration
2484 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002485 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002486 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002487 return;
2488 }
2489
2490 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002491 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00002492 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00002493 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002494 FieldDeclarator DeclaratorInfo(DS);
2495
2496 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002497 if (!FirstDeclarator)
2498 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002499
Steve Naroff97170802007-08-20 22:28:22 +00002500 /// struct-declarator: declarator
2501 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002502 if (Tok.isNot(tok::colon)) {
2503 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2504 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002505 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002506 }
Mike Stump11289f42009-09-09 15:08:12 +00002507
Chris Lattner76c72282007-10-09 17:33:22 +00002508 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002509 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002510 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002511 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002512 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002513 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002514 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002515 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002516
Steve Naroff97170802007-08-20 22:28:22 +00002517 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002518 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002519
John McCallcfefb6d2009-11-03 02:38:08 +00002520 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002521 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002522 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002523
Steve Naroff97170802007-08-20 22:28:22 +00002524 // If we don't have a comma, it is either the end of the list (a ';')
2525 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002526 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002527 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002528
Steve Naroff97170802007-08-20 22:28:22 +00002529 // Consume the comma.
2530 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002531
John McCallcfefb6d2009-11-03 02:38:08 +00002532 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002533 }
Steve Naroff97170802007-08-20 22:28:22 +00002534}
2535
2536/// ParseStructUnionBody
2537/// struct-contents:
2538/// struct-declaration-list
2539/// [EXT] empty
2540/// [GNU] "struct-declaration-list" without terminatoring ';'
2541/// struct-declaration-list:
2542/// struct-declaration
2543/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002544/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002545///
Chris Lattner1300fb92007-01-23 23:42:53 +00002546void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002547 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002548 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2549 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002550
Chris Lattner90a26b02007-01-23 04:38:16 +00002551 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002552
Douglas Gregor658b9552009-01-09 22:42:13 +00002553 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002554 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002555
Chris Lattner7b9ace62007-01-23 20:11:08 +00002556 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2557 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00002558 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00002559 Diag(Tok, diag::ext_empty_struct_union)
2560 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00002561
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002562 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002563
Chris Lattner7b9ace62007-01-23 20:11:08 +00002564 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002565 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002566 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002567
Chris Lattner736ed5d2007-06-09 05:59:07 +00002568 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002569 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00002570 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00002571 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00002572 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00002573 ConsumeToken();
2574 continue;
2575 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002576
2577 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002578 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002579
John McCallcfefb6d2009-11-03 02:38:08 +00002580 if (!Tok.is(tok::at)) {
2581 struct CFieldCallback : FieldCallback {
2582 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002583 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002584 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002585
John McCall48871652010-08-21 09:40:31 +00002586 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002587 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002588 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2589
John McCall48871652010-08-21 09:40:31 +00002590 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002591 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002592 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002593 FD.D.getDeclSpec().getSourceRange().getBegin(),
2594 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002595 FieldDecls.push_back(Field);
2596 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002597 }
John McCallcfefb6d2009-11-03 02:38:08 +00002598 } Callback(*this, TagDecl, FieldDecls);
2599
2600 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002601 } else { // Handle @defs
2602 ConsumeToken();
2603 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2604 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002605 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002606 continue;
2607 }
2608 ConsumeToken();
2609 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2610 if (!Tok.is(tok::identifier)) {
2611 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002612 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002613 continue;
2614 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002615 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002616 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002617 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002618 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2619 ConsumeToken();
2620 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002621 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002622
Chris Lattner76c72282007-10-09 17:33:22 +00002623 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002624 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002625 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002626 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002627 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002628 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002629 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2630 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002631 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002632 // If we stopped at a ';', eat it.
2633 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002634 }
2635 }
Mike Stump11289f42009-09-09 15:08:12 +00002636
Steve Naroff33a1e802007-10-29 21:38:07 +00002637 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002638
John McCall084e83d2011-03-24 11:26:52 +00002639 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002640 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002641 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002642
Douglas Gregor0be31a22010-07-02 17:43:08 +00002643 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00002644 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00002645 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00002646 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002647 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002648 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00002649}
2650
Chris Lattner3b561a32006-08-13 00:12:11 +00002651/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002652/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002653/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002654///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002655/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2656/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002657/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002658/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002659///
Douglas Gregor0bf31402010-10-08 23:50:27 +00002660/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2661/// [C++0x] enum-head '{' enumerator-list ',' '}'
2662///
2663/// enum-head: [C++0x]
2664/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2665/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2666///
2667/// enum-key: [C++0x]
2668/// 'enum'
2669/// 'enum' 'class'
2670/// 'enum' 'struct'
2671///
2672/// enum-base: [C++0x]
2673/// ':' type-specifier-seq
2674///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002675/// [C++] elaborated-type-specifier:
2676/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2677///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002678void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002679 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002680 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002681 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002682 if (Tok.is(tok::code_completion)) {
2683 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002684 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002685 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002686 }
John McCallcb432fa2011-07-06 05:58:41 +00002687
2688 bool IsScopedEnum = false;
2689 bool IsScopedUsingClassTag = false;
2690
2691 if (getLang().CPlusPlus0x &&
2692 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
2693 IsScopedEnum = true;
2694 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2695 ConsumeToken();
2696 }
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002697
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002698 // If attributes exist after tag, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002699 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002700 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002701
Douglas Gregor8b7d4032011-09-08 17:18:35 +00002702 bool AllowFixedUnderlyingType
2703 = getLang().CPlusPlus0x || getLang().Microsoft || getLang().ObjC2;
John McCallcb432fa2011-07-06 05:58:41 +00002704
Abramo Bagnarad7548482010-05-19 21:37:53 +00002705 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00002706 if (getLang().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00002707 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2708 // if a fixed underlying type is allowed.
2709 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2710
John McCallba7bf592010-08-24 05:47:05 +00002711 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00002712 return;
2713
2714 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002715 Diag(Tok, diag::err_expected_ident);
2716 if (Tok.isNot(tok::l_brace)) {
2717 // Has no name and is not a definition.
2718 // Skip the rest of this declarator, up until the comma or semicolon.
2719 SkipUntil(tok::comma, true);
2720 return;
2721 }
2722 }
2723 }
Mike Stump11289f42009-09-09 15:08:12 +00002724
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002725 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002726 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2727 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002728 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00002729
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002730 // Skip the rest of this declarator, up until the comma or semicolon.
2731 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002732 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002733 }
Mike Stump11289f42009-09-09 15:08:12 +00002734
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002735 // If an identifier is present, consume and remember it.
2736 IdentifierInfo *Name = 0;
2737 SourceLocation NameLoc;
2738 if (Tok.is(tok::identifier)) {
2739 Name = Tok.getIdentifierInfo();
2740 NameLoc = ConsumeToken();
2741 }
Mike Stump11289f42009-09-09 15:08:12 +00002742
Douglas Gregor0bf31402010-10-08 23:50:27 +00002743 if (!Name && IsScopedEnum) {
2744 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2745 // declaration of a scoped enumeration.
2746 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2747 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002748 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002749 }
2750
2751 TypeResult BaseType;
2752
Douglas Gregord1f69f62010-12-01 17:42:47 +00002753 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002754 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002755 bool PossibleBitfield = false;
2756 if (getCurScope()->getFlags() & Scope::ClassScope) {
2757 // If we're in class scope, this can either be an enum declaration with
2758 // an underlying type, or a declaration of a bitfield member. We try to
2759 // use a simple disambiguation scheme first to catch the common cases
2760 // (integer literal, sizeof); if it's still ambiguous, we then consider
2761 // anything that's a simple-type-specifier followed by '(' as an
2762 // expression. This suffices because function types are not valid
2763 // underlying types anyway.
2764 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2765 // If the next token starts an expression, we know we're parsing a
2766 // bit-field. This is the common case.
2767 if (TPR == TPResult::True())
2768 PossibleBitfield = true;
2769 // If the next token starts a type-specifier-seq, it may be either a
2770 // a fixed underlying type or the start of a function-style cast in C++;
2771 // lookahead one more token to see if it's obvious that we have a
2772 // fixed underlying type.
2773 else if (TPR == TPResult::False() &&
2774 GetLookAheadToken(2).getKind() == tok::semi) {
2775 // Consume the ':'.
2776 ConsumeToken();
2777 } else {
2778 // We have the start of a type-specifier-seq, so we have to perform
2779 // tentative parsing to determine whether we have an expression or a
2780 // type.
2781 TentativeParsingAction TPA(*this);
2782
2783 // Consume the ':'.
2784 ConsumeToken();
2785
Douglas Gregora1aec292011-02-22 20:32:04 +00002786 if ((getLang().CPlusPlus &&
2787 isCXXDeclarationSpecifier() != TPResult::True()) ||
2788 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002789 // We'll parse this as a bitfield later.
2790 PossibleBitfield = true;
2791 TPA.Revert();
2792 } else {
2793 // We have a type-specifier-seq.
2794 TPA.Commit();
2795 }
2796 }
2797 } else {
2798 // Consume the ':'.
2799 ConsumeToken();
2800 }
2801
2802 if (!PossibleBitfield) {
2803 SourceRange Range;
2804 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002805
Douglas Gregor8b7d4032011-09-08 17:18:35 +00002806 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregora1aec292011-02-22 20:32:04 +00002807 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2808 << Range;
Douglas Gregord1f69f62010-12-01 17:42:47 +00002809 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002810 }
2811
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002812 // There are three options here. If we have 'enum foo;', then this is a
2813 // forward declaration. If we have 'enum foo {...' then this is a
2814 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2815 //
2816 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2817 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2818 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2819 //
John McCallfaf5fb42010-08-26 23:41:50 +00002820 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002821 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002822 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002823 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002824 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002825 else
John McCallfaf5fb42010-08-26 23:41:50 +00002826 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002827
2828 // enums cannot be templates, although they can be referenced from a
2829 // template.
2830 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002831 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002832 Diag(Tok, diag::err_enum_template);
2833
2834 // Skip the rest of this declarator, up until the comma or semicolon.
2835 SkipUntil(tok::comma, true);
2836 return;
2837 }
2838
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002839 if (!Name && TUK != Sema::TUK_Definition) {
2840 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2841
2842 // Skip the rest of this declarator, up until the comma or semicolon.
2843 SkipUntil(tok::comma, true);
2844 return;
2845 }
2846
Douglas Gregord6ab8742009-05-28 23:31:59 +00002847 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002848 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002849 const char *PrevSpec = 0;
2850 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002851 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002852 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregor26701a42011-09-09 02:06:17 +00002853 AS, DS.isModulePrivateSpecified(),
John McCallfaf5fb42010-08-26 23:41:50 +00002854 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002855 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002856 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002857
Douglas Gregorba41d012010-04-24 16:38:41 +00002858 if (IsDependent) {
2859 // This enum has a dependent nested-name-specifier. Handle it as a
2860 // dependent tag.
2861 if (!Name) {
2862 DS.SetTypeSpecError();
2863 Diag(Tok, diag::err_expected_type_name_after_typename);
2864 return;
2865 }
2866
Douglas Gregor0be31a22010-07-02 17:43:08 +00002867 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002868 TUK, SS, Name, StartLoc,
2869 NameLoc);
2870 if (Type.isInvalid()) {
2871 DS.SetTypeSpecError();
2872 return;
2873 }
2874
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002875 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2876 NameLoc.isValid() ? NameLoc : StartLoc,
2877 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002878 Diag(StartLoc, DiagID) << PrevSpec;
2879
2880 return;
2881 }
Mike Stump11289f42009-09-09 15:08:12 +00002882
John McCall48871652010-08-21 09:40:31 +00002883 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002884 // The action failed to produce an enumeration tag. If this is a
2885 // definition, consume the entire definition.
2886 if (Tok.is(tok::l_brace)) {
2887 ConsumeBrace();
2888 SkipUntil(tok::r_brace);
2889 }
2890
2891 DS.SetTypeSpecError();
2892 return;
2893 }
2894
Chris Lattner76c72282007-10-09 17:33:22 +00002895 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002896 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002897
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002898 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2899 NameLoc.isValid() ? NameLoc : StartLoc,
2900 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002901 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002902}
2903
Chris Lattnerc1915e22007-01-25 07:29:02 +00002904/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2905/// enumerator-list:
2906/// enumerator
2907/// enumerator-list ',' enumerator
2908/// enumerator:
2909/// enumeration-constant
2910/// enumeration-constant '=' constant-expression
2911/// enumeration-constant:
2912/// identifier
2913///
John McCall48871652010-08-21 09:40:31 +00002914void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002915 // Enter the scope of the enum body and start the definition.
2916 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002917 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002918
Chris Lattnerc1915e22007-01-25 07:29:02 +00002919 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002920
Chris Lattner37256fb2007-08-27 17:24:30 +00002921 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002922 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002923 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002924
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002925 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002926
John McCall48871652010-08-21 09:40:31 +00002927 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002928
Chris Lattnerc1915e22007-01-25 07:29:02 +00002929 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002930 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002931 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2932 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002933
John McCall811a0f52010-10-22 23:36:17 +00002934 // If attributes exist after the enumerator, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002935 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002936 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002937
Chris Lattnerc1915e22007-01-25 07:29:02 +00002938 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002939 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002940 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002941 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002942 AssignedVal = ParseConstantExpression();
2943 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002944 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002945 }
Mike Stump11289f42009-09-09 15:08:12 +00002946
Chris Lattnerc1915e22007-01-25 07:29:02 +00002947 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002948 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2949 LastEnumConstDecl,
2950 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002951 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002952 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002953 EnumConstantDecls.push_back(EnumConstDecl);
2954 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002955
Douglas Gregorce66d022010-09-07 14:51:08 +00002956 if (Tok.is(tok::identifier)) {
2957 // We're missing a comma between enumerators.
2958 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2959 Diag(Loc, diag::err_enumerator_list_missing_comma)
2960 << FixItHint::CreateInsertion(Loc, ", ");
2961 continue;
2962 }
2963
Chris Lattner76c72282007-10-09 17:33:22 +00002964 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002965 break;
2966 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002967
2968 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002969 !(getLang().C99 || getLang().CPlusPlus0x))
2970 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2971 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002972 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002973 }
Mike Stump11289f42009-09-09 15:08:12 +00002974
Chris Lattnerc1915e22007-01-25 07:29:02 +00002975 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002976 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002977
Chris Lattnerc1915e22007-01-25 07:29:02 +00002978 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002979 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002980 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002981
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002982 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2983 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002984 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002985
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002986 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002987 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002988}
Chris Lattner3b561a32006-08-13 00:12:11 +00002989
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002990/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002991/// start of a type-qualifier-list.
2992bool Parser::isTypeQualifier() const {
2993 switch (Tok.getKind()) {
2994 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002995
2996 // type-qualifier only in OpenCL
2997 case tok::kw_private:
2998 return getLang().OpenCL;
2999
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003000 // type-qualifier
3001 case tok::kw_const:
3002 case tok::kw_volatile:
3003 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003004 case tok::kw___private:
3005 case tok::kw___local:
3006 case tok::kw___global:
3007 case tok::kw___constant:
3008 case tok::kw___read_only:
3009 case tok::kw___read_write:
3010 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003011 return true;
3012 }
3013}
3014
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003015/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3016/// is definitely a type-specifier. Return false if it isn't part of a type
3017/// specifier or if we're not sure.
3018bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3019 switch (Tok.getKind()) {
3020 default: return false;
3021 // type-specifiers
3022 case tok::kw_short:
3023 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003024 case tok::kw___int64:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003025 case tok::kw_signed:
3026 case tok::kw_unsigned:
3027 case tok::kw__Complex:
3028 case tok::kw__Imaginary:
3029 case tok::kw_void:
3030 case tok::kw_char:
3031 case tok::kw_wchar_t:
3032 case tok::kw_char16_t:
3033 case tok::kw_char32_t:
3034 case tok::kw_int:
3035 case tok::kw_float:
3036 case tok::kw_double:
3037 case tok::kw_bool:
3038 case tok::kw__Bool:
3039 case tok::kw__Decimal32:
3040 case tok::kw__Decimal64:
3041 case tok::kw__Decimal128:
3042 case tok::kw___vector:
3043
3044 // struct-or-union-specifier (C99) or class-specifier (C++)
3045 case tok::kw_class:
3046 case tok::kw_struct:
3047 case tok::kw_union:
3048 // enum-specifier
3049 case tok::kw_enum:
3050
3051 // typedef-name
3052 case tok::annot_typename:
3053 return true;
3054 }
3055}
3056
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003057/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003058/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003059bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003060 switch (Tok.getKind()) {
3061 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003062
Chris Lattner020bab92009-01-04 23:41:41 +00003063 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003064 if (TryAltiVecVectorToken())
3065 return true;
3066 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003067 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003068 // Annotate typenames and C++ scope specifiers. If we get one, just
3069 // recurse to handle whatever we get.
3070 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003071 return true;
3072 if (Tok.is(tok::identifier))
3073 return false;
3074 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003075
Chris Lattner020bab92009-01-04 23:41:41 +00003076 case tok::coloncolon: // ::foo::bar
3077 if (NextToken().is(tok::kw_new) || // ::new
3078 NextToken().is(tok::kw_delete)) // ::delete
3079 return false;
3080
Chris Lattner020bab92009-01-04 23:41:41 +00003081 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003082 return true;
3083 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003084
Chris Lattnere37e2332006-08-15 04:50:22 +00003085 // GNU attributes support.
3086 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003087 // GNU typeof support.
3088 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003089
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003090 // type-specifiers
3091 case tok::kw_short:
3092 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003093 case tok::kw___int64:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003094 case tok::kw_signed:
3095 case tok::kw_unsigned:
3096 case tok::kw__Complex:
3097 case tok::kw__Imaginary:
3098 case tok::kw_void:
3099 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003100 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003101 case tok::kw_char16_t:
3102 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003103 case tok::kw_int:
3104 case tok::kw_float:
3105 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003106 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003107 case tok::kw__Bool:
3108 case tok::kw__Decimal32:
3109 case tok::kw__Decimal64:
3110 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003111 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003112
Chris Lattner861a2262008-04-13 18:59:07 +00003113 // struct-or-union-specifier (C99) or class-specifier (C++)
3114 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003115 case tok::kw_struct:
3116 case tok::kw_union:
3117 // enum-specifier
3118 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003119
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003120 // type-qualifier
3121 case tok::kw_const:
3122 case tok::kw_volatile:
3123 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003124
3125 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003126 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003127 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003128
Chris Lattner409bf7d2008-10-20 00:25:30 +00003129 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3130 case tok::less:
3131 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003132
Steve Naroff44ac7772008-12-25 14:16:32 +00003133 case tok::kw___cdecl:
3134 case tok::kw___stdcall:
3135 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003136 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003137 case tok::kw___w64:
3138 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003139 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003140 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003141 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003142
3143 case tok::kw___private:
3144 case tok::kw___local:
3145 case tok::kw___global:
3146 case tok::kw___constant:
3147 case tok::kw___read_only:
3148 case tok::kw___read_write:
3149 case tok::kw___write_only:
3150
Eli Friedman53339e02009-06-08 23:27:34 +00003151 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003152
3153 case tok::kw_private:
3154 return getLang().OpenCL;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003155 }
3156}
3157
Chris Lattneracd58a32006-08-06 17:24:14 +00003158/// isDeclarationSpecifier() - Return true if the current token is part of a
3159/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003160///
3161/// \param DisambiguatingWithExpression True to indicate that the purpose of
3162/// this check is to disambiguate between an expression and a declaration.
3163bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003164 switch (Tok.getKind()) {
3165 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003166
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003167 case tok::kw_private:
3168 return getLang().OpenCL;
3169
Chris Lattner020bab92009-01-04 23:41:41 +00003170 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003171 // Unfortunate hack to support "Class.factoryMethod" notation.
3172 if (getLang().ObjC1 && NextToken().is(tok::period))
3173 return false;
John Thompson22334602010-02-05 00:12:22 +00003174 if (TryAltiVecVectorToken())
3175 return true;
3176 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003177 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003178 // Annotate typenames and C++ scope specifiers. If we get one, just
3179 // recurse to handle whatever we get.
3180 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003181 return true;
3182 if (Tok.is(tok::identifier))
3183 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003184
3185 // If we're in Objective-C and we have an Objective-C class type followed
3186 // by an identifier and then either ':' or ']', in a place where an
3187 // expression is permitted, then this is probably a class message send
3188 // missing the initial '['. In this case, we won't consider this to be
3189 // the start of a declaration.
3190 if (DisambiguatingWithExpression &&
3191 isStartOfObjCClassMessageMissingOpenBracket())
3192 return false;
3193
John McCall1f476a12010-02-26 08:45:28 +00003194 return isDeclarationSpecifier();
3195
Chris Lattner020bab92009-01-04 23:41:41 +00003196 case tok::coloncolon: // ::foo::bar
3197 if (NextToken().is(tok::kw_new) || // ::new
3198 NextToken().is(tok::kw_delete)) // ::delete
3199 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003200
Chris Lattner020bab92009-01-04 23:41:41 +00003201 // Annotate typenames and C++ scope specifiers. If we get one, just
3202 // recurse to handle whatever we get.
3203 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003204 return true;
3205 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003206
Chris Lattneracd58a32006-08-06 17:24:14 +00003207 // storage-class-specifier
3208 case tok::kw_typedef:
3209 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003210 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003211 case tok::kw_static:
3212 case tok::kw_auto:
3213 case tok::kw_register:
3214 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003215
Douglas Gregor26701a42011-09-09 02:06:17 +00003216 // Modules
3217 case tok::kw___module_private__:
3218
Chris Lattneracd58a32006-08-06 17:24:14 +00003219 // type-specifiers
3220 case tok::kw_short:
3221 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003222 case tok::kw___int64:
Chris Lattneracd58a32006-08-06 17:24:14 +00003223 case tok::kw_signed:
3224 case tok::kw_unsigned:
3225 case tok::kw__Complex:
3226 case tok::kw__Imaginary:
3227 case tok::kw_void:
3228 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003229 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003230 case tok::kw_char16_t:
3231 case tok::kw_char32_t:
3232
Chris Lattneracd58a32006-08-06 17:24:14 +00003233 case tok::kw_int:
3234 case tok::kw_float:
3235 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003236 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003237 case tok::kw__Bool:
3238 case tok::kw__Decimal32:
3239 case tok::kw__Decimal64:
3240 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003241 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003242
Chris Lattner861a2262008-04-13 18:59:07 +00003243 // struct-or-union-specifier (C99) or class-specifier (C++)
3244 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003245 case tok::kw_struct:
3246 case tok::kw_union:
3247 // enum-specifier
3248 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003249
Chris Lattneracd58a32006-08-06 17:24:14 +00003250 // type-qualifier
3251 case tok::kw_const:
3252 case tok::kw_volatile:
3253 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003254
Chris Lattneracd58a32006-08-06 17:24:14 +00003255 // function-specifier
3256 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003257 case tok::kw_virtual:
3258 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003259
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003260 // static_assert-declaration
3261 case tok::kw__Static_assert:
3262
Chris Lattner599e47e2007-08-09 17:01:07 +00003263 // GNU typeof support.
3264 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003265
Chris Lattner599e47e2007-08-09 17:01:07 +00003266 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003267 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00003268 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003269
Francois Pichete878cb62011-06-19 08:02:06 +00003270 // C++0x decltype.
3271 case tok::kw_decltype:
3272 return true;
3273
Chris Lattner8b2ec162008-07-26 03:38:44 +00003274 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3275 case tok::less:
3276 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003277
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003278 // typedef-name
3279 case tok::annot_typename:
3280 return !DisambiguatingWithExpression ||
3281 !isStartOfObjCClassMessageMissingOpenBracket();
3282
Steve Narofff192fab2009-01-06 19:34:12 +00003283 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003284 case tok::kw___cdecl:
3285 case tok::kw___stdcall:
3286 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003287 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003288 case tok::kw___w64:
3289 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003290 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003291 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003292 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003293 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003294
3295 case tok::kw___private:
3296 case tok::kw___local:
3297 case tok::kw___global:
3298 case tok::kw___constant:
3299 case tok::kw___read_only:
3300 case tok::kw___read_write:
3301 case tok::kw___write_only:
3302
Eli Friedman53339e02009-06-08 23:27:34 +00003303 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003304 }
3305}
3306
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003307bool Parser::isConstructorDeclarator() {
3308 TentativeParsingAction TPA(*this);
3309
3310 // Parse the C++ scope specifier.
3311 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003312 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00003313 TPA.Revert();
3314 return false;
3315 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003316
3317 // Parse the constructor name.
3318 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3319 // We already know that we have a constructor name; just consume
3320 // the token.
3321 ConsumeToken();
3322 } else {
3323 TPA.Revert();
3324 return false;
3325 }
3326
3327 // Current class name must be followed by a left parentheses.
3328 if (Tok.isNot(tok::l_paren)) {
3329 TPA.Revert();
3330 return false;
3331 }
3332 ConsumeParen();
3333
3334 // A right parentheses or ellipsis signals that we have a constructor.
3335 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3336 TPA.Revert();
3337 return true;
3338 }
3339
3340 // If we need to, enter the specified scope.
3341 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003342 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003343 DeclScopeObj.EnterDeclaratorScope();
3344
Francois Pichet79f3a872011-01-31 04:54:32 +00003345 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003346 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003347 MaybeParseMicrosoftAttributes(Attrs);
3348
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003349 // Check whether the next token(s) are part of a declaration
3350 // specifier, in which case we have the start of a parameter and,
3351 // therefore, we know that this is a constructor.
3352 bool IsConstructor = isDeclarationSpecifier();
3353 TPA.Revert();
3354 return IsConstructor;
3355}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003356
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003357/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003358/// type-qualifier-list: [C99 6.7.5]
3359/// type-qualifier
3360/// [vendor] attributes
3361/// [ only if VendorAttributesAllowed=true ]
3362/// type-qualifier-list type-qualifier
3363/// [vendor] type-qualifier-list attributes
3364/// [ only if VendorAttributesAllowed=true ]
3365/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3366/// [ only if CXX0XAttributesAllowed=true ]
3367/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003368///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003369void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3370 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00003371 bool CXX0XAttributesAllowed) {
3372 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3373 SourceLocation Loc = Tok.getLocation();
John McCall084e83d2011-03-24 11:26:52 +00003374 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003375 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003376 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00003377 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003378 else
3379 Diag(Loc, diag::err_attributes_not_allowed);
3380 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003381
3382 SourceLocation EndLoc;
3383
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003384 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003385 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003386 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003387 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003388 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003389
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003390 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003391 case tok::code_completion:
3392 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003393 return cutOffParsing();
Douglas Gregor28c78432010-08-27 17:35:51 +00003394
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003395 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003396 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3397 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003398 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003399 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003400 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3401 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003402 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003403 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003404 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3405 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003406 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003407
3408 // OpenCL qualifiers:
3409 case tok::kw_private:
3410 if (!getLang().OpenCL)
3411 goto DoneWithTypeQuals;
3412 case tok::kw___private:
3413 case tok::kw___global:
3414 case tok::kw___local:
3415 case tok::kw___constant:
3416 case tok::kw___read_only:
3417 case tok::kw___write_only:
3418 case tok::kw___read_write:
3419 ParseOpenCLQualifiers(DS);
3420 break;
3421
Eli Friedman53339e02009-06-08 23:27:34 +00003422 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003423 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003424 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00003425 case tok::kw___cdecl:
3426 case tok::kw___stdcall:
3427 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003428 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00003429 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003430 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003431 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003432 continue;
3433 }
3434 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003435 case tok::kw___pascal:
3436 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003437 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003438 continue;
3439 }
3440 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003441 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003442 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003443 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003444 continue; // do *not* consume the next token!
3445 }
3446 // otherwise, FALL THROUGH!
3447 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003448 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003449 // If this is not a type-qualifier token, we're done reading type
3450 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003451 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003452 if (EndLoc.isValid())
3453 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003454 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003455 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003456
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003457 // If the specifier combination wasn't legal, issue a diagnostic.
3458 if (isInvalid) {
3459 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003460 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003461 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003462 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003463 }
3464}
3465
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003466
3467/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3468///
3469void Parser::ParseDeclarator(Declarator &D) {
3470 /// This implements the 'declarator' production in the C grammar, then checks
3471 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003472 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003473}
3474
Sebastian Redlbd150f42008-11-21 19:14:01 +00003475/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3476/// is parsed by the function passed to it. Pass null, and the direct-declarator
3477/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003478/// ptr-operator production.
3479///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003480/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3481/// [C] pointer[opt] direct-declarator
3482/// [C++] direct-declarator
3483/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003484///
3485/// pointer: [C99 6.7.5]
3486/// '*' type-qualifier-list[opt]
3487/// '*' type-qualifier-list[opt] pointer
3488///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003489/// ptr-operator:
3490/// '*' cv-qualifier-seq[opt]
3491/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003492/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003493/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003494/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003495/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003496void Parser::ParseDeclaratorInternal(Declarator &D,
3497 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003498 if (Diags.hasAllExtensionsSilenced())
3499 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003500
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003501 // C++ member pointers start with a '::' or a nested-name.
3502 // Member pointers get special handling, since there's no place for the
3503 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00003504 if (getLang().CPlusPlus &&
3505 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3506 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003507 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003508 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00003509
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003510 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003511 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003512 // The scope spec really belongs to the direct-declarator.
3513 D.getCXXScopeSpec() = SS;
3514 if (DirectDeclParser)
3515 (this->*DirectDeclParser)(D);
3516 return;
3517 }
3518
3519 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003520 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003521 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003522 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003523 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003524
3525 // Recurse to parse whatever is left.
3526 ParseDeclaratorInternal(D, DirectDeclParser);
3527
3528 // Sema will have to catch (syntactically invalid) pointers into global
3529 // scope. It has to catch pointers into namespace scope anyway.
3530 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003531 Loc),
3532 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003533 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003534 return;
3535 }
3536 }
3537
3538 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00003539 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00003540 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00003541 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00003542 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00003543 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003544 if (DirectDeclParser)
3545 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003546 return;
3547 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003548
Sebastian Redled0f3b02009-03-15 22:02:01 +00003549 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3550 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00003551 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003552 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00003553
Chris Lattner9eac9312009-03-27 04:18:06 +00003554 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00003555 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00003556 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003557
Bill Wendling3708c182007-05-27 10:15:43 +00003558 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003559 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003560
Bill Wendling3708c182007-05-27 10:15:43 +00003561 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003562 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00003563 if (Kind == tok::star)
3564 // Remember that we parsed a pointer type, and remember the type-quals.
3565 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00003566 DS.getConstSpecLoc(),
3567 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00003568 DS.getRestrictSpecLoc()),
3569 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003570 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00003571 else
3572 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00003573 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003574 Loc),
3575 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003576 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003577 } else {
3578 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00003579 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00003580
Sebastian Redl3b27be62009-03-23 00:00:23 +00003581 // Complain about rvalue references in C++03, but then go on and build
3582 // the declarator.
3583 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00003584 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00003585
Bill Wendling93efb222007-06-02 23:28:54 +00003586 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3587 // cv-qualifiers are introduced through the use of a typedef or of a
3588 // template type argument, in which case the cv-qualifiers are ignored.
3589 //
3590 // [GNU] Retricted references are allowed.
3591 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003592 // [C++0x] Attributes on references are not allowed.
3593 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003594 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00003595
3596 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3597 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3598 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003599 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00003600 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3601 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003602 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00003603 }
Bill Wendling3708c182007-05-27 10:15:43 +00003604
3605 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003606 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00003607
Douglas Gregor66583c52008-11-03 15:51:28 +00003608 if (D.getNumTypeObjects() > 0) {
3609 // C++ [dcl.ref]p4: There shall be no references to references.
3610 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3611 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003612 if (const IdentifierInfo *II = D.getIdentifier())
3613 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3614 << II;
3615 else
3616 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3617 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00003618
Sebastian Redlbd150f42008-11-21 19:14:01 +00003619 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00003620 // can go ahead and build the (technically ill-formed)
3621 // declarator: reference collapsing will take care of it.
3622 }
3623 }
3624
Bill Wendling3708c182007-05-27 10:15:43 +00003625 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00003626 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00003627 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00003628 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003629 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003630 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00003631}
3632
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003633/// ParseDirectDeclarator
3634/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00003635/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003636/// '(' declarator ')'
3637/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00003638/// [C90] direct-declarator '[' constant-expression[opt] ']'
3639/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3640/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3641/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3642/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003643/// direct-declarator '(' parameter-type-list ')'
3644/// direct-declarator '(' identifier-list[opt] ')'
3645/// [GNU] direct-declarator '(' parameter-forward-declarations
3646/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003647/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3648/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00003649/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003650///
3651/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00003652/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00003653/// '::'[opt] nested-name-specifier[opt] type-name
3654///
3655/// id-expression: [C++ 5.1]
3656/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003657/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003658///
3659/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00003660/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003661/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003662/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00003663/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00003664/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00003665///
Chris Lattneracd58a32006-08-06 17:24:14 +00003666void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003667 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003668
Douglas Gregor7861a802009-11-03 01:35:08 +00003669 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3670 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003671 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00003672 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00003673 }
3674
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003675 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003676 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00003677 // Change the declaration context for name lookup, until this function
3678 // is exited (and the declarator has been parsed).
3679 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003680 }
3681
Douglas Gregor27b4c162010-12-23 22:44:42 +00003682 // C++0x [dcl.fct]p14:
3683 // There is a syntactic ambiguity when an ellipsis occurs at the end
3684 // of a parameter-declaration-clause without a preceding comma. In
3685 // this case, the ellipsis is parsed as part of the
3686 // abstract-declarator if the type of the parameter names a template
3687 // parameter pack that has not been expanded; otherwise, it is parsed
3688 // as part of the parameter-declaration-clause.
3689 if (Tok.is(tok::ellipsis) &&
3690 !((D.getContext() == Declarator::PrototypeContext ||
3691 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00003692 NextToken().is(tok::r_paren) &&
3693 !Actions.containsUnexpandedParameterPacks(D)))
3694 D.setEllipsisLoc(ConsumeToken());
3695
Douglas Gregor7861a802009-11-03 01:35:08 +00003696 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3697 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3698 // We found something that indicates the start of an unqualified-id.
3699 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00003700 bool AllowConstructorName;
3701 if (D.getDeclSpec().hasTypeSpecifier())
3702 AllowConstructorName = false;
3703 else if (D.getCXXScopeSpec().isSet())
3704 AllowConstructorName =
3705 (D.getContext() == Declarator::FileContext ||
3706 (D.getContext() == Declarator::MemberContext &&
3707 D.getDeclSpec().isFriendSpecified()));
3708 else
3709 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3710
Douglas Gregor7861a802009-11-03 01:35:08 +00003711 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3712 /*EnteringContext=*/true,
3713 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003714 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00003715 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003716 D.getName()) ||
3717 // Once we're past the identifier, if the scope was bad, mark the
3718 // whole declarator bad.
3719 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003720 D.SetIdentifier(0, Tok.getLocation());
3721 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00003722 } else {
3723 // Parsed the unqualified-id; update range information and move along.
3724 if (D.getSourceRange().getBegin().isInvalid())
3725 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3726 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003727 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003728 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003729 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003730 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003731 assert(!getLang().CPlusPlus &&
3732 "There's a C++-specific check for tok::identifier above");
3733 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3734 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3735 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00003736 goto PastIdentifier;
3737 }
3738
3739 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003740 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00003741 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00003742 // Example: 'char (*X)' or 'int (*XX)(void)'
3743 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003744
3745 // If the declarator was parenthesized, we entered the declarator
3746 // scope when parsing the parenthesized declarator, then exited
3747 // the scope already. Re-enter the scope, if we need to.
3748 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003749 // If there was an error parsing parenthesized declarator, declarator
3750 // scope may have been enterred before. Don't do it again.
3751 if (!D.isInvalidType() &&
3752 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003753 // Change the declaration context for name lookup, until this function
3754 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003755 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003756 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003757 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003758 // This could be something simple like "int" (in which case the declarator
3759 // portion is empty), if an abstract-declarator is allowed.
3760 D.SetIdentifier(0, Tok.getLocation());
3761 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00003762 if (D.getContext() == Declarator::MemberContext)
3763 Diag(Tok, diag::err_expected_member_name_or_semi)
3764 << D.getDeclSpec().getSourceRange();
3765 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00003766 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003767 else
Chris Lattner6d29c102008-11-18 07:48:38 +00003768 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00003769 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00003770 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00003771 }
Mike Stump11289f42009-09-09 15:08:12 +00003772
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003773 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00003774 assert(D.isPastIdentifier() &&
3775 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00003776
Alexis Hunt96d5c762009-11-21 08:43:09 +00003777 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00003778 if (D.getIdentifier())
3779 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003780
Chris Lattneracd58a32006-08-06 17:24:14 +00003781 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00003782 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003783 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3784 // In such a case, check if we actually have a function declarator; if it
3785 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003786 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3787 // When not in file scope, warn for ambiguous function declarators, just
3788 // in case the author intended it as a variable definition.
3789 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3790 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3791 break;
3792 }
John McCall084e83d2011-03-24 11:26:52 +00003793 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003794 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00003795 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00003796 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00003797 } else {
3798 break;
3799 }
3800 }
3801}
3802
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003803/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3804/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00003805/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003806/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3807///
3808/// direct-declarator:
3809/// '(' declarator ')'
3810/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003811/// direct-declarator '(' parameter-type-list ')'
3812/// direct-declarator '(' identifier-list[opt] ')'
3813/// [GNU] direct-declarator '(' parameter-forward-declarations
3814/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003815///
3816void Parser::ParseParenDeclarator(Declarator &D) {
3817 SourceLocation StartLoc = ConsumeParen();
3818 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00003819
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003820 // Eat any attributes before we look at whether this is a grouping or function
3821 // declarator paren. If this is a grouping paren, the attribute applies to
3822 // the type being built up, for example:
3823 // int (__attribute__(()) *x)(long y)
3824 // If this ends up not being a grouping paren, the attribute applies to the
3825 // first argument, for example:
3826 // int (__attribute__(()) int x)
3827 // In either case, we need to eat any attributes to be able to determine what
3828 // sort of paren this is.
3829 //
John McCall084e83d2011-03-24 11:26:52 +00003830 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003831 bool RequiresArg = false;
3832 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003833 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003834
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003835 // We require that the argument list (if this is a non-grouping paren) be
3836 // present even if the attribute list was empty.
3837 RequiresArg = true;
3838 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003839 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003840 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003841 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet17ed0202011-08-18 09:59:55 +00003842 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +00003843 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall53fa7142010-12-24 02:08:15 +00003844 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003845 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003846 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003847 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003848 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003849
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003850 // If we haven't past the identifier yet (or where the identifier would be
3851 // stored, if this is an abstract declarator), then this is probably just
3852 // grouping parens. However, if this could be an abstract-declarator, then
3853 // this could also be the start of function arguments (consider 'void()').
3854 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003855
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003856 if (!D.mayOmitIdentifier()) {
3857 // If this can't be an abstract-declarator, this *must* be a grouping
3858 // paren, because we haven't seen the identifier yet.
3859 isGrouping = true;
3860 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003861 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003862 isDeclarationSpecifier()) { // 'int(int)' is a function.
3863 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3864 // considered to be a type, not a K&R identifier-list.
3865 isGrouping = false;
3866 } else {
3867 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3868 isGrouping = true;
3869 }
Mike Stump11289f42009-09-09 15:08:12 +00003870
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003871 // If this is a grouping paren, handle:
3872 // direct-declarator: '(' declarator ')'
3873 // direct-declarator: '(' attributes declarator ')'
3874 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003875 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003876 D.setGroupingParens(true);
3877
Sebastian Redlbd150f42008-11-21 19:14:01 +00003878 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003879 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003880 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003881 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3882 attrs, EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003883
3884 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003885 return;
3886 }
Mike Stump11289f42009-09-09 15:08:12 +00003887
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003888 // Okay, if this wasn't a grouping paren, it must be the start of a function
3889 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003890 // identifier (and remember where it would have been), then call into
3891 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003892 D.SetIdentifier(0, Tok.getLocation());
3893
John McCall53fa7142010-12-24 02:08:15 +00003894 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003895}
3896
3897/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3898/// declarator D up to a paren, which indicates that we are parsing function
3899/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003900///
Douglas Gregor9e66af42011-07-05 16:44:18 +00003901/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003902/// after the open paren - they should be considered to be the first argument of
3903/// a parameter. If RequiresArg is true, then the first argument of the
3904/// function is required to be present and required to not be an identifier
3905/// list.
3906///
Douglas Gregor9e66af42011-07-05 16:44:18 +00003907/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3908/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
3909/// (C++0x) trailing-return-type[opt].
3910///
3911/// [C++0x] exception-specification:
3912/// dynamic-exception-specification
3913/// noexcept-specification
3914///
3915void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3916 ParsedAttributes &attrs,
3917 bool RequiresArg) {
3918 // lparen is already consumed!
3919 assert(D.isPastIdentifier() && "Should not call before identifier!");
3920
3921 // This should be true when the function has typed arguments.
3922 // Otherwise, it is treated as a K&R-style function.
3923 bool HasProto = false;
3924 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003925 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00003926 // Remember where we see an ellipsis, if any.
3927 SourceLocation EllipsisLoc;
3928
3929 DeclSpec DS(AttrFactory);
3930 bool RefQualifierIsLValueRef = true;
3931 SourceLocation RefQualifierLoc;
3932 ExceptionSpecificationType ESpecType = EST_None;
3933 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003934 SmallVector<ParsedType, 2> DynamicExceptions;
3935 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00003936 ExprResult NoexceptExpr;
3937 ParsedType TrailingReturnType;
3938
3939 SourceLocation EndLoc;
3940
3941 if (isFunctionDeclaratorIdentifierList()) {
3942 if (RequiresArg)
3943 Diag(Tok, diag::err_argument_required_after_attribute);
3944
3945 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
3946
3947 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3948 } else {
3949 // Enter function-declaration scope, limiting any declarators to the
3950 // function prototype scope, including parameter declarators.
3951 ParseScope PrototypeScope(this,
3952 Scope::FunctionPrototypeScope|Scope::DeclScope);
3953
3954 if (Tok.isNot(tok::r_paren))
3955 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
3956 else if (RequiresArg)
3957 Diag(Tok, diag::err_argument_required_after_attribute);
3958
3959 HasProto = ParamInfo.size() || getLang().CPlusPlus;
3960
3961 // If we have the closing ')', eat it.
3962 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3963
3964 if (getLang().CPlusPlus) {
3965 MaybeParseCXX0XAttributes(attrs);
3966
3967 // Parse cv-qualifier-seq[opt].
3968 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
3969 if (!DS.getSourceRange().getEnd().isInvalid())
3970 EndLoc = DS.getSourceRange().getEnd();
3971
3972 // Parse ref-qualifier[opt].
3973 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3974 if (!getLang().CPlusPlus0x)
3975 Diag(Tok, diag::ext_ref_qualifier);
3976
3977 RefQualifierIsLValueRef = Tok.is(tok::amp);
3978 RefQualifierLoc = ConsumeToken();
3979 EndLoc = RefQualifierLoc;
3980 }
3981
3982 // Parse exception-specification[opt].
3983 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3984 DynamicExceptions,
3985 DynamicExceptionRanges,
3986 NoexceptExpr);
3987 if (ESpecType != EST_None)
3988 EndLoc = ESpecRange.getEnd();
3989
3990 // Parse trailing-return-type[opt].
3991 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003992 SourceRange Range;
3993 TrailingReturnType = ParseTrailingReturnType(Range).get();
3994 if (Range.getEnd().isValid())
3995 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00003996 }
3997 }
3998
3999 // Leave prototype scope.
4000 PrototypeScope.Exit();
4001 }
4002
4003 // Remember that we parsed a function type, and remember the attributes.
4004 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4005 /*isVariadic=*/EllipsisLoc.isValid(),
4006 EllipsisLoc,
4007 ParamInfo.data(), ParamInfo.size(),
4008 DS.getTypeQualifiers(),
4009 RefQualifierIsLValueRef,
4010 RefQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004011 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004012 ESpecType, ESpecRange.getBegin(),
4013 DynamicExceptions.data(),
4014 DynamicExceptionRanges.data(),
4015 DynamicExceptions.size(),
4016 NoexceptExpr.isUsable() ?
4017 NoexceptExpr.get() : 0,
4018 LParenLoc, EndLoc, D,
4019 TrailingReturnType),
4020 attrs, EndLoc);
4021}
4022
4023/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4024/// identifier list form for a K&R-style function: void foo(a,b,c)
4025///
4026/// Note that identifier-lists are only allowed for normal declarators, not for
4027/// abstract-declarators.
4028bool Parser::isFunctionDeclaratorIdentifierList() {
4029 return !getLang().CPlusPlus
4030 && Tok.is(tok::identifier)
4031 && !TryAltiVecVectorToken()
4032 // K&R identifier lists can't have typedefs as identifiers, per C99
4033 // 6.7.5.3p11.
4034 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4035 // Identifier lists follow a really simple grammar: the identifiers can
4036 // be followed *only* by a ", identifier" or ")". However, K&R
4037 // identifier lists are really rare in the brave new modern world, and
4038 // it is very common for someone to typo a type in a non-K&R style
4039 // list. If we are presented with something like: "void foo(intptr x,
4040 // float y)", we don't want to start parsing the function declarator as
4041 // though it is a K&R style declarator just because intptr is an
4042 // invalid type.
4043 //
4044 // To handle this, we check to see if the token after the first
4045 // identifier is a "," or ")". Only then do we parse it as an
4046 // identifier list.
4047 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4048}
4049
4050/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4051/// we found a K&R-style identifier list instead of a typed parameter list.
4052///
4053/// After returning, ParamInfo will hold the parsed parameters.
4054///
4055/// identifier-list: [C99 6.7.5]
4056/// identifier
4057/// identifier-list ',' identifier
4058///
4059void Parser::ParseFunctionDeclaratorIdentifierList(
4060 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004061 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004062 // If there was no identifier specified for the declarator, either we are in
4063 // an abstract-declarator, or we are in a parameter declarator which was found
4064 // to be abstract. In abstract-declarators, identifier lists are not valid:
4065 // diagnose this.
4066 if (!D.getIdentifier())
4067 Diag(Tok, diag::ext_ident_list_in_param);
4068
4069 // Maintain an efficient lookup of params we have seen so far.
4070 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4071
4072 while (1) {
4073 // If this isn't an identifier, report the error and skip until ')'.
4074 if (Tok.isNot(tok::identifier)) {
4075 Diag(Tok, diag::err_expected_ident);
4076 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4077 // Forget we parsed anything.
4078 ParamInfo.clear();
4079 return;
4080 }
4081
4082 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4083
4084 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4085 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4086 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4087
4088 // Verify that the argument identifier has not already been mentioned.
4089 if (!ParamsSoFar.insert(ParmII)) {
4090 Diag(Tok, diag::err_param_redefinition) << ParmII;
4091 } else {
4092 // Remember this identifier in ParamInfo.
4093 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4094 Tok.getLocation(),
4095 0));
4096 }
4097
4098 // Eat the identifier.
4099 ConsumeToken();
4100
4101 // The list continues if we see a comma.
4102 if (Tok.isNot(tok::comma))
4103 break;
4104 ConsumeToken();
4105 }
4106}
4107
4108/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4109/// after the opening parenthesis. This function will not parse a K&R-style
4110/// identifier list.
4111///
4112/// D is the declarator being parsed. If attrs is non-null, then the caller
4113/// parsed those arguments immediately after the open paren - they should be
4114/// considered to be the first argument of a parameter.
4115///
4116/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4117/// be the location of the ellipsis, if any was parsed.
4118///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004119/// parameter-type-list: [C99 6.7.5]
4120/// parameter-list
4121/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004122/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004123///
4124/// parameter-list: [C99 6.7.5]
4125/// parameter-declaration
4126/// parameter-list ',' parameter-declaration
4127///
4128/// parameter-declaration: [C99 6.7.5]
4129/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004130/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004131/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00004132/// declaration-specifiers abstract-declarator[opt]
4133/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00004134/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004135/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004136///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004137void Parser::ParseParameterDeclarationClause(
4138 Declarator &D,
4139 ParsedAttributes &attrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004140 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004141 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00004142
Chris Lattner371ed4e2008-04-06 06:57:35 +00004143 while (1) {
4144 if (Tok.is(tok::ellipsis)) {
Douglas Gregor94349fd2009-02-18 07:07:28 +00004145 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004146 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004147 }
Mike Stump11289f42009-09-09 15:08:12 +00004148
Chris Lattner371ed4e2008-04-06 06:57:35 +00004149 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004150 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004151 DeclSpec DS(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004152
4153 // Skip any Microsoft attributes before a param.
4154 if (getLang().Microsoft && Tok.is(tok::l_square))
4155 ParseMicrosoftAttributes(DS.getAttributes());
4156
4157 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004158
4159 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004160 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004161 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4162 // attributes lost? Should they even be allowed?
4163 // FIXME: If we can leave the attributes in the token stream somehow, we can
4164 // get rid of a parameter (attrs) and this statement. It might be too much
4165 // hassle.
John McCall53fa7142010-12-24 02:08:15 +00004166 DS.takeAttributesFrom(attrs);
4167
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004168 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004169
Chris Lattner371ed4e2008-04-06 06:57:35 +00004170 // Parse the declarator. This is "PrototypeContext", because we must
4171 // accept either 'declarator' or 'abstract-declarator' here.
4172 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4173 ParseDeclarator(ParmDecl);
4174
4175 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00004176 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00004177
Chris Lattner371ed4e2008-04-06 06:57:35 +00004178 // Remember this parsed parameter in ParamInfo.
4179 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00004180
Douglas Gregor4d87df52008-12-16 21:30:33 +00004181 // DefArgToks is used when the parsing of default arguments needs
4182 // to be delayed.
4183 CachedTokens *DefArgToks = 0;
4184
Chris Lattner371ed4e2008-04-06 06:57:35 +00004185 // If no parameter was specified, verify that *something* was specified,
4186 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004187 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4188 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00004189 // Completely missing, emit error.
4190 Diag(DSStart, diag::err_missing_param);
4191 } else {
4192 // Otherwise, we have something. Add it and let semantic analysis try
4193 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00004194
Chris Lattner371ed4e2008-04-06 06:57:35 +00004195 // Inform the actions module about the parameter declarator, so it gets
4196 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00004197 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004198
4199 // Parse the default argument, if any. We parse the default
4200 // arguments in all dialects; the semantic analysis in
4201 // ActOnParamDefaultArgument will reject the default argument in
4202 // C.
4203 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00004204 SourceLocation EqualLoc = Tok.getLocation();
4205
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004206 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00004207 if (D.getContext() == Declarator::MemberContext) {
4208 // If we're inside a class definition, cache the tokens
4209 // corresponding to the default argument. We'll actually parse
4210 // them when we see the end of the class definition.
4211 // FIXME: Templates will require something similar.
4212 // FIXME: Can we use a smart pointer for Toks?
4213 DefArgToks = new CachedTokens;
4214
Mike Stump11289f42009-09-09 15:08:12 +00004215 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00004216 /*StopAtSemi=*/true,
4217 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004218 delete DefArgToks;
4219 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00004220 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004221 } else {
4222 // Mark the end of the default argument so that we know when to
4223 // stop when we parse it later on.
4224 Token DefArgEnd;
4225 DefArgEnd.startToken();
4226 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4227 DefArgEnd.setLocation(Tok.getLocation());
4228 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00004229 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00004230 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004231 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004232 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004233 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00004234 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004235
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004236 // The argument isn't actually potentially evaluated unless it is
4237 // used.
4238 EnterExpressionEvaluationContext Eval(Actions,
4239 Sema::PotentiallyEvaluatedIfUsed);
4240
John McCalldadc5752010-08-24 06:29:42 +00004241 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004242 if (DefArgResult.isInvalid()) {
4243 Actions.ActOnParamDefaultArgumentError(Param);
4244 SkipUntil(tok::comma, tok::r_paren, true, true);
4245 } else {
4246 // Inform the actions module about the default argument
4247 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00004248 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004249 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004250 }
4251 }
Mike Stump11289f42009-09-09 15:08:12 +00004252
4253 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4254 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00004255 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00004256 }
4257
4258 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004259 if (Tok.isNot(tok::comma)) {
4260 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004261 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4262
4263 if (!getLang().CPlusPlus) {
4264 // We have ellipsis without a preceding ',', which is ill-formed
4265 // in C. Complain and provide the fix.
4266 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00004267 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004268 }
4269 }
4270
4271 break;
4272 }
Mike Stump11289f42009-09-09 15:08:12 +00004273
Chris Lattner371ed4e2008-04-06 06:57:35 +00004274 // Consume the comma.
4275 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00004276 }
Mike Stump11289f42009-09-09 15:08:12 +00004277
Chris Lattner6c940e62008-04-06 06:34:08 +00004278}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004279
Chris Lattnere8074e62006-08-06 18:30:15 +00004280/// [C90] direct-declarator '[' constant-expression[opt] ']'
4281/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4282/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4283/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4284/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4285void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00004286 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00004287
Chris Lattner84a11622008-12-18 07:27:21 +00004288 // C array syntax has many features, but by-far the most common is [] and [4].
4289 // This code does a fast path to handle some of the most obvious cases.
4290 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004291 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004292 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004293 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004294
Chris Lattner84a11622008-12-18 07:27:21 +00004295 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004296 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004297 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00004298 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004299 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004300 return;
4301 } else if (Tok.getKind() == tok::numeric_constant &&
4302 GetLookAheadToken(1).is(tok::r_square)) {
4303 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00004304 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00004305 ConsumeToken();
4306
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004307 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004308 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004309 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004310
Chris Lattner84a11622008-12-18 07:27:21 +00004311 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004312 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004313 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00004314 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004315 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004316 return;
4317 }
Mike Stump11289f42009-09-09 15:08:12 +00004318
Chris Lattnere8074e62006-08-06 18:30:15 +00004319 // If valid, this location is the position where we read the 'static' keyword.
4320 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004321 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004322 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004323
Chris Lattnere8074e62006-08-06 18:30:15 +00004324 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004325 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004326 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004327 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004328
Chris Lattnere8074e62006-08-06 18:30:15 +00004329 // If we haven't already read 'static', check to see if there is one after the
4330 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004331 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004332 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004333
Chris Lattnere8074e62006-08-06 18:30:15 +00004334 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004335 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004336 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004337
Chris Lattner521ff2b2008-04-06 05:26:30 +00004338 // Handle the case where we have '[*]' as the array size. However, a leading
4339 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4340 // the the token after the star is a ']'. Since stars in arrays are
4341 // infrequent, use of lookahead is not costly here.
4342 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004343 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004344
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004345 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004346 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004347 StaticLoc = SourceLocation(); // Drop the static.
4348 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004349 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004350 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004351 // Note, in C89, this production uses the constant-expr production instead
4352 // of assignment-expr. The only difference is that assignment-expr allows
4353 // things like '=' and '*='. Sema rejects these in C89 mode because they
4354 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004355
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004356 // Parse the constant-expression or assignment-expression now (depending
4357 // on dialect).
4358 if (getLang().CPlusPlus)
4359 NumElements = ParseConstantExpression();
4360 else
4361 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00004362 }
Mike Stump11289f42009-09-09 15:08:12 +00004363
Chris Lattner62591722006-08-12 18:40:58 +00004364 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004365 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004366 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004367 // If the expression was invalid, skip it.
4368 SkipUntil(tok::r_square);
4369 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004370 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004371
4372 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4373
John McCall084e83d2011-03-24 11:26:52 +00004374 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004375 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004376
Chris Lattner84a11622008-12-18 07:27:21 +00004377 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004378 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004379 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004380 NumElements.release(),
4381 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004382 attrs, EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00004383}
4384
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004385/// [GNU] typeof-specifier:
4386/// typeof ( expressions )
4387/// typeof ( type-name )
4388/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004389///
4390void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004391 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004392 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004393 SourceLocation StartLoc = ConsumeToken();
4394
John McCalle8595032010-01-13 20:03:27 +00004395 const bool hasParens = Tok.is(tok::l_paren);
4396
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004397 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004398 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004399 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004400 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4401 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004402 if (hasParens)
4403 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004404
4405 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004406 // FIXME: Not accurate, the range gets one token more than it should.
4407 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004408 else
4409 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004410
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004411 if (isCastExpr) {
4412 if (!CastTy) {
4413 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004414 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00004415 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004416
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004417 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004418 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004419 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4420 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00004421 DiagID, CastTy))
4422 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004423 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004424 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004425
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004426 // If we get here, the operand to the typeof was an expresion.
4427 if (Operand.isInvalid()) {
4428 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00004429 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00004430 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004431
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004432 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004433 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004434 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4435 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00004436 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00004437 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00004438}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004439
4440
4441/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4442/// from TryAltiVecVectorToken.
4443bool Parser::TryAltiVecVectorTokenOutOfLine() {
4444 Token Next = NextToken();
4445 switch (Next.getKind()) {
4446 default: return false;
4447 case tok::kw_short:
4448 case tok::kw_long:
4449 case tok::kw_signed:
4450 case tok::kw_unsigned:
4451 case tok::kw_void:
4452 case tok::kw_char:
4453 case tok::kw_int:
4454 case tok::kw_float:
4455 case tok::kw_double:
4456 case tok::kw_bool:
4457 case tok::kw___pixel:
4458 Tok.setKind(tok::kw___vector);
4459 return true;
4460 case tok::identifier:
4461 if (Next.getIdentifierInfo() == Ident_pixel) {
4462 Tok.setKind(tok::kw___vector);
4463 return true;
4464 }
4465 return false;
4466 }
4467}
4468
4469bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4470 const char *&PrevSpec, unsigned &DiagID,
4471 bool &isInvalid) {
4472 if (Tok.getIdentifierInfo() == Ident_vector) {
4473 Token Next = NextToken();
4474 switch (Next.getKind()) {
4475 case tok::kw_short:
4476 case tok::kw_long:
4477 case tok::kw_signed:
4478 case tok::kw_unsigned:
4479 case tok::kw_void:
4480 case tok::kw_char:
4481 case tok::kw_int:
4482 case tok::kw_float:
4483 case tok::kw_double:
4484 case tok::kw_bool:
4485 case tok::kw___pixel:
4486 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4487 return true;
4488 case tok::identifier:
4489 if (Next.getIdentifierInfo() == Ident_pixel) {
4490 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4491 return true;
4492 }
4493 break;
4494 default:
4495 break;
4496 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00004497 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004498 DS.isTypeAltiVecVector()) {
4499 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4500 return true;
4501 }
4502 return false;
4503}