blob: b5ce193521e8e124b388bd08b6b47379ca331793 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000022#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000023#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// C99 6.7: Declarations.
28//===----------------------------------------------------------------------===//
29
30/// ParseTypeName
31/// type-name: [C99 6.7.6]
32/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000033///
34/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000035TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000036 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000037 AccessSpecifier AS,
38 Decl **OwnedType) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000039 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith7796eb52012-03-12 08:56:40 +000040
Reid Spencer5f016e22007-07-11 17:01:13 +000041 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000042 DeclSpec DS(AttrFactory);
Richard Smith7796eb52012-03-12 08:56:40 +000043 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000044 if (OwnedType)
45 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000046
Reid Spencer5f016e22007-07-11 17:01:13 +000047 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000048 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000049 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000050 if (Range)
51 *Range = DeclaratorInfo.getSourceRange();
52
Chris Lattnereaaebc72009-04-25 08:06:05 +000053 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000054 return true;
55
Douglas Gregor23c94db2010-07-02 17:43:08 +000056 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000057}
58
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000059
60/// isAttributeLateParsed - Return true if the attribute has arguments that
61/// require late parsing.
62static bool isAttributeLateParsed(const IdentifierInfo &II) {
63 return llvm::StringSwitch<bool>(II.getName())
64#include "clang/Parse/AttrLateParsed.inc"
65 .Default(false);
66}
67
68
Sean Huntbbd37c62009-11-21 08:43:09 +000069/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000070///
71/// [GNU] attributes:
72/// attribute
73/// attributes attribute
74///
75/// [GNU] attribute:
76/// '__attribute__' '(' '(' attribute-list ')' ')'
77///
78/// [GNU] attribute-list:
79/// attrib
80/// attribute_list ',' attrib
81///
82/// [GNU] attrib:
83/// empty
84/// attrib-name
85/// attrib-name '(' identifier ')'
86/// attrib-name '(' identifier ',' nonempty-expr-list ')'
87/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
88///
89/// [GNU] attrib-name:
90/// identifier
91/// typespec
92/// typequal
93/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000094///
Reid Spencer5f016e22007-07-11 17:01:13 +000095/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000096/// token lookahead. Comment from gcc: "If they start with an identifier
97/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000098/// start with that identifier; otherwise they are an expression list."
99///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000100/// GCC does not require the ',' between attribs in an attribute-list.
101///
Reid Spencer5f016e22007-07-11 17:01:13 +0000102/// At the moment, I am not doing 2 token lookahead. I am also unaware of
103/// any attributes that don't work (based on my limited testing). Most
104/// attributes are very simple in practice. Until we find a bug, I don't see
105/// a pressing need to implement the 2 token lookahead.
106
John McCall7f040a92010-12-24 02:08:15 +0000107void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000108 SourceLocation *endLoc,
109 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000110 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Chris Lattner04d66662007-10-09 17:33:22 +0000112 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 ConsumeToken();
114 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
115 "attribute")) {
116 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000117 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 }
119 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
120 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000121 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 }
123 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000124 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
125 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000126 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
128 ConsumeToken();
129 continue;
130 }
131 // we have an identifier or declaration specifier (const, int, etc.)
132 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
133 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000135 if (Tok.is(tok::l_paren)) {
136 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000137 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000138 LateParsedAttribute *LA =
139 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
140 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000141
142 // Attributes in a class are parsed at the end of the class, along
143 // with other late-parsed declarations.
144 if (!ClassStack.empty())
145 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000147 // consume everything up to and including the matching right parens
148 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000150 Token Eof;
151 Eof.startToken();
152 Eof.setLocation(Tok.getLocation());
153 LA->Toks.push_back(Eof);
154 } else {
155 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 }
157 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000158 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
159 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 }
161 }
162 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000164 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000165 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
166 SkipUntil(tok::r_paren, false);
167 }
John McCall7f040a92010-12-24 02:08:15 +0000168 if (endLoc)
169 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000171}
172
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000173
174/// Parse the arguments to a parameterized GNU attribute
175void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
176 SourceLocation AttrNameLoc,
177 ParsedAttributes &Attrs,
178 SourceLocation *EndLoc) {
179
180 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
181
182 // Availability attributes have their own grammar.
183 if (AttrName->isStr("availability")) {
184 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
185 return;
186 }
187 // Thread safety attributes fit into the FIXME case above, so we
188 // just parse the arguments as a list of expressions
189 if (IsThreadSafetyAttribute(AttrName->getName())) {
190 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
191 return;
192 }
193
194 ConsumeParen(); // ignore the left paren loc for now
195
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000196 IdentifierInfo *ParmName = 0;
197 SourceLocation ParmLoc;
198 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000199
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000200 switch (Tok.getKind()) {
201 case tok::kw_char:
202 case tok::kw_wchar_t:
203 case tok::kw_char16_t:
204 case tok::kw_char32_t:
205 case tok::kw_bool:
206 case tok::kw_short:
207 case tok::kw_int:
208 case tok::kw_long:
209 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000210 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000211 case tok::kw_signed:
212 case tok::kw_unsigned:
213 case tok::kw_float:
214 case tok::kw_double:
215 case tok::kw_void:
216 case tok::kw_typeof:
217 // __attribute__(( vec_type_hint(char) ))
218 // FIXME: Don't just discard the builtin type token.
219 ConsumeToken();
220 BuiltinType = true;
221 break;
222
223 case tok::identifier:
224 ParmName = Tok.getIdentifierInfo();
225 ParmLoc = ConsumeToken();
226 break;
227
228 default:
229 break;
230 }
231
232 ExprVector ArgExprs(Actions);
233
234 if (!BuiltinType &&
235 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
236 // Eat the comma.
237 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000238 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000239
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000240 // Parse the non-empty comma-separated list of expressions.
241 while (1) {
242 ExprResult ArgExpr(ParseAssignmentExpression());
243 if (ArgExpr.isInvalid()) {
244 SkipUntil(tok::r_paren);
245 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000246 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000247 ArgExprs.push_back(ArgExpr.release());
248 if (Tok.isNot(tok::comma))
249 break;
250 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000251 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000252 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000253 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
254 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
255 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000256 while (Tok.is(tok::identifier)) {
257 ConsumeToken();
258 if (Tok.is(tok::greater))
259 break;
260 if (Tok.is(tok::comma)) {
261 ConsumeToken();
262 continue;
263 }
264 }
265 if (Tok.isNot(tok::greater))
266 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000267 SkipUntil(tok::r_paren, false, true); // skip until ')'
268 }
269 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000270
271 SourceLocation RParen = Tok.getLocation();
272 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
273 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000274 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000275 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Michael Hane53ac8a2012-03-07 00:12:16 +0000276 if (BuiltinType && attr->getKind() == AttributeList::AT_iboutletcollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000277 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000278 }
279}
280
281
Eli Friedmana23b4852009-06-08 07:21:15 +0000282/// ParseMicrosoftDeclSpec - Parse an __declspec construct
283///
284/// [MS] decl-specifier:
285/// __declspec ( extended-decl-modifier-seq )
286///
287/// [MS] extended-decl-modifier-seq:
288/// extended-decl-modifier[opt]
289/// extended-decl-modifier extended-decl-modifier-seq
290
John McCall7f040a92010-12-24 02:08:15 +0000291void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000292 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000293
Steve Narofff59e17e2008-12-24 20:59:21 +0000294 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000295 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
296 "declspec")) {
297 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000298 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000299 }
Francois Pichet373197b2011-05-07 19:04:49 +0000300
Eli Friedman290eeb02009-06-08 23:27:34 +0000301 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000302 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
303 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000304
305 // FIXME: Remove this when we have proper __declspec(property()) support.
306 // Just skip everything inside property().
307 if (AttrName->getName() == "property") {
308 ConsumeParen();
309 SkipUntil(tok::r_paren);
310 }
Eli Friedmana23b4852009-06-08 07:21:15 +0000311 if (Tok.is(tok::l_paren)) {
312 ConsumeParen();
313 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
314 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000315 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000316 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000317 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000318 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
319 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000320 }
321 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
322 SkipUntil(tok::r_paren, false);
323 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000324 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
325 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000326 }
327 }
328 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
329 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000330 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000331}
332
John McCall7f040a92010-12-24 02:08:15 +0000333void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000334 // Treat these like attributes
335 // FIXME: Allow Sema to distinguish between these and real attributes!
336 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000337 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000338 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000339 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000340 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000341 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
342 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000343 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
344 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000345 // FIXME: Support these properly!
346 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000347 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
348 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000349 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000350}
351
John McCall7f040a92010-12-24 02:08:15 +0000352void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000353 // Treat these like attributes
354 while (Tok.is(tok::kw___pascal)) {
355 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
356 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000357 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
358 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000359 }
John McCall7f040a92010-12-24 02:08:15 +0000360}
361
Peter Collingbournef315fa82011-02-14 01:42:53 +0000362void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
363 // Treat these like attributes
364 while (Tok.is(tok::kw___kernel)) {
365 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000366 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
367 AttrNameLoc, 0, AttrNameLoc, 0,
368 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000369 }
370}
371
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000372void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
373 SourceLocation Loc = Tok.getLocation();
374 switch(Tok.getKind()) {
375 // OpenCL qualifiers:
376 case tok::kw___private:
377 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000378 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000379 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000380 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000381 break;
382
383 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000384 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000385 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000386 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000387 break;
388
389 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000390 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000391 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000392 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000393 break;
394
395 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000396 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000397 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000398 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000399 break;
400
401 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000402 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000403 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000404 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000405 break;
406
407 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000408 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000409 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000410 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000411 break;
412
413 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000414 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000415 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000416 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000417 break;
418 default: break;
419 }
420}
421
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000422/// \brief Parse a version number.
423///
424/// version:
425/// simple-integer
426/// simple-integer ',' simple-integer
427/// simple-integer ',' simple-integer ',' simple-integer
428VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
429 Range = Tok.getLocation();
430
431 if (!Tok.is(tok::numeric_constant)) {
432 Diag(Tok, diag::err_expected_version);
433 SkipUntil(tok::comma, tok::r_paren, true, true, true);
434 return VersionTuple();
435 }
436
437 // Parse the major (and possibly minor and subminor) versions, which
438 // are stored in the numeric constant. We utilize a quirk of the
439 // lexer, which is that it handles something like 1.2.3 as a single
440 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000441 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000442 Buffer.resize(Tok.getLength()+1);
443 const char *ThisTokBegin = &Buffer[0];
444
445 // Get the spelling of the token, which eliminates trigraphs, etc.
446 bool Invalid = false;
447 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
448 if (Invalid)
449 return VersionTuple();
450
451 // Parse the major version.
452 unsigned AfterMajor = 0;
453 unsigned Major = 0;
454 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
455 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
456 ++AfterMajor;
457 }
458
459 if (AfterMajor == 0) {
460 Diag(Tok, diag::err_expected_version);
461 SkipUntil(tok::comma, tok::r_paren, true, true, true);
462 return VersionTuple();
463 }
464
465 if (AfterMajor == ActualLength) {
466 ConsumeToken();
467
468 // We only had a single version component.
469 if (Major == 0) {
470 Diag(Tok, diag::err_zero_version);
471 return VersionTuple();
472 }
473
474 return VersionTuple(Major);
475 }
476
477 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
478 Diag(Tok, diag::err_expected_version);
479 SkipUntil(tok::comma, tok::r_paren, true, true, true);
480 return VersionTuple();
481 }
482
483 // Parse the minor version.
484 unsigned AfterMinor = AfterMajor + 1;
485 unsigned Minor = 0;
486 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
487 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
488 ++AfterMinor;
489 }
490
491 if (AfterMinor == ActualLength) {
492 ConsumeToken();
493
494 // We had major.minor.
495 if (Major == 0 && Minor == 0) {
496 Diag(Tok, diag::err_zero_version);
497 return VersionTuple();
498 }
499
500 return VersionTuple(Major, Minor);
501 }
502
503 // If what follows is not a '.', we have a problem.
504 if (ThisTokBegin[AfterMinor] != '.') {
505 Diag(Tok, diag::err_expected_version);
506 SkipUntil(tok::comma, tok::r_paren, true, true, true);
507 return VersionTuple();
508 }
509
510 // Parse the subminor version.
511 unsigned AfterSubminor = AfterMinor + 1;
512 unsigned Subminor = 0;
513 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
514 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
515 ++AfterSubminor;
516 }
517
518 if (AfterSubminor != ActualLength) {
519 Diag(Tok, diag::err_expected_version);
520 SkipUntil(tok::comma, tok::r_paren, true, true, true);
521 return VersionTuple();
522 }
523 ConsumeToken();
524 return VersionTuple(Major, Minor, Subminor);
525}
526
527/// \brief Parse the contents of the "availability" attribute.
528///
529/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000530/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000531///
532/// platform:
533/// identifier
534///
535/// version-arg-list:
536/// version-arg
537/// version-arg ',' version-arg-list
538///
539/// version-arg:
540/// 'introduced' '=' version
541/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000542/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000543/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000544/// opt-message:
545/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000546void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
547 SourceLocation AvailabilityLoc,
548 ParsedAttributes &attrs,
549 SourceLocation *endLoc) {
550 SourceLocation PlatformLoc;
551 IdentifierInfo *Platform = 0;
552
553 enum { Introduced, Deprecated, Obsoleted, Unknown };
554 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000555 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000556
557 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000558 BalancedDelimiterTracker T(*this, tok::l_paren);
559 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000560 Diag(Tok, diag::err_expected_lparen);
561 return;
562 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000563
564 // Parse the platform name,
565 if (Tok.isNot(tok::identifier)) {
566 Diag(Tok, diag::err_availability_expected_platform);
567 SkipUntil(tok::r_paren);
568 return;
569 }
570 Platform = Tok.getIdentifierInfo();
571 PlatformLoc = ConsumeToken();
572
573 // Parse the ',' following the platform name.
574 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
575 return;
576
577 // If we haven't grabbed the pointers for the identifiers
578 // "introduced", "deprecated", and "obsoleted", do so now.
579 if (!Ident_introduced) {
580 Ident_introduced = PP.getIdentifierInfo("introduced");
581 Ident_deprecated = PP.getIdentifierInfo("deprecated");
582 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000583 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000584 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000585 }
586
587 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000588 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000589 do {
590 if (Tok.isNot(tok::identifier)) {
591 Diag(Tok, diag::err_availability_expected_change);
592 SkipUntil(tok::r_paren);
593 return;
594 }
595 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
596 SourceLocation KeywordLoc = ConsumeToken();
597
Douglas Gregorb53e4172011-03-26 03:35:55 +0000598 if (Keyword == Ident_unavailable) {
599 if (UnavailableLoc.isValid()) {
600 Diag(KeywordLoc, diag::err_availability_redundant)
601 << Keyword << SourceRange(UnavailableLoc);
602 }
603 UnavailableLoc = KeywordLoc;
604
605 if (Tok.isNot(tok::comma))
606 break;
607
608 ConsumeToken();
609 continue;
610 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000611
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000612 if (Tok.isNot(tok::equal)) {
613 Diag(Tok, diag::err_expected_equal_after)
614 << Keyword;
615 SkipUntil(tok::r_paren);
616 return;
617 }
618 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000619 if (Keyword == Ident_message) {
620 if (!isTokenStringLiteral()) {
621 Diag(Tok, diag::err_expected_string_literal);
622 SkipUntil(tok::r_paren);
623 return;
624 }
625 MessageExpr = ParseStringLiteralExpression();
626 break;
627 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000628
629 SourceRange VersionRange;
630 VersionTuple Version = ParseVersionTuple(VersionRange);
631
632 if (Version.empty()) {
633 SkipUntil(tok::r_paren);
634 return;
635 }
636
637 unsigned Index;
638 if (Keyword == Ident_introduced)
639 Index = Introduced;
640 else if (Keyword == Ident_deprecated)
641 Index = Deprecated;
642 else if (Keyword == Ident_obsoleted)
643 Index = Obsoleted;
644 else
645 Index = Unknown;
646
647 if (Index < Unknown) {
648 if (!Changes[Index].KeywordLoc.isInvalid()) {
649 Diag(KeywordLoc, diag::err_availability_redundant)
650 << Keyword
651 << SourceRange(Changes[Index].KeywordLoc,
652 Changes[Index].VersionRange.getEnd());
653 }
654
655 Changes[Index].KeywordLoc = KeywordLoc;
656 Changes[Index].Version = Version;
657 Changes[Index].VersionRange = VersionRange;
658 } else {
659 Diag(KeywordLoc, diag::err_availability_unknown_change)
660 << Keyword << VersionRange;
661 }
662
663 if (Tok.isNot(tok::comma))
664 break;
665
666 ConsumeToken();
667 } while (true);
668
669 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000670 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000671 return;
672
673 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000674 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000675
Douglas Gregorb53e4172011-03-26 03:35:55 +0000676 // The 'unavailable' availability cannot be combined with any other
677 // availability changes. Make sure that hasn't happened.
678 if (UnavailableLoc.isValid()) {
679 bool Complained = false;
680 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
681 if (Changes[Index].KeywordLoc.isValid()) {
682 if (!Complained) {
683 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
684 << SourceRange(Changes[Index].KeywordLoc,
685 Changes[Index].VersionRange.getEnd());
686 Complained = true;
687 }
688
689 // Clear out the availability.
690 Changes[Index] = AvailabilityChange();
691 }
692 }
693 }
694
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000695 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000696 attrs.addNew(&Availability,
697 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000698 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000699 Platform, PlatformLoc,
700 Changes[Introduced],
701 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000702 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000703 UnavailableLoc, MessageExpr.take(),
704 false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000705}
706
Caitlin Sadowskieff98fc2011-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() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000718 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000719}
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
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000738 // Enter the scope of nested classes
739 if (!AlreadyHasClassScope)
740 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
741 Class.TagOrTemplate);
742
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000743 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) {
744 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
745 }
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000746
747 if (!AlreadyHasClassScope)
748 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
749 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000750}
751
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000752
753/// \brief Parse all attributes in LAs, and attach them to Decl D.
754void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
755 bool EnterScope, bool OnDefinition) {
756 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000757 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000758 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
759 }
760 LAs.clear();
761}
762
763
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000764/// \brief Finish parsing an attribute for which parsing was delayed.
765/// This will be called at the end of parsing a class declaration
766/// for each LateParsedAttribute. We consume the saved tokens and
767/// create an attribute with the arguments filled in. We add this
768/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000769void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
770 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000771 // Save the current token position.
772 SourceLocation OrigLoc = Tok.getLocation();
773
774 // Append the current token at the end of the new token stream so that it
775 // doesn't get lost.
776 LA.Toks.push_back(Tok);
777 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
778 // Consume the previously pushed token.
779 ConsumeAnyToken();
780
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000781 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
782 Diag(Tok, diag::warn_attribute_on_function_definition)
783 << LA.AttrName.getName();
784 }
785
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000786 ParsedAttributes Attrs(AttrFactory);
787 SourceLocation endLoc;
788
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000789 if (LA.Decls.size() == 1) {
790 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000791
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000792 // If the Decl is templatized, add template parameters to scope.
793 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
794 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
795 if (HasTemplateScope)
796 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000797
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000798 // If the Decl is on a function, add function parameters to the scope.
799 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
800 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
801 if (HasFunctionScope)
802 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
803
804 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
805
806 if (HasFunctionScope) {
807 Actions.ActOnExitFunctionContext();
808 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
809 }
810 if (HasTemplateScope) {
811 TempScope.Exit();
812 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000813 } else if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000814 // If there are multiple decls, then the decl cannot be within the
815 // function scope.
816 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000817 } else {
818 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000819 }
820
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000821 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
822 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
823 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000824
825 if (Tok.getLocation() != OrigLoc) {
826 // Due to a parsing error, we either went over the cached tokens or
827 // there are still cached tokens left, so we skip the leftover tokens.
828 // Since this is an uncommon situation that should be avoided, use the
829 // expensive isBeforeInTranslationUnit call.
830 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
831 OrigLoc))
832 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000833 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000834 }
835}
836
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000837/// \brief Wrapper around a case statement checking if AttrName is
838/// one of the thread safety attributes
839bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
840 return llvm::StringSwitch<bool>(AttrName)
841 .Case("guarded_by", true)
842 .Case("guarded_var", true)
843 .Case("pt_guarded_by", true)
844 .Case("pt_guarded_var", true)
845 .Case("lockable", true)
846 .Case("scoped_lockable", true)
847 .Case("no_thread_safety_analysis", true)
848 .Case("acquired_after", true)
849 .Case("acquired_before", true)
850 .Case("exclusive_lock_function", true)
851 .Case("shared_lock_function", true)
852 .Case("exclusive_trylock_function", true)
853 .Case("shared_trylock_function", true)
854 .Case("unlock_function", true)
855 .Case("lock_returned", true)
856 .Case("locks_excluded", true)
857 .Case("exclusive_locks_required", true)
858 .Case("shared_locks_required", true)
859 .Default(false);
860}
861
862/// \brief Parse the contents of thread safety attributes. These
863/// should always be parsed as an expression list.
864///
865/// We need to special case the parsing due to the fact that if the first token
866/// of the first argument is an identifier, the main parse loop will store
867/// that token as a "parameter" and the rest of
868/// the arguments will be added to a list of "arguments". However,
869/// subsequent tokens in the first argument are lost. We instead parse each
870/// argument as an expression and add all arguments to the list of "arguments".
871/// In future, we will take advantage of this special case to also
872/// deal with some argument scoping issues here (for example, referring to a
873/// function parameter in the attribute on that function).
874void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
875 SourceLocation AttrNameLoc,
876 ParsedAttributes &Attrs,
877 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000878 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000879
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000880 BalancedDelimiterTracker T(*this, tok::l_paren);
881 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000882
883 ExprVector ArgExprs(Actions);
884 bool ArgExprsOk = true;
885
886 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +0000887 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000888 ExprResult ArgExpr(ParseAssignmentExpression());
889 if (ArgExpr.isInvalid()) {
890 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000891 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000892 break;
893 } else {
894 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000895 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000896 if (Tok.isNot(tok::comma))
897 break;
898 ConsumeToken(); // Eat the comma, move to the next argument
899 }
900 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000901 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000902 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
903 ArgExprs.take(), ArgExprs.size());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000904 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000905 if (EndLoc)
906 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000907}
908
John McCall7f040a92010-12-24 02:08:15 +0000909void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
910 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
911 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000912}
913
Reid Spencer5f016e22007-07-11 17:01:13 +0000914/// ParseDeclaration - Parse a full 'declaration', which consists of
915/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000916/// 'Context' should be a Declarator::TheContext value. This returns the
917/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000918///
919/// declaration: [C99 6.7]
920/// block-declaration ->
921/// simple-declaration
922/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000923/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000924/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000925/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000926/// [C++] using-declaration
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000927/// [C++0x/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000928/// others... [FIXME]
929///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000930Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
931 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000932 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000933 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000934 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000935 // Must temporarily exit the objective-c container scope for
936 // parsing c none objective-c decls.
937 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000938
John McCalld226f652010-08-21 09:40:31 +0000939 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000940 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000941 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000942 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000943 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000944 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000945 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000946 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000947 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000948 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +0000949 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000950 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000951 SourceLocation InlineLoc = ConsumeToken();
952 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
953 break;
954 }
John McCall7f040a92010-12-24 02:08:15 +0000955 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000956 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000957 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000958 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000959 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000960 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000961 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000962 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000963 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000964 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000965 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000966 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000967 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000968 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000969 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000970 default:
John McCall7f040a92010-12-24 02:08:15 +0000971 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000972 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000973
Chris Lattner682bf922009-03-29 16:50:03 +0000974 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000975 // single decl, convert it now. Alias declarations can also declare a type;
976 // include that too if it is present.
977 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000978}
979
980/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
981/// declaration-specifiers init-declarator-list[opt] ';'
982///[C90/C++]init-declarator-list ';' [TODO]
983/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000984///
Richard Smithad762fc2011-04-14 22:09:26 +0000985/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
986/// attribute-specifier-seq[opt] type-specifier-seq declarator
987///
Chris Lattnercd147752009-03-29 17:27:48 +0000988/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000989/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000990///
991/// If FRI is non-null, we might be parsing a for-range-declaration instead
992/// of a simple-declaration. If we find that we are, we also parse the
993/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000994Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
995 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000996 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000997 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000998 bool RequireSemi,
999 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001001 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001002 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001003
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001004 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001005 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001006
Reid Spencer5f016e22007-07-11 17:01:13 +00001007 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1008 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001009 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +00001010 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001011 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001012 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001013 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001014 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001015 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001016
1017 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001018}
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Richard Smith0706df42011-10-19 21:33:05 +00001020/// Returns true if this might be the start of a declarator, or a common typo
1021/// for a declarator.
1022bool Parser::MightBeDeclarator(unsigned Context) {
1023 switch (Tok.getKind()) {
1024 case tok::annot_cxxscope:
1025 case tok::annot_template_id:
1026 case tok::caret:
1027 case tok::code_completion:
1028 case tok::coloncolon:
1029 case tok::ellipsis:
1030 case tok::kw___attribute:
1031 case tok::kw_operator:
1032 case tok::l_paren:
1033 case tok::star:
1034 return true;
1035
1036 case tok::amp:
1037 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001038 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001039
Richard Smith1c94c162012-01-09 22:31:44 +00001040 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001041 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001042 NextToken().is(tok::l_square);
1043
1044 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001045 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001046
Richard Smith0706df42011-10-19 21:33:05 +00001047 case tok::identifier:
1048 switch (NextToken().getKind()) {
1049 case tok::code_completion:
1050 case tok::coloncolon:
1051 case tok::comma:
1052 case tok::equal:
1053 case tok::equalequal: // Might be a typo for '='.
1054 case tok::kw_alignas:
1055 case tok::kw_asm:
1056 case tok::kw___attribute:
1057 case tok::l_brace:
1058 case tok::l_paren:
1059 case tok::l_square:
1060 case tok::less:
1061 case tok::r_brace:
1062 case tok::r_paren:
1063 case tok::r_square:
1064 case tok::semi:
1065 return true;
1066
1067 case tok::colon:
1068 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001069 // and in block scope it's probably a label. Inside a class definition,
1070 // this is a bit-field.
1071 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001072 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001073
1074 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001075 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001076
1077 default:
1078 return false;
1079 }
1080
1081 default:
1082 return false;
1083 }
1084}
1085
John McCalld8ac0572009-11-03 19:26:08 +00001086/// ParseDeclGroup - Having concluded that this is either a function
1087/// definition or a group of object declarations, actually parse the
1088/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001089Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1090 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001091 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001092 SourceLocation *DeclEnd,
1093 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001094 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001095 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001096 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001097
John McCalld8ac0572009-11-03 19:26:08 +00001098 // Bail out if the first declarator didn't seem well-formed.
1099 if (!D.hasName() && !D.mayOmitIdentifier()) {
1100 // Skip until ; or }.
1101 SkipUntil(tok::r_brace, true, true);
1102 if (Tok.is(tok::semi))
1103 ConsumeToken();
1104 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001105 }
Mike Stump1eb44332009-09-09 15:08:12 +00001106
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001107 // Save late-parsed attributes for now; they need to be parsed in the
1108 // appropriate function scope after the function Decl has been constructed.
1109 LateParsedAttrList LateParsedAttrs;
1110 if (D.isFunctionDeclarator())
1111 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1112
Chris Lattnerc82daef2010-07-11 22:24:20 +00001113 // Check to see if we have a function *definition* which must have a body.
1114 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1115 // Look at the next token to make sure that this isn't a function
1116 // declaration. We have to check this because __attribute__ might be the
1117 // start of a function definition in GCC-extended K&R C.
1118 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001119
Chris Lattner004659a2010-07-11 22:42:07 +00001120 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001121 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1122 Diag(Tok, diag::err_function_declared_typedef);
1123
1124 // Recover by treating the 'typedef' as spurious.
1125 DS.ClearStorageClassSpecs();
1126 }
1127
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001128 Decl *TheDecl =
1129 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001130 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001131 }
1132
1133 if (isDeclarationSpecifier()) {
1134 // If there is an invalid declaration specifier right after the function
1135 // prototype, then we must be in a missing semicolon case where this isn't
1136 // actually a body. Just fall through into the code that handles it as a
1137 // prototype, and let the top-level code handle the erroneous declspec
1138 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001139 } else {
1140 Diag(Tok, diag::err_expected_fn_body);
1141 SkipUntil(tok::semi);
1142 return DeclGroupPtrTy();
1143 }
1144 }
1145
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001146 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001147 return DeclGroupPtrTy();
1148
1149 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1150 // must parse and analyze the for-range-initializer before the declaration is
1151 // analyzed.
1152 if (FRI && Tok.is(tok::colon)) {
1153 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001154 if (Tok.is(tok::l_brace))
1155 FRI->RangeExpr = ParseBraceInitializer();
1156 else
1157 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001158 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1159 Actions.ActOnCXXForRangeDecl(ThisDecl);
1160 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001161 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001162 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1163 }
1164
Chris Lattner5f9e2722011-07-23 10:55:15 +00001165 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001166 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001167 if (LateParsedAttrs.size() > 0)
1168 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001169 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001170 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001171 DeclsInGroup.push_back(FirstDecl);
1172
Richard Smith0706df42011-10-19 21:33:05 +00001173 bool ExpectSemi = Context != Declarator::ForContext;
1174
John McCalld8ac0572009-11-03 19:26:08 +00001175 // If we don't have a comma, it is either the end of the list (a ';') or an
1176 // error, bail out.
1177 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001178 SourceLocation CommaLoc = ConsumeToken();
1179
1180 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1181 // This comma was followed by a line-break and something which can't be
1182 // the start of a declarator. The comma was probably a typo for a
1183 // semicolon.
1184 Diag(CommaLoc, diag::err_expected_semi_declaration)
1185 << FixItHint::CreateReplacement(CommaLoc, ";");
1186 ExpectSemi = false;
1187 break;
1188 }
John McCalld8ac0572009-11-03 19:26:08 +00001189
1190 // Parse the next declarator.
1191 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001192 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001193
1194 // Accept attributes in an init-declarator. In the first declarator in a
1195 // declaration, these would be part of the declspec. In subsequent
1196 // declarators, they become part of the declarator itself, so that they
1197 // don't apply to declarators after *this* one. Examples:
1198 // short __attribute__((common)) var; -> declspec
1199 // short var __attribute__((common)); -> declarator
1200 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001201 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001202
1203 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001204 if (!D.isInvalidType()) {
1205 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1206 D.complete(ThisDecl);
1207 if (ThisDecl)
1208 DeclsInGroup.push_back(ThisDecl);
1209 }
John McCalld8ac0572009-11-03 19:26:08 +00001210 }
1211
1212 if (DeclEnd)
1213 *DeclEnd = Tok.getLocation();
1214
Richard Smith0706df42011-10-19 21:33:05 +00001215 if (ExpectSemi &&
John McCalld8ac0572009-11-03 19:26:08 +00001216 ExpectAndConsume(tok::semi,
1217 Context == Declarator::FileContext
1218 ? diag::err_invalid_token_after_toplevel_declarator
1219 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001220 // Okay, there was no semicolon and one was expected. If we see a
1221 // declaration specifier, just assume it was missing and continue parsing.
1222 // Otherwise things are very confused and we skip to recover.
1223 if (!isDeclarationSpecifier()) {
1224 SkipUntil(tok::r_brace, true, true);
1225 if (Tok.is(tok::semi))
1226 ConsumeToken();
1227 }
John McCalld8ac0572009-11-03 19:26:08 +00001228 }
1229
Douglas Gregor23c94db2010-07-02 17:43:08 +00001230 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001231 DeclsInGroup.data(),
1232 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001233}
1234
Richard Smithad762fc2011-04-14 22:09:26 +00001235/// Parse an optional simple-asm-expr and attributes, and attach them to a
1236/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001237bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001238 // If a simple-asm-expr is present, parse it.
1239 if (Tok.is(tok::kw_asm)) {
1240 SourceLocation Loc;
1241 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1242 if (AsmLabel.isInvalid()) {
1243 SkipUntil(tok::semi, true, true);
1244 return true;
1245 }
1246
1247 D.setAsmLabel(AsmLabel.release());
1248 D.SetRangeEnd(Loc);
1249 }
1250
1251 MaybeParseGNUAttributes(D);
1252 return false;
1253}
1254
Douglas Gregor1426e532009-05-12 21:31:51 +00001255/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1256/// declarator'. This method parses the remainder of the declaration
1257/// (including any attributes or initializer, among other things) and
1258/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001259///
Reid Spencer5f016e22007-07-11 17:01:13 +00001260/// init-declarator: [C99 6.7]
1261/// declarator
1262/// declarator '=' initializer
1263/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1264/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001265/// [C++] declarator initializer[opt]
1266///
1267/// [C++] initializer:
1268/// [C++] '=' initializer-clause
1269/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001270/// [C++0x] '=' 'default' [TODO]
1271/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001272/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001273///
1274/// According to the standard grammar, =default and =delete are function
1275/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001276///
John McCalld226f652010-08-21 09:40:31 +00001277Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001278 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001279 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001280 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Richard Smithad762fc2011-04-14 22:09:26 +00001282 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1283}
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Richard Smithad762fc2011-04-14 22:09:26 +00001285Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1286 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001287 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001288 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001289 switch (TemplateInfo.Kind) {
1290 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001291 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001292 break;
1293
1294 case ParsedTemplateInfo::Template:
1295 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001296 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001297 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001298 TemplateInfo.TemplateParams->data(),
1299 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001300 D);
1301 break;
1302
1303 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001304 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001305 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001306 TemplateInfo.ExternLoc,
1307 TemplateInfo.TemplateLoc,
1308 D);
1309 if (ThisRes.isInvalid()) {
1310 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001311 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001312 }
1313
1314 ThisDecl = ThisRes.get();
1315 break;
1316 }
1317 }
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Richard Smith34b41d92011-02-20 03:19:35 +00001319 bool TypeContainsAuto =
1320 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1321
Douglas Gregor1426e532009-05-12 21:31:51 +00001322 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001323 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001324 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001325 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001326 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001327 if (D.isFunctionDeclarator())
1328 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1329 << 1 /* delete */;
1330 else
1331 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001332 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001333 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001334 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1335 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001336 else
1337 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001338 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001339 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001340 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001341 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001342 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001343
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001344 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001345 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001346 cutOffParsing();
1347 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001348 }
1349
John McCall60d7b3a2010-08-24 06:29:42 +00001350 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001351
David Blaikie4e4d0842012-03-11 07:00:24 +00001352 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001353 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001354 ExitScope();
1355 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001356
Douglas Gregor1426e532009-05-12 21:31:51 +00001357 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001358 SkipUntil(tok::comma, true, true);
1359 Actions.ActOnInitializerError(ThisDecl);
1360 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001361 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1362 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001363 }
1364 } else if (Tok.is(tok::l_paren)) {
1365 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001366 BalancedDelimiterTracker T(*this, tok::l_paren);
1367 T.consumeOpen();
1368
Douglas Gregor1426e532009-05-12 21:31:51 +00001369 ExprVector Exprs(Actions);
1370 CommaLocsTy CommaLocs;
1371
David Blaikie4e4d0842012-03-11 07:00:24 +00001372 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001373 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001374 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001375 }
1376
Douglas Gregor1426e532009-05-12 21:31:51 +00001377 if (ParseExpressionList(Exprs, CommaLocs)) {
1378 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001379
David Blaikie4e4d0842012-03-11 07:00:24 +00001380 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001381 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001382 ExitScope();
1383 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001384 } else {
1385 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001386 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001387
1388 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1389 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001390
David Blaikie4e4d0842012-03-11 07:00:24 +00001391 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001392 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001393 ExitScope();
1394 }
1395
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001396 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1397 T.getCloseLocation(),
1398 move_arg(Exprs));
1399 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1400 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001401 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001402 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001403 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001404 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1405
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001406 if (D.getCXXScopeSpec().isSet()) {
1407 EnterScope(0);
1408 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1409 }
1410
1411 ExprResult Init(ParseBraceInitializer());
1412
1413 if (D.getCXXScopeSpec().isSet()) {
1414 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1415 ExitScope();
1416 }
1417
1418 if (Init.isInvalid()) {
1419 Actions.ActOnInitializerError(ThisDecl);
1420 } else
1421 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1422 /*DirectInit=*/true, TypeContainsAuto);
1423
Douglas Gregor1426e532009-05-12 21:31:51 +00001424 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001425 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001426 }
1427
Richard Smith483b9f32011-02-21 20:05:19 +00001428 Actions.FinalizeDeclaration(ThisDecl);
1429
Douglas Gregor1426e532009-05-12 21:31:51 +00001430 return ThisDecl;
1431}
1432
Reid Spencer5f016e22007-07-11 17:01:13 +00001433/// ParseSpecifierQualifierList
1434/// specifier-qualifier-list:
1435/// type-specifier specifier-qualifier-list[opt]
1436/// type-qualifier specifier-qualifier-list[opt]
1437/// [GNU] attributes specifier-qualifier-list[opt]
1438///
Richard Smith69730c12012-03-12 07:56:15 +00001439void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1440 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001441 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1442 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001443 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001444 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Reid Spencer5f016e22007-07-11 17:01:13 +00001446 // Validate declspec for type-name.
1447 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith69730c12012-03-12 07:56:15 +00001448 if (DSC == DSC_type_specifier && !DS.hasTypeSpecifier()) {
1449 Diag(Tok, diag::err_expected_type);
1450 DS.SetTypeSpecError();
1451 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1452 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001453 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001454 if (!DS.hasTypeSpecifier())
1455 DS.SetTypeSpecError();
1456 }
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Reid Spencer5f016e22007-07-11 17:01:13 +00001458 // Issue diagnostic and remove storage class if present.
1459 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1460 if (DS.getStorageClassSpecLoc().isValid())
1461 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1462 else
1463 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1464 DS.ClearStorageClassSpecs();
1465 }
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Reid Spencer5f016e22007-07-11 17:01:13 +00001467 // Issue diagnostic and remove function specfier if present.
1468 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001469 if (DS.isInlineSpecified())
1470 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1471 if (DS.isVirtualSpecified())
1472 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1473 if (DS.isExplicitSpecified())
1474 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001475 DS.ClearFunctionSpecs();
1476 }
Richard Smith69730c12012-03-12 07:56:15 +00001477
1478 // Issue diagnostic and remove constexpr specfier if present.
1479 if (DS.isConstexprSpecified()) {
1480 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1481 DS.ClearConstexprSpec();
1482 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001483}
1484
Chris Lattnerc199ab32009-04-12 20:42:31 +00001485/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1486/// specified token is valid after the identifier in a declarator which
1487/// immediately follows the declspec. For example, these things are valid:
1488///
1489/// int x [ 4]; // direct-declarator
1490/// int x ( int y); // direct-declarator
1491/// int(int x ) // direct-declarator
1492/// int x ; // simple-declaration
1493/// int x = 17; // init-declarator-list
1494/// int x , y; // init-declarator-list
1495/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001496/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001497/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001498///
1499/// This is not, because 'x' does not immediately follow the declspec (though
1500/// ')' happens to be valid anyway).
1501/// int (x)
1502///
1503static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1504 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1505 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001506 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001507}
1508
Chris Lattnere40c2952009-04-14 21:34:55 +00001509
1510/// ParseImplicitInt - This method is called when we have an non-typename
1511/// identifier in a declspec (which normally terminates the decl spec) when
1512/// the declspec has no type specifier. In this case, the declspec is either
1513/// malformed or is "implicit int" (in K&R and C89).
1514///
1515/// This method handles diagnosing this prettily and returns false if the
1516/// declspec is done being processed. If it recovers and thinks there may be
1517/// other pieces of declspec after it, it returns true.
1518///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001519bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001520 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001521 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001522 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Chris Lattnere40c2952009-04-14 21:34:55 +00001524 SourceLocation Loc = Tok.getLocation();
1525 // If we see an identifier that is not a type name, we normally would
1526 // parse it as the identifer being declared. However, when a typename
1527 // is typo'd or the definition is not included, this will incorrectly
1528 // parse the typename as the identifier name and fall over misparsing
1529 // later parts of the diagnostic.
1530 //
1531 // As such, we try to do some look-ahead in cases where this would
1532 // otherwise be an "implicit-int" case to see if this is invalid. For
1533 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1534 // an identifier with implicit int, we'd get a parse error because the
1535 // next token is obviously invalid for a type. Parse these as a case
1536 // with an invalid type specifier.
1537 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Chris Lattnere40c2952009-04-14 21:34:55 +00001539 // Since we know that this either implicit int (which is rare) or an
Richard Smith69730c12012-03-12 07:56:15 +00001540 // error, do lookahead to try to do better recovery. This never applies within
1541 // a type specifier.
1542 // FIXME: Don't bail out here in languages with no implicit int (like
1543 // C++ with no -fms-extensions). This is much more likely to be an undeclared
1544 // type or typo than a use of implicit int.
1545 if (DSC != DSC_type_specifier &&
1546 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001547 // If this token is valid for implicit int, e.g. "static x = 4", then
1548 // we just avoid eating the identifier, so it will be parsed as the
1549 // identifier in the declarator.
1550 return false;
1551 }
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Chris Lattnere40c2952009-04-14 21:34:55 +00001553 // Otherwise, if we don't consume this token, we are going to emit an
1554 // error anyway. Try to recover from various common problems. Check
1555 // to see if this was a reference to a tag name without a tag specified.
1556 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001557 //
1558 // C++ doesn't need this, and isTagName doesn't take SS.
1559 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001560 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001561 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Douglas Gregor23c94db2010-07-02 17:43:08 +00001563 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001564 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001565 case DeclSpec::TST_enum:
1566 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1567 case DeclSpec::TST_union:
1568 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1569 case DeclSpec::TST_struct:
1570 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1571 case DeclSpec::TST_class:
1572 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001573 }
Mike Stump1eb44332009-09-09 15:08:12 +00001574
Chris Lattnerf4382f52009-04-14 22:17:06 +00001575 if (TagName) {
1576 Diag(Loc, diag::err_use_of_tag_name_without_tag)
David Blaikie4e4d0842012-03-11 07:00:24 +00001577 << Tok.getIdentifierInfo() << TagName << getLangOpts().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001578 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001579
Chris Lattnerf4382f52009-04-14 22:17:06 +00001580 // Parse this as a tag as if the missing tag were present.
1581 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001582 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001583 else
Richard Smith69730c12012-03-12 07:56:15 +00001584 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1585 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001586 return true;
1587 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001588 }
Mike Stump1eb44332009-09-09 15:08:12 +00001589
Douglas Gregora786fdb2009-10-13 23:27:22 +00001590 // This is almost certainly an invalid type name. Let the action emit a
1591 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001592 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001593 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001594 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001595 // The action emitted a diagnostic, so we don't have to.
1596 if (T) {
1597 // The action has suggested that the type T could be used. Set that as
1598 // the type in the declaration specifiers, consume the would-be type
1599 // name token, and we're done.
1600 const char *PrevSpec;
1601 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001602 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001603 DS.SetRangeEnd(Tok.getLocation());
1604 ConsumeToken();
1605
1606 // There may be other declaration specifiers after this.
1607 return true;
1608 }
1609
1610 // Fall through; the action had no suggestion for us.
1611 } else {
1612 // The action did not emit a diagnostic, so emit one now.
1613 SourceRange R;
1614 if (SS) R = SS->getRange();
1615 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1616 }
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Douglas Gregora786fdb2009-10-13 23:27:22 +00001618 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001619 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001620 DS.SetRangeEnd(Tok.getLocation());
1621 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Chris Lattnere40c2952009-04-14 21:34:55 +00001623 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1624 // avoid rippling error messages on subsequent uses of the same type,
1625 // could be useful if #include was forgotten.
1626 return false;
1627}
1628
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001629/// \brief Determine the declaration specifier context from the declarator
1630/// context.
1631///
1632/// \param Context the declarator context, which is one of the
1633/// Declarator::TheContext enumerator values.
1634Parser::DeclSpecContext
1635Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1636 if (Context == Declarator::MemberContext)
1637 return DSC_class;
1638 if (Context == Declarator::FileContext)
1639 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001640 if (Context == Declarator::TrailingReturnContext)
1641 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001642 return DSC_normal;
1643}
1644
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001645/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1646///
1647/// FIXME: Simply returns an alignof() expression if the argument is a
1648/// type. Ideally, the type should be propagated directly into Sema.
1649///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001650/// [C11] type-id
1651/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001652/// [C++0x] type-id ...[opt]
1653/// [C++0x] assignment-expression ...[opt]
1654ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1655 SourceLocation &EllipsisLoc) {
1656 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001657 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001658 SourceLocation TypeLoc = Tok.getLocation();
1659 ParsedType Ty = ParseTypeName().get();
1660 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001661 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1662 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001663 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001664 ER = ParseConstantExpression();
1665
David Blaikie4e4d0842012-03-11 07:00:24 +00001666 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001667 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001668
1669 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001670}
1671
1672/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1673/// attribute to Attrs.
1674///
1675/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001676/// [C11] '_Alignas' '(' type-id ')'
1677/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001678/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1679/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001680void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1681 SourceLocation *endLoc) {
1682 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1683 "Not an alignment-specifier!");
1684
1685 SourceLocation KWLoc = Tok.getLocation();
1686 ConsumeToken();
1687
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001688 BalancedDelimiterTracker T(*this, tok::l_paren);
1689 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001690 return;
1691
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001692 SourceLocation EllipsisLoc;
1693 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001694 if (ArgExpr.isInvalid()) {
1695 SkipUntil(tok::r_paren);
1696 return;
1697 }
1698
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001699 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001700 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001701 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001702
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001703 // FIXME: Handle pack-expansions here.
1704 if (EllipsisLoc.isValid()) {
1705 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1706 return;
1707 }
1708
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001709 ExprVector ArgExprs(Actions);
1710 ArgExprs.push_back(ArgExpr.release());
1711 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001712 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001713}
1714
Reid Spencer5f016e22007-07-11 17:01:13 +00001715/// ParseDeclarationSpecifiers
1716/// declaration-specifiers: [C99 6.7]
1717/// storage-class-specifier declaration-specifiers[opt]
1718/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001719/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001720/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001721/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001722/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001723///
1724/// storage-class-specifier: [C99 6.7.1]
1725/// 'typedef'
1726/// 'extern'
1727/// 'static'
1728/// 'auto'
1729/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001730/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001731/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001732/// function-specifier: [C99 6.7.4]
1733/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001734/// [C++] 'virtual'
1735/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001736/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001737/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001738/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001739
Reid Spencer5f016e22007-07-11 17:01:13 +00001740///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001741void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001742 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001743 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001744 DeclSpecContext DSContext,
1745 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00001746 if (DS.getSourceRange().isInvalid()) {
1747 DS.SetRangeStart(Tok.getLocation());
1748 DS.SetRangeEnd(Tok.getLocation());
1749 }
1750
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001751 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00001752 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001753 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001754 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001755 unsigned DiagID = 0;
1756
Reid Spencer5f016e22007-07-11 17:01:13 +00001757 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001758
Reid Spencer5f016e22007-07-11 17:01:13 +00001759 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001760 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001761 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001762 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1763 MaybeParseCXX0XAttributes(DS.getAttributes());
1764
Reid Spencer5f016e22007-07-11 17:01:13 +00001765 // If this is not a declaration specifier token, we're done reading decl
1766 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001767 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001768 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001769
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001770 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001771 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001772 if (DS.hasTypeSpecifier()) {
1773 bool AllowNonIdentifiers
1774 = (getCurScope()->getFlags() & (Scope::ControlScope |
1775 Scope::BlockScope |
1776 Scope::TemplateParamScope |
1777 Scope::FunctionPrototypeScope |
1778 Scope::AtCatchScope)) == 0;
1779 bool AllowNestedNameSpecifiers
1780 = DSContext == DSC_top_level ||
1781 (DSContext == DSC_class && DS.isFriendSpecified());
1782
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001783 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1784 AllowNonIdentifiers,
1785 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001786 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001787 }
1788
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001789 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1790 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1791 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001792 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1793 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001794 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001795 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00001796 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00001797 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001798
1799 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001800 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001801 }
1802
Chris Lattner5e02c472009-01-05 00:07:25 +00001803 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001804 // C++ scope specifier. Annotate and loop, or bail out on error.
1805 if (TryAnnotateCXXScopeToken(true)) {
1806 if (!DS.hasTypeSpecifier())
1807 DS.SetTypeSpecError();
1808 goto DoneWithDeclSpec;
1809 }
John McCall2e0a7152010-03-01 18:20:46 +00001810 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1811 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001812 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001813
1814 case tok::annot_cxxscope: {
1815 if (DS.hasTypeSpecifier())
1816 goto DoneWithDeclSpec;
1817
John McCallaa87d332009-12-12 11:40:51 +00001818 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001819 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1820 Tok.getAnnotationRange(),
1821 SS);
John McCallaa87d332009-12-12 11:40:51 +00001822
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001823 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001824 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001825 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001826 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001827 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001828 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001829
1830 // C++ [class.qual]p2:
1831 // In a lookup in which the constructor is an acceptable lookup
1832 // result and the nested-name-specifier nominates a class C:
1833 //
1834 // - if the name specified after the
1835 // nested-name-specifier, when looked up in C, is the
1836 // injected-class-name of C (Clause 9), or
1837 //
1838 // - if the name specified after the nested-name-specifier
1839 // is the same as the identifier or the
1840 // simple-template-id's template-name in the last
1841 // component of the nested-name-specifier,
1842 //
1843 // the name is instead considered to name the constructor of
1844 // class C.
1845 //
1846 // Thus, if the template-name is actually the constructor
1847 // name, then the code is ill-formed; this interpretation is
1848 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001849 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001850 if ((DSContext == DSC_top_level ||
1851 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1852 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001853 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001854 if (isConstructorDeclarator()) {
1855 // The user meant this to be an out-of-line constructor
1856 // definition, but template arguments are not allowed
1857 // there. Just allow this as a constructor; we'll
1858 // complain about it later.
1859 goto DoneWithDeclSpec;
1860 }
1861
1862 // The user meant this to name a type, but it actually names
1863 // a constructor with some extraneous template
1864 // arguments. Complain, then parse it as a type as the user
1865 // intended.
1866 Diag(TemplateId->TemplateNameLoc,
1867 diag::err_out_of_line_template_id_names_constructor)
1868 << TemplateId->Name;
1869 }
1870
John McCallaa87d332009-12-12 11:40:51 +00001871 DS.getTypeSpecScope() = SS;
1872 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001873 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001874 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001875 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001876 continue;
1877 }
1878
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001879 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001880 DS.getTypeSpecScope() = SS;
1881 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001882 if (Tok.getAnnotationValue()) {
1883 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001884 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1885 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001886 PrevSpec, DiagID, T);
1887 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001888 else
1889 DS.SetTypeSpecError();
1890 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1891 ConsumeToken(); // The typename
1892 }
1893
Douglas Gregor9135c722009-03-25 15:40:00 +00001894 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001895 goto DoneWithDeclSpec;
1896
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001897 // If we're in a context where the identifier could be a class name,
1898 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001899 if ((DSContext == DSC_top_level ||
1900 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001901 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001902 &SS)) {
1903 if (isConstructorDeclarator())
1904 goto DoneWithDeclSpec;
1905
1906 // As noted in C++ [class.qual]p2 (cited above), when the name
1907 // of the class is qualified in a context where it could name
1908 // a constructor, its a constructor name. However, we've
1909 // looked at the declarator, and the user probably meant this
1910 // to be a type. Complain that it isn't supposed to be treated
1911 // as a type, then proceed to parse it as a type.
1912 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1913 << Next.getIdentifierInfo();
1914 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001915
John McCallb3d87482010-08-24 05:47:05 +00001916 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1917 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001918 getCurScope(), &SS,
1919 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001920 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00001921 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001922
Chris Lattnerf4382f52009-04-14 22:17:06 +00001923 // If the referenced identifier is not a type, then this declspec is
1924 // erroneous: We already checked about that it has no type specifier, and
1925 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001926 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001927 if (TypeRep == 0) {
1928 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00001929 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001930 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001931 }
Mike Stump1eb44332009-09-09 15:08:12 +00001932
John McCallaa87d332009-12-12 11:40:51 +00001933 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001934 ConsumeToken(); // The C++ scope.
1935
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001936 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001937 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001938 if (isInvalid)
1939 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001941 DS.SetRangeEnd(Tok.getLocation());
1942 ConsumeToken(); // The typename.
1943
1944 continue;
1945 }
Mike Stump1eb44332009-09-09 15:08:12 +00001946
Chris Lattner80d0c892009-01-21 19:48:37 +00001947 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001948 if (Tok.getAnnotationValue()) {
1949 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001950 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001951 DiagID, T);
1952 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001953 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001954
1955 if (isInvalid)
1956 break;
1957
Chris Lattner80d0c892009-01-21 19:48:37 +00001958 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1959 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001960
Chris Lattner80d0c892009-01-21 19:48:37 +00001961 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1962 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001963 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00001964 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001965 ParseObjCProtocolQualifiers(DS);
1966
Chris Lattner80d0c892009-01-21 19:48:37 +00001967 continue;
1968 }
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Douglas Gregorbfad9152011-04-28 15:48:45 +00001970 case tok::kw___is_signed:
1971 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1972 // typically treats it as a trait. If we see __is_signed as it appears
1973 // in libstdc++, e.g.,
1974 //
1975 // static const bool __is_signed;
1976 //
1977 // then treat __is_signed as an identifier rather than as a keyword.
1978 if (DS.getTypeSpecType() == TST_bool &&
1979 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1980 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1981 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1982 Tok.setKind(tok::identifier);
1983 }
1984
1985 // We're done with the declaration-specifiers.
1986 goto DoneWithDeclSpec;
1987
Chris Lattner3bd934a2008-07-26 01:18:38 +00001988 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00001989 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001990 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001991 // In C++, check to see if this is a scope specifier like foo::bar::, if
1992 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00001993 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00001994 if (TryAnnotateCXXScopeToken(true)) {
1995 if (!DS.hasTypeSpecifier())
1996 DS.SetTypeSpecError();
1997 goto DoneWithDeclSpec;
1998 }
1999 if (!Tok.is(tok::identifier))
2000 continue;
2001 }
Mike Stump1eb44332009-09-09 15:08:12 +00002002
Chris Lattner3bd934a2008-07-26 01:18:38 +00002003 // This identifier can only be a typedef name if we haven't already seen
2004 // a type-specifier. Without this check we misparse:
2005 // typedef int X; struct Y { short X; }; as 'short int'.
2006 if (DS.hasTypeSpecifier())
2007 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002008
John Thompson82287d12010-02-05 00:12:22 +00002009 // Check for need to substitute AltiVec keyword tokens.
2010 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2011 break;
2012
John McCallb3d87482010-08-24 05:47:05 +00002013 ParsedType TypeRep =
2014 Actions.getTypeName(*Tok.getIdentifierInfo(),
2015 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002016
Chris Lattnerc199ab32009-04-12 20:42:31 +00002017 // If this is not a typedef name, don't parse it as part of the declspec,
2018 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002019 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002020 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002021 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002022 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002023
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002024 // If we're in a context where the identifier could be a class name,
2025 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002026 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002027 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002028 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002029 goto DoneWithDeclSpec;
2030
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002031 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002032 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002033 if (isInvalid)
2034 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002035
Chris Lattner3bd934a2008-07-26 01:18:38 +00002036 DS.SetRangeEnd(Tok.getLocation());
2037 ConsumeToken(); // The identifier
2038
2039 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2040 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002041 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002042 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002043 ParseObjCProtocolQualifiers(DS);
2044
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002045 // Need to support trailing type qualifiers (e.g. "id<p> const").
2046 // If a type specifier follows, it will be diagnosed elsewhere.
2047 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002048 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002049
2050 // type-name
2051 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002052 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002053 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002054 // This template-id does not refer to a type name, so we're
2055 // done with the type-specifiers.
2056 goto DoneWithDeclSpec;
2057 }
2058
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002059 // If we're in a context where the template-id could be a
2060 // constructor name or specialization, check whether this is a
2061 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002062 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002063 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002064 isConstructorDeclarator())
2065 goto DoneWithDeclSpec;
2066
Douglas Gregor39a8de12009-02-25 19:37:18 +00002067 // Turn the template-id annotation token into a type annotation
2068 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002069 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002070 continue;
2071 }
2072
Reid Spencer5f016e22007-07-11 17:01:13 +00002073 // GNU attributes support.
2074 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002075 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002076 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002077
2078 // Microsoft declspec support.
2079 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002080 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002081 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002082
Steve Naroff239f0732008-12-25 14:16:32 +00002083 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002084 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00002085 // FIXME: Add handling here!
2086 break;
2087
2088 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002089 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002090 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002091 case tok::kw___cdecl:
2092 case tok::kw___stdcall:
2093 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002094 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002095 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002096 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002097 continue;
2098
Dawn Perchik52fc3142010-09-03 01:29:35 +00002099 // Borland single token adornments.
2100 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002101 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002102 continue;
2103
Peter Collingbournef315fa82011-02-14 01:42:53 +00002104 // OpenCL single token adornments.
2105 case tok::kw___kernel:
2106 ParseOpenCLAttributes(DS.getAttributes());
2107 continue;
2108
Reid Spencer5f016e22007-07-11 17:01:13 +00002109 // storage-class-specifier
2110 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002111 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2112 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002113 break;
2114 case tok::kw_extern:
2115 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002116 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002117 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2118 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002119 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002120 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002121 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2122 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002123 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002124 case tok::kw_static:
2125 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002126 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002127 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2128 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002129 break;
2130 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002131 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002132 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002133 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2134 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002135 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002136 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002137 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002138 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002139 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2140 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002141 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002142 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2143 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002144 break;
2145 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002146 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2147 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002148 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002149 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002150 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2151 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002152 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002153 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002154 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002155 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002156
Reid Spencer5f016e22007-07-11 17:01:13 +00002157 // function-specifier
2158 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002159 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002160 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002161 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002162 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002163 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002164 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002165 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002166 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002167
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002168 // alignment-specifier
2169 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002170 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002171 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002172 ParseAlignmentSpecifier(DS.getAttributes());
2173 continue;
2174
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002175 // friend
2176 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002177 if (DSContext == DSC_class)
2178 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2179 else {
2180 PrevSpec = ""; // not actually used by the diagnostic
2181 DiagID = diag::err_friend_invalid_in_context;
2182 isInvalid = true;
2183 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002184 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Douglas Gregor8d267c52011-09-09 02:06:17 +00002186 // Modules
2187 case tok::kw___module_private__:
2188 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2189 break;
2190
Sebastian Redl2ac67232009-11-05 15:47:02 +00002191 // constexpr
2192 case tok::kw_constexpr:
2193 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2194 break;
2195
Chris Lattner80d0c892009-01-21 19:48:37 +00002196 // type-specifier
2197 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002198 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2199 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002200 break;
2201 case tok::kw_long:
2202 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002203 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2204 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002205 else
John McCallfec54012009-08-03 20:12:06 +00002206 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2207 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002208 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002209 case tok::kw___int64:
2210 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2211 DiagID);
2212 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002213 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002214 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2215 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002216 break;
2217 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002218 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2219 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002220 break;
2221 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002222 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2223 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002224 break;
2225 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002226 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2227 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002228 break;
2229 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002230 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2231 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002232 break;
2233 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002234 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2235 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002236 break;
2237 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002238 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2239 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002240 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002241 case tok::kw___int128:
2242 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2243 DiagID);
2244 break;
2245 case tok::kw_half:
2246 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2247 DiagID);
2248 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002249 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002250 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2251 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002252 break;
2253 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002254 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2255 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002256 break;
2257 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002258 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2259 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002260 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002261 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002262 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2263 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002264 break;
2265 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002266 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2267 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002268 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002269 case tok::kw_bool:
2270 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002271 if (Tok.is(tok::kw_bool) &&
2272 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2273 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2274 PrevSpec = ""; // Not used by the diagnostic.
2275 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002276 // For better error recovery.
2277 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002278 isInvalid = true;
2279 } else {
2280 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2281 DiagID);
2282 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002283 break;
2284 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002285 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2286 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002287 break;
2288 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002289 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2290 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002291 break;
2292 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002293 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2294 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002295 break;
John Thompson82287d12010-02-05 00:12:22 +00002296 case tok::kw___vector:
2297 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2298 break;
2299 case tok::kw___pixel:
2300 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2301 break;
John McCalla5fc4722011-04-09 22:50:59 +00002302 case tok::kw___unknown_anytype:
2303 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2304 PrevSpec, DiagID);
2305 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002306
2307 // class-specifier:
2308 case tok::kw_class:
2309 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002310 case tok::kw_union: {
2311 tok::TokenKind Kind = Tok.getKind();
2312 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002313 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2314 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002315 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002316 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002317
2318 // enum-specifier:
2319 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002320 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002321 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002322 continue;
2323
2324 // cv-qualifier:
2325 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002326 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002327 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002328 break;
2329 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002330 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002331 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002332 break;
2333 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002334 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002335 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002336 break;
2337
Douglas Gregord57959a2009-03-27 23:10:48 +00002338 // C++ typename-specifier:
2339 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002340 if (TryAnnotateTypeOrScopeToken()) {
2341 DS.SetTypeSpecError();
2342 goto DoneWithDeclSpec;
2343 }
2344 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002345 continue;
2346 break;
2347
Chris Lattner80d0c892009-01-21 19:48:37 +00002348 // GNU typeof support.
2349 case tok::kw_typeof:
2350 ParseTypeofSpecifier(DS);
2351 continue;
2352
David Blaikie42d6d0c2011-12-04 05:04:18 +00002353 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002354 ParseDecltypeSpecifier(DS);
2355 continue;
2356
Sean Huntdb5d44b2011-05-19 05:37:45 +00002357 case tok::kw___underlying_type:
2358 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002359 continue;
2360
2361 case tok::kw__Atomic:
2362 ParseAtomicSpecifier(DS);
2363 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002364
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002365 // OpenCL qualifiers:
2366 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002367 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002368 goto DoneWithDeclSpec;
2369 case tok::kw___private:
2370 case tok::kw___global:
2371 case tok::kw___local:
2372 case tok::kw___constant:
2373 case tok::kw___read_only:
2374 case tok::kw___write_only:
2375 case tok::kw___read_write:
2376 ParseOpenCLQualifiers(DS);
2377 break;
2378
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002379 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002380 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002381 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2382 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002383 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002384 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002385
Douglas Gregor46f936e2010-11-19 17:10:50 +00002386 if (!ParseObjCProtocolQualifiers(DS))
2387 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2388 << FixItHint::CreateInsertion(Loc, "id")
2389 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002390
2391 // Need to support trailing type qualifiers (e.g. "id<p> const").
2392 // If a type specifier follows, it will be diagnosed elsewhere.
2393 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002394 }
John McCallfec54012009-08-03 20:12:06 +00002395 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002396 if (isInvalid) {
2397 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002398 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002399
2400 if (DiagID == diag::ext_duplicate_declspec)
2401 Diag(Tok, DiagID)
2402 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2403 else
2404 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002405 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002406
Chris Lattner81c018d2008-03-13 06:29:04 +00002407 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002408 if (DiagID != diag::err_bool_redeclaration)
2409 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002410 }
2411}
Douglas Gregoradcac882008-12-01 23:54:00 +00002412
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002413/// ParseStructDeclaration - Parse a struct declaration without the terminating
2414/// semicolon.
2415///
Reid Spencer5f016e22007-07-11 17:01:13 +00002416/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002417/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002418/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002419/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002420/// struct-declarator-list:
2421/// struct-declarator
2422/// struct-declarator-list ',' struct-declarator
2423/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2424/// struct-declarator:
2425/// declarator
2426/// [GNU] declarator attributes[opt]
2427/// declarator[opt] ':' constant-expression
2428/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2429///
Chris Lattnere1359422008-04-10 06:46:29 +00002430void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002431ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002432
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002433 if (Tok.is(tok::kw___extension__)) {
2434 // __extension__ silences extension warnings in the subexpression.
2435 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002436 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002437 return ParseStructDeclaration(DS, Fields);
2438 }
Mike Stump1eb44332009-09-09 15:08:12 +00002439
Steve Naroff28a7ca82007-08-20 22:28:22 +00002440 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002441 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002442
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002443 // If there are no declarators, this is a free-standing declaration
2444 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002445 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002446 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002447 return;
2448 }
2449
2450 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002451 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002452 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002453 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002454 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002455 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002456 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002457
2458 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002459 if (!FirstDeclarator)
2460 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002461
Steve Naroff28a7ca82007-08-20 22:28:22 +00002462 /// struct-declarator: declarator
2463 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002464 if (Tok.isNot(tok::colon)) {
2465 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2466 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002467 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002468 }
Mike Stump1eb44332009-09-09 15:08:12 +00002469
Chris Lattner04d66662007-10-09 17:33:22 +00002470 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002471 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002472 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002473 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002474 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002475 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002476 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002477 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002478
Steve Naroff28a7ca82007-08-20 22:28:22 +00002479 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002480 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002481
John McCallbdd563e2009-11-03 02:38:08 +00002482 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002483 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002484 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002485
Steve Naroff28a7ca82007-08-20 22:28:22 +00002486 // If we don't have a comma, it is either the end of the list (a ';')
2487 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002488 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002489 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002490
Steve Naroff28a7ca82007-08-20 22:28:22 +00002491 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002492 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002493
John McCallbdd563e2009-11-03 02:38:08 +00002494 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002495 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002496}
2497
2498/// ParseStructUnionBody
2499/// struct-contents:
2500/// struct-declaration-list
2501/// [EXT] empty
2502/// [GNU] "struct-declaration-list" without terminatoring ';'
2503/// struct-declaration-list:
2504/// struct-declaration
2505/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002506/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002507///
Reid Spencer5f016e22007-07-11 17:01:13 +00002508void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002509 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002510 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2511 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002512
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002513 BalancedDelimiterTracker T(*this, tok::l_brace);
2514 if (T.consumeOpen())
2515 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002516
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002517 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002518 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002519
Reid Spencer5f016e22007-07-11 17:01:13 +00002520 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2521 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002522 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002523 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2524 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2525 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002526
Chris Lattner5f9e2722011-07-23 10:55:15 +00002527 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002528
Reid Spencer5f016e22007-07-11 17:01:13 +00002529 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002530 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002531 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002532
Reid Spencer5f016e22007-07-11 17:01:13 +00002533 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002534 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002535 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002536 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002537 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002538 ConsumeToken();
2539 continue;
2540 }
Chris Lattnere1359422008-04-10 06:46:29 +00002541
2542 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002543 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002544
John McCallbdd563e2009-11-03 02:38:08 +00002545 if (!Tok.is(tok::at)) {
2546 struct CFieldCallback : FieldCallback {
2547 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002548 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002549 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002550
John McCalld226f652010-08-21 09:40:31 +00002551 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002552 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002553 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2554
John McCalld226f652010-08-21 09:40:31 +00002555 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002556 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002557 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002558 FD.D.getDeclSpec().getSourceRange().getBegin(),
2559 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002560 FieldDecls.push_back(Field);
2561 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002562 }
John McCallbdd563e2009-11-03 02:38:08 +00002563 } Callback(*this, TagDecl, FieldDecls);
2564
2565 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002566 } else { // Handle @defs
2567 ConsumeToken();
2568 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2569 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002570 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002571 continue;
2572 }
2573 ConsumeToken();
2574 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2575 if (!Tok.is(tok::identifier)) {
2576 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002577 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002578 continue;
2579 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002580 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002581 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002582 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002583 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2584 ConsumeToken();
2585 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002586 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002587
Chris Lattner04d66662007-10-09 17:33:22 +00002588 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002589 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002590 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002591 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002592 break;
2593 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002594 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2595 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002596 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002597 // If we stopped at a ';', eat it.
2598 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002599 }
2600 }
Mike Stump1eb44332009-09-09 15:08:12 +00002601
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002602 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002603
John McCall0b7e6782011-03-24 11:26:52 +00002604 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002605 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002606 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002607
Douglas Gregor23c94db2010-07-02 17:43:08 +00002608 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002609 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002610 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002611 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002612 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002613 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2614 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002615}
2616
Reid Spencer5f016e22007-07-11 17:01:13 +00002617/// ParseEnumSpecifier
2618/// enum-specifier: [C99 6.7.2.2]
2619/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002620///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002621/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2622/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002623/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2624/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002625/// 'enum' identifier
2626/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002627///
Richard Smith1af83c42012-03-23 03:33:32 +00002628/// [C++11] enum-head '{' enumerator-list[opt] '}'
2629/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002630///
Richard Smith1af83c42012-03-23 03:33:32 +00002631/// enum-head: [C++11]
2632/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2633/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2634/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002635///
Richard Smith1af83c42012-03-23 03:33:32 +00002636/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002637/// 'enum'
2638/// 'enum' 'class'
2639/// 'enum' 'struct'
2640///
Richard Smith1af83c42012-03-23 03:33:32 +00002641/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002642/// ':' type-specifier-seq
2643///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002644/// [C++] elaborated-type-specifier:
2645/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2646///
Chris Lattner4c97d762009-04-12 21:49:30 +00002647void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002648 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002649 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002650 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002651 if (Tok.is(tok::code_completion)) {
2652 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002653 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002654 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002655 }
John McCall57c13002011-07-06 05:58:41 +00002656
Richard Smithbdad7a22012-01-10 01:33:14 +00002657 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002658 bool IsScopedUsingClassTag = false;
2659
David Blaikie4e4d0842012-03-11 07:00:24 +00002660 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002661 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002662 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002663 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002664 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002665 }
Richard Smith1af83c42012-03-23 03:33:32 +00002666
2667 // C++11 [temp.explicit]p12: The usual access controls do not apply to names
2668 // used to specify explicit instantiations. We extend this to also cover
2669 // explicit specializations.
2670 Sema::SuppressAccessChecksRAII SuppressAccess(Actions,
2671 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2672 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2673
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002674 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002675 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002676 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002677
Aaron Ballman6454a022012-03-01 04:09:28 +00002678 // If declspecs exist after tag, parse them.
2679 while (Tok.is(tok::kw___declspec))
2680 ParseMicrosoftDeclSpec(attrs);
2681
Richard Smith7796eb52012-03-12 08:56:40 +00002682 // Enum definitions should not be parsed in a trailing-return-type.
2683 bool AllowDeclaration = DSC != DSC_trailing;
2684
2685 bool AllowFixedUnderlyingType = AllowDeclaration &&
2686 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2687 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002688
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002689 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002690 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002691 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2692 // if a fixed underlying type is allowed.
2693 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2694
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002695 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2696 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002697 return;
2698
2699 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002700 Diag(Tok, diag::err_expected_ident);
2701 if (Tok.isNot(tok::l_brace)) {
2702 // Has no name and is not a definition.
2703 // Skip the rest of this declarator, up until the comma or semicolon.
2704 SkipUntil(tok::comma, true);
2705 return;
2706 }
2707 }
2708 }
Mike Stump1eb44332009-09-09 15:08:12 +00002709
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002710 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002711 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00002712 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002713 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002714
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002715 // Skip the rest of this declarator, up until the comma or semicolon.
2716 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002717 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002718 }
Mike Stump1eb44332009-09-09 15:08:12 +00002719
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002720 // If an identifier is present, consume and remember it.
2721 IdentifierInfo *Name = 0;
2722 SourceLocation NameLoc;
2723 if (Tok.is(tok::identifier)) {
2724 Name = Tok.getIdentifierInfo();
2725 NameLoc = ConsumeToken();
2726 }
Mike Stump1eb44332009-09-09 15:08:12 +00002727
Richard Smithbdad7a22012-01-10 01:33:14 +00002728 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002729 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2730 // declaration of a scoped enumeration.
2731 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00002732 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002733 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002734 }
2735
Richard Smith1af83c42012-03-23 03:33:32 +00002736 // Stop suppressing access control now we've parsed the enum name.
2737 SuppressAccess.done();
2738
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002739 TypeResult BaseType;
2740
Douglas Gregora61b3e72010-12-01 17:42:47 +00002741 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002742 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002743 bool PossibleBitfield = false;
2744 if (getCurScope()->getFlags() & Scope::ClassScope) {
2745 // If we're in class scope, this can either be an enum declaration with
2746 // an underlying type, or a declaration of a bitfield member. We try to
2747 // use a simple disambiguation scheme first to catch the common cases
2748 // (integer literal, sizeof); if it's still ambiguous, we then consider
2749 // anything that's a simple-type-specifier followed by '(' as an
2750 // expression. This suffices because function types are not valid
2751 // underlying types anyway.
2752 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2753 // If the next token starts an expression, we know we're parsing a
2754 // bit-field. This is the common case.
2755 if (TPR == TPResult::True())
2756 PossibleBitfield = true;
2757 // If the next token starts a type-specifier-seq, it may be either a
2758 // a fixed underlying type or the start of a function-style cast in C++;
2759 // lookahead one more token to see if it's obvious that we have a
2760 // fixed underlying type.
2761 else if (TPR == TPResult::False() &&
2762 GetLookAheadToken(2).getKind() == tok::semi) {
2763 // Consume the ':'.
2764 ConsumeToken();
2765 } else {
2766 // We have the start of a type-specifier-seq, so we have to perform
2767 // tentative parsing to determine whether we have an expression or a
2768 // type.
2769 TentativeParsingAction TPA(*this);
2770
2771 // Consume the ':'.
2772 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00002773
2774 // If we see a type specifier followed by an open-brace, we have an
2775 // ambiguity between an underlying type and a C++11 braced
2776 // function-style cast. Resolve this by always treating it as an
2777 // underlying type.
2778 // FIXME: The standard is not entirely clear on how to disambiguate in
2779 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00002780 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00002781 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002782 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002783 // We'll parse this as a bitfield later.
2784 PossibleBitfield = true;
2785 TPA.Revert();
2786 } else {
2787 // We have a type-specifier-seq.
2788 TPA.Commit();
2789 }
2790 }
2791 } else {
2792 // Consume the ':'.
2793 ConsumeToken();
2794 }
2795
2796 if (!PossibleBitfield) {
2797 SourceRange Range;
2798 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002799
David Blaikie4e4d0842012-03-11 07:00:24 +00002800 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002801 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2802 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00002803 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002804 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00002805 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002806 }
2807
Richard Smithbdad7a22012-01-10 01:33:14 +00002808 // There are four options here. If we have 'friend enum foo;' then this is a
2809 // friend declaration, and cannot have an accompanying definition. If we have
2810 // 'enum foo;', then this is a forward declaration. If we have
2811 // 'enum foo {...' then this is a definition. Otherwise we have something
2812 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002813 //
2814 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2815 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2816 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2817 //
John McCallf312b1e2010-08-26 23:41:50 +00002818 Sema::TagUseKind TUK;
Richard Smithbdad7a22012-01-10 01:33:14 +00002819 if (DS.isFriendSpecified())
2820 TUK = Sema::TUK_Friend;
Richard Smith7796eb52012-03-12 08:56:40 +00002821 else if (!AllowDeclaration)
2822 TUK = Sema::TUK_Reference;
Richard Smithbdad7a22012-01-10 01:33:14 +00002823 else if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002824 TUK = Sema::TUK_Definition;
Richard Smith69730c12012-03-12 07:56:15 +00002825 else if (Tok.is(tok::semi) && DSC != DSC_type_specifier)
John McCallf312b1e2010-08-26 23:41:50 +00002826 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002827 else
John McCallf312b1e2010-08-26 23:41:50 +00002828 TUK = Sema::TUK_Reference;
Richard Smith1af83c42012-03-23 03:33:32 +00002829
2830 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002831 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002832 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00002833 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
2834 // Skip the rest of this declarator, up until the comma or semicolon.
2835 Diag(Tok, diag::err_enum_template);
2836 SkipUntil(tok::comma, true);
2837 return;
2838 }
2839
2840 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
2841 // Enumerations can't be explicitly instantiated.
2842 DS.SetTypeSpecError();
2843 Diag(StartLoc, diag::err_explicit_instantiation_enum);
2844 return;
2845 }
2846
2847 assert(TemplateInfo.TemplateParams && "no template parameters");
2848 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
2849 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002850 }
Richard Smith1af83c42012-03-23 03:33:32 +00002851
Douglas Gregorb9075602011-02-22 02:55:24 +00002852 if (!Name && TUK != Sema::TUK_Definition) {
2853 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00002854
Douglas Gregorb9075602011-02-22 02:55:24 +00002855 // Skip the rest of this declarator, up until the comma or semicolon.
2856 SkipUntil(tok::comma, true);
2857 return;
2858 }
Richard Smith1af83c42012-03-23 03:33:32 +00002859
Douglas Gregor402abb52009-05-28 23:31:59 +00002860 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002861 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002862 const char *PrevSpec = 0;
2863 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002864 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002865 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00002866 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00002867 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002868 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002869
Douglas Gregor48c89f42010-04-24 16:38:41 +00002870 if (IsDependent) {
2871 // This enum has a dependent nested-name-specifier. Handle it as a
2872 // dependent tag.
2873 if (!Name) {
2874 DS.SetTypeSpecError();
2875 Diag(Tok, diag::err_expected_type_name_after_typename);
2876 return;
2877 }
2878
Douglas Gregor23c94db2010-07-02 17:43:08 +00002879 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002880 TUK, SS, Name, StartLoc,
2881 NameLoc);
2882 if (Type.isInvalid()) {
2883 DS.SetTypeSpecError();
2884 return;
2885 }
2886
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002887 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2888 NameLoc.isValid() ? NameLoc : StartLoc,
2889 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002890 Diag(StartLoc, DiagID) << PrevSpec;
2891
2892 return;
2893 }
Mike Stump1eb44332009-09-09 15:08:12 +00002894
John McCalld226f652010-08-21 09:40:31 +00002895 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002896 // The action failed to produce an enumeration tag. If this is a
2897 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00002898 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002899 ConsumeBrace();
2900 SkipUntil(tok::r_brace);
2901 }
2902
2903 DS.SetTypeSpecError();
2904 return;
2905 }
Richard Smithbdad7a22012-01-10 01:33:14 +00002906
Richard Smith7796eb52012-03-12 08:56:40 +00002907 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00002908 if (TUK == Sema::TUK_Friend) {
Richard Smithbdad7a22012-01-10 01:33:14 +00002909 Diag(Tok, diag::err_friend_decl_defines_type)
2910 << SourceRange(DS.getFriendSpecLoc());
Richard Smith1af83c42012-03-23 03:33:32 +00002911 ConsumeBrace();
2912 SkipUntil(tok::r_brace);
2913 } else {
2914 ParseEnumBody(StartLoc, TagDecl);
2915 }
Richard Smithbdad7a22012-01-10 01:33:14 +00002916 }
Mike Stump1eb44332009-09-09 15:08:12 +00002917
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002918 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2919 NameLoc.isValid() ? NameLoc : StartLoc,
2920 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002921 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002922}
2923
2924/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2925/// enumerator-list:
2926/// enumerator
2927/// enumerator-list ',' enumerator
2928/// enumerator:
2929/// enumeration-constant
2930/// enumeration-constant '=' constant-expression
2931/// enumeration-constant:
2932/// identifier
2933///
John McCalld226f652010-08-21 09:40:31 +00002934void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002935 // Enter the scope of the enum body and start the definition.
2936 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002937 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002938
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002939 BalancedDelimiterTracker T(*this, tok::l_brace);
2940 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00002941
Chris Lattner7946dd32007-08-27 17:24:30 +00002942 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00002943 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002944 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002945
Chris Lattner5f9e2722011-07-23 10:55:15 +00002946 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002947
John McCalld226f652010-08-21 09:40:31 +00002948 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002949
Reid Spencer5f016e22007-07-11 17:01:13 +00002950 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002951 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002952 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2953 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002954
John McCall5b629aa2010-10-22 23:36:17 +00002955 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002956 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002957 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002958
Reid Spencer5f016e22007-07-11 17:01:13 +00002959 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002960 ExprResult AssignedVal;
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00002961 ParsingDeclRAIIObject PD(*this);
2962
Chris Lattner04d66662007-10-09 17:33:22 +00002963 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002964 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002965 AssignedVal = ParseConstantExpression();
2966 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002967 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002968 }
Mike Stump1eb44332009-09-09 15:08:12 +00002969
Reid Spencer5f016e22007-07-11 17:01:13 +00002970 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002971 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2972 LastEnumConstDecl,
2973 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002974 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002975 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00002976 PD.complete(EnumConstDecl);
2977
Reid Spencer5f016e22007-07-11 17:01:13 +00002978 EnumConstantDecls.push_back(EnumConstDecl);
2979 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002980
Douglas Gregor751f6922010-09-07 14:51:08 +00002981 if (Tok.is(tok::identifier)) {
2982 // We're missing a comma between enumerators.
2983 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2984 Diag(Loc, diag::err_enumerator_list_missing_comma)
2985 << FixItHint::CreateInsertion(Loc, ", ");
2986 continue;
2987 }
2988
Chris Lattner04d66662007-10-09 17:33:22 +00002989 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002990 break;
2991 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002992
Richard Smith7fe62082011-10-15 05:09:34 +00002993 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002994 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002995 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00002996 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00002997 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00002998 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00002999 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3000 << FixItHint::CreateRemoval(CommaLoc);
3001 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003002 }
Mike Stump1eb44332009-09-09 15:08:12 +00003003
Reid Spencer5f016e22007-07-11 17:01:13 +00003004 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003005 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003006
Reid Spencer5f016e22007-07-11 17:01:13 +00003007 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003008 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003009 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003010
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003011 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3012 EnumDecl, EnumConstantDecls.data(),
3013 EnumConstantDecls.size(), getCurScope(),
3014 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003015
Douglas Gregor72de6672009-01-08 20:45:30 +00003016 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003017 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3018 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003019}
3020
3021/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003022/// start of a type-qualifier-list.
3023bool Parser::isTypeQualifier() const {
3024 switch (Tok.getKind()) {
3025 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003026
3027 // type-qualifier only in OpenCL
3028 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003029 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003030
Steve Naroff5f8aa692008-02-11 23:15:56 +00003031 // type-qualifier
3032 case tok::kw_const:
3033 case tok::kw_volatile:
3034 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003035 case tok::kw___private:
3036 case tok::kw___local:
3037 case tok::kw___global:
3038 case tok::kw___constant:
3039 case tok::kw___read_only:
3040 case tok::kw___read_write:
3041 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003042 return true;
3043 }
3044}
3045
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003046/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3047/// is definitely a type-specifier. Return false if it isn't part of a type
3048/// specifier or if we're not sure.
3049bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3050 switch (Tok.getKind()) {
3051 default: return false;
3052 // type-specifiers
3053 case tok::kw_short:
3054 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003055 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003056 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003057 case tok::kw_signed:
3058 case tok::kw_unsigned:
3059 case tok::kw__Complex:
3060 case tok::kw__Imaginary:
3061 case tok::kw_void:
3062 case tok::kw_char:
3063 case tok::kw_wchar_t:
3064 case tok::kw_char16_t:
3065 case tok::kw_char32_t:
3066 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003067 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003068 case tok::kw_float:
3069 case tok::kw_double:
3070 case tok::kw_bool:
3071 case tok::kw__Bool:
3072 case tok::kw__Decimal32:
3073 case tok::kw__Decimal64:
3074 case tok::kw__Decimal128:
3075 case tok::kw___vector:
3076
3077 // struct-or-union-specifier (C99) or class-specifier (C++)
3078 case tok::kw_class:
3079 case tok::kw_struct:
3080 case tok::kw_union:
3081 // enum-specifier
3082 case tok::kw_enum:
3083
3084 // typedef-name
3085 case tok::annot_typename:
3086 return true;
3087 }
3088}
3089
Steve Naroff5f8aa692008-02-11 23:15:56 +00003090/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003091/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003092bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003093 switch (Tok.getKind()) {
3094 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003095
Chris Lattner166a8fc2009-01-04 23:41:41 +00003096 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003097 if (TryAltiVecVectorToken())
3098 return true;
3099 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003100 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003101 // Annotate typenames and C++ scope specifiers. If we get one, just
3102 // recurse to handle whatever we get.
3103 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003104 return true;
3105 if (Tok.is(tok::identifier))
3106 return false;
3107 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003108
Chris Lattner166a8fc2009-01-04 23:41:41 +00003109 case tok::coloncolon: // ::foo::bar
3110 if (NextToken().is(tok::kw_new) || // ::new
3111 NextToken().is(tok::kw_delete)) // ::delete
3112 return false;
3113
Chris Lattner166a8fc2009-01-04 23:41:41 +00003114 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003115 return true;
3116 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003117
Reid Spencer5f016e22007-07-11 17:01:13 +00003118 // GNU attributes support.
3119 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003120 // GNU typeof support.
3121 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003122
Reid Spencer5f016e22007-07-11 17:01:13 +00003123 // type-specifiers
3124 case tok::kw_short:
3125 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003126 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003127 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003128 case tok::kw_signed:
3129 case tok::kw_unsigned:
3130 case tok::kw__Complex:
3131 case tok::kw__Imaginary:
3132 case tok::kw_void:
3133 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003134 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003135 case tok::kw_char16_t:
3136 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003137 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003138 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003139 case tok::kw_float:
3140 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003141 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003142 case tok::kw__Bool:
3143 case tok::kw__Decimal32:
3144 case tok::kw__Decimal64:
3145 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003146 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003147
Chris Lattner99dc9142008-04-13 18:59:07 +00003148 // struct-or-union-specifier (C99) or class-specifier (C++)
3149 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003150 case tok::kw_struct:
3151 case tok::kw_union:
3152 // enum-specifier
3153 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003154
Reid Spencer5f016e22007-07-11 17:01:13 +00003155 // type-qualifier
3156 case tok::kw_const:
3157 case tok::kw_volatile:
3158 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003159
3160 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003161 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003162 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003163
Chris Lattner7c186be2008-10-20 00:25:30 +00003164 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3165 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003166 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003167
Steve Naroff239f0732008-12-25 14:16:32 +00003168 case tok::kw___cdecl:
3169 case tok::kw___stdcall:
3170 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003171 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003172 case tok::kw___w64:
3173 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003174 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003175 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003176 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003177
3178 case tok::kw___private:
3179 case tok::kw___local:
3180 case tok::kw___global:
3181 case tok::kw___constant:
3182 case tok::kw___read_only:
3183 case tok::kw___read_write:
3184 case tok::kw___write_only:
3185
Eli Friedman290eeb02009-06-08 23:27:34 +00003186 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003187
3188 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003189 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003190
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003191 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003192 case tok::kw__Atomic:
3193 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003194 }
3195}
3196
3197/// isDeclarationSpecifier() - Return true if the current token is part of a
3198/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003199///
3200/// \param DisambiguatingWithExpression True to indicate that the purpose of
3201/// this check is to disambiguate between an expression and a declaration.
3202bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003203 switch (Tok.getKind()) {
3204 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003205
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003206 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003207 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003208
Chris Lattner166a8fc2009-01-04 23:41:41 +00003209 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003210 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003211 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003212 return false;
John Thompson82287d12010-02-05 00:12:22 +00003213 if (TryAltiVecVectorToken())
3214 return true;
3215 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003216 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003217 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003218 // Annotate typenames and C++ scope specifiers. If we get one, just
3219 // recurse to handle whatever we get.
3220 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003221 return true;
3222 if (Tok.is(tok::identifier))
3223 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003224
3225 // If we're in Objective-C and we have an Objective-C class type followed
3226 // by an identifier and then either ':' or ']', in a place where an
3227 // expression is permitted, then this is probably a class message send
3228 // missing the initial '['. In this case, we won't consider this to be
3229 // the start of a declaration.
3230 if (DisambiguatingWithExpression &&
3231 isStartOfObjCClassMessageMissingOpenBracket())
3232 return false;
3233
John McCall9ba61662010-02-26 08:45:28 +00003234 return isDeclarationSpecifier();
3235
Chris Lattner166a8fc2009-01-04 23:41:41 +00003236 case tok::coloncolon: // ::foo::bar
3237 if (NextToken().is(tok::kw_new) || // ::new
3238 NextToken().is(tok::kw_delete)) // ::delete
3239 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003240
Chris Lattner166a8fc2009-01-04 23:41:41 +00003241 // Annotate typenames and C++ scope specifiers. If we get one, just
3242 // recurse to handle whatever we get.
3243 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003244 return true;
3245 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003246
Reid Spencer5f016e22007-07-11 17:01:13 +00003247 // storage-class-specifier
3248 case tok::kw_typedef:
3249 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003250 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003251 case tok::kw_static:
3252 case tok::kw_auto:
3253 case tok::kw_register:
3254 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003255
Douglas Gregor8d267c52011-09-09 02:06:17 +00003256 // Modules
3257 case tok::kw___module_private__:
3258
Reid Spencer5f016e22007-07-11 17:01:13 +00003259 // type-specifiers
3260 case tok::kw_short:
3261 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003262 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003263 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003264 case tok::kw_signed:
3265 case tok::kw_unsigned:
3266 case tok::kw__Complex:
3267 case tok::kw__Imaginary:
3268 case tok::kw_void:
3269 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003270 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003271 case tok::kw_char16_t:
3272 case tok::kw_char32_t:
3273
Reid Spencer5f016e22007-07-11 17:01:13 +00003274 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003275 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003276 case tok::kw_float:
3277 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003278 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003279 case tok::kw__Bool:
3280 case tok::kw__Decimal32:
3281 case tok::kw__Decimal64:
3282 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003283 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003284
Chris Lattner99dc9142008-04-13 18:59:07 +00003285 // struct-or-union-specifier (C99) or class-specifier (C++)
3286 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003287 case tok::kw_struct:
3288 case tok::kw_union:
3289 // enum-specifier
3290 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003291
Reid Spencer5f016e22007-07-11 17:01:13 +00003292 // type-qualifier
3293 case tok::kw_const:
3294 case tok::kw_volatile:
3295 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003296
Reid Spencer5f016e22007-07-11 17:01:13 +00003297 // function-specifier
3298 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003299 case tok::kw_virtual:
3300 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003301
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003302 // static_assert-declaration
3303 case tok::kw__Static_assert:
3304
Chris Lattner1ef08762007-08-09 17:01:07 +00003305 // GNU typeof support.
3306 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003307
Chris Lattner1ef08762007-08-09 17:01:07 +00003308 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003309 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003310 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003311
Francois Pichete3d49b42011-06-19 08:02:06 +00003312 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003313 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003314 return true;
3315
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003316 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003317 case tok::kw__Atomic:
3318 return true;
3319
Chris Lattnerf3948c42008-07-26 03:38:44 +00003320 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3321 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003322 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003323
Douglas Gregord9d75e52011-04-27 05:41:15 +00003324 // typedef-name
3325 case tok::annot_typename:
3326 return !DisambiguatingWithExpression ||
3327 !isStartOfObjCClassMessageMissingOpenBracket();
3328
Steve Naroff47f52092009-01-06 19:34:12 +00003329 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003330 case tok::kw___cdecl:
3331 case tok::kw___stdcall:
3332 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003333 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003334 case tok::kw___w64:
3335 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003336 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003337 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003338 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003339 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003340
3341 case tok::kw___private:
3342 case tok::kw___local:
3343 case tok::kw___global:
3344 case tok::kw___constant:
3345 case tok::kw___read_only:
3346 case tok::kw___read_write:
3347 case tok::kw___write_only:
3348
Eli Friedman290eeb02009-06-08 23:27:34 +00003349 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003350 }
3351}
3352
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003353bool Parser::isConstructorDeclarator() {
3354 TentativeParsingAction TPA(*this);
3355
3356 // Parse the C++ scope specifier.
3357 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003358 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3359 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003360 TPA.Revert();
3361 return false;
3362 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003363
3364 // Parse the constructor name.
3365 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3366 // We already know that we have a constructor name; just consume
3367 // the token.
3368 ConsumeToken();
3369 } else {
3370 TPA.Revert();
3371 return false;
3372 }
3373
Richard Smith22592862012-03-27 23:05:05 +00003374 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003375 if (Tok.isNot(tok::l_paren)) {
3376 TPA.Revert();
3377 return false;
3378 }
3379 ConsumeParen();
3380
Richard Smith22592862012-03-27 23:05:05 +00003381 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3382 // that we have a constructor.
3383 if (Tok.is(tok::r_paren) ||
3384 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003385 TPA.Revert();
3386 return true;
3387 }
3388
3389 // If we need to, enter the specified scope.
3390 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003391 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003392 DeclScopeObj.EnterDeclaratorScope();
3393
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003394 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003395 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003396 MaybeParseMicrosoftAttributes(Attrs);
3397
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003398 // Check whether the next token(s) are part of a declaration
3399 // specifier, in which case we have the start of a parameter and,
3400 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003401 bool IsConstructor = false;
3402 if (isDeclarationSpecifier())
3403 IsConstructor = true;
3404 else if (Tok.is(tok::identifier) ||
3405 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3406 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3407 // This might be a parenthesized member name, but is more likely to
3408 // be a constructor declaration with an invalid argument type. Keep
3409 // looking.
3410 if (Tok.is(tok::annot_cxxscope))
3411 ConsumeToken();
3412 ConsumeToken();
3413
3414 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003415 // which must have one of the following syntactic forms (see the
3416 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003417 switch (Tok.getKind()) {
3418 case tok::l_paren:
3419 // C(X ( int));
3420 case tok::l_square:
3421 // C(X [ 5]);
3422 // C(X [ [attribute]]);
3423 case tok::coloncolon:
3424 // C(X :: Y);
3425 // C(X :: *p);
3426 case tok::r_paren:
3427 // C(X )
3428 // Assume this isn't a constructor, rather than assuming it's a
3429 // constructor with an unnamed parameter of an ill-formed type.
3430 break;
3431
3432 default:
3433 IsConstructor = true;
3434 break;
3435 }
3436 }
3437
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003438 TPA.Revert();
3439 return IsConstructor;
3440}
Reid Spencer5f016e22007-07-11 17:01:13 +00003441
3442/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003443/// type-qualifier-list: [C99 6.7.5]
3444/// type-qualifier
3445/// [vendor] attributes
3446/// [ only if VendorAttributesAllowed=true ]
3447/// type-qualifier-list type-qualifier
3448/// [vendor] type-qualifier-list attributes
3449/// [ only if VendorAttributesAllowed=true ]
3450/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3451/// [ only if CXX0XAttributesAllowed=true ]
3452/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003453///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003454void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3455 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003456 bool CXX0XAttributesAllowed) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003457 if (getLangOpts().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003458 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003459 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003460 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003461 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003462 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003463 else
3464 Diag(Loc, diag::err_attributes_not_allowed);
3465 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003466
3467 SourceLocation EndLoc;
3468
Reid Spencer5f016e22007-07-11 17:01:13 +00003469 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003470 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003471 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003472 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003473 SourceLocation Loc = Tok.getLocation();
3474
3475 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003476 case tok::code_completion:
3477 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003478 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003479
Reid Spencer5f016e22007-07-11 17:01:13 +00003480 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003481 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003482 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003483 break;
3484 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003485 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003486 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003487 break;
3488 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003489 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003490 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003491 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003492
3493 // OpenCL qualifiers:
3494 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003495 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003496 goto DoneWithTypeQuals;
3497 case tok::kw___private:
3498 case tok::kw___global:
3499 case tok::kw___local:
3500 case tok::kw___constant:
3501 case tok::kw___read_only:
3502 case tok::kw___write_only:
3503 case tok::kw___read_write:
3504 ParseOpenCLQualifiers(DS);
3505 break;
3506
Eli Friedman290eeb02009-06-08 23:27:34 +00003507 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003508 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003509 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003510 case tok::kw___cdecl:
3511 case tok::kw___stdcall:
3512 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003513 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003514 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003515 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003516 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003517 continue;
3518 }
3519 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003520 case tok::kw___pascal:
3521 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003522 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003523 continue;
3524 }
3525 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003526 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003527 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003528 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003529 continue; // do *not* consume the next token!
3530 }
3531 // otherwise, FALL THROUGH!
3532 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003533 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003534 // If this is not a type-qualifier token, we're done reading type
3535 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003536 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003537 if (EndLoc.isValid())
3538 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003539 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003540 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003541
Reid Spencer5f016e22007-07-11 17:01:13 +00003542 // If the specifier combination wasn't legal, issue a diagnostic.
3543 if (isInvalid) {
3544 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003545 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003546 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003547 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003548 }
3549}
3550
3551
3552/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3553///
3554void Parser::ParseDeclarator(Declarator &D) {
3555 /// This implements the 'declarator' production in the C grammar, then checks
3556 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003557 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003558}
3559
Richard Smith9988f282012-03-29 01:16:42 +00003560static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3561 if (Kind == tok::star || Kind == tok::caret)
3562 return true;
3563
3564 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3565 if (!Lang.CPlusPlus)
3566 return false;
3567
3568 return Kind == tok::amp || Kind == tok::ampamp;
3569}
3570
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003571/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3572/// is parsed by the function passed to it. Pass null, and the direct-declarator
3573/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003574/// ptr-operator production.
3575///
Richard Smith0706df42011-10-19 21:33:05 +00003576/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003577/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3578/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003579///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003580/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3581/// [C] pointer[opt] direct-declarator
3582/// [C++] direct-declarator
3583/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003584///
3585/// pointer: [C99 6.7.5]
3586/// '*' type-qualifier-list[opt]
3587/// '*' type-qualifier-list[opt] pointer
3588///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003589/// ptr-operator:
3590/// '*' cv-qualifier-seq[opt]
3591/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003592/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003593/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003594/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003595/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003596void Parser::ParseDeclaratorInternal(Declarator &D,
3597 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003598 if (Diags.hasAllExtensionsSilenced())
3599 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003600
Sebastian Redlf30208a2009-01-24 21:16:55 +00003601 // C++ member pointers start with a '::' or a nested-name.
3602 // Member pointers get special handling, since there's no place for the
3603 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003604 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003605 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3606 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003607 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3608 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003609 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003610 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003611
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003612 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003613 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003614 // The scope spec really belongs to the direct-declarator.
3615 D.getCXXScopeSpec() = SS;
3616 if (DirectDeclParser)
3617 (this->*DirectDeclParser)(D);
3618 return;
3619 }
3620
3621 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003622 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003623 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003624 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003625 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003626
3627 // Recurse to parse whatever is left.
3628 ParseDeclaratorInternal(D, DirectDeclParser);
3629
3630 // Sema will have to catch (syntactically invalid) pointers into global
3631 // scope. It has to catch pointers into namespace scope anyway.
3632 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003633 Loc),
3634 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003635 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003636 return;
3637 }
3638 }
3639
3640 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003641 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003642 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003643 if (DirectDeclParser)
3644 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003645 return;
3646 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003647
Sebastian Redl05532f22009-03-15 22:02:01 +00003648 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3649 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003650 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003651 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003652
Chris Lattner9af55002009-03-27 04:18:06 +00003653 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003654 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003655 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003656
Reid Spencer5f016e22007-07-11 17:01:13 +00003657 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003658 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003659
Reid Spencer5f016e22007-07-11 17:01:13 +00003660 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003661 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003662 if (Kind == tok::star)
3663 // Remember that we parsed a pointer type, and remember the type-quals.
3664 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003665 DS.getConstSpecLoc(),
3666 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003667 DS.getRestrictSpecLoc()),
3668 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003669 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003670 else
3671 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003672 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003673 Loc),
3674 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003675 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003676 } else {
3677 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003678 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003679
Sebastian Redl743de1f2009-03-23 00:00:23 +00003680 // Complain about rvalue references in C++03, but then go on and build
3681 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003682 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003683 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003684 diag::warn_cxx98_compat_rvalue_reference :
3685 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003686
Reid Spencer5f016e22007-07-11 17:01:13 +00003687 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3688 // cv-qualifiers are introduced through the use of a typedef or of a
3689 // template type argument, in which case the cv-qualifiers are ignored.
3690 //
3691 // [GNU] Retricted references are allowed.
3692 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003693 // [C++0x] Attributes on references are not allowed.
3694 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003695 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003696
3697 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3698 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3699 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003700 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003701 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3702 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003703 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003704 }
3705
3706 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003707 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003708
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003709 if (D.getNumTypeObjects() > 0) {
3710 // C++ [dcl.ref]p4: There shall be no references to references.
3711 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3712 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003713 if (const IdentifierInfo *II = D.getIdentifier())
3714 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3715 << II;
3716 else
3717 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3718 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003719
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003720 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003721 // can go ahead and build the (technically ill-formed)
3722 // declarator: reference collapsing will take care of it.
3723 }
3724 }
3725
Reid Spencer5f016e22007-07-11 17:01:13 +00003726 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003727 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003728 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003729 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003730 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003731 }
3732}
3733
Richard Smith9988f282012-03-29 01:16:42 +00003734static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
3735 SourceLocation EllipsisLoc) {
3736 if (EllipsisLoc.isValid()) {
3737 FixItHint Insertion;
3738 if (!D.getEllipsisLoc().isValid()) {
3739 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
3740 D.setEllipsisLoc(EllipsisLoc);
3741 }
3742 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
3743 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
3744 }
3745}
3746
Reid Spencer5f016e22007-07-11 17:01:13 +00003747/// ParseDirectDeclarator
3748/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003749/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003750/// '(' declarator ')'
3751/// [GNU] '(' attributes declarator ')'
3752/// [C90] direct-declarator '[' constant-expression[opt] ']'
3753/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3754/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3755/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3756/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3757/// direct-declarator '(' parameter-type-list ')'
3758/// direct-declarator '(' identifier-list[opt] ')'
3759/// [GNU] direct-declarator '(' parameter-forward-declarations
3760/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003761/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3762/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003763/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003764///
3765/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003766/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003767/// '::'[opt] nested-name-specifier[opt] type-name
3768///
3769/// id-expression: [C++ 5.1]
3770/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003771/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003772///
3773/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003774/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003775/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003776/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003777/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003778/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003779///
Richard Smith5d8388c2012-03-27 01:42:32 +00003780/// Note, any additional constructs added here may need corresponding changes
3781/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00003782void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003783 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003784
David Blaikie4e4d0842012-03-11 07:00:24 +00003785 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003786 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003787 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003788 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3789 D.getContext() == Declarator::MemberContext;
3790 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
3791 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003792 }
3793
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003794 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003795 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003796 // Change the declaration context for name lookup, until this function
3797 // is exited (and the declarator has been parsed).
3798 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003799 }
3800
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003801 // C++0x [dcl.fct]p14:
3802 // There is a syntactic ambiguity when an ellipsis occurs at the end
3803 // of a parameter-declaration-clause without a preceding comma. In
3804 // this case, the ellipsis is parsed as part of the
3805 // abstract-declarator if the type of the parameter names a template
3806 // parameter pack that has not been expanded; otherwise, it is parsed
3807 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00003808 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003809 !((D.getContext() == Declarator::PrototypeContext ||
3810 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003811 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00003812 !Actions.containsUnexpandedParameterPacks(D))) {
3813 SourceLocation EllipsisLoc = ConsumeToken();
3814 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
3815 // The ellipsis was put in the wrong place. Recover, and explain to
3816 // the user what they should have done.
3817 ParseDeclarator(D);
3818 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
3819 return;
3820 } else
3821 D.setEllipsisLoc(EllipsisLoc);
3822
3823 // The ellipsis can't be followed by a parenthesized declarator. We
3824 // check for that in ParseParenDeclarator, after we have disambiguated
3825 // the l_paren token.
3826 }
3827
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003828 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3829 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3830 // We found something that indicates the start of an unqualified-id.
3831 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003832 bool AllowConstructorName;
3833 if (D.getDeclSpec().hasTypeSpecifier())
3834 AllowConstructorName = false;
3835 else if (D.getCXXScopeSpec().isSet())
3836 AllowConstructorName =
3837 (D.getContext() == Declarator::FileContext ||
3838 (D.getContext() == Declarator::MemberContext &&
3839 D.getDeclSpec().isFriendSpecified()));
3840 else
3841 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3842
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003843 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003844 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3845 /*EnteringContext=*/true,
3846 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003847 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003848 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003849 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003850 D.getName()) ||
3851 // Once we're past the identifier, if the scope was bad, mark the
3852 // whole declarator bad.
3853 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003854 D.SetIdentifier(0, Tok.getLocation());
3855 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003856 } else {
3857 // Parsed the unqualified-id; update range information and move along.
3858 if (D.getSourceRange().getBegin().isInvalid())
3859 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3860 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003861 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003862 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003863 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003864 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003865 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003866 "There's a C++-specific check for tok::identifier above");
3867 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3868 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3869 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003870 goto PastIdentifier;
3871 }
Richard Smith9988f282012-03-29 01:16:42 +00003872
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003873 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003874 // direct-declarator: '(' declarator ')'
3875 // direct-declarator: '(' attributes declarator ')'
3876 // Example: 'char (*X)' or 'int (*XX)(void)'
3877 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003878
3879 // If the declarator was parenthesized, we entered the declarator
3880 // scope when parsing the parenthesized declarator, then exited
3881 // the scope already. Re-enter the scope, if we need to.
3882 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003883 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00003884 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003885 if (!D.isInvalidType() &&
3886 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003887 // Change the declaration context for name lookup, until this function
3888 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003889 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003890 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003891 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003892 // This could be something simple like "int" (in which case the declarator
3893 // portion is empty), if an abstract-declarator is allowed.
3894 D.SetIdentifier(0, Tok.getLocation());
3895 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003896 if (D.getContext() == Declarator::MemberContext)
3897 Diag(Tok, diag::err_expected_member_name_or_semi)
3898 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00003899 else if (getLangOpts().CPlusPlus)
3900 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003901 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003902 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003903 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003904 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003905 }
Mike Stump1eb44332009-09-09 15:08:12 +00003906
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003907 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003908 assert(D.isPastIdentifier() &&
3909 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003910
Sean Huntbbd37c62009-11-21 08:43:09 +00003911 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003912 if (D.getIdentifier())
3913 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003914
Reid Spencer5f016e22007-07-11 17:01:13 +00003915 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003916 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00003917 // Enter function-declaration scope, limiting any declarators to the
3918 // function prototype scope, including parameter declarators.
3919 ParseScope PrototypeScope(this,
3920 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003921 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3922 // In such a case, check if we actually have a function declarator; if it
3923 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00003924 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003925 // When not in file scope, warn for ambiguous function declarators, just
3926 // in case the author intended it as a variable definition.
3927 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3928 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3929 break;
3930 }
John McCall0b7e6782011-03-24 11:26:52 +00003931 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003932 BalancedDelimiterTracker T(*this, tok::l_paren);
3933 T.consumeOpen();
3934 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00003935 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00003936 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003937 ParseBracketDeclarator(D);
3938 } else {
3939 break;
3940 }
3941 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00003942}
Reid Spencer5f016e22007-07-11 17:01:13 +00003943
Chris Lattneref4715c2008-04-06 05:45:57 +00003944/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3945/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003946/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003947/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3948///
3949/// direct-declarator:
3950/// '(' declarator ')'
3951/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003952/// direct-declarator '(' parameter-type-list ')'
3953/// direct-declarator '(' identifier-list[opt] ')'
3954/// [GNU] direct-declarator '(' parameter-forward-declarations
3955/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003956///
3957void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003958 BalancedDelimiterTracker T(*this, tok::l_paren);
3959 T.consumeOpen();
3960
Chris Lattneref4715c2008-04-06 05:45:57 +00003961 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003962
Chris Lattner7399ee02008-10-20 02:05:46 +00003963 // Eat any attributes before we look at whether this is a grouping or function
3964 // declarator paren. If this is a grouping paren, the attribute applies to
3965 // the type being built up, for example:
3966 // int (__attribute__(()) *x)(long y)
3967 // If this ends up not being a grouping paren, the attribute applies to the
3968 // first argument, for example:
3969 // int (__attribute__(()) int x)
3970 // In either case, we need to eat any attributes to be able to determine what
3971 // sort of paren this is.
3972 //
John McCall0b7e6782011-03-24 11:26:52 +00003973 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003974 bool RequiresArg = false;
3975 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003976 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003977
Chris Lattner7399ee02008-10-20 02:05:46 +00003978 // We require that the argument list (if this is a non-grouping paren) be
3979 // present even if the attribute list was empty.
3980 RequiresArg = true;
3981 }
Steve Naroff239f0732008-12-25 14:16:32 +00003982 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003983 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003984 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003985 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00003986 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00003987 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003988 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003989 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003990 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003991 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003992
Chris Lattneref4715c2008-04-06 05:45:57 +00003993 // If we haven't past the identifier yet (or where the identifier would be
3994 // stored, if this is an abstract declarator), then this is probably just
3995 // grouping parens. However, if this could be an abstract-declarator, then
3996 // this could also be the start of function arguments (consider 'void()').
3997 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003998
Chris Lattneref4715c2008-04-06 05:45:57 +00003999 if (!D.mayOmitIdentifier()) {
4000 // If this can't be an abstract-declarator, this *must* be a grouping
4001 // paren, because we haven't seen the identifier yet.
4002 isGrouping = true;
4003 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004004 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4005 NextToken().is(tok::r_paren)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00004006 isDeclarationSpecifier()) { // 'int(int)' is a function.
4007 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4008 // considered to be a type, not a K&R identifier-list.
4009 isGrouping = false;
4010 } else {
4011 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4012 isGrouping = true;
4013 }
Mike Stump1eb44332009-09-09 15:08:12 +00004014
Chris Lattneref4715c2008-04-06 05:45:57 +00004015 // If this is a grouping paren, handle:
4016 // direct-declarator: '(' declarator ')'
4017 // direct-declarator: '(' attributes declarator ')'
4018 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004019 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4020 D.setEllipsisLoc(SourceLocation());
4021
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004022 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004023 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004024 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004025 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004026 T.consumeClose();
4027 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4028 T.getCloseLocation()),
4029 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004030
4031 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004032
4033 // An ellipsis cannot be placed outside parentheses.
4034 if (EllipsisLoc.isValid())
4035 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4036
Chris Lattneref4715c2008-04-06 05:45:57 +00004037 return;
4038 }
Mike Stump1eb44332009-09-09 15:08:12 +00004039
Chris Lattneref4715c2008-04-06 05:45:57 +00004040 // Okay, if this wasn't a grouping paren, it must be the start of a function
4041 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004042 // identifier (and remember where it would have been), then call into
4043 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004044 D.SetIdentifier(0, Tok.getLocation());
4045
David Blaikie42d6d0c2011-12-04 05:04:18 +00004046 // Enter function-declaration scope, limiting any declarators to the
4047 // function prototype scope, including parameter declarators.
4048 ParseScope PrototypeScope(this,
4049 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004050 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004051 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004052}
4053
4054/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4055/// declarator D up to a paren, which indicates that we are parsing function
4056/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004057///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004058/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00004059/// after the open paren - they should be considered to be the first argument of
4060/// a parameter. If RequiresArg is true, then the first argument of the
4061/// function is required to be present and required to not be an identifier
4062/// list.
4063///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004064/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
4065/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
4066/// (C++0x) trailing-return-type[opt].
4067///
4068/// [C++0x] exception-specification:
4069/// dynamic-exception-specification
4070/// noexcept-specification
4071///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004072void Parser::ParseFunctionDeclarator(Declarator &D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004073 ParsedAttributes &attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004074 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004075 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004076 assert(getCurScope()->isFunctionPrototypeScope() &&
4077 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004078 // lparen is already consumed!
4079 assert(D.isPastIdentifier() && "Should not call before identifier!");
4080
4081 // This should be true when the function has typed arguments.
4082 // Otherwise, it is treated as a K&R-style function.
4083 bool HasProto = false;
4084 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004085 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004086 // Remember where we see an ellipsis, if any.
4087 SourceLocation EllipsisLoc;
4088
4089 DeclSpec DS(AttrFactory);
4090 bool RefQualifierIsLValueRef = true;
4091 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004092 SourceLocation ConstQualifierLoc;
4093 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004094 ExceptionSpecificationType ESpecType = EST_None;
4095 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004096 SmallVector<ParsedType, 2> DynamicExceptions;
4097 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004098 ExprResult NoexceptExpr;
4099 ParsedType TrailingReturnType;
4100
James Molloy16f1f712012-02-29 10:24:19 +00004101 Actions.ActOnStartFunctionDeclarator();
4102
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004103 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004104 if (isFunctionDeclaratorIdentifierList()) {
4105 if (RequiresArg)
4106 Diag(Tok, diag::err_argument_required_after_attribute);
4107
4108 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4109
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004110 Tracker.consumeClose();
4111 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004112 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004113 if (Tok.isNot(tok::r_paren))
4114 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4115 else if (RequiresArg)
4116 Diag(Tok, diag::err_argument_required_after_attribute);
4117
David Blaikie4e4d0842012-03-11 07:00:24 +00004118 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004119
4120 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004121 Tracker.consumeClose();
4122 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004123
David Blaikie4e4d0842012-03-11 07:00:24 +00004124 if (getLangOpts().CPlusPlus) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004125 MaybeParseCXX0XAttributes(attrs);
4126
4127 // Parse cv-qualifier-seq[opt].
4128 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor43f51032011-10-19 06:04:55 +00004129 if (!DS.getSourceRange().getEnd().isInvalid()) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004130 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor43f51032011-10-19 06:04:55 +00004131 ConstQualifierLoc = DS.getConstSpecLoc();
4132 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4133 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004134
4135 // Parse ref-qualifier[opt].
4136 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004137 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004138 diag::warn_cxx98_compat_ref_qualifier :
4139 diag::ext_ref_qualifier);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004140
4141 RefQualifierIsLValueRef = Tok.is(tok::amp);
4142 RefQualifierLoc = ConsumeToken();
4143 EndLoc = RefQualifierLoc;
4144 }
4145
4146 // Parse exception-specification[opt].
4147 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4148 DynamicExceptions,
4149 DynamicExceptionRanges,
4150 NoexceptExpr);
4151 if (ESpecType != EST_None)
4152 EndLoc = ESpecRange.getEnd();
4153
4154 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004155 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004156 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004157 SourceRange Range;
4158 TrailingReturnType = ParseTrailingReturnType(Range).get();
4159 if (Range.getEnd().isValid())
4160 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004161 }
4162 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004163 }
4164
4165 // Remember that we parsed a function type, and remember the attributes.
4166 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4167 /*isVariadic=*/EllipsisLoc.isValid(),
4168 EllipsisLoc,
4169 ParamInfo.data(), ParamInfo.size(),
4170 DS.getTypeQualifiers(),
4171 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004172 RefQualifierLoc, ConstQualifierLoc,
4173 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004174 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004175 ESpecType, ESpecRange.getBegin(),
4176 DynamicExceptions.data(),
4177 DynamicExceptionRanges.data(),
4178 DynamicExceptions.size(),
4179 NoexceptExpr.isUsable() ?
4180 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004181 Tracker.getOpenLocation(),
4182 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004183 TrailingReturnType),
4184 attrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004185
4186 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004187}
4188
4189/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4190/// identifier list form for a K&R-style function: void foo(a,b,c)
4191///
4192/// Note that identifier-lists are only allowed for normal declarators, not for
4193/// abstract-declarators.
4194bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004195 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004196 && Tok.is(tok::identifier)
4197 && !TryAltiVecVectorToken()
4198 // K&R identifier lists can't have typedefs as identifiers, per C99
4199 // 6.7.5.3p11.
4200 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4201 // Identifier lists follow a really simple grammar: the identifiers can
4202 // be followed *only* by a ", identifier" or ")". However, K&R
4203 // identifier lists are really rare in the brave new modern world, and
4204 // it is very common for someone to typo a type in a non-K&R style
4205 // list. If we are presented with something like: "void foo(intptr x,
4206 // float y)", we don't want to start parsing the function declarator as
4207 // though it is a K&R style declarator just because intptr is an
4208 // invalid type.
4209 //
4210 // To handle this, we check to see if the token after the first
4211 // identifier is a "," or ")". Only then do we parse it as an
4212 // identifier list.
4213 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4214}
4215
4216/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4217/// we found a K&R-style identifier list instead of a typed parameter list.
4218///
4219/// After returning, ParamInfo will hold the parsed parameters.
4220///
4221/// identifier-list: [C99 6.7.5]
4222/// identifier
4223/// identifier-list ',' identifier
4224///
4225void Parser::ParseFunctionDeclaratorIdentifierList(
4226 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004227 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004228 // If there was no identifier specified for the declarator, either we are in
4229 // an abstract-declarator, or we are in a parameter declarator which was found
4230 // to be abstract. In abstract-declarators, identifier lists are not valid:
4231 // diagnose this.
4232 if (!D.getIdentifier())
4233 Diag(Tok, diag::ext_ident_list_in_param);
4234
4235 // Maintain an efficient lookup of params we have seen so far.
4236 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4237
4238 while (1) {
4239 // If this isn't an identifier, report the error and skip until ')'.
4240 if (Tok.isNot(tok::identifier)) {
4241 Diag(Tok, diag::err_expected_ident);
4242 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4243 // Forget we parsed anything.
4244 ParamInfo.clear();
4245 return;
4246 }
4247
4248 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4249
4250 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4251 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4252 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4253
4254 // Verify that the argument identifier has not already been mentioned.
4255 if (!ParamsSoFar.insert(ParmII)) {
4256 Diag(Tok, diag::err_param_redefinition) << ParmII;
4257 } else {
4258 // Remember this identifier in ParamInfo.
4259 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4260 Tok.getLocation(),
4261 0));
4262 }
4263
4264 // Eat the identifier.
4265 ConsumeToken();
4266
4267 // The list continues if we see a comma.
4268 if (Tok.isNot(tok::comma))
4269 break;
4270 ConsumeToken();
4271 }
4272}
4273
4274/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4275/// after the opening parenthesis. This function will not parse a K&R-style
4276/// identifier list.
4277///
4278/// D is the declarator being parsed. If attrs is non-null, then the caller
4279/// parsed those arguments immediately after the open paren - they should be
4280/// considered to be the first argument of a parameter.
4281///
4282/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4283/// be the location of the ellipsis, if any was parsed.
4284///
Reid Spencer5f016e22007-07-11 17:01:13 +00004285/// parameter-type-list: [C99 6.7.5]
4286/// parameter-list
4287/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004288/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004289///
4290/// parameter-list: [C99 6.7.5]
4291/// parameter-declaration
4292/// parameter-list ',' parameter-declaration
4293///
4294/// parameter-declaration: [C99 6.7.5]
4295/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004296/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004297/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004298/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004299/// declaration-specifiers abstract-declarator[opt]
4300/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004301/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004302/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4303///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004304void Parser::ParseParameterDeclarationClause(
4305 Declarator &D,
4306 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004307 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004308 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004309
Chris Lattnerf97409f2008-04-06 06:57:35 +00004310 while (1) {
4311 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004312 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004313 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004314 }
Mike Stump1eb44332009-09-09 15:08:12 +00004315
Chris Lattnerf97409f2008-04-06 06:57:35 +00004316 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004317 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004318 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004319
John McCall7f040a92010-12-24 02:08:15 +00004320 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004321 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004322 ParseMicrosoftAttributes(DS.getAttributes());
4323
4324 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004325
4326 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004327 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004328 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4329 // attributes lost? Should they even be allowed?
4330 // FIXME: If we can leave the attributes in the token stream somehow, we can
4331 // get rid of a parameter (attrs) and this statement. It might be too much
4332 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004333 DS.takeAttributesFrom(attrs);
4334
Chris Lattnere64c5492009-02-27 18:38:20 +00004335 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004336
Chris Lattnerf97409f2008-04-06 06:57:35 +00004337 // Parse the declarator. This is "PrototypeContext", because we must
4338 // accept either 'declarator' or 'abstract-declarator' here.
4339 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4340 ParseDeclarator(ParmDecl);
4341
4342 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004343 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004344
Chris Lattnerf97409f2008-04-06 06:57:35 +00004345 // Remember this parsed parameter in ParamInfo.
4346 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004347
Douglas Gregor72b505b2008-12-16 21:30:33 +00004348 // DefArgToks is used when the parsing of default arguments needs
4349 // to be delayed.
4350 CachedTokens *DefArgToks = 0;
4351
Chris Lattnerf97409f2008-04-06 06:57:35 +00004352 // If no parameter was specified, verify that *something* was specified,
4353 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004354 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4355 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004356 // Completely missing, emit error.
4357 Diag(DSStart, diag::err_missing_param);
4358 } else {
4359 // Otherwise, we have something. Add it and let semantic analysis try
4360 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004361
Chris Lattnerf97409f2008-04-06 06:57:35 +00004362 // Inform the actions module about the parameter declarator, so it gets
4363 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004364 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004365
4366 // Parse the default argument, if any. We parse the default
4367 // arguments in all dialects; the semantic analysis in
4368 // ActOnParamDefaultArgument will reject the default argument in
4369 // C.
4370 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004371 SourceLocation EqualLoc = Tok.getLocation();
4372
Chris Lattner04421082008-04-08 04:40:51 +00004373 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004374 if (D.getContext() == Declarator::MemberContext) {
4375 // If we're inside a class definition, cache the tokens
4376 // corresponding to the default argument. We'll actually parse
4377 // them when we see the end of the class definition.
4378 // FIXME: Templates will require something similar.
4379 // FIXME: Can we use a smart pointer for Toks?
4380 DefArgToks = new CachedTokens;
4381
Mike Stump1eb44332009-09-09 15:08:12 +00004382 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004383 /*StopAtSemi=*/true,
4384 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004385 delete DefArgToks;
4386 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004387 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004388 } else {
4389 // Mark the end of the default argument so that we know when to
4390 // stop when we parse it later on.
4391 Token DefArgEnd;
4392 DefArgEnd.startToken();
4393 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4394 DefArgEnd.setLocation(Tok.getLocation());
4395 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004396 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004397 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004398 }
Chris Lattner04421082008-04-08 04:40:51 +00004399 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004400 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004401 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004402
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004403 // The argument isn't actually potentially evaluated unless it is
4404 // used.
4405 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004406 Sema::PotentiallyEvaluatedIfUsed,
4407 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004408
Sebastian Redl84407ba2012-03-14 15:54:00 +00004409 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004410 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4411 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004412 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004413 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004414 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004415 if (DefArgResult.isInvalid()) {
4416 Actions.ActOnParamDefaultArgumentError(Param);
4417 SkipUntil(tok::comma, tok::r_paren, true, true);
4418 } else {
4419 // Inform the actions module about the default argument
4420 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004421 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004422 }
Chris Lattner04421082008-04-08 04:40:51 +00004423 }
4424 }
Mike Stump1eb44332009-09-09 15:08:12 +00004425
4426 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4427 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004428 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004429 }
4430
4431 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004432 if (Tok.isNot(tok::comma)) {
4433 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004434 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4435
David Blaikie4e4d0842012-03-11 07:00:24 +00004436 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004437 // We have ellipsis without a preceding ',', which is ill-formed
4438 // in C. Complain and provide the fix.
4439 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004440 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004441 }
4442 }
4443
4444 break;
4445 }
Mike Stump1eb44332009-09-09 15:08:12 +00004446
Chris Lattnerf97409f2008-04-06 06:57:35 +00004447 // Consume the comma.
4448 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004449 }
Mike Stump1eb44332009-09-09 15:08:12 +00004450
Chris Lattner66d28652008-04-06 06:34:08 +00004451}
Chris Lattneref4715c2008-04-06 05:45:57 +00004452
Reid Spencer5f016e22007-07-11 17:01:13 +00004453/// [C90] direct-declarator '[' constant-expression[opt] ']'
4454/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4455/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4456/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4457/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4458void Parser::ParseBracketDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004459 BalancedDelimiterTracker T(*this, tok::l_square);
4460 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004461
Chris Lattner378c7e42008-12-18 07:27:21 +00004462 // C array syntax has many features, but by-far the most common is [] and [4].
4463 // This code does a fast path to handle some of the most obvious cases.
4464 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004465 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004466 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004467 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004468
Chris Lattner378c7e42008-12-18 07:27:21 +00004469 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004470 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004471 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004472 T.getOpenLocation(),
4473 T.getCloseLocation()),
4474 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004475 return;
4476 } else if (Tok.getKind() == tok::numeric_constant &&
4477 GetLookAheadToken(1).is(tok::r_square)) {
4478 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004479 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004480 ConsumeToken();
4481
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004482 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004483 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004484 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004485
Chris Lattner378c7e42008-12-18 07:27:21 +00004486 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004487 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004488 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004489 T.getOpenLocation(),
4490 T.getCloseLocation()),
4491 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004492 return;
4493 }
Mike Stump1eb44332009-09-09 15:08:12 +00004494
Reid Spencer5f016e22007-07-11 17:01:13 +00004495 // If valid, this location is the position where we read the 'static' keyword.
4496 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004497 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004498 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004499
Reid Spencer5f016e22007-07-11 17:01:13 +00004500 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004501 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004502 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004503 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004504
Reid Spencer5f016e22007-07-11 17:01:13 +00004505 // If we haven't already read 'static', check to see if there is one after the
4506 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004507 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004508 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004509
Reid Spencer5f016e22007-07-11 17:01:13 +00004510 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4511 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004512 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004513
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004514 // Handle the case where we have '[*]' as the array size. However, a leading
4515 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4516 // the the token after the star is a ']'. Since stars in arrays are
4517 // infrequent, use of lookahead is not costly here.
4518 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004519 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004520
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004521 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004522 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004523 StaticLoc = SourceLocation(); // Drop the static.
4524 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004525 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004526 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004527 // Note, in C89, this production uses the constant-expr production instead
4528 // of assignment-expr. The only difference is that assignment-expr allows
4529 // things like '=' and '*='. Sema rejects these in C89 mode because they
4530 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004531
Douglas Gregore0762c92009-06-19 23:52:42 +00004532 // Parse the constant-expression or assignment-expression now (depending
4533 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004534 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004535 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004536 } else {
4537 EnterExpressionEvaluationContext Unevaluated(Actions,
4538 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004539 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004540 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004541 }
Mike Stump1eb44332009-09-09 15:08:12 +00004542
Reid Spencer5f016e22007-07-11 17:01:13 +00004543 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004544 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004545 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004546 // If the expression was invalid, skip it.
4547 SkipUntil(tok::r_square);
4548 return;
4549 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004550
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004551 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004552
John McCall0b7e6782011-03-24 11:26:52 +00004553 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004554 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004555
Chris Lattner378c7e42008-12-18 07:27:21 +00004556 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004557 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004558 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004559 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004560 T.getOpenLocation(),
4561 T.getCloseLocation()),
4562 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004563}
4564
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004565/// [GNU] typeof-specifier:
4566/// typeof ( expressions )
4567/// typeof ( type-name )
4568/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004569///
4570void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004571 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004572 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004573 SourceLocation StartLoc = ConsumeToken();
4574
John McCallcfb708c2010-01-13 20:03:27 +00004575 const bool hasParens = Tok.is(tok::l_paren);
4576
Eli Friedman71b8fb52012-01-21 01:01:51 +00004577 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4578
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004579 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004580 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004581 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004582 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4583 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004584 if (hasParens)
4585 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004586
4587 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004588 // FIXME: Not accurate, the range gets one token more than it should.
4589 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004590 else
4591 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004592
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004593 if (isCastExpr) {
4594 if (!CastTy) {
4595 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004596 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004597 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004598
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004599 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004600 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004601 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4602 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004603 DiagID, CastTy))
4604 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004605 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004606 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004607
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004608 // If we get here, the operand to the typeof was an expresion.
4609 if (Operand.isInvalid()) {
4610 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004611 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004612 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004613
Eli Friedman71b8fb52012-01-21 01:01:51 +00004614 // We might need to transform the operand if it is potentially evaluated.
4615 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4616 if (Operand.isInvalid()) {
4617 DS.SetTypeSpecError();
4618 return;
4619 }
4620
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004621 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004622 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004623 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4624 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004625 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004626 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004627}
Chris Lattner1b492422010-02-28 18:33:55 +00004628
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004629/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004630/// _Atomic ( type-name )
4631///
4632void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4633 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4634
4635 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004636 BalancedDelimiterTracker T(*this, tok::l_paren);
4637 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004638 SkipUntil(tok::r_paren);
4639 return;
4640 }
4641
4642 TypeResult Result = ParseTypeName();
4643 if (Result.isInvalid()) {
4644 SkipUntil(tok::r_paren);
4645 return;
4646 }
4647
4648 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004649 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004650
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004651 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004652 return;
4653
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004654 DS.setTypeofParensRange(T.getRange());
4655 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004656
4657 const char *PrevSpec = 0;
4658 unsigned DiagID;
4659 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4660 DiagID, Result.release()))
4661 Diag(StartLoc, DiagID) << PrevSpec;
4662}
4663
Chris Lattner1b492422010-02-28 18:33:55 +00004664
4665/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4666/// from TryAltiVecVectorToken.
4667bool Parser::TryAltiVecVectorTokenOutOfLine() {
4668 Token Next = NextToken();
4669 switch (Next.getKind()) {
4670 default: return false;
4671 case tok::kw_short:
4672 case tok::kw_long:
4673 case tok::kw_signed:
4674 case tok::kw_unsigned:
4675 case tok::kw_void:
4676 case tok::kw_char:
4677 case tok::kw_int:
4678 case tok::kw_float:
4679 case tok::kw_double:
4680 case tok::kw_bool:
4681 case tok::kw___pixel:
4682 Tok.setKind(tok::kw___vector);
4683 return true;
4684 case tok::identifier:
4685 if (Next.getIdentifierInfo() == Ident_pixel) {
4686 Tok.setKind(tok::kw___vector);
4687 return true;
4688 }
4689 return false;
4690 }
4691}
4692
4693bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4694 const char *&PrevSpec, unsigned &DiagID,
4695 bool &isInvalid) {
4696 if (Tok.getIdentifierInfo() == Ident_vector) {
4697 Token Next = NextToken();
4698 switch (Next.getKind()) {
4699 case tok::kw_short:
4700 case tok::kw_long:
4701 case tok::kw_signed:
4702 case tok::kw_unsigned:
4703 case tok::kw_void:
4704 case tok::kw_char:
4705 case tok::kw_int:
4706 case tok::kw_float:
4707 case tok::kw_double:
4708 case tok::kw_bool:
4709 case tok::kw___pixel:
4710 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4711 return true;
4712 case tok::identifier:
4713 if (Next.getIdentifierInfo() == Ident_pixel) {
4714 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4715 return true;
4716 }
4717 break;
4718 default:
4719 break;
4720 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004721 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004722 DS.isTypeAltiVecVector()) {
4723 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4724 return true;
4725 }
4726 return false;
4727}