blob: 3f15b5b089a44b76156d76766bf55c8b7c0b2637 [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Larisse Voufo7c64ef02013-06-21 00:08:46 +000016#include "clang/AST/DeclTemplate.h"
Benjamin Kramer9852f582012-12-01 16:35:25 +000017#include "clang/Basic/AddressSpaces.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000018#include "clang/Basic/CharInfo.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000019#include "clang/Basic/OpenCL.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000021#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000022#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000024#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000027#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// C99 6.7: Declarations.
32//===----------------------------------------------------------------------===//
33
34/// ParseTypeName
35/// type-name: [C99 6.7.6]
36/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000037///
38/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000039TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000040 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000041 AccessSpecifier AS,
Richard Smith6b3d3e52013-02-20 19:22:51 +000042 Decl **OwnedType,
43 ParsedAttributes *Attrs) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000044 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000045 if (DSC == DSC_normal)
46 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000047
Reid Spencer5f016e22007-07-11 17:01:13 +000048 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000049 DeclSpec DS(AttrFactory);
Richard Smith6b3d3e52013-02-20 19:22:51 +000050 if (Attrs)
51 DS.addAttributes(Attrs->getList());
Richard Smith7796eb52012-03-12 08:56:40 +000052 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000053 if (OwnedType)
54 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000055
Reid Spencer5f016e22007-07-11 17:01:13 +000056 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000057 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000058 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000059 if (Range)
60 *Range = DeclaratorInfo.getSourceRange();
61
Chris Lattnereaaebc72009-04-25 08:06:05 +000062 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000063 return true;
64
Douglas Gregor23c94db2010-07-02 17:43:08 +000065 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000066}
67
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000068
69/// isAttributeLateParsed - Return true if the attribute has arguments that
70/// require late parsing.
71static bool isAttributeLateParsed(const IdentifierInfo &II) {
72 return llvm::StringSwitch<bool>(II.getName())
73#include "clang/Parse/AttrLateParsed.inc"
74 .Default(false);
75}
76
Sean Huntbbd37c62009-11-21 08:43:09 +000077/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000078///
79/// [GNU] attributes:
80/// attribute
81/// attributes attribute
82///
83/// [GNU] attribute:
84/// '__attribute__' '(' '(' attribute-list ')' ')'
85///
86/// [GNU] attribute-list:
87/// attrib
88/// attribute_list ',' attrib
89///
90/// [GNU] attrib:
91/// empty
92/// attrib-name
93/// attrib-name '(' identifier ')'
94/// attrib-name '(' identifier ',' nonempty-expr-list ')'
95/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
96///
97/// [GNU] attrib-name:
98/// identifier
99/// typespec
100/// typequal
101/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +0000102///
Reid Spencer5f016e22007-07-11 17:01:13 +0000103/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +0000104/// token lookahead. Comment from gcc: "If they start with an identifier
105/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +0000106/// start with that identifier; otherwise they are an expression list."
107///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000108/// GCC does not require the ',' between attribs in an attribute-list.
109///
Reid Spencer5f016e22007-07-11 17:01:13 +0000110/// At the moment, I am not doing 2 token lookahead. I am also unaware of
111/// any attributes that don't work (based on my limited testing). Most
112/// attributes are very simple in practice. Until we find a bug, I don't see
113/// a pressing need to implement the 2 token lookahead.
114
John McCall7f040a92010-12-24 02:08:15 +0000115void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000116 SourceLocation *endLoc,
117 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000118 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Chris Lattner04d66662007-10-09 17:33:22 +0000120 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 ConsumeToken();
122 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
123 "attribute")) {
124 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000125 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 }
127 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
128 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000129 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 }
131 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000132 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
133 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000134 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
136 ConsumeToken();
137 continue;
138 }
139 // we have an identifier or declaration specifier (const, int, etc.)
140 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
141 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000143 if (Tok.is(tok::l_paren)) {
144 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000145 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000146 LateParsedAttribute *LA =
147 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
148 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000149
Bill Wendlingad017fa2012-12-20 19:22:21 +0000150 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000151 // with other late-parsed declarations.
DeLesley Hutchins161db022012-11-02 21:44:32 +0000152 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000153 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000155 // consume everything up to and including the matching right parens
156 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000158 Token Eof;
159 Eof.startToken();
160 Eof.setLocation(Tok.getLocation());
161 LA->Toks.push_back(Eof);
162 } else {
Michael Han6880f492012-10-03 01:56:22 +0000163 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000164 0, SourceLocation(), AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 }
166 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000167 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000168 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 }
170 }
171 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000173 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000174 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
175 SkipUntil(tok::r_paren, false);
176 }
John McCall7f040a92010-12-24 02:08:15 +0000177 if (endLoc)
178 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000180}
181
Douglas Gregor92eb7d82013-05-02 23:25:32 +0000182/// \brief Determine whether the given attribute has all expression arguments.
183static bool attributeHasExprArgs(const IdentifierInfo &II) {
184 return llvm::StringSwitch<bool>(II.getName())
185#include "clang/Parse/AttrExprArgs.inc"
186 .Default(false);
187}
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000188
Michael Han6880f492012-10-03 01:56:22 +0000189/// Parse the arguments to a parameterized GNU attribute or
190/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000191void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
192 SourceLocation AttrNameLoc,
193 ParsedAttributes &Attrs,
Michael Han6880f492012-10-03 01:56:22 +0000194 SourceLocation *EndLoc,
195 IdentifierInfo *ScopeName,
196 SourceLocation ScopeLoc,
197 AttributeList::Syntax Syntax) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000198
199 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
200
201 // Availability attributes have their own grammar.
202 if (AttrName->isStr("availability")) {
203 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
204 return;
205 }
206 // Thread safety attributes fit into the FIXME case above, so we
207 // just parse the arguments as a list of expressions
208 if (IsThreadSafetyAttribute(AttrName->getName())) {
209 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
210 return;
211 }
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000212 // Type safety attributes have their own grammar.
213 if (AttrName->isStr("type_tag_for_datatype")) {
214 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
215 return;
216 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000217
218 ConsumeParen(); // ignore the left paren loc for now
219
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000220 IdentifierInfo *ParmName = 0;
221 SourceLocation ParmLoc;
222 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000223
Joey Gouly37453b92013-03-08 09:42:32 +0000224 TypeResult T;
225 SourceRange TypeRange;
226 bool TypeParsed = false;
227
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000228 switch (Tok.getKind()) {
229 case tok::kw_char:
230 case tok::kw_wchar_t:
231 case tok::kw_char16_t:
232 case tok::kw_char32_t:
233 case tok::kw_bool:
234 case tok::kw_short:
235 case tok::kw_int:
236 case tok::kw_long:
237 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000238 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000239 case tok::kw_signed:
240 case tok::kw_unsigned:
241 case tok::kw_float:
242 case tok::kw_double:
243 case tok::kw_void:
244 case tok::kw_typeof:
245 // __attribute__(( vec_type_hint(char) ))
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000246 BuiltinType = true;
Joey Gouly37453b92013-03-08 09:42:32 +0000247 T = ParseTypeName(&TypeRange);
248 TypeParsed = true;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000249 break;
250
251 case tok::identifier:
Joey Gouly37453b92013-03-08 09:42:32 +0000252 if (AttrName->isStr("vec_type_hint")) {
253 T = ParseTypeName(&TypeRange);
254 TypeParsed = true;
255 break;
256 }
Douglas Gregor92eb7d82013-05-02 23:25:32 +0000257 // If the attribute has all expression arguments, and not a "parameter",
258 // break out to handle it below.
259 if (attributeHasExprArgs(*AttrName))
260 break;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000261 ParmName = Tok.getIdentifierInfo();
262 ParmLoc = ConsumeToken();
263 break;
264
265 default:
266 break;
267 }
268
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +0000269 ExprVector ArgExprs;
Joey Gouly37453b92013-03-08 09:42:32 +0000270 bool isInvalid = false;
271 bool isParmType = false;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000272
Joey Gouly37453b92013-03-08 09:42:32 +0000273 if (!BuiltinType && !AttrName->isStr("vec_type_hint") &&
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000274 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
275 // Eat the comma.
276 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000277 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000278
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000279 // Parse the non-empty comma-separated list of expressions.
280 while (1) {
281 ExprResult ArgExpr(ParseAssignmentExpression());
282 if (ArgExpr.isInvalid()) {
283 SkipUntil(tok::r_paren);
284 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000285 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000286 ArgExprs.push_back(ArgExpr.release());
287 if (Tok.isNot(tok::comma))
288 break;
289 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000290 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000291 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000292 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
293 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
294 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000295 while (Tok.is(tok::identifier)) {
296 ConsumeToken();
297 if (Tok.is(tok::greater))
298 break;
299 if (Tok.is(tok::comma)) {
300 ConsumeToken();
301 continue;
302 }
303 }
304 if (Tok.isNot(tok::greater))
305 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000306 SkipUntil(tok::r_paren, false, true); // skip until ')'
307 }
Joey Gouly37453b92013-03-08 09:42:32 +0000308 } else if (AttrName->isStr("vec_type_hint")) {
309 if (T.get() && !T.isInvalid())
310 isParmType = true;
311 else {
312 if (Tok.is(tok::identifier))
313 ConsumeToken();
314 if (TypeParsed)
315 isInvalid = true;
316 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000317 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000318
319 SourceLocation RParen = Tok.getLocation();
Joey Gouly37453b92013-03-08 09:42:32 +0000320 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) &&
321 !isInvalid) {
Michael Han45bed132012-10-04 16:42:52 +0000322 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Joey Gouly37453b92013-03-08 09:42:32 +0000323 if (isParmType) {
Joey Gouly37453b92013-03-08 09:42:32 +0000324 Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName,
325 ScopeLoc, ParmName, ParmLoc, T.get(), Syntax);
326 } else {
327 AttributeList *attr = Attrs.addNew(
328 AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, ParmName,
329 ParmLoc, ArgExprs.data(), ArgExprs.size(), Syntax);
330 if (BuiltinType &&
331 attr->getKind() == AttributeList::AT_IBOutletCollection)
332 Diag(Tok, diag::err_iboutletcollection_builtintype);
333 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000334 }
335}
336
Chad Rosier8decdee2012-06-26 22:30:43 +0000337/// \brief Parses a single argument for a declspec, including the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000338/// surrounding parens.
Chad Rosier8decdee2012-06-26 22:30:43 +0000339void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000340 SourceLocation AttrNameLoc,
341 ParsedAttributes &Attrs)
342{
343 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000344 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000345 AttrName->getNameStart(), tok::r_paren))
346 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000347
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000348 ExprResult ArgExpr(ParseConstantExpression());
349 if (ArgExpr.isInvalid()) {
350 T.skipToEnd();
351 return;
352 }
353 Expr *ExprList = ArgExpr.take();
Chad Rosier8decdee2012-06-26 22:30:43 +0000354 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000355 &ExprList, 1, AttributeList::AS_Declspec);
356
357 T.consumeClose();
358}
359
Chad Rosier8decdee2012-06-26 22:30:43 +0000360/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000361/// arguments.
362bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
363 return llvm::StringSwitch<bool>(Ident->getName())
364 .Case("dllimport", true)
365 .Case("dllexport", true)
366 .Case("noreturn", true)
367 .Case("nothrow", true)
368 .Case("noinline", true)
369 .Case("naked", true)
370 .Case("appdomain", true)
371 .Case("process", true)
372 .Case("jitintrinsic", true)
373 .Case("noalias", true)
374 .Case("restrict", true)
375 .Case("novtable", true)
376 .Case("selectany", true)
377 .Case("thread", true)
Aaron Ballman3ce0de62013-05-04 16:58:37 +0000378 .Case("safebuffers", true )
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000379 .Default(false);
380}
381
Chad Rosier8decdee2012-06-26 22:30:43 +0000382/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000383/// parameters). Will return false if we properly handled the declspec, or
384/// true if it is an unknown declspec.
Chad Rosier8decdee2012-06-26 22:30:43 +0000385void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000386 SourceLocation Loc,
387 ParsedAttributes &Attrs) {
388 // Try to handle the easy case first -- these declspecs all take a single
389 // parameter as their argument.
390 if (llvm::StringSwitch<bool>(Ident->getName())
391 .Case("uuid", true)
392 .Case("align", true)
393 .Case("allocate", true)
394 .Default(false)) {
395 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
396 } else if (Ident->getName() == "deprecated") {
Chad Rosier8decdee2012-06-26 22:30:43 +0000397 // The deprecated declspec has an optional single argument, so we will
398 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000399 // not.
400 if (Tok.getKind() == tok::l_paren)
401 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
402 else
Chad Rosier8decdee2012-06-26 22:30:43 +0000403 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000404 AttributeList::AS_Declspec);
405 } else if (Ident->getName() == "property") {
406 // The property declspec is more complex in that it can take one or two
Chad Rosier8decdee2012-06-26 22:30:43 +0000407 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000408 // must be named get or put.
John McCall76da55d2013-04-16 07:28:30 +0000409 if (Tok.isNot(tok::l_paren)) {
410 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
411 << Ident->getNameStart();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000412 return;
John McCall76da55d2013-04-16 07:28:30 +0000413 }
414 BalancedDelimiterTracker T(*this, tok::l_paren);
415 T.expectAndConsume(diag::err_expected_lparen_after,
416 Ident->getNameStart(), tok::r_paren);
417
418 enum AccessorKind {
419 AK_Invalid = -1,
420 AK_Put = 0, AK_Get = 1 // indices into AccessorNames
421 };
422 IdentifierInfo *AccessorNames[] = { 0, 0 };
423 bool HasInvalidAccessor = false;
424
425 // Parse the accessor specifications.
426 while (true) {
427 // Stop if this doesn't look like an accessor spec.
428 if (!Tok.is(tok::identifier)) {
429 // If the user wrote a completely empty list, use a special diagnostic.
430 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
431 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
432 Diag(Loc, diag::err_ms_property_no_getter_or_putter);
433 break;
434 }
435
436 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
437 break;
438 }
439
440 AccessorKind Kind;
441 SourceLocation KindLoc = Tok.getLocation();
442 StringRef KindStr = Tok.getIdentifierInfo()->getName();
443 if (KindStr == "get") {
444 Kind = AK_Get;
445 } else if (KindStr == "put") {
446 Kind = AK_Put;
447
448 // Recover from the common mistake of using 'set' instead of 'put'.
449 } else if (KindStr == "set") {
450 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
451 << FixItHint::CreateReplacement(KindLoc, "put");
452 Kind = AK_Put;
453
454 // Handle the mistake of forgetting the accessor kind by skipping
455 // this accessor.
456 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
457 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
458 ConsumeToken();
459 HasInvalidAccessor = true;
460 goto next_property_accessor;
461
462 // Otherwise, complain about the unknown accessor kind.
463 } else {
464 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
465 HasInvalidAccessor = true;
466 Kind = AK_Invalid;
467
468 // Try to keep parsing unless it doesn't look like an accessor spec.
469 if (!NextToken().is(tok::equal)) break;
470 }
471
472 // Consume the identifier.
473 ConsumeToken();
474
475 // Consume the '='.
476 if (Tok.is(tok::equal)) {
477 ConsumeToken();
478 } else {
479 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
480 << KindStr;
481 break;
482 }
483
484 // Expect the method name.
485 if (!Tok.is(tok::identifier)) {
486 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
487 break;
488 }
489
490 if (Kind == AK_Invalid) {
491 // Just drop invalid accessors.
492 } else if (AccessorNames[Kind] != NULL) {
493 // Complain about the repeated accessor, ignore it, and keep parsing.
494 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
495 } else {
496 AccessorNames[Kind] = Tok.getIdentifierInfo();
497 }
498 ConsumeToken();
499
500 next_property_accessor:
501 // Keep processing accessors until we run out.
502 if (Tok.is(tok::comma)) {
503 ConsumeAnyToken();
504 continue;
505
506 // If we run into the ')', stop without consuming it.
507 } else if (Tok.is(tok::r_paren)) {
508 break;
509 } else {
510 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
511 break;
512 }
513 }
514
515 // Only add the property attribute if it was well-formed.
516 if (!HasInvalidAccessor) {
517 Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), 0,
518 SourceLocation(),
519 AccessorNames[AK_Get], AccessorNames[AK_Put],
520 AttributeList::AS_Declspec);
521 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000522 T.skipToEnd();
523 } else {
524 // We don't recognize this as a valid declspec, but instead of creating the
525 // attribute and allowing sema to warn about it, we will warn here instead.
526 // This is because some attributes have multiple spellings, but we need to
527 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosier8decdee2012-06-26 22:30:43 +0000528 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000529 // both locations.
530 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
531
532 // If there's an open paren, we should eat the open and close parens under
533 // the assumption that this unknown declspec has parameters.
534 BalancedDelimiterTracker T(*this, tok::l_paren);
535 if (!T.consumeOpen())
536 T.skipToEnd();
537 }
538}
539
Eli Friedmana23b4852009-06-08 07:21:15 +0000540/// [MS] decl-specifier:
541/// __declspec ( extended-decl-modifier-seq )
542///
543/// [MS] extended-decl-modifier-seq:
544/// extended-decl-modifier[opt]
545/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000546void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000547 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000548
Steve Narofff59e17e2008-12-24 20:59:21 +0000549 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000550 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000551 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000552 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000553 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000554
Chad Rosier8decdee2012-06-26 22:30:43 +0000555 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000556 // you can specify multiple attributes per declspec.
557 while (Tok.getKind() != tok::r_paren) {
558 // We expect either a well-known identifier or a generic string. Anything
559 // else is a malformed declspec.
560 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000561 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000562 Tok.getKind() != tok::kw_restrict) {
563 Diag(Tok, diag::err_ms_declspec_type);
564 T.skipToEnd();
565 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000566 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000567
568 IdentifierInfo *AttrName;
569 SourceLocation AttrNameLoc;
570 if (IsString) {
571 SmallString<8> StrBuffer;
572 bool Invalid = false;
573 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
574 if (Invalid) {
575 T.skipToEnd();
576 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000577 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000578 AttrName = PP.getIdentifierInfo(Str);
579 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000580 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000581 AttrName = Tok.getIdentifierInfo();
582 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000583 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000584
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000585 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-06-26 22:30:43 +0000586 // If we have a generic string, we will allow it because there is no
587 // documented list of allowable string declspecs, but we know they exist
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000588 // (for instance, SAL declspecs in older versions of MSVC).
589 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000590 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000591 // arguments and can be turned into an attribute directly.
Chad Rosier8decdee2012-06-26 22:30:43 +0000592 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000593 0, 0, AttributeList::AS_Declspec);
594 else
595 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000596 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000597 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000598}
599
John McCall7f040a92010-12-24 02:08:15 +0000600void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000601 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000602 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000603 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000604 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballmanaa9df092013-05-22 23:25:32 +0000605 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
606 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000607 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
608 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000609 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000610 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman290eeb02009-06-08 23:27:34 +0000611 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000612}
613
John McCall7f040a92010-12-24 02:08:15 +0000614void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000615 // Treat these like attributes
616 while (Tok.is(tok::kw___pascal)) {
617 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
618 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000619 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000620 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000621 }
John McCall7f040a92010-12-24 02:08:15 +0000622}
623
Peter Collingbournef315fa82011-02-14 01:42:53 +0000624void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
625 // Treat these like attributes
626 while (Tok.is(tok::kw___kernel)) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000627 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbournef315fa82011-02-14 01:42:53 +0000628 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith5cd532c2013-01-29 01:24:26 +0000629 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
630 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000631 }
632}
633
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000634void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000635 // FIXME: The mapping from attribute spelling to semantics should be
636 // performed in Sema, not here.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000637 SourceLocation Loc = Tok.getLocation();
638 switch(Tok.getKind()) {
639 // OpenCL qualifiers:
640 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000641 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000642 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000643 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000644 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000645 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000646
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000647 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000648 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000649 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000650 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000651 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000652
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000653 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000654 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000655 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000656 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000657 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000658
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000659 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000660 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000661 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000662 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000663 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000664
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000665 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000666 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000667 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000668 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000669 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000670
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000671 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000672 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000673 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000674 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000675 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000676
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000677 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000678 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000679 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000680 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000681 break;
682 default: break;
683 }
684}
685
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000686/// \brief Parse a version number.
687///
688/// version:
689/// simple-integer
690/// simple-integer ',' simple-integer
691/// simple-integer ',' simple-integer ',' simple-integer
692VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
693 Range = Tok.getLocation();
694
695 if (!Tok.is(tok::numeric_constant)) {
696 Diag(Tok, diag::err_expected_version);
697 SkipUntil(tok::comma, tok::r_paren, true, true, true);
698 return VersionTuple();
699 }
700
701 // Parse the major (and possibly minor and subminor) versions, which
702 // are stored in the numeric constant. We utilize a quirk of the
703 // lexer, which is that it handles something like 1.2.3 as a single
704 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000705 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000706 Buffer.resize(Tok.getLength()+1);
707 const char *ThisTokBegin = &Buffer[0];
708
709 // Get the spelling of the token, which eliminates trigraphs, etc.
710 bool Invalid = false;
711 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
712 if (Invalid)
713 return VersionTuple();
714
715 // Parse the major version.
716 unsigned AfterMajor = 0;
717 unsigned Major = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000718 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000719 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
720 ++AfterMajor;
721 }
722
723 if (AfterMajor == 0) {
724 Diag(Tok, diag::err_expected_version);
725 SkipUntil(tok::comma, tok::r_paren, true, true, true);
726 return VersionTuple();
727 }
728
729 if (AfterMajor == ActualLength) {
730 ConsumeToken();
731
732 // We only had a single version component.
733 if (Major == 0) {
734 Diag(Tok, diag::err_zero_version);
735 return VersionTuple();
736 }
737
738 return VersionTuple(Major);
739 }
740
741 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
742 Diag(Tok, diag::err_expected_version);
743 SkipUntil(tok::comma, tok::r_paren, true, true, true);
744 return VersionTuple();
745 }
746
747 // Parse the minor version.
748 unsigned AfterMinor = AfterMajor + 1;
749 unsigned Minor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000750 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000751 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
752 ++AfterMinor;
753 }
754
755 if (AfterMinor == ActualLength) {
756 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000757
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000758 // We had major.minor.
759 if (Major == 0 && Minor == 0) {
760 Diag(Tok, diag::err_zero_version);
761 return VersionTuple();
762 }
763
Chad Rosier8decdee2012-06-26 22:30:43 +0000764 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000765 }
766
767 // If what follows is not a '.', we have a problem.
768 if (ThisTokBegin[AfterMinor] != '.') {
769 Diag(Tok, diag::err_expected_version);
770 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000771 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000772 }
773
774 // Parse the subminor version.
775 unsigned AfterSubminor = AfterMinor + 1;
776 unsigned Subminor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000777 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000778 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
779 ++AfterSubminor;
780 }
781
782 if (AfterSubminor != ActualLength) {
783 Diag(Tok, diag::err_expected_version);
784 SkipUntil(tok::comma, tok::r_paren, true, true, true);
785 return VersionTuple();
786 }
787 ConsumeToken();
788 return VersionTuple(Major, Minor, Subminor);
789}
790
791/// \brief Parse the contents of the "availability" attribute.
792///
793/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000794/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000795///
796/// platform:
797/// identifier
798///
799/// version-arg-list:
800/// version-arg
801/// version-arg ',' version-arg-list
802///
803/// version-arg:
804/// 'introduced' '=' version
805/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000806/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000807/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000808/// opt-message:
809/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000810void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
811 SourceLocation AvailabilityLoc,
812 ParsedAttributes &attrs,
813 SourceLocation *endLoc) {
814 SourceLocation PlatformLoc;
815 IdentifierInfo *Platform = 0;
816
817 enum { Introduced, Deprecated, Obsoleted, Unknown };
818 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000819 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000820
821 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000822 BalancedDelimiterTracker T(*this, tok::l_paren);
823 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000824 Diag(Tok, diag::err_expected_lparen);
825 return;
826 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000827
828 // Parse the platform name,
829 if (Tok.isNot(tok::identifier)) {
830 Diag(Tok, diag::err_availability_expected_platform);
831 SkipUntil(tok::r_paren);
832 return;
833 }
834 Platform = Tok.getIdentifierInfo();
835 PlatformLoc = ConsumeToken();
836
837 // Parse the ',' following the platform name.
838 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
839 return;
840
841 // If we haven't grabbed the pointers for the identifiers
842 // "introduced", "deprecated", and "obsoleted", do so now.
843 if (!Ident_introduced) {
844 Ident_introduced = PP.getIdentifierInfo("introduced");
845 Ident_deprecated = PP.getIdentifierInfo("deprecated");
846 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000847 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000848 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000849 }
850
851 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000852 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000853 do {
854 if (Tok.isNot(tok::identifier)) {
855 Diag(Tok, diag::err_availability_expected_change);
856 SkipUntil(tok::r_paren);
857 return;
858 }
859 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
860 SourceLocation KeywordLoc = ConsumeToken();
861
Douglas Gregorb53e4172011-03-26 03:35:55 +0000862 if (Keyword == Ident_unavailable) {
863 if (UnavailableLoc.isValid()) {
864 Diag(KeywordLoc, diag::err_availability_redundant)
865 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000866 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000867 UnavailableLoc = KeywordLoc;
868
869 if (Tok.isNot(tok::comma))
870 break;
871
872 ConsumeToken();
873 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000874 }
875
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000876 if (Tok.isNot(tok::equal)) {
877 Diag(Tok, diag::err_expected_equal_after)
878 << Keyword;
879 SkipUntil(tok::r_paren);
880 return;
881 }
882 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000883 if (Keyword == Ident_message) {
884 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000885 Diag(Tok, diag::err_expected_string_literal)
886 << /*Source='availability attribute'*/2;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000887 SkipUntil(tok::r_paren);
888 return;
889 }
890 MessageExpr = ParseStringLiteralExpression();
891 break;
892 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000893
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000894 SourceRange VersionRange;
895 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000896
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000897 if (Version.empty()) {
898 SkipUntil(tok::r_paren);
899 return;
900 }
901
902 unsigned Index;
903 if (Keyword == Ident_introduced)
904 Index = Introduced;
905 else if (Keyword == Ident_deprecated)
906 Index = Deprecated;
907 else if (Keyword == Ident_obsoleted)
908 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000909 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000910 Index = Unknown;
911
912 if (Index < Unknown) {
913 if (!Changes[Index].KeywordLoc.isInvalid()) {
914 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000915 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000916 << SourceRange(Changes[Index].KeywordLoc,
917 Changes[Index].VersionRange.getEnd());
918 }
919
920 Changes[Index].KeywordLoc = KeywordLoc;
921 Changes[Index].Version = Version;
922 Changes[Index].VersionRange = VersionRange;
923 } else {
924 Diag(KeywordLoc, diag::err_availability_unknown_change)
925 << Keyword << VersionRange;
926 }
927
928 if (Tok.isNot(tok::comma))
929 break;
930
931 ConsumeToken();
932 } while (true);
933
934 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000935 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000936 return;
937
938 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000939 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000940
Douglas Gregorb53e4172011-03-26 03:35:55 +0000941 // The 'unavailable' availability cannot be combined with any other
942 // availability changes. Make sure that hasn't happened.
943 if (UnavailableLoc.isValid()) {
944 bool Complained = false;
945 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
946 if (Changes[Index].KeywordLoc.isValid()) {
947 if (!Complained) {
948 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
949 << SourceRange(Changes[Index].KeywordLoc,
950 Changes[Index].VersionRange.getEnd());
951 Complained = true;
952 }
953
954 // Clear out the availability.
955 Changes[Index] = AvailabilityChange();
956 }
957 }
958 }
959
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000960 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000961 attrs.addNew(&Availability,
962 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000963 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000964 Platform, PlatformLoc,
965 Changes[Introduced],
966 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000967 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000968 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000969 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000970}
971
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000972
Bill Wendlingad017fa2012-12-20 19:22:21 +0000973// Late Parsed Attributes:
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000974// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
975
976void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
977
978void Parser::LateParsedClass::ParseLexedAttributes() {
979 Self->ParseLexedAttributes(*Class);
980}
981
982void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000983 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000984}
985
986/// Wrapper class which calls ParseLexedAttribute, after setting up the
987/// scope appropriately.
988void Parser::ParseLexedAttributes(ParsingClass &Class) {
989 // Deal with templates
990 // FIXME: Test cases to make sure this does the right thing for templates.
991 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
992 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
993 HasTemplateScope);
994 if (HasTemplateScope)
995 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
996
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000997 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000998 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000999 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001000 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1001 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1002
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +00001003 // Enter the scope of nested classes
1004 if (!AlreadyHasClassScope)
1005 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1006 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +00001007 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001008 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1009 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1010 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001011 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001012
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +00001013 if (!AlreadyHasClassScope)
1014 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1015 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001016}
1017
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001018
1019/// \brief Parse all attributes in LAs, and attach them to Decl D.
1020void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1021 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +00001022 assert(LAs.parseSoon() &&
1023 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001024 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +00001025 if (D)
1026 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001027 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +00001028 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001029 }
1030 LAs.clear();
1031}
1032
1033
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001034/// \brief Finish parsing an attribute for which parsing was delayed.
1035/// This will be called at the end of parsing a class declaration
1036/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +00001037/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001038/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001039void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1040 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001041 // Save the current token position.
1042 SourceLocation OrigLoc = Tok.getLocation();
1043
1044 // Append the current token at the end of the new token stream so that it
1045 // doesn't get lost.
1046 LA.Toks.push_back(Tok);
1047 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1048 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +00001049 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001050
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001051 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smithcd8ab512013-01-17 01:30:42 +00001052 // FIXME: Do not warn on C++11 attributes, once we start supporting
1053 // them here.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001054 Diag(Tok, diag::warn_attribute_on_function_definition)
1055 << LA.AttrName.getName();
1056 }
1057
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001058 ParsedAttributes Attrs(AttrFactory);
1059 SourceLocation endLoc;
1060
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001061 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001062 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001063 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1064 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +00001065
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001066 // Allow 'this' within late-parsed attributes.
Richard Smithcafeb942013-06-07 02:33:37 +00001067 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1068 ND && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001069
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001070 if (LA.Decls.size() == 1) {
1071 // If the Decl is templatized, add template parameters to scope.
1072 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1073 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1074 if (HasTemplateScope)
1075 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001076
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001077 // If the Decl is on a function, add function parameters to the scope.
1078 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1079 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1080 if (HasFunScope)
1081 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001082
Michael Han6880f492012-10-03 01:56:22 +00001083 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +00001084 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +00001085
1086 if (HasFunScope) {
1087 Actions.ActOnExitFunctionContext();
1088 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1089 }
1090 if (HasTemplateScope) {
1091 TempScope.Exit();
1092 }
1093 } else {
1094 // If there are multiple decls, then the decl cannot be within the
1095 // function scope.
Michael Han6880f492012-10-03 01:56:22 +00001096 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +00001097 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001098 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +00001099 } else {
1100 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +00001101 }
1102
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001103 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
1104 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1105 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001106
1107 if (Tok.getLocation() != OrigLoc) {
1108 // Due to a parsing error, we either went over the cached tokens or
1109 // there are still cached tokens left, so we skip the leftover tokens.
1110 // Since this is an uncommon situation that should be avoided, use the
1111 // expensive isBeforeInTranslationUnit call.
1112 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1113 OrigLoc))
1114 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001115 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001116 }
1117}
1118
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001119/// \brief Wrapper around a case statement checking if AttrName is
1120/// one of the thread safety attributes
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001121bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001122 return llvm::StringSwitch<bool>(AttrName)
1123 .Case("guarded_by", true)
1124 .Case("guarded_var", true)
1125 .Case("pt_guarded_by", true)
1126 .Case("pt_guarded_var", true)
1127 .Case("lockable", true)
1128 .Case("scoped_lockable", true)
1129 .Case("no_thread_safety_analysis", true)
1130 .Case("acquired_after", true)
1131 .Case("acquired_before", true)
1132 .Case("exclusive_lock_function", true)
1133 .Case("shared_lock_function", true)
1134 .Case("exclusive_trylock_function", true)
1135 .Case("shared_trylock_function", true)
1136 .Case("unlock_function", true)
1137 .Case("lock_returned", true)
1138 .Case("locks_excluded", true)
1139 .Case("exclusive_locks_required", true)
1140 .Case("shared_locks_required", true)
1141 .Default(false);
1142}
1143
1144/// \brief Parse the contents of thread safety attributes. These
1145/// should always be parsed as an expression list.
1146///
1147/// We need to special case the parsing due to the fact that if the first token
1148/// of the first argument is an identifier, the main parse loop will store
1149/// that token as a "parameter" and the rest of
1150/// the arguments will be added to a list of "arguments". However,
1151/// subsequent tokens in the first argument are lost. We instead parse each
1152/// argument as an expression and add all arguments to the list of "arguments".
1153/// In future, we will take advantage of this special case to also
1154/// deal with some argument scoping issues here (for example, referring to a
1155/// function parameter in the attribute on that function).
1156void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1157 SourceLocation AttrNameLoc,
1158 ParsedAttributes &Attrs,
1159 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001160 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001161
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001162 BalancedDelimiterTracker T(*this, tok::l_paren);
1163 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001164
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001165 ExprVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001166 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001167
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001168 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001169 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinsed4330b2013-02-07 19:01:07 +00001170 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001171 ExprResult ArgExpr(ParseAssignmentExpression());
1172 if (ArgExpr.isInvalid()) {
1173 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001174 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001175 break;
1176 } else {
1177 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001178 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001179 if (Tok.isNot(tok::comma))
1180 break;
1181 ConsumeToken(); // Eat the comma, move to the next argument
1182 }
1183 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001184 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001185 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001186 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001187 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001188 if (EndLoc)
1189 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001190}
1191
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001192void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1193 SourceLocation AttrNameLoc,
1194 ParsedAttributes &Attrs,
1195 SourceLocation *EndLoc) {
1196 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1197
1198 BalancedDelimiterTracker T(*this, tok::l_paren);
1199 T.consumeOpen();
1200
1201 if (Tok.isNot(tok::identifier)) {
1202 Diag(Tok, diag::err_expected_ident);
1203 T.skipToEnd();
1204 return;
1205 }
1206 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1207 SourceLocation ArgumentKindLoc = ConsumeToken();
1208
1209 if (Tok.isNot(tok::comma)) {
1210 Diag(Tok, diag::err_expected_comma);
1211 T.skipToEnd();
1212 return;
1213 }
1214 ConsumeToken();
1215
1216 SourceRange MatchingCTypeRange;
1217 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1218 if (MatchingCType.isInvalid()) {
1219 T.skipToEnd();
1220 return;
1221 }
1222
1223 bool LayoutCompatible = false;
1224 bool MustBeNull = false;
1225 while (Tok.is(tok::comma)) {
1226 ConsumeToken();
1227 if (Tok.isNot(tok::identifier)) {
1228 Diag(Tok, diag::err_expected_ident);
1229 T.skipToEnd();
1230 return;
1231 }
1232 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1233 if (Flag->isStr("layout_compatible"))
1234 LayoutCompatible = true;
1235 else if (Flag->isStr("must_be_null"))
1236 MustBeNull = true;
1237 else {
1238 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1239 T.skipToEnd();
1240 return;
1241 }
1242 ConsumeToken(); // consume flag
1243 }
1244
1245 if (!T.consumeClose()) {
1246 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1247 ArgumentKind, ArgumentKindLoc,
1248 MatchingCType.release(), LayoutCompatible,
1249 MustBeNull, AttributeList::AS_GNU);
1250 }
1251
1252 if (EndLoc)
1253 *EndLoc = T.getCloseLocation();
1254}
1255
Richard Smith6ee326a2012-04-10 01:32:12 +00001256/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1257/// of a C++11 attribute-specifier in a location where an attribute is not
1258/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1259/// situation.
1260///
1261/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1262/// this doesn't appear to actually be an attribute-specifier, and the caller
1263/// should try to parse it.
1264bool Parser::DiagnoseProhibitedCXX11Attribute() {
1265 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1266
1267 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1268 case CAK_NotAttributeSpecifier:
1269 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1270 return false;
1271
1272 case CAK_InvalidAttributeSpecifier:
1273 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1274 return false;
1275
1276 case CAK_AttributeSpecifier:
1277 // Parse and discard the attributes.
1278 SourceLocation BeginLoc = ConsumeBracket();
1279 ConsumeBracket();
1280 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1281 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1282 SourceLocation EndLoc = ConsumeBracket();
1283 Diag(BeginLoc, diag::err_attributes_not_allowed)
1284 << SourceRange(BeginLoc, EndLoc);
1285 return true;
1286 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001287 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001288}
1289
Richard Smith975d52c2013-02-20 01:17:14 +00001290/// \brief We have found the opening square brackets of a C++11
1291/// attribute-specifier in a location where an attribute is not permitted, but
1292/// we know where the attributes ought to be written. Parse them anyway, and
1293/// provide a fixit moving them to the right place.
Richard Smith05321402013-02-19 23:47:15 +00001294void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1295 SourceLocation CorrectLocation) {
1296 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1297 Tok.is(tok::kw_alignas));
1298
1299 // Consume the attributes.
1300 SourceLocation Loc = Tok.getLocation();
1301 ParseCXX11Attributes(Attrs);
1302 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1303
1304 Diag(Loc, diag::err_attributes_not_allowed)
1305 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1306 << FixItHint::CreateRemoval(AttrRange);
1307}
1308
John McCall7f040a92010-12-24 02:08:15 +00001309void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1310 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1311 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001312}
1313
Michael Hanf64231e2012-11-06 19:34:54 +00001314void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1315 AttributeList *AttrList = attrs.getList();
1316 while (AttrList) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001317 if (AttrList->isCXX11Attribute()) {
Richard Smithd03de6a2013-01-29 10:02:16 +00001318 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Hanf64231e2012-11-06 19:34:54 +00001319 << AttrList->getName();
1320 AttrList->setInvalid();
1321 }
1322 AttrList = AttrList->getNext();
1323 }
1324}
1325
Reid Spencer5f016e22007-07-11 17:01:13 +00001326/// ParseDeclaration - Parse a full 'declaration', which consists of
1327/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001328/// 'Context' should be a Declarator::TheContext value. This returns the
1329/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001330///
1331/// declaration: [C99 6.7]
1332/// block-declaration ->
1333/// simple-declaration
1334/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001335/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001336/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001337/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001338/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001339/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001340/// others... [FIXME]
1341///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001342Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1343 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001344 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001345 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001346 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001347 // Must temporarily exit the objective-c container scope for
1348 // parsing c none objective-c decls.
1349 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001350
John McCalld226f652010-08-21 09:40:31 +00001351 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001352 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001353 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001354 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001355 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001356 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001357 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001358 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001359 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001360 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001361 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001362 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001363 SourceLocation InlineLoc = ConsumeToken();
1364 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1365 break;
1366 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001367 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001368 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001369 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001370 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001371 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001372 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001373 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001374 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001375 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001376 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001377 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001378 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001379 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001380 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001381 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001382 default:
John McCall7f040a92010-12-24 02:08:15 +00001383 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001384 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001385
Chris Lattner682bf922009-03-29 16:50:03 +00001386 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001387 // single decl, convert it now. Alias declarations can also declare a type;
1388 // include that too if it is present.
1389 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001390}
1391
1392/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1393/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001394/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1395/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001396///[C90/C++]init-declarator-list ';' [TODO]
1397/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001398///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001399/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001400/// attribute-specifier-seq[opt] type-specifier-seq declarator
1401///
Chris Lattnercd147752009-03-29 17:27:48 +00001402/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001403/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001404///
1405/// If FRI is non-null, we might be parsing a for-range-declaration instead
1406/// of a simple-declaration. If we find that we are, we also parse the
1407/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001408Parser::DeclGroupPtrTy
1409Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1410 SourceLocation &DeclEnd,
Richard Smith68ea3ae2013-02-22 09:06:26 +00001411 ParsedAttributesWithRange &Attrs,
Sean Hunt2edf0a22012-06-23 05:07:58 +00001412 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001413 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001414 ParsingDeclSpec DS(*this);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001415
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001416 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001417 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001418
Reid Spencer5f016e22007-07-11 17:01:13 +00001419 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1420 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001421 if (Tok.is(tok::semi)) {
Richard Smith68ea3ae2013-02-22 09:06:26 +00001422 ProhibitAttributes(Attrs);
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001423 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001424 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001425 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001426 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001427 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001428 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001429 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001430
Richard Smith68ea3ae2013-02-22 09:06:26 +00001431 DS.takeAttributesFrom(Attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00001432 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001433}
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Richard Smith0706df42011-10-19 21:33:05 +00001435/// Returns true if this might be the start of a declarator, or a common typo
1436/// for a declarator.
1437bool Parser::MightBeDeclarator(unsigned Context) {
1438 switch (Tok.getKind()) {
1439 case tok::annot_cxxscope:
1440 case tok::annot_template_id:
1441 case tok::caret:
1442 case tok::code_completion:
1443 case tok::coloncolon:
1444 case tok::ellipsis:
1445 case tok::kw___attribute:
1446 case tok::kw_operator:
1447 case tok::l_paren:
1448 case tok::star:
1449 return true;
1450
1451 case tok::amp:
1452 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001453 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001454
Richard Smith1c94c162012-01-09 22:31:44 +00001455 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001456 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001457 NextToken().is(tok::l_square);
1458
1459 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001460 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001461
Richard Smith0706df42011-10-19 21:33:05 +00001462 case tok::identifier:
1463 switch (NextToken().getKind()) {
1464 case tok::code_completion:
1465 case tok::coloncolon:
1466 case tok::comma:
1467 case tok::equal:
1468 case tok::equalequal: // Might be a typo for '='.
1469 case tok::kw_alignas:
1470 case tok::kw_asm:
1471 case tok::kw___attribute:
1472 case tok::l_brace:
1473 case tok::l_paren:
1474 case tok::l_square:
1475 case tok::less:
1476 case tok::r_brace:
1477 case tok::r_paren:
1478 case tok::r_square:
1479 case tok::semi:
1480 return true;
1481
1482 case tok::colon:
1483 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001484 // and in block scope it's probably a label. Inside a class definition,
1485 // this is a bit-field.
1486 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001487 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001488
1489 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001490 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001491
1492 default:
1493 return false;
1494 }
1495
1496 default:
1497 return false;
1498 }
1499}
1500
Richard Smith994d73f2012-04-11 20:59:20 +00001501/// Skip until we reach something which seems like a sensible place to pick
1502/// up parsing after a malformed declaration. This will sometimes stop sooner
1503/// than SkipUntil(tok::r_brace) would, but will never stop later.
1504void Parser::SkipMalformedDecl() {
1505 while (true) {
1506 switch (Tok.getKind()) {
1507 case tok::l_brace:
1508 // Skip until matching }, then stop. We've probably skipped over
1509 // a malformed class or function definition or similar.
1510 ConsumeBrace();
1511 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1512 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1513 // This declaration isn't over yet. Keep skipping.
1514 continue;
1515 }
1516 if (Tok.is(tok::semi))
1517 ConsumeToken();
1518 return;
1519
1520 case tok::l_square:
1521 ConsumeBracket();
1522 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1523 continue;
1524
1525 case tok::l_paren:
1526 ConsumeParen();
1527 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1528 continue;
1529
1530 case tok::r_brace:
1531 return;
1532
1533 case tok::semi:
1534 ConsumeToken();
1535 return;
1536
1537 case tok::kw_inline:
1538 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001539 // a good place to pick back up parsing, except in an Objective-C
1540 // @interface context.
1541 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1542 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001543 return;
1544 break;
1545
1546 case tok::kw_namespace:
1547 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001548 // place to pick back up parsing, except in an Objective-C
1549 // @interface context.
1550 if (Tok.isAtStartOfLine() &&
1551 (!ParsingInObjCContainer || CurParsedObjCImpl))
1552 return;
1553 break;
1554
1555 case tok::at:
1556 // @end is very much like } in Objective-C contexts.
1557 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1558 ParsingInObjCContainer)
1559 return;
1560 break;
1561
1562 case tok::minus:
1563 case tok::plus:
1564 // - and + probably start new method declarations in Objective-C contexts.
1565 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001566 return;
1567 break;
1568
1569 case tok::eof:
1570 return;
1571
1572 default:
1573 break;
1574 }
1575
1576 ConsumeAnyToken();
1577 }
1578}
1579
John McCalld8ac0572009-11-03 19:26:08 +00001580/// ParseDeclGroup - Having concluded that this is either a function
1581/// definition or a group of object declarations, actually parse the
1582/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001583Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1584 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001585 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001586 SourceLocation *DeclEnd,
1587 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001588 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001589 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001590 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001591
John McCalld8ac0572009-11-03 19:26:08 +00001592 // Bail out if the first declarator didn't seem well-formed.
1593 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001594 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001595 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001596 }
Mike Stump1eb44332009-09-09 15:08:12 +00001597
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001598 // Save late-parsed attributes for now; they need to be parsed in the
1599 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001600 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1601 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001602 if (D.isFunctionDeclarator())
1603 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1604
Chris Lattnerc82daef2010-07-11 22:24:20 +00001605 // Check to see if we have a function *definition* which must have a body.
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001606 if (D.isFunctionDeclarator() &&
Chris Lattnerc82daef2010-07-11 22:24:20 +00001607 // Look at the next token to make sure that this isn't a function
1608 // declaration. We have to check this because __attribute__ might be the
1609 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001610 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001611
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001612 if (AllowFunctionDefinitions) {
1613 if (isStartOfFunctionDefinition(D)) {
1614 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1615 Diag(Tok, diag::err_function_declared_typedef);
John McCalld8ac0572009-11-03 19:26:08 +00001616
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001617 // Recover by treating the 'typedef' as spurious.
1618 DS.ClearStorageClassSpecs();
1619 }
1620
1621 Decl *TheDecl =
1622 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1623 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld8ac0572009-11-03 19:26:08 +00001624 }
1625
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001626 if (isDeclarationSpecifier()) {
1627 // If there is an invalid declaration specifier right after the function
1628 // prototype, then we must be in a missing semicolon case where this isn't
1629 // actually a body. Just fall through into the code that handles it as a
1630 // prototype, and let the top-level code handle the erroneous declspec
1631 // where it would otherwise expect a comma or semicolon.
1632 } else {
1633 Diag(Tok, diag::err_expected_fn_body);
1634 SkipUntil(tok::semi);
1635 return DeclGroupPtrTy();
1636 }
John McCalld8ac0572009-11-03 19:26:08 +00001637 } else {
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001638 if (Tok.is(tok::l_brace)) {
1639 Diag(Tok, diag::err_function_definition_not_allowed);
1640 SkipUntil(tok::r_brace, true, true);
1641 }
John McCalld8ac0572009-11-03 19:26:08 +00001642 }
1643 }
1644
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001645 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001646 return DeclGroupPtrTy();
1647
1648 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1649 // must parse and analyze the for-range-initializer before the declaration is
1650 // analyzed.
Douglas Gregor12849d02013-04-08 20:52:24 +00001651 //
1652 // Handle the Objective-C for-in loop variable similarly, although we
1653 // don't need to parse the container in advance.
1654 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1655 bool IsForRangeLoop = false;
1656 if (Tok.is(tok::colon)) {
1657 IsForRangeLoop = true;
1658 FRI->ColonLoc = ConsumeToken();
1659 if (Tok.is(tok::l_brace))
1660 FRI->RangeExpr = ParseBraceInitializer();
1661 else
1662 FRI->RangeExpr = ParseExpression();
1663 }
1664
Richard Smithad762fc2011-04-14 22:09:26 +00001665 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor12849d02013-04-08 20:52:24 +00001666 if (IsForRangeLoop)
1667 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001668 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001669 D.complete(ThisDecl);
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001670 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001671 }
1672
Chris Lattner5f9e2722011-07-23 10:55:15 +00001673 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001674 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001675 if (LateParsedAttrs.size() > 0)
1676 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001677 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001678 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001679 DeclsInGroup.push_back(FirstDecl);
1680
Richard Smith0706df42011-10-19 21:33:05 +00001681 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001682
John McCalld8ac0572009-11-03 19:26:08 +00001683 // If we don't have a comma, it is either the end of the list (a ';') or an
1684 // error, bail out.
1685 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001686 SourceLocation CommaLoc = ConsumeToken();
1687
1688 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1689 // This comma was followed by a line-break and something which can't be
1690 // the start of a declarator. The comma was probably a typo for a
1691 // semicolon.
1692 Diag(CommaLoc, diag::err_expected_semi_declaration)
1693 << FixItHint::CreateReplacement(CommaLoc, ";");
1694 ExpectSemi = false;
1695 break;
1696 }
John McCalld8ac0572009-11-03 19:26:08 +00001697
1698 // Parse the next declarator.
1699 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001700 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001701
1702 // Accept attributes in an init-declarator. In the first declarator in a
1703 // declaration, these would be part of the declspec. In subsequent
1704 // declarators, they become part of the declarator itself, so that they
1705 // don't apply to declarators after *this* one. Examples:
1706 // short __attribute__((common)) var; -> declspec
1707 // short var __attribute__((common)); -> declarator
1708 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001709 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001710
1711 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001712 if (!D.isInvalidType()) {
1713 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1714 D.complete(ThisDecl);
1715 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001716 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001717 }
John McCalld8ac0572009-11-03 19:26:08 +00001718 }
1719
1720 if (DeclEnd)
1721 *DeclEnd = Tok.getLocation();
1722
Richard Smith0706df42011-10-19 21:33:05 +00001723 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001724 ExpectAndConsumeSemi(Context == Declarator::FileContext
1725 ? diag::err_invalid_token_after_toplevel_declarator
1726 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001727 // Okay, there was no semicolon and one was expected. If we see a
1728 // declaration specifier, just assume it was missing and continue parsing.
1729 // Otherwise things are very confused and we skip to recover.
1730 if (!isDeclarationSpecifier()) {
1731 SkipUntil(tok::r_brace, true, true);
1732 if (Tok.is(tok::semi))
1733 ConsumeToken();
1734 }
John McCalld8ac0572009-11-03 19:26:08 +00001735 }
1736
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001737 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +00001738}
1739
Richard Smithad762fc2011-04-14 22:09:26 +00001740/// Parse an optional simple-asm-expr and attributes, and attach them to a
1741/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001742bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001743 // If a simple-asm-expr is present, parse it.
1744 if (Tok.is(tok::kw_asm)) {
1745 SourceLocation Loc;
1746 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1747 if (AsmLabel.isInvalid()) {
1748 SkipUntil(tok::semi, true, true);
1749 return true;
1750 }
1751
1752 D.setAsmLabel(AsmLabel.release());
1753 D.SetRangeEnd(Loc);
1754 }
1755
1756 MaybeParseGNUAttributes(D);
1757 return false;
1758}
1759
Douglas Gregor1426e532009-05-12 21:31:51 +00001760/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1761/// declarator'. This method parses the remainder of the declaration
1762/// (including any attributes or initializer, among other things) and
1763/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001764///
Reid Spencer5f016e22007-07-11 17:01:13 +00001765/// init-declarator: [C99 6.7]
1766/// declarator
1767/// declarator '=' initializer
1768/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1769/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001770/// [C++] declarator initializer[opt]
1771///
1772/// [C++] initializer:
1773/// [C++] '=' initializer-clause
1774/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001775/// [C++0x] '=' 'default' [TODO]
1776/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001777/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001778///
1779/// According to the standard grammar, =default and =delete are function
1780/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001781///
John McCalld226f652010-08-21 09:40:31 +00001782Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001783 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001784 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001785 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Richard Smithad762fc2011-04-14 22:09:26 +00001787 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1788}
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Richard Smithad762fc2011-04-14 22:09:26 +00001790Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1791 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001792 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001793 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001794 switch (TemplateInfo.Kind) {
1795 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001796 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001797 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001798
Douglas Gregord5a423b2009-09-25 18:43:00 +00001799 case ParsedTemplateInfo::Template:
Larisse Voufoef4579c2013-08-06 01:03:05 +00001800 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001801 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001802 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001803 D);
Larisse Voufo06935f32013-08-06 03:43:07 +00001804 if (Tok.is(tok::semi) &&
1805 Actions.HandleVariableRedeclaration(ThisDecl, D.getCXXScopeSpec())) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00001806 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001807 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001808 }
Larisse Voufoef4579c2013-08-06 01:03:05 +00001809 if (VarTemplateDecl *VT =
1810 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : 0)
1811 // Re-direct this decl to refer to the templated decl so that we can
1812 // initialize it.
1813 ThisDecl = VT->getTemplatedDecl();
1814 break;
1815 }
1816 case ParsedTemplateInfo::ExplicitInstantiation: {
1817 if (Tok.is(tok::semi)) {
1818 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1819 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1820 if (ThisRes.isInvalid()) {
1821 SkipUntil(tok::semi, true, true);
1822 return 0;
1823 }
1824 ThisDecl = ThisRes.get();
1825 } else {
1826 // FIXME: This check should be for a variable template instantiation only.
1827
1828 // Check that this is a valid instantiation
1829 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1830 // If the declarator-id is not a template-id, issue a diagnostic and
1831 // recover by ignoring the 'template' keyword.
1832 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1833 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1834 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1835 } else {
1836 SourceLocation LAngleLoc =
1837 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1838 Diag(D.getIdentifierLoc(),
1839 diag::err_explicit_instantiation_with_definition)
1840 << SourceRange(TemplateInfo.TemplateLoc)
1841 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1842
1843 // Recover as if it were an explicit specialization.
1844 TemplateParameterLists FakedParamLists;
1845 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1846 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1847 LAngleLoc));
1848
1849 ThisDecl =
1850 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1851 }
1852 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00001853 break;
1854 }
1855 }
Mike Stump1eb44332009-09-09 15:08:12 +00001856
Richard Smitha2c36462013-04-26 16:15:35 +00001857 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith34b41d92011-02-20 03:19:35 +00001858
Douglas Gregor1426e532009-05-12 21:31:51 +00001859 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001860 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001861 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001862 ConsumeToken();
Larisse Voufoef4579c2013-08-06 01:03:05 +00001863
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001864 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001865 if (D.isFunctionDeclarator())
1866 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1867 << 1 /* delete */;
1868 else
1869 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001870 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001871 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001872 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1873 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001874 else
1875 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001876 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001877 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001878 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001879 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001880 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001881
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001882 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001883 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001884 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001885 cutOffParsing();
1886 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001887 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001888
John McCall60d7b3a2010-08-24 06:29:42 +00001889 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001890
David Blaikie4e4d0842012-03-11 07:00:24 +00001891 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001892 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001893 ExitScope();
1894 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001895
Douglas Gregor1426e532009-05-12 21:31:51 +00001896 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001897 SkipUntil(tok::comma, true, true);
1898 Actions.ActOnInitializerError(ThisDecl);
1899 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001900 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1901 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001902 }
1903 } else if (Tok.is(tok::l_paren)) {
1904 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001905 BalancedDelimiterTracker T(*this, tok::l_paren);
1906 T.consumeOpen();
1907
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001908 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001909 CommaLocsTy CommaLocs;
1910
David Blaikie4e4d0842012-03-11 07:00:24 +00001911 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001912 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001913 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001914 }
1915
Douglas Gregor1426e532009-05-12 21:31:51 +00001916 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001917 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001918 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001919
David Blaikie4e4d0842012-03-11 07:00:24 +00001920 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001921 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001922 ExitScope();
1923 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001924 } else {
1925 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001926 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001927
1928 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1929 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001930
David Blaikie4e4d0842012-03-11 07:00:24 +00001931 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001932 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001933 ExitScope();
1934 }
1935
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001936 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1937 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001938 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001939 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1940 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001941 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001942 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001943 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001944 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001945 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1946
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001947 if (D.getCXXScopeSpec().isSet()) {
1948 EnterScope(0);
1949 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1950 }
1951
1952 ExprResult Init(ParseBraceInitializer());
1953
1954 if (D.getCXXScopeSpec().isSet()) {
1955 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1956 ExitScope();
1957 }
1958
1959 if (Init.isInvalid()) {
1960 Actions.ActOnInitializerError(ThisDecl);
1961 } else
1962 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1963 /*DirectInit=*/true, TypeContainsAuto);
1964
Douglas Gregor1426e532009-05-12 21:31:51 +00001965 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001966 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001967 }
1968
Richard Smith483b9f32011-02-21 20:05:19 +00001969 Actions.FinalizeDeclaration(ThisDecl);
1970
Douglas Gregor1426e532009-05-12 21:31:51 +00001971 return ThisDecl;
1972}
1973
Reid Spencer5f016e22007-07-11 17:01:13 +00001974/// ParseSpecifierQualifierList
1975/// specifier-qualifier-list:
1976/// type-specifier specifier-qualifier-list[opt]
1977/// type-qualifier specifier-qualifier-list[opt]
1978/// [GNU] attributes specifier-qualifier-list[opt]
1979///
Richard Smith69730c12012-03-12 07:56:15 +00001980void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1981 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001982 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1983 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001984 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001985 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001986
Reid Spencer5f016e22007-07-11 17:01:13 +00001987 // Validate declspec for type-name.
1988 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001989 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1990 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001991 Diag(Tok, diag::err_expected_type);
1992 DS.SetTypeSpecError();
1993 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1994 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001995 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001996 if (!DS.hasTypeSpecifier())
1997 DS.SetTypeSpecError();
1998 }
Mike Stump1eb44332009-09-09 15:08:12 +00001999
Reid Spencer5f016e22007-07-11 17:01:13 +00002000 // Issue diagnostic and remove storage class if present.
2001 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
2002 if (DS.getStorageClassSpecLoc().isValid())
2003 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2004 else
Richard Smithec642442013-04-12 22:46:28 +00002005 Diag(DS.getThreadStorageClassSpecLoc(),
2006 diag::err_typename_invalid_storageclass);
Reid Spencer5f016e22007-07-11 17:01:13 +00002007 DS.ClearStorageClassSpecs();
2008 }
Mike Stump1eb44332009-09-09 15:08:12 +00002009
Reid Spencer5f016e22007-07-11 17:01:13 +00002010 // Issue diagnostic and remove function specfier if present.
2011 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00002012 if (DS.isInlineSpecified())
2013 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2014 if (DS.isVirtualSpecified())
2015 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2016 if (DS.isExplicitSpecified())
2017 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00002018 DS.ClearFunctionSpecs();
2019 }
Richard Smith69730c12012-03-12 07:56:15 +00002020
2021 // Issue diagnostic and remove constexpr specfier if present.
2022 if (DS.isConstexprSpecified()) {
2023 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2024 DS.ClearConstexprSpec();
2025 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002026}
2027
Chris Lattnerc199ab32009-04-12 20:42:31 +00002028/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2029/// specified token is valid after the identifier in a declarator which
2030/// immediately follows the declspec. For example, these things are valid:
2031///
2032/// int x [ 4]; // direct-declarator
2033/// int x ( int y); // direct-declarator
2034/// int(int x ) // direct-declarator
2035/// int x ; // simple-declaration
2036/// int x = 17; // init-declarator-list
2037/// int x , y; // init-declarator-list
2038/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00002039/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00002040/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00002041///
2042/// This is not, because 'x' does not immediately follow the declspec (though
2043/// ')' happens to be valid anyway).
2044/// int (x)
2045///
2046static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2047 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2048 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00002049 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00002050}
2051
Chris Lattnere40c2952009-04-14 21:34:55 +00002052
2053/// ParseImplicitInt - This method is called when we have an non-typename
2054/// identifier in a declspec (which normally terminates the decl spec) when
2055/// the declspec has no type specifier. In this case, the declspec is either
2056/// malformed or is "implicit int" (in K&R and C89).
2057///
2058/// This method handles diagnosing this prettily and returns false if the
2059/// declspec is done being processed. If it recovers and thinks there may be
2060/// other pieces of declspec after it, it returns true.
2061///
Chris Lattnerf4382f52009-04-14 22:17:06 +00002062bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002063 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00002064 AccessSpecifier AS, DeclSpecContext DSC,
2065 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00002066 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002067
Chris Lattnere40c2952009-04-14 21:34:55 +00002068 SourceLocation Loc = Tok.getLocation();
2069 // If we see an identifier that is not a type name, we normally would
2070 // parse it as the identifer being declared. However, when a typename
2071 // is typo'd or the definition is not included, this will incorrectly
2072 // parse the typename as the identifier name and fall over misparsing
2073 // later parts of the diagnostic.
2074 //
2075 // As such, we try to do some look-ahead in cases where this would
2076 // otherwise be an "implicit-int" case to see if this is invalid. For
2077 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2078 // an identifier with implicit int, we'd get a parse error because the
2079 // next token is obviously invalid for a type. Parse these as a case
2080 // with an invalid type specifier.
2081 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00002082
Chris Lattnere40c2952009-04-14 21:34:55 +00002083 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00002084 // error, do lookahead to try to do better recovery. This never applies
2085 // within a type specifier. Outside of C++, we allow this even if the
2086 // language doesn't "officially" support implicit int -- we support
Richard Smith58eb3702013-04-30 22:43:51 +00002087 // implicit int as an extension in C99 and C11.
Richard Smitha971d242012-05-09 20:55:26 +00002088 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith58eb3702013-04-30 22:43:51 +00002089 !getLangOpts().CPlusPlus &&
Richard Smith69730c12012-03-12 07:56:15 +00002090 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00002091 // If this token is valid for implicit int, e.g. "static x = 4", then
2092 // we just avoid eating the identifier, so it will be parsed as the
2093 // identifier in the declarator.
2094 return false;
2095 }
Mike Stump1eb44332009-09-09 15:08:12 +00002096
Richard Smith827adaf2012-05-15 21:01:51 +00002097 if (getLangOpts().CPlusPlus &&
2098 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2099 // Don't require a type specifier if we have the 'auto' storage class
2100 // specifier in C++98 -- we'll promote it to a type specifier.
2101 return false;
2102 }
2103
Chris Lattnere40c2952009-04-14 21:34:55 +00002104 // Otherwise, if we don't consume this token, we are going to emit an
2105 // error anyway. Try to recover from various common problems. Check
2106 // to see if this was a reference to a tag name without a tag specified.
2107 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00002108 //
2109 // C++ doesn't need this, and isTagName doesn't take SS.
2110 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002111 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002112 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00002113
Douglas Gregor23c94db2010-07-02 17:43:08 +00002114 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00002115 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002116 case DeclSpec::TST_enum:
2117 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2118 case DeclSpec::TST_union:
2119 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2120 case DeclSpec::TST_struct:
2121 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00002122 case DeclSpec::TST_interface:
2123 TagName="__interface"; FixitTagName = "__interface ";
2124 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002125 case DeclSpec::TST_class:
2126 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00002127 }
Mike Stump1eb44332009-09-09 15:08:12 +00002128
Chris Lattnerf4382f52009-04-14 22:17:06 +00002129 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002130 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2131 LookupResult R(Actions, TokenName, SourceLocation(),
2132 Sema::LookupOrdinaryName);
2133
Chris Lattnerf4382f52009-04-14 22:17:06 +00002134 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002135 << TokenName << TagName << getLangOpts().CPlusPlus
2136 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2137
2138 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2139 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2140 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00002141 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002142 << TokenName << TagName;
2143 }
Mike Stump1eb44332009-09-09 15:08:12 +00002144
Chris Lattnerf4382f52009-04-14 22:17:06 +00002145 // Parse this as a tag as if the missing tag were present.
2146 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00002147 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00002148 else
Richard Smith69730c12012-03-12 07:56:15 +00002149 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00002150 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00002151 return true;
2152 }
Chris Lattnere40c2952009-04-14 21:34:55 +00002153 }
Mike Stump1eb44332009-09-09 15:08:12 +00002154
Richard Smith8f0a7e72012-05-15 21:29:55 +00002155 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00002156 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00002157 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
2158 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00002159 // Look ahead to the next token to try to figure out what this declaration
2160 // was supposed to be.
2161 switch (NextToken().getKind()) {
2162 case tok::comma:
2163 case tok::equal:
2164 case tok::kw_asm:
2165 case tok::l_brace:
2166 case tok::l_square:
2167 case tok::semi:
2168 // This looks like a variable declaration. The type is probably missing.
2169 // We're done parsing decl-specifiers.
2170 return false;
2171
2172 case tok::l_paren: {
2173 // static x(4); // 'x' is not a type
2174 // x(int n); // 'x' is not a type
2175 // x (*p)[]; // 'x' is a type
2176 //
2177 // Since we're in an error case (or the rare 'implicit int in C++' MS
2178 // extension), we can afford to perform a tentative parse to determine
2179 // which case we're in.
2180 TentativeParsingAction PA(*this);
2181 ConsumeToken();
2182 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2183 PA.Revert();
2184 if (TPR == TPResult::False())
2185 return false;
2186 // The identifier is followed by a parenthesized declarator.
2187 // It's supposed to be a type.
2188 break;
2189 }
2190
2191 default:
2192 // This is probably supposed to be a type. This includes cases like:
2193 // int f(itn);
2194 // struct S { unsinged : 4; };
2195 break;
2196 }
2197 }
2198
Chad Rosier8decdee2012-06-26 22:30:43 +00002199 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00002200 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00002201 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002202 IdentifierInfo *II = Tok.getIdentifierInfo();
2203 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00002204 // The action emitted a diagnostic, so we don't have to.
2205 if (T) {
2206 // The action has suggested that the type T could be used. Set that as
2207 // the type in the declaration specifiers, consume the would-be type
2208 // name token, and we're done.
2209 const char *PrevSpec;
2210 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00002211 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00002212 DS.SetRangeEnd(Tok.getLocation());
2213 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002214 // There may be other declaration specifiers after this.
2215 return true;
2216 } else if (II != Tok.getIdentifierInfo()) {
2217 // If no type was suggested, the correction is to a keyword
2218 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002219 // There may be other declaration specifiers after this.
2220 return true;
2221 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002222
Douglas Gregora786fdb2009-10-13 23:27:22 +00002223 // Fall through; the action had no suggestion for us.
2224 } else {
2225 // The action did not emit a diagnostic, so emit one now.
2226 SourceRange R;
2227 if (SS) R = SS->getRange();
2228 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2229 }
Mike Stump1eb44332009-09-09 15:08:12 +00002230
Douglas Gregora786fdb2009-10-13 23:27:22 +00002231 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002232 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002233 DS.SetRangeEnd(Tok.getLocation());
2234 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002235
Chris Lattnere40c2952009-04-14 21:34:55 +00002236 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2237 // avoid rippling error messages on subsequent uses of the same type,
2238 // could be useful if #include was forgotten.
2239 return false;
2240}
2241
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002242/// \brief Determine the declaration specifier context from the declarator
2243/// context.
2244///
2245/// \param Context the declarator context, which is one of the
2246/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002247Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002248Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2249 if (Context == Declarator::MemberContext)
2250 return DSC_class;
2251 if (Context == Declarator::FileContext)
2252 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002253 if (Context == Declarator::TrailingReturnContext)
2254 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002255 return DSC_normal;
2256}
2257
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002258/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2259///
2260/// FIXME: Simply returns an alignof() expression if the argument is a
2261/// type. Ideally, the type should be propagated directly into Sema.
2262///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002263/// [C11] type-id
2264/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002265/// [C++0x] type-id ...[opt]
2266/// [C++0x] assignment-expression ...[opt]
2267ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2268 SourceLocation &EllipsisLoc) {
2269 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002270 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002271 SourceLocation TypeLoc = Tok.getLocation();
2272 ParsedType Ty = ParseTypeName().get();
2273 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002274 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2275 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002276 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002277 ER = ParseConstantExpression();
2278
Richard Smith80ad52f2013-01-02 11:42:31 +00002279 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002280 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002281
2282 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002283}
2284
2285/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2286/// attribute to Attrs.
2287///
2288/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002289/// [C11] '_Alignas' '(' type-id ')'
2290/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002291/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2292/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002293void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smithf6565a92013-02-22 08:32:16 +00002294 SourceLocation *EndLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002295 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2296 "Not an alignment-specifier!");
2297
Richard Smith33f04a22013-01-29 01:48:07 +00002298 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2299 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002300
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002301 BalancedDelimiterTracker T(*this, tok::l_paren);
2302 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002303 return;
2304
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002305 SourceLocation EllipsisLoc;
2306 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002307 if (ArgExpr.isInvalid()) {
2308 SkipUntil(tok::r_paren);
2309 return;
2310 }
2311
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002312 T.consumeClose();
Richard Smithf6565a92013-02-22 08:32:16 +00002313 if (EndLoc)
2314 *EndLoc = T.getCloseLocation();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002315
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002316 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002317 ArgExprs.push_back(ArgExpr.release());
Richard Smith33f04a22013-01-29 01:48:07 +00002318 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
Richard Smithf6565a92013-02-22 08:32:16 +00002319 ArgExprs.data(), 1, AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002320}
2321
Reid Spencer5f016e22007-07-11 17:01:13 +00002322/// ParseDeclarationSpecifiers
2323/// declaration-specifiers: [C99 6.7]
2324/// storage-class-specifier declaration-specifiers[opt]
2325/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002326/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002327/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002328/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002329/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002330///
2331/// storage-class-specifier: [C99 6.7.1]
2332/// 'typedef'
2333/// 'extern'
2334/// 'static'
2335/// 'auto'
2336/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002337/// [C++] 'mutable'
Richard Smithec642442013-04-12 22:46:28 +00002338/// [C++11] 'thread_local'
2339/// [C11] '_Thread_local'
Reid Spencer5f016e22007-07-11 17:01:13 +00002340/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002341/// function-specifier: [C99 6.7.4]
2342/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002343/// [C++] 'virtual'
2344/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002345/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002346/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002347/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002348
Reid Spencer5f016e22007-07-11 17:01:13 +00002349///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002350void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002351 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002352 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002353 DeclSpecContext DSContext,
2354 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002355 if (DS.getSourceRange().isInvalid()) {
2356 DS.SetRangeStart(Tok.getLocation());
2357 DS.SetRangeEnd(Tok.getLocation());
2358 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002359
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002360 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002361 bool AttrsLastTime = false;
2362 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002363 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002364 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002365 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002366 unsigned DiagID = 0;
2367
Reid Spencer5f016e22007-07-11 17:01:13 +00002368 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002369
Reid Spencer5f016e22007-07-11 17:01:13 +00002370 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002371 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002372 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002373 if (!AttrsLastTime)
2374 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002375 else {
2376 // Reject C++11 attributes that appertain to decl specifiers as
2377 // we don't support any C++11 attributes that appertain to decl
2378 // specifiers. This also conforms to what g++ 4.8 is doing.
2379 ProhibitCXX11Attributes(attrs);
2380
Sean Hunt2edf0a22012-06-23 05:07:58 +00002381 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002382 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002383
Reid Spencer5f016e22007-07-11 17:01:13 +00002384 // If this is not a declaration specifier token, we're done reading decl
2385 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002386 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002387 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002388
Sean Hunt2edf0a22012-06-23 05:07:58 +00002389 case tok::l_square:
2390 case tok::kw_alignas:
Richard Smith672edb02013-02-22 09:15:49 +00002391 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Sean Hunt2edf0a22012-06-23 05:07:58 +00002392 goto DoneWithDeclSpec;
2393
2394 ProhibitAttributes(attrs);
2395 // FIXME: It would be good to recover by accepting the attributes,
2396 // but attempting to do that now would cause serious
2397 // madness in terms of diagnostics.
2398 attrs.clear();
2399 attrs.Range = SourceRange();
2400
2401 ParseCXX11Attributes(attrs);
2402 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002403 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002404
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002405 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002406 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002407 if (DS.hasTypeSpecifier()) {
2408 bool AllowNonIdentifiers
2409 = (getCurScope()->getFlags() & (Scope::ControlScope |
2410 Scope::BlockScope |
2411 Scope::TemplateParamScope |
2412 Scope::FunctionPrototypeScope |
2413 Scope::AtCatchScope)) == 0;
2414 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002415 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002416 (DSContext == DSC_class && DS.isFriendSpecified());
2417
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002418 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002419 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002420 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002421 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002422 }
2423
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002424 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2425 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2426 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002427 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002428 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002429 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002430 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002431 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002432 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002433
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002434 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002435 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002436 }
2437
Chris Lattner5e02c472009-01-05 00:07:25 +00002438 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002439 // C++ scope specifier. Annotate and loop, or bail out on error.
2440 if (TryAnnotateCXXScopeToken(true)) {
2441 if (!DS.hasTypeSpecifier())
2442 DS.SetTypeSpecError();
2443 goto DoneWithDeclSpec;
2444 }
John McCall2e0a7152010-03-01 18:20:46 +00002445 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2446 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002447 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002448
2449 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002450 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002451 goto DoneWithDeclSpec;
2452
John McCallaa87d332009-12-12 11:40:51 +00002453 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002454 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2455 Tok.getAnnotationRange(),
2456 SS);
John McCallaa87d332009-12-12 11:40:51 +00002457
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002458 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002459 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002460 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002461 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002462 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002463 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002464
2465 // C++ [class.qual]p2:
2466 // In a lookup in which the constructor is an acceptable lookup
2467 // result and the nested-name-specifier nominates a class C:
2468 //
2469 // - if the name specified after the
2470 // nested-name-specifier, when looked up in C, is the
2471 // injected-class-name of C (Clause 9), or
2472 //
2473 // - if the name specified after the nested-name-specifier
2474 // is the same as the identifier or the
2475 // simple-template-id's template-name in the last
2476 // component of the nested-name-specifier,
2477 //
2478 // the name is instead considered to name the constructor of
2479 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002480 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002481 // Thus, if the template-name is actually the constructor
2482 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002483 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002484 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002485 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCallba9d8532010-04-13 06:39:49 +00002486 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002487 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002488 if (isConstructorDeclarator()) {
2489 // The user meant this to be an out-of-line constructor
2490 // definition, but template arguments are not allowed
2491 // there. Just allow this as a constructor; we'll
2492 // complain about it later.
2493 goto DoneWithDeclSpec;
2494 }
2495
2496 // The user meant this to name a type, but it actually names
2497 // a constructor with some extraneous template
2498 // arguments. Complain, then parse it as a type as the user
2499 // intended.
2500 Diag(TemplateId->TemplateNameLoc,
2501 diag::err_out_of_line_template_id_names_constructor)
2502 << TemplateId->Name;
2503 }
2504
John McCallaa87d332009-12-12 11:40:51 +00002505 DS.getTypeSpecScope() = SS;
2506 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002507 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002508 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002509 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002510 continue;
2511 }
2512
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002513 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002514 DS.getTypeSpecScope() = SS;
2515 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002516 if (Tok.getAnnotationValue()) {
2517 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002518 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002519 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002520 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002521 if (isInvalid)
2522 break;
John McCallb3d87482010-08-24 05:47:05 +00002523 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002524 else
2525 DS.SetTypeSpecError();
2526 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2527 ConsumeToken(); // The typename
2528 }
2529
Douglas Gregor9135c722009-03-25 15:40:00 +00002530 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002531 goto DoneWithDeclSpec;
2532
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002533 // If we're in a context where the identifier could be a class name,
2534 // check whether this is a constructor declaration.
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002535 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002536 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002537 &SS)) {
2538 if (isConstructorDeclarator())
2539 goto DoneWithDeclSpec;
2540
2541 // As noted in C++ [class.qual]p2 (cited above), when the name
2542 // of the class is qualified in a context where it could name
2543 // a constructor, its a constructor name. However, we've
2544 // looked at the declarator, and the user probably meant this
2545 // to be a type. Complain that it isn't supposed to be treated
2546 // as a type, then proceed to parse it as a type.
2547 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2548 << Next.getIdentifierInfo();
2549 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002550
John McCallb3d87482010-08-24 05:47:05 +00002551 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2552 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002553 getCurScope(), &SS,
2554 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002555 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002556 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002557
Chris Lattnerf4382f52009-04-14 22:17:06 +00002558 // If the referenced identifier is not a type, then this declspec is
2559 // erroneous: We already checked about that it has no type specifier, and
2560 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002561 // typename.
David Blaikie7247c882013-05-15 07:37:26 +00002562 if (!TypeRep) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00002563 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002564 ParsedAttributesWithRange Attrs(AttrFactory);
2565 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2566 if (!Attrs.empty()) {
2567 AttrsLastTime = true;
2568 attrs.takeAllFrom(Attrs);
2569 }
2570 continue;
2571 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002572 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002573 }
Mike Stump1eb44332009-09-09 15:08:12 +00002574
John McCallaa87d332009-12-12 11:40:51 +00002575 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002576 ConsumeToken(); // The C++ scope.
2577
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002578 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002579 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002580 if (isInvalid)
2581 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002582
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002583 DS.SetRangeEnd(Tok.getLocation());
2584 ConsumeToken(); // The typename.
2585
2586 continue;
2587 }
Mike Stump1eb44332009-09-09 15:08:12 +00002588
Chris Lattner80d0c892009-01-21 19:48:37 +00002589 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002590 if (Tok.getAnnotationValue()) {
2591 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002592 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002593 DiagID, T);
2594 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002595 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002596
Chris Lattner5c5db552010-04-05 18:18:31 +00002597 if (isInvalid)
2598 break;
2599
Chris Lattner80d0c892009-01-21 19:48:37 +00002600 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2601 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002602
Chris Lattner80d0c892009-01-21 19:48:37 +00002603 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2604 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002605 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002606 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002607 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002608
Chris Lattner80d0c892009-01-21 19:48:37 +00002609 continue;
2610 }
Mike Stump1eb44332009-09-09 15:08:12 +00002611
Douglas Gregorbfad9152011-04-28 15:48:45 +00002612 case tok::kw___is_signed:
2613 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2614 // typically treats it as a trait. If we see __is_signed as it appears
2615 // in libstdc++, e.g.,
2616 //
2617 // static const bool __is_signed;
2618 //
2619 // then treat __is_signed as an identifier rather than as a keyword.
2620 if (DS.getTypeSpecType() == TST_bool &&
2621 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2622 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2623 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2624 Tok.setKind(tok::identifier);
2625 }
2626
2627 // We're done with the declaration-specifiers.
2628 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002629
Chris Lattner3bd934a2008-07-26 01:18:38 +00002630 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002631 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002632 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002633 // In C++, check to see if this is a scope specifier like foo::bar::, if
2634 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002635 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002636 if (TryAnnotateCXXScopeToken(true)) {
2637 if (!DS.hasTypeSpecifier())
2638 DS.SetTypeSpecError();
2639 goto DoneWithDeclSpec;
2640 }
2641 if (!Tok.is(tok::identifier))
2642 continue;
2643 }
Mike Stump1eb44332009-09-09 15:08:12 +00002644
Chris Lattner3bd934a2008-07-26 01:18:38 +00002645 // This identifier can only be a typedef name if we haven't already seen
2646 // a type-specifier. Without this check we misparse:
2647 // typedef int X; struct Y { short X; }; as 'short int'.
2648 if (DS.hasTypeSpecifier())
2649 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002650
John Thompson82287d12010-02-05 00:12:22 +00002651 // Check for need to substitute AltiVec keyword tokens.
2652 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2653 break;
2654
Richard Smithf63eee72012-05-09 18:56:43 +00002655 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2656 // allow the use of a typedef name as a type specifier.
2657 if (DS.isTypeAltiVecVector())
2658 goto DoneWithDeclSpec;
2659
John McCallb3d87482010-08-24 05:47:05 +00002660 ParsedType TypeRep =
2661 Actions.getTypeName(*Tok.getIdentifierInfo(),
2662 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002663
Chris Lattnerc199ab32009-04-12 20:42:31 +00002664 // If this is not a typedef name, don't parse it as part of the declspec,
2665 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002666 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002667 ParsedAttributesWithRange Attrs(AttrFactory);
2668 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2669 if (!Attrs.empty()) {
2670 AttrsLastTime = true;
2671 attrs.takeAllFrom(Attrs);
2672 }
2673 continue;
2674 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002675 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002676 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002677
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002678 // If we're in a context where the identifier could be a class name,
2679 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002680 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002681 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002682 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002683 goto DoneWithDeclSpec;
2684
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002685 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002686 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002687 if (isInvalid)
2688 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002689
Chris Lattner3bd934a2008-07-26 01:18:38 +00002690 DS.SetRangeEnd(Tok.getLocation());
2691 ConsumeToken(); // The identifier
2692
2693 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2694 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002695 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002696 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002697 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002698
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002699 // Need to support trailing type qualifiers (e.g. "id<p> const").
2700 // If a type specifier follows, it will be diagnosed elsewhere.
2701 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002702 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002703
2704 // type-name
2705 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002706 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002707 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002708 // This template-id does not refer to a type name, so we're
2709 // done with the type-specifiers.
2710 goto DoneWithDeclSpec;
2711 }
2712
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002713 // If we're in a context where the template-id could be a
2714 // constructor name or specialization, check whether this is a
2715 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002716 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002717 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002718 isConstructorDeclarator())
2719 goto DoneWithDeclSpec;
2720
Douglas Gregor39a8de12009-02-25 19:37:18 +00002721 // Turn the template-id annotation token into a type annotation
2722 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002723 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002724 continue;
2725 }
2726
Reid Spencer5f016e22007-07-11 17:01:13 +00002727 // GNU attributes support.
2728 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002729 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002730 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002731
2732 // Microsoft declspec support.
2733 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002734 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002735 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002736
Steve Naroff239f0732008-12-25 14:16:32 +00002737 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002738 case tok::kw___forceinline: {
Chad Rosier22aa6902012-12-21 22:24:43 +00002739 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002740 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002741 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002742 // FIXME: This does not work correctly if it is set to be a declspec
2743 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002744 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002745 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002746 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002747 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002748
Aaron Ballmanaa9df092013-05-22 23:25:32 +00002749 case tok::kw___sptr:
2750 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00002751 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002752 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002753 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002754 case tok::kw___cdecl:
2755 case tok::kw___stdcall:
2756 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002757 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002758 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002759 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002760 continue;
2761
Dawn Perchik52fc3142010-09-03 01:29:35 +00002762 // Borland single token adornments.
2763 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002764 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002765 continue;
2766
Peter Collingbournef315fa82011-02-14 01:42:53 +00002767 // OpenCL single token adornments.
2768 case tok::kw___kernel:
2769 ParseOpenCLAttributes(DS.getAttributes());
2770 continue;
2771
Reid Spencer5f016e22007-07-11 17:01:13 +00002772 // storage-class-specifier
2773 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002774 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2775 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002776 break;
2777 case tok::kw_extern:
Richard Smithec642442013-04-12 22:46:28 +00002778 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002779 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002780 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2781 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002782 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002783 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002784 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2785 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002786 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002787 case tok::kw_static:
Richard Smithec642442013-04-12 22:46:28 +00002788 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002789 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002790 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2791 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002792 break;
2793 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002794 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002795 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002796 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2797 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002798 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002799 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002800 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002801 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002802 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2803 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002804 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002805 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2806 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002807 break;
2808 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002809 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2810 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002811 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002812 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002813 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2814 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002815 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002816 case tok::kw___thread:
Richard Smithec642442013-04-12 22:46:28 +00002817 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2818 PrevSpec, DiagID);
2819 break;
2820 case tok::kw_thread_local:
2821 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2822 PrevSpec, DiagID);
2823 break;
2824 case tok::kw__Thread_local:
2825 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2826 Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002827 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002828
Reid Spencer5f016e22007-07-11 17:01:13 +00002829 // function-specifier
2830 case tok::kw_inline:
Chad Rosier22aa6902012-12-21 22:24:43 +00002831 isInvalid = DS.setFunctionSpecInline(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002832 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002833 case tok::kw_virtual:
Chad Rosier22aa6902012-12-21 22:24:43 +00002834 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002835 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002836 case tok::kw_explicit:
Chad Rosier22aa6902012-12-21 22:24:43 +00002837 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002838 break;
Richard Smithde03c152013-01-17 22:16:11 +00002839 case tok::kw__Noreturn:
2840 if (!getLangOpts().C11)
2841 Diag(Loc, diag::ext_c11_noreturn);
2842 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2843 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002844
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002845 // alignment-specifier
2846 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002847 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002848 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002849 ParseAlignmentSpecifier(DS.getAttributes());
2850 continue;
2851
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002852 // friend
2853 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002854 if (DSContext == DSC_class)
2855 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2856 else {
2857 PrevSpec = ""; // not actually used by the diagnostic
2858 DiagID = diag::err_friend_invalid_in_context;
2859 isInvalid = true;
2860 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002861 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002862
Douglas Gregor8d267c52011-09-09 02:06:17 +00002863 // Modules
2864 case tok::kw___module_private__:
2865 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2866 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002867
Sebastian Redl2ac67232009-11-05 15:47:02 +00002868 // constexpr
2869 case tok::kw_constexpr:
2870 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2871 break;
2872
Chris Lattner80d0c892009-01-21 19:48:37 +00002873 // type-specifier
2874 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002875 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2876 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002877 break;
2878 case tok::kw_long:
2879 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002880 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2881 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002882 else
John McCallfec54012009-08-03 20:12:06 +00002883 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2884 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002885 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002886 case tok::kw___int64:
2887 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2888 DiagID);
2889 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002890 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002891 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2892 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002893 break;
2894 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002895 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2896 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002897 break;
2898 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002899 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2900 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002901 break;
2902 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002903 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2904 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002905 break;
2906 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002907 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2908 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002909 break;
2910 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002911 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2912 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002913 break;
2914 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002915 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2916 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002917 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002918 case tok::kw___int128:
2919 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2920 DiagID);
2921 break;
2922 case tok::kw_half:
2923 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2924 DiagID);
2925 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002926 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002927 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2928 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002929 break;
2930 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002931 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2932 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002933 break;
2934 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002935 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2936 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002937 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002938 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002939 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2940 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002941 break;
2942 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002943 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2944 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002945 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002946 case tok::kw_bool:
2947 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002948 if (Tok.is(tok::kw_bool) &&
2949 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2950 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2951 PrevSpec = ""; // Not used by the diagnostic.
2952 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002953 // For better error recovery.
2954 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002955 isInvalid = true;
2956 } else {
2957 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2958 DiagID);
2959 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002960 break;
2961 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002962 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2963 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002964 break;
2965 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002966 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2967 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002968 break;
2969 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002970 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2971 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002972 break;
John Thompson82287d12010-02-05 00:12:22 +00002973 case tok::kw___vector:
2974 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2975 break;
2976 case tok::kw___pixel:
2977 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2978 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002979 case tok::kw_image1d_t:
2980 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2981 PrevSpec, DiagID);
2982 break;
2983 case tok::kw_image1d_array_t:
2984 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2985 PrevSpec, DiagID);
2986 break;
2987 case tok::kw_image1d_buffer_t:
2988 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2989 PrevSpec, DiagID);
2990 break;
2991 case tok::kw_image2d_t:
2992 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2993 PrevSpec, DiagID);
2994 break;
2995 case tok::kw_image2d_array_t:
2996 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2997 PrevSpec, DiagID);
2998 break;
2999 case tok::kw_image3d_t:
3000 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
3001 PrevSpec, DiagID);
3002 break;
Guy Benyei21f18c42013-02-07 10:55:47 +00003003 case tok::kw_sampler_t:
3004 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
3005 PrevSpec, DiagID);
3006 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00003007 case tok::kw_event_t:
3008 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
3009 PrevSpec, DiagID);
3010 break;
John McCalla5fc4722011-04-09 22:50:59 +00003011 case tok::kw___unknown_anytype:
3012 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3013 PrevSpec, DiagID);
3014 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00003015
3016 // class-specifier:
3017 case tok::kw_class:
3018 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003019 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00003020 case tok::kw_union: {
3021 tok::TokenKind Kind = Tok.getKind();
3022 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00003023
3024 // These are attributes following class specifiers.
3025 // To produce better diagnostic, we parse them when
3026 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00003027 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00003028 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00003029 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00003030
3031 // If there are attributes following class specifier,
3032 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00003033 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00003034 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00003035 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00003036 }
Chris Lattner80d0c892009-01-21 19:48:37 +00003037 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00003038 }
Chris Lattner80d0c892009-01-21 19:48:37 +00003039
3040 // enum-specifier:
3041 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00003042 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00003043 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00003044 continue;
3045
3046 // cv-qualifier:
3047 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003048 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003049 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003050 break;
3051 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003052 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003053 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003054 break;
3055 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003056 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003057 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003058 break;
3059
Douglas Gregord57959a2009-03-27 23:10:48 +00003060 // C++ typename-specifier:
3061 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00003062 if (TryAnnotateTypeOrScopeToken()) {
3063 DS.SetTypeSpecError();
3064 goto DoneWithDeclSpec;
3065 }
3066 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00003067 continue;
3068 break;
3069
Chris Lattner80d0c892009-01-21 19:48:37 +00003070 // GNU typeof support.
3071 case tok::kw_typeof:
3072 ParseTypeofSpecifier(DS);
3073 continue;
3074
David Blaikie42d6d0c2011-12-04 05:04:18 +00003075 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00003076 ParseDecltypeSpecifier(DS);
3077 continue;
3078
Sean Huntdb5d44b2011-05-19 05:37:45 +00003079 case tok::kw___underlying_type:
3080 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00003081 continue;
3082
3083 case tok::kw__Atomic:
Richard Smith4cf4a5e2013-03-28 01:55:44 +00003084 // C11 6.7.2.4/4:
3085 // If the _Atomic keyword is immediately followed by a left parenthesis,
3086 // it is interpreted as a type specifier (with a type name), not as a
3087 // type qualifier.
3088 if (NextToken().is(tok::l_paren)) {
3089 ParseAtomicSpecifier(DS);
3090 continue;
3091 }
3092 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3093 getLangOpts());
3094 break;
Sean Huntdb5d44b2011-05-19 05:37:45 +00003095
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003096 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00003097 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003098 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003099 goto DoneWithDeclSpec;
3100 case tok::kw___private:
3101 case tok::kw___global:
3102 case tok::kw___local:
3103 case tok::kw___constant:
3104 case tok::kw___read_only:
3105 case tok::kw___write_only:
3106 case tok::kw___read_write:
3107 ParseOpenCLQualifiers(DS);
3108 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00003109
Steve Naroffd3ded1f2008-06-05 00:02:44 +00003110 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00003111 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00003112 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3113 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00003114 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00003115 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00003116
Douglas Gregor46f936e2010-11-19 17:10:50 +00003117 if (!ParseObjCProtocolQualifiers(DS))
3118 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3119 << FixItHint::CreateInsertion(Loc, "id")
3120 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00003121
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00003122 // Need to support trailing type qualifiers (e.g. "id<p> const").
3123 // If a type specifier follows, it will be diagnosed elsewhere.
3124 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00003125 }
John McCallfec54012009-08-03 20:12:06 +00003126 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00003127 if (isInvalid) {
3128 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00003129 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00003130
Douglas Gregorae2fb142010-08-23 14:34:43 +00003131 if (DiagID == diag::ext_duplicate_declspec)
3132 Diag(Tok, DiagID)
3133 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3134 else
3135 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003136 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00003137
Chris Lattner81c018d2008-03-13 06:29:04 +00003138 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00003139 if (DiagID != diag::err_bool_redeclaration)
3140 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00003141
3142 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003143 }
3144}
Douglas Gregoradcac882008-12-01 23:54:00 +00003145
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003146/// ParseStructDeclaration - Parse a struct declaration without the terminating
3147/// semicolon.
3148///
Reid Spencer5f016e22007-07-11 17:01:13 +00003149/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003150/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00003151/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003152/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00003153/// struct-declarator-list:
3154/// struct-declarator
3155/// struct-declarator-list ',' struct-declarator
3156/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3157/// struct-declarator:
3158/// declarator
3159/// [GNU] declarator attributes[opt]
3160/// declarator[opt] ':' constant-expression
3161/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3162///
Chris Lattnere1359422008-04-10 06:46:29 +00003163void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003164ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003165
Chris Lattnerc46d1a12008-10-20 06:45:43 +00003166 if (Tok.is(tok::kw___extension__)) {
3167 // __extension__ silences extension warnings in the subexpression.
3168 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003169 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00003170 return ParseStructDeclaration(DS, Fields);
3171 }
Mike Stump1eb44332009-09-09 15:08:12 +00003172
Steve Naroff28a7ca82007-08-20 22:28:22 +00003173 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003174 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003175
Douglas Gregor4920f1f2009-01-12 22:49:06 +00003176 // If there are no declarators, this is a free-standing declaration
3177 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00003178 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003179 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3180 DS);
3181 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00003182 return;
3183 }
3184
3185 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00003186 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00003187 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003188 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003189 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00003190 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00003191
Bill Wendlingad017fa2012-12-20 19:22:21 +00003192 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00003193 if (!FirstDeclarator)
3194 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00003195
Steve Naroff28a7ca82007-08-20 22:28:22 +00003196 /// struct-declarator: declarator
3197 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003198 if (Tok.isNot(tok::colon)) {
3199 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3200 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00003201 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003202 }
Mike Stump1eb44332009-09-09 15:08:12 +00003203
Chris Lattner04d66662007-10-09 17:33:22 +00003204 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00003205 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00003206 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003207 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00003208 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00003209 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00003210 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00003211 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003212
Steve Naroff28a7ca82007-08-20 22:28:22 +00003213 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003214 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003215
John McCallbdd563e2009-11-03 02:38:08 +00003216 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00003217 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00003218
Steve Naroff28a7ca82007-08-20 22:28:22 +00003219 // If we don't have a comma, it is either the end of the list (a ';')
3220 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00003221 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003222 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00003223
Steve Naroff28a7ca82007-08-20 22:28:22 +00003224 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00003225 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003226
John McCallbdd563e2009-11-03 02:38:08 +00003227 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003228 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003229}
3230
3231/// ParseStructUnionBody
3232/// struct-contents:
3233/// struct-declaration-list
3234/// [EXT] empty
3235/// [GNU] "struct-declaration-list" without terminatoring ';'
3236/// struct-declaration-list:
3237/// struct-declaration
3238/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003239/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003240///
Reid Spencer5f016e22007-07-11 17:01:13 +00003241void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003242 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003243 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3244 "parsing struct/union body");
Andy Gibbsf50f3f72013-04-03 09:31:19 +00003245 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump1eb44332009-09-09 15:08:12 +00003246
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003247 BalancedDelimiterTracker T(*this, tok::l_brace);
3248 if (T.consumeOpen())
3249 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003250
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003251 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003252 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003253
Chris Lattner5f9e2722011-07-23 10:55:15 +00003254 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003255
Reid Spencer5f016e22007-07-11 17:01:13 +00003256 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003257 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003258 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003259
Reid Spencer5f016e22007-07-11 17:01:13 +00003260 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003261 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003262 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003263 continue;
3264 }
Chris Lattnere1359422008-04-10 06:46:29 +00003265
Andy Gibbs74b9fa12013-04-03 09:46:04 +00003266 // Parse _Static_assert declaration.
3267 if (Tok.is(tok::kw__Static_assert)) {
3268 SourceLocation DeclEnd;
3269 ParseStaticAssertDeclaration(DeclEnd);
3270 continue;
3271 }
3272
Argyrios Kyrtzidisbd957452013-04-18 01:42:35 +00003273 if (Tok.is(tok::annot_pragma_pack)) {
3274 HandlePragmaPack();
3275 continue;
3276 }
3277
3278 if (Tok.is(tok::annot_pragma_align)) {
3279 HandlePragmaAlign();
3280 continue;
3281 }
3282
John McCallbdd563e2009-11-03 02:38:08 +00003283 if (!Tok.is(tok::at)) {
3284 struct CFieldCallback : FieldCallback {
3285 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003286 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003287 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003288
John McCalld226f652010-08-21 09:40:31 +00003289 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003290 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003291 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3292
Eli Friedmandcdff462012-08-08 23:53:27 +00003293 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003294 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003295 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003296 FD.D.getDeclSpec().getSourceRange().getBegin(),
3297 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003298 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003299 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003300 }
John McCallbdd563e2009-11-03 02:38:08 +00003301 } Callback(*this, TagDecl, FieldDecls);
3302
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003303 // Parse all the comma separated declarators.
3304 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003305 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003306 } else { // Handle @defs
3307 ConsumeToken();
3308 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3309 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003310 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003311 continue;
3312 }
3313 ConsumeToken();
3314 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3315 if (!Tok.is(tok::identifier)) {
3316 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003317 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003318 continue;
3319 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003320 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003321 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003322 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003323 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3324 ConsumeToken();
3325 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003326 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003327
Chris Lattner04d66662007-10-09 17:33:22 +00003328 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003329 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003330 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003331 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003332 break;
3333 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003334 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3335 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003336 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003337 // If we stopped at a ';', eat it.
3338 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003339 }
3340 }
Mike Stump1eb44332009-09-09 15:08:12 +00003341
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003342 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003343
John McCall0b7e6782011-03-24 11:26:52 +00003344 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003345 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003346 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003347
Douglas Gregor23c94db2010-07-02 17:43:08 +00003348 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003349 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003350 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003351 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003352 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003353 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3354 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003355}
3356
Reid Spencer5f016e22007-07-11 17:01:13 +00003357/// ParseEnumSpecifier
3358/// enum-specifier: [C99 6.7.2.2]
3359/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003360///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003361/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3362/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003363/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3364/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003365/// 'enum' identifier
3366/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003367///
Richard Smith1af83c42012-03-23 03:33:32 +00003368/// [C++11] enum-head '{' enumerator-list[opt] '}'
3369/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003370///
Richard Smith1af83c42012-03-23 03:33:32 +00003371/// enum-head: [C++11]
3372/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3373/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3374/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003375///
Richard Smith1af83c42012-03-23 03:33:32 +00003376/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003377/// 'enum'
3378/// 'enum' 'class'
3379/// 'enum' 'struct'
3380///
Richard Smith1af83c42012-03-23 03:33:32 +00003381/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003382/// ':' type-specifier-seq
3383///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003384/// [C++] elaborated-type-specifier:
3385/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3386///
Chris Lattner4c97d762009-04-12 21:49:30 +00003387void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003388 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003389 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003390 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003391 if (Tok.is(tok::code_completion)) {
3392 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003393 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003394 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003395 }
John McCall57c13002011-07-06 05:58:41 +00003396
Sean Hunt2edf0a22012-06-23 05:07:58 +00003397 // If attributes exist after tag, parse them.
3398 ParsedAttributesWithRange attrs(AttrFactory);
3399 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003400 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003401
3402 // If declspecs exist after tag, parse them.
3403 while (Tok.is(tok::kw___declspec))
3404 ParseMicrosoftDeclSpec(attrs);
3405
Richard Smithbdad7a22012-01-10 01:33:14 +00003406 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003407 bool IsScopedUsingClassTag = false;
3408
John McCall1e12b3d2012-06-23 22:30:04 +00003409 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieued5a2922013-04-23 02:47:36 +00003410 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3411 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3412 : diag::ext_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003413 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003414 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003415
Bill Wendlingad017fa2012-12-20 19:22:21 +00003416 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003417 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003418 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003419
3420 // They are allowed afterwards, though.
3421 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003422 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003423 while (Tok.is(tok::kw___declspec))
3424 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003425 }
Richard Smith1af83c42012-03-23 03:33:32 +00003426
John McCall13489672012-05-07 06:16:58 +00003427 // C++11 [temp.explicit]p12:
3428 // The usual access controls do not apply to names used to specify
3429 // explicit instantiations.
3430 // We extend this to also cover explicit specializations. Note that
3431 // we don't suppress if this turns out to be an elaborated type
3432 // specifier.
3433 bool shouldDelayDiagsInTag =
3434 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3435 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3436 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003437
Richard Smith7796eb52012-03-12 08:56:40 +00003438 // Enum definitions should not be parsed in a trailing-return-type.
3439 bool AllowDeclaration = DSC != DSC_trailing;
3440
3441 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003442 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003443 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003444
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003445 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003446 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003447 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3448 // if a fixed underlying type is allowed.
3449 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003450
3451 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith725fe0e2013-04-01 21:43:41 +00003452 /*EnteringContext=*/true))
John McCall9ba61662010-02-26 08:45:28 +00003453 return;
3454
3455 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003456 Diag(Tok, diag::err_expected_ident);
3457 if (Tok.isNot(tok::l_brace)) {
3458 // Has no name and is not a definition.
3459 // Skip the rest of this declarator, up until the comma or semicolon.
3460 SkipUntil(tok::comma, true);
3461 return;
3462 }
3463 }
3464 }
Mike Stump1eb44332009-09-09 15:08:12 +00003465
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003466 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003467 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003468 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003469 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003470
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003471 // Skip the rest of this declarator, up until the comma or semicolon.
3472 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003473 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003474 }
Mike Stump1eb44332009-09-09 15:08:12 +00003475
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003476 // If an identifier is present, consume and remember it.
3477 IdentifierInfo *Name = 0;
3478 SourceLocation NameLoc;
3479 if (Tok.is(tok::identifier)) {
3480 Name = Tok.getIdentifierInfo();
3481 NameLoc = ConsumeToken();
3482 }
Mike Stump1eb44332009-09-09 15:08:12 +00003483
Richard Smithbdad7a22012-01-10 01:33:14 +00003484 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003485 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3486 // declaration of a scoped enumeration.
3487 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003488 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003489 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003490 }
3491
John McCall13489672012-05-07 06:16:58 +00003492 // Okay, end the suppression area. We'll decide whether to emit the
3493 // diagnostics in a second.
3494 if (shouldDelayDiagsInTag)
3495 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003496
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003497 TypeResult BaseType;
3498
Douglas Gregora61b3e72010-12-01 17:42:47 +00003499 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003500 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003501 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003502 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003503 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003504 // If we're in class scope, this can either be an enum declaration with
3505 // an underlying type, or a declaration of a bitfield member. We try to
3506 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003507 // (integer literal, sizeof); if it's still ambiguous, we then consider
3508 // anything that's a simple-type-specifier followed by '(' as an
3509 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003510 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003511 EnterExpressionEvaluationContext Unevaluated(Actions,
3512 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003513 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003514 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003515 // bit-field. This is the common case.
3516 if (TPR == TPResult::True())
3517 PossibleBitfield = true;
3518 // If the next token starts a type-specifier-seq, it may be either a
3519 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003520 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003521 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003522 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003523 GetLookAheadToken(2).getKind() == tok::semi) {
3524 // Consume the ':'.
3525 ConsumeToken();
3526 } else {
3527 // We have the start of a type-specifier-seq, so we have to perform
3528 // tentative parsing to determine whether we have an expression or a
3529 // type.
3530 TentativeParsingAction TPA(*this);
3531
3532 // Consume the ':'.
3533 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003534
3535 // If we see a type specifier followed by an open-brace, we have an
3536 // ambiguity between an underlying type and a C++11 braced
3537 // function-style cast. Resolve this by always treating it as an
3538 // underlying type.
3539 // FIXME: The standard is not entirely clear on how to disambiguate in
3540 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003541 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003542 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003543 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003544 // We'll parse this as a bitfield later.
3545 PossibleBitfield = true;
3546 TPA.Revert();
3547 } else {
3548 // We have a type-specifier-seq.
3549 TPA.Commit();
3550 }
3551 }
3552 } else {
3553 // Consume the ':'.
3554 ConsumeToken();
3555 }
3556
3557 if (!PossibleBitfield) {
3558 SourceRange Range;
3559 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003560
Richard Smith80ad52f2013-01-02 11:42:31 +00003561 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003562 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003563 } else if (!getLangOpts().ObjC2) {
3564 if (getLangOpts().CPlusPlus)
3565 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3566 else
3567 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3568 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003569 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003570 }
3571
Richard Smithbdad7a22012-01-10 01:33:14 +00003572 // There are four options here. If we have 'friend enum foo;' then this is a
3573 // friend declaration, and cannot have an accompanying definition. If we have
3574 // 'enum foo;', then this is a forward declaration. If we have
3575 // 'enum foo {...' then this is a definition. Otherwise we have something
3576 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003577 //
3578 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3579 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3580 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3581 //
John McCallf312b1e2010-08-26 23:41:50 +00003582 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003583 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003584 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003585 } else if (Tok.is(tok::l_brace)) {
3586 if (DS.isFriendSpecified()) {
3587 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3588 << SourceRange(DS.getFriendSpecLoc());
3589 ConsumeBrace();
3590 SkipUntil(tok::r_brace);
3591 TUK = Sema::TUK_Friend;
3592 } else {
3593 TUK = Sema::TUK_Definition;
3594 }
Richard Smithc9f35172012-06-25 21:37:02 +00003595 } else if (DSC != DSC_type_specifier &&
3596 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003597 (Tok.isAtStartOfLine() &&
3598 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003599 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3600 if (Tok.isNot(tok::semi)) {
3601 // A semicolon was missing after this declaration. Diagnose and recover.
3602 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3603 "enum");
3604 PP.EnterToken(Tok);
3605 Tok.setKind(tok::semi);
3606 }
John McCall13489672012-05-07 06:16:58 +00003607 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003608 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003609 }
3610
3611 // If this is an elaborated type specifier, and we delayed
3612 // diagnostics before, just merge them into the current pool.
3613 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3614 diagsFromTag.redelay();
3615 }
Richard Smith1af83c42012-03-23 03:33:32 +00003616
3617 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003618 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003619 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003620 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003621 // Skip the rest of this declarator, up until the comma or semicolon.
3622 Diag(Tok, diag::err_enum_template);
3623 SkipUntil(tok::comma, true);
3624 return;
3625 }
3626
3627 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3628 // Enumerations can't be explicitly instantiated.
3629 DS.SetTypeSpecError();
3630 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3631 return;
3632 }
3633
3634 assert(TemplateInfo.TemplateParams && "no template parameters");
3635 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3636 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003637 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003638
Sean Hunt2edf0a22012-06-23 05:07:58 +00003639 if (TUK == Sema::TUK_Reference)
3640 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003641
Douglas Gregorb9075602011-02-22 02:55:24 +00003642 if (!Name && TUK != Sema::TUK_Definition) {
3643 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003644
Douglas Gregorb9075602011-02-22 02:55:24 +00003645 // Skip the rest of this declarator, up until the comma or semicolon.
3646 SkipUntil(tok::comma, true);
3647 return;
3648 }
Richard Smith1af83c42012-03-23 03:33:32 +00003649
Douglas Gregor402abb52009-05-28 23:31:59 +00003650 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003651 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003652 const char *PrevSpec = 0;
3653 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003654 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003655 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003656 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003657 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003658 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003659
Douglas Gregor48c89f42010-04-24 16:38:41 +00003660 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003661 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003662 // dependent tag.
3663 if (!Name) {
3664 DS.SetTypeSpecError();
3665 Diag(Tok, diag::err_expected_type_name_after_typename);
3666 return;
3667 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003668
Douglas Gregor23c94db2010-07-02 17:43:08 +00003669 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003670 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003671 NameLoc);
3672 if (Type.isInvalid()) {
3673 DS.SetTypeSpecError();
3674 return;
3675 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003676
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003677 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3678 NameLoc.isValid() ? NameLoc : StartLoc,
3679 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003680 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003681
Douglas Gregor48c89f42010-04-24 16:38:41 +00003682 return;
3683 }
Mike Stump1eb44332009-09-09 15:08:12 +00003684
John McCalld226f652010-08-21 09:40:31 +00003685 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003686 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003687 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003688 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003689 ConsumeBrace();
3690 SkipUntil(tok::r_brace);
3691 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003692
Douglas Gregor48c89f42010-04-24 16:38:41 +00003693 DS.SetTypeSpecError();
3694 return;
3695 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003696
Richard Smithc9f35172012-06-25 21:37:02 +00003697 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003698 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003699
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003700 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3701 NameLoc.isValid() ? NameLoc : StartLoc,
3702 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003703 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003704}
3705
3706/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3707/// enumerator-list:
3708/// enumerator
3709/// enumerator-list ',' enumerator
3710/// enumerator:
3711/// enumeration-constant
3712/// enumeration-constant '=' constant-expression
3713/// enumeration-constant:
3714/// identifier
3715///
John McCalld226f652010-08-21 09:40:31 +00003716void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003717 // Enter the scope of the enum body and start the definition.
3718 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003719 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003720
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003721 BalancedDelimiterTracker T(*this, tok::l_brace);
3722 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003723
Chris Lattner7946dd32007-08-27 17:24:30 +00003724 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003725 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003726 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003727
Chris Lattner5f9e2722011-07-23 10:55:15 +00003728 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003729
John McCalld226f652010-08-21 09:40:31 +00003730 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003731
Reid Spencer5f016e22007-07-11 17:01:13 +00003732 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003733 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003734 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3735 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003736
John McCall5b629aa2010-10-22 23:36:17 +00003737 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003738 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003739 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003740 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003741 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003742
Reid Spencer5f016e22007-07-11 17:01:13 +00003743 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003744 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003745 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003746
Chris Lattner04d66662007-10-09 17:33:22 +00003747 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003748 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003749 AssignedVal = ParseConstantExpression();
3750 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003751 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003752 }
Mike Stump1eb44332009-09-09 15:08:12 +00003753
Reid Spencer5f016e22007-07-11 17:01:13 +00003754 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003755 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3756 LastEnumConstDecl,
3757 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003758 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003759 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003760 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003761
Reid Spencer5f016e22007-07-11 17:01:13 +00003762 EnumConstantDecls.push_back(EnumConstDecl);
3763 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003764
Douglas Gregor751f6922010-09-07 14:51:08 +00003765 if (Tok.is(tok::identifier)) {
3766 // We're missing a comma between enumerators.
3767 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003768 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003769 << FixItHint::CreateInsertion(Loc, ", ");
3770 continue;
3771 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003772
Chris Lattner04d66662007-10-09 17:33:22 +00003773 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003774 break;
3775 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003776
Richard Smith7fe62082011-10-15 05:09:34 +00003777 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003778 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003779 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3780 diag::ext_enumerator_list_comma_cxx :
3781 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003782 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003783 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003784 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3785 << FixItHint::CreateRemoval(CommaLoc);
3786 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003787 }
Mike Stump1eb44332009-09-09 15:08:12 +00003788
Reid Spencer5f016e22007-07-11 17:01:13 +00003789 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003790 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003791
Reid Spencer5f016e22007-07-11 17:01:13 +00003792 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003793 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003794 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003795
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003796 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenko9ff2b422013-04-27 20:23:52 +00003797 EnumDecl, EnumConstantDecls,
3798 getCurScope(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003799 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003800
Douglas Gregor72de6672009-01-08 20:45:30 +00003801 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003802 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3803 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003804
3805 // The next token must be valid after an enum definition. If not, a ';'
3806 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003807 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3808 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003809 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3810 // Push this token back into the preprocessor and change our current token
3811 // to ';' so that the rest of the code recovers as though there were an
3812 // ';' after the definition.
3813 PP.EnterToken(Tok);
3814 Tok.setKind(tok::semi);
3815 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003816}
3817
3818/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003819/// start of a type-qualifier-list.
3820bool Parser::isTypeQualifier() const {
3821 switch (Tok.getKind()) {
3822 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003823
3824 // type-qualifier only in OpenCL
3825 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003826 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003827
Steve Naroff5f8aa692008-02-11 23:15:56 +00003828 // type-qualifier
3829 case tok::kw_const:
3830 case tok::kw_volatile:
3831 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003832 case tok::kw___private:
3833 case tok::kw___local:
3834 case tok::kw___global:
3835 case tok::kw___constant:
3836 case tok::kw___read_only:
3837 case tok::kw___read_write:
3838 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003839 return true;
3840 }
3841}
3842
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003843/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3844/// is definitely a type-specifier. Return false if it isn't part of a type
3845/// specifier or if we're not sure.
3846bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3847 switch (Tok.getKind()) {
3848 default: return false;
3849 // type-specifiers
3850 case tok::kw_short:
3851 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003852 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003853 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003854 case tok::kw_signed:
3855 case tok::kw_unsigned:
3856 case tok::kw__Complex:
3857 case tok::kw__Imaginary:
3858 case tok::kw_void:
3859 case tok::kw_char:
3860 case tok::kw_wchar_t:
3861 case tok::kw_char16_t:
3862 case tok::kw_char32_t:
3863 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003864 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003865 case tok::kw_float:
3866 case tok::kw_double:
3867 case tok::kw_bool:
3868 case tok::kw__Bool:
3869 case tok::kw__Decimal32:
3870 case tok::kw__Decimal64:
3871 case tok::kw__Decimal128:
3872 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003873
Guy Benyeib13621d2012-12-18 14:38:23 +00003874 // OpenCL specific types:
3875 case tok::kw_image1d_t:
3876 case tok::kw_image1d_array_t:
3877 case tok::kw_image1d_buffer_t:
3878 case tok::kw_image2d_t:
3879 case tok::kw_image2d_array_t:
3880 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003881 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003882 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003883
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003884 // struct-or-union-specifier (C99) or class-specifier (C++)
3885 case tok::kw_class:
3886 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003887 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003888 case tok::kw_union:
3889 // enum-specifier
3890 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003891
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003892 // typedef-name
3893 case tok::annot_typename:
3894 return true;
3895 }
3896}
3897
Steve Naroff5f8aa692008-02-11 23:15:56 +00003898/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003899/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003900bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003901 switch (Tok.getKind()) {
3902 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003903
Chris Lattner166a8fc2009-01-04 23:41:41 +00003904 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003905 if (TryAltiVecVectorToken())
3906 return true;
3907 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003908 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003909 // Annotate typenames and C++ scope specifiers. If we get one, just
3910 // recurse to handle whatever we get.
3911 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003912 return true;
3913 if (Tok.is(tok::identifier))
3914 return false;
3915 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003916
Chris Lattner166a8fc2009-01-04 23:41:41 +00003917 case tok::coloncolon: // ::foo::bar
3918 if (NextToken().is(tok::kw_new) || // ::new
3919 NextToken().is(tok::kw_delete)) // ::delete
3920 return false;
3921
Chris Lattner166a8fc2009-01-04 23:41:41 +00003922 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003923 return true;
3924 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003925
Reid Spencer5f016e22007-07-11 17:01:13 +00003926 // GNU attributes support.
3927 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003928 // GNU typeof support.
3929 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003930
Reid Spencer5f016e22007-07-11 17:01:13 +00003931 // type-specifiers
3932 case tok::kw_short:
3933 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003934 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003935 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003936 case tok::kw_signed:
3937 case tok::kw_unsigned:
3938 case tok::kw__Complex:
3939 case tok::kw__Imaginary:
3940 case tok::kw_void:
3941 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003942 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003943 case tok::kw_char16_t:
3944 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003945 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003946 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003947 case tok::kw_float:
3948 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003949 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003950 case tok::kw__Bool:
3951 case tok::kw__Decimal32:
3952 case tok::kw__Decimal64:
3953 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003954 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003955
Guy Benyeib13621d2012-12-18 14:38:23 +00003956 // OpenCL specific types:
3957 case tok::kw_image1d_t:
3958 case tok::kw_image1d_array_t:
3959 case tok::kw_image1d_buffer_t:
3960 case tok::kw_image2d_t:
3961 case tok::kw_image2d_array_t:
3962 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003963 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003964 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003965
Chris Lattner99dc9142008-04-13 18:59:07 +00003966 // struct-or-union-specifier (C99) or class-specifier (C++)
3967 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003968 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003969 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003970 case tok::kw_union:
3971 // enum-specifier
3972 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003973
Reid Spencer5f016e22007-07-11 17:01:13 +00003974 // type-qualifier
3975 case tok::kw_const:
3976 case tok::kw_volatile:
3977 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003978
John McCallb8a8de32012-11-14 00:49:39 +00003979 // Debugger support.
3980 case tok::kw___unknown_anytype:
3981
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003982 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003983 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003984 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003985
Chris Lattner7c186be2008-10-20 00:25:30 +00003986 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3987 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003988 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003989
Steve Naroff239f0732008-12-25 14:16:32 +00003990 case tok::kw___cdecl:
3991 case tok::kw___stdcall:
3992 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003993 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003994 case tok::kw___w64:
3995 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003996 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003997 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003998 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003999
4000 case tok::kw___private:
4001 case tok::kw___local:
4002 case tok::kw___global:
4003 case tok::kw___constant:
4004 case tok::kw___read_only:
4005 case tok::kw___read_write:
4006 case tok::kw___write_only:
4007
Eli Friedman290eeb02009-06-08 23:27:34 +00004008 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004009
4010 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004011 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00004012
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004013 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00004014 case tok::kw__Atomic:
4015 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00004016 }
4017}
4018
4019/// isDeclarationSpecifier() - Return true if the current token is part of a
4020/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00004021///
4022/// \param DisambiguatingWithExpression True to indicate that the purpose of
4023/// this check is to disambiguate between an expression and a declaration.
4024bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004025 switch (Tok.getKind()) {
4026 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004027
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004028 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004029 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004030
Chris Lattner166a8fc2009-01-04 23:41:41 +00004031 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00004032 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00004033 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00004034 return false;
John Thompson82287d12010-02-05 00:12:22 +00004035 if (TryAltiVecVectorToken())
4036 return true;
4037 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00004038 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00004039 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00004040 // Annotate typenames and C++ scope specifiers. If we get one, just
4041 // recurse to handle whatever we get.
4042 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004043 return true;
4044 if (Tok.is(tok::identifier))
4045 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00004046
Douglas Gregor9497a732010-09-16 01:51:54 +00004047 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00004048 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00004049 // expression is permitted, then this is probably a class message send
4050 // missing the initial '['. In this case, we won't consider this to be
4051 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00004052 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00004053 isStartOfObjCClassMessageMissingOpenBracket())
4054 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00004055
John McCall9ba61662010-02-26 08:45:28 +00004056 return isDeclarationSpecifier();
4057
Chris Lattner166a8fc2009-01-04 23:41:41 +00004058 case tok::coloncolon: // ::foo::bar
4059 if (NextToken().is(tok::kw_new) || // ::new
4060 NextToken().is(tok::kw_delete)) // ::delete
4061 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004062
Chris Lattner166a8fc2009-01-04 23:41:41 +00004063 // Annotate typenames and C++ scope specifiers. If we get one, just
4064 // recurse to handle whatever we get.
4065 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004066 return true;
4067 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004068
Reid Spencer5f016e22007-07-11 17:01:13 +00004069 // storage-class-specifier
4070 case tok::kw_typedef:
4071 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00004072 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00004073 case tok::kw_static:
4074 case tok::kw_auto:
4075 case tok::kw_register:
4076 case tok::kw___thread:
Richard Smithec642442013-04-12 22:46:28 +00004077 case tok::kw_thread_local:
4078 case tok::kw__Thread_local:
Mike Stump1eb44332009-09-09 15:08:12 +00004079
Douglas Gregor8d267c52011-09-09 02:06:17 +00004080 // Modules
4081 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00004082
John McCallb8a8de32012-11-14 00:49:39 +00004083 // Debugger support
4084 case tok::kw___unknown_anytype:
4085
Reid Spencer5f016e22007-07-11 17:01:13 +00004086 // type-specifiers
4087 case tok::kw_short:
4088 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00004089 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00004090 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00004091 case tok::kw_signed:
4092 case tok::kw_unsigned:
4093 case tok::kw__Complex:
4094 case tok::kw__Imaginary:
4095 case tok::kw_void:
4096 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00004097 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00004098 case tok::kw_char16_t:
4099 case tok::kw_char32_t:
4100
Reid Spencer5f016e22007-07-11 17:01:13 +00004101 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004102 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00004103 case tok::kw_float:
4104 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00004105 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00004106 case tok::kw__Bool:
4107 case tok::kw__Decimal32:
4108 case tok::kw__Decimal64:
4109 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00004110 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00004111
Guy Benyeib13621d2012-12-18 14:38:23 +00004112 // OpenCL specific types:
4113 case tok::kw_image1d_t:
4114 case tok::kw_image1d_array_t:
4115 case tok::kw_image1d_buffer_t:
4116 case tok::kw_image2d_t:
4117 case tok::kw_image2d_array_t:
4118 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00004119 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00004120 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00004121
Chris Lattner99dc9142008-04-13 18:59:07 +00004122 // struct-or-union-specifier (C99) or class-specifier (C++)
4123 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00004124 case tok::kw_struct:
4125 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00004126 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00004127 // enum-specifier
4128 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00004129
Reid Spencer5f016e22007-07-11 17:01:13 +00004130 // type-qualifier
4131 case tok::kw_const:
4132 case tok::kw_volatile:
4133 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00004134
Reid Spencer5f016e22007-07-11 17:01:13 +00004135 // function-specifier
4136 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00004137 case tok::kw_virtual:
4138 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00004139 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00004140
Richard Smith4cd81c52013-01-29 09:02:09 +00004141 // alignment-specifier
4142 case tok::kw__Alignas:
4143
Richard Smith53aec2a2012-10-25 00:00:53 +00004144 // friend keyword.
4145 case tok::kw_friend:
4146
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00004147 // static_assert-declaration
4148 case tok::kw__Static_assert:
4149
Chris Lattner1ef08762007-08-09 17:01:07 +00004150 // GNU typeof support.
4151 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00004152
Chris Lattner1ef08762007-08-09 17:01:07 +00004153 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00004154 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00004155
Richard Smith53aec2a2012-10-25 00:00:53 +00004156 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00004157 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00004158 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00004159
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004160 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00004161 case tok::kw__Atomic:
4162 return true;
4163
Chris Lattnerf3948c42008-07-26 03:38:44 +00004164 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4165 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00004166 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00004167
Douglas Gregord9d75e52011-04-27 05:41:15 +00004168 // typedef-name
4169 case tok::annot_typename:
4170 return !DisambiguatingWithExpression ||
4171 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00004172
Steve Naroff47f52092009-01-06 19:34:12 +00004173 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00004174 case tok::kw___cdecl:
4175 case tok::kw___stdcall:
4176 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004177 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00004178 case tok::kw___w64:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004179 case tok::kw___sptr:
4180 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00004181 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004182 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00004183 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004184 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004185 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004186
4187 case tok::kw___private:
4188 case tok::kw___local:
4189 case tok::kw___global:
4190 case tok::kw___constant:
4191 case tok::kw___read_only:
4192 case tok::kw___read_write:
4193 case tok::kw___write_only:
4194
Eli Friedman290eeb02009-06-08 23:27:34 +00004195 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00004196 }
4197}
4198
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004199bool Parser::isConstructorDeclarator() {
4200 TentativeParsingAction TPA(*this);
4201
4202 // Parse the C++ scope specifier.
4203 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00004204 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004205 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00004206 TPA.Revert();
4207 return false;
4208 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004209
4210 // Parse the constructor name.
4211 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4212 // We already know that we have a constructor name; just consume
4213 // the token.
4214 ConsumeToken();
4215 } else {
4216 TPA.Revert();
4217 return false;
4218 }
4219
Richard Smith22592862012-03-27 23:05:05 +00004220 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004221 if (Tok.isNot(tok::l_paren)) {
4222 TPA.Revert();
4223 return false;
4224 }
4225 ConsumeParen();
4226
Richard Smith22592862012-03-27 23:05:05 +00004227 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4228 // that we have a constructor.
4229 if (Tok.is(tok::r_paren) ||
4230 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004231 TPA.Revert();
4232 return true;
4233 }
4234
4235 // If we need to, enter the specified scope.
4236 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00004237 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004238 DeclScopeObj.EnterDeclaratorScope();
4239
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004240 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00004241 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004242 MaybeParseMicrosoftAttributes(Attrs);
4243
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004244 // Check whether the next token(s) are part of a declaration
4245 // specifier, in which case we have the start of a parameter and,
4246 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004247 bool IsConstructor = false;
4248 if (isDeclarationSpecifier())
4249 IsConstructor = true;
4250 else if (Tok.is(tok::identifier) ||
4251 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4252 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4253 // This might be a parenthesized member name, but is more likely to
4254 // be a constructor declaration with an invalid argument type. Keep
4255 // looking.
4256 if (Tok.is(tok::annot_cxxscope))
4257 ConsumeToken();
4258 ConsumeToken();
4259
4260 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004261 // which must have one of the following syntactic forms (see the
4262 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004263 switch (Tok.getKind()) {
4264 case tok::l_paren:
4265 // C(X ( int));
4266 case tok::l_square:
4267 // C(X [ 5]);
4268 // C(X [ [attribute]]);
4269 case tok::coloncolon:
4270 // C(X :: Y);
4271 // C(X :: *p);
4272 case tok::r_paren:
4273 // C(X )
4274 // Assume this isn't a constructor, rather than assuming it's a
4275 // constructor with an unnamed parameter of an ill-formed type.
4276 break;
4277
4278 default:
4279 IsConstructor = true;
4280 break;
4281 }
4282 }
4283
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004284 TPA.Revert();
4285 return IsConstructor;
4286}
Reid Spencer5f016e22007-07-11 17:01:13 +00004287
4288/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004289/// type-qualifier-list: [C99 6.7.5]
4290/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004291/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004292/// [ only if VendorAttributesAllowed=true ]
4293/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004294/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004295/// [ only if VendorAttributesAllowed=true ]
4296/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004297/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004298/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004299///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004300void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4301 bool VendorAttributesAllowed,
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004302 bool CXX11AttributesAllowed,
4303 bool AtomicAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004304 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004305 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004306 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004307 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004308 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004309 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004310
4311 SourceLocation EndLoc;
4312
Reid Spencer5f016e22007-07-11 17:01:13 +00004313 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004314 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004315 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004316 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004317 SourceLocation Loc = Tok.getLocation();
4318
4319 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004320 case tok::code_completion:
4321 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004322 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004323
Reid Spencer5f016e22007-07-11 17:01:13 +00004324 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004325 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004326 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004327 break;
4328 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004329 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004330 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004331 break;
4332 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004333 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004334 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004335 break;
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004336 case tok::kw__Atomic:
4337 if (!AtomicAllowed)
4338 goto DoneWithTypeQuals;
4339 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4340 getLangOpts());
4341 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004342
4343 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004344 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004345 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004346 goto DoneWithTypeQuals;
4347 case tok::kw___private:
4348 case tok::kw___global:
4349 case tok::kw___local:
4350 case tok::kw___constant:
4351 case tok::kw___read_only:
4352 case tok::kw___write_only:
4353 case tok::kw___read_write:
4354 ParseOpenCLQualifiers(DS);
4355 break;
4356
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004357 case tok::kw___sptr:
4358 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00004359 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004360 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004361 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004362 case tok::kw___cdecl:
4363 case tok::kw___stdcall:
4364 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004365 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004366 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004367 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004368 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004369 continue;
4370 }
4371 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004372 case tok::kw___pascal:
4373 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004374 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004375 continue;
4376 }
4377 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004378 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004379 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004380 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004381 continue; // do *not* consume the next token!
4382 }
4383 // otherwise, FALL THROUGH!
4384 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004385 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004386 // If this is not a type-qualifier token, we're done reading type
4387 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004388 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004389 if (EndLoc.isValid())
4390 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004391 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004392 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004393
Reid Spencer5f016e22007-07-11 17:01:13 +00004394 // If the specifier combination wasn't legal, issue a diagnostic.
4395 if (isInvalid) {
4396 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004397 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004398 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004399 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004400 }
4401}
4402
4403
4404/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4405///
4406void Parser::ParseDeclarator(Declarator &D) {
4407 /// This implements the 'declarator' production in the C grammar, then checks
4408 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004409 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004410}
4411
Richard Smith9988f282012-03-29 01:16:42 +00004412static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4413 if (Kind == tok::star || Kind == tok::caret)
4414 return true;
4415
4416 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4417 if (!Lang.CPlusPlus)
4418 return false;
4419
4420 return Kind == tok::amp || Kind == tok::ampamp;
4421}
4422
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004423/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4424/// is parsed by the function passed to it. Pass null, and the direct-declarator
4425/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004426/// ptr-operator production.
4427///
Richard Smith0706df42011-10-19 21:33:05 +00004428/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004429/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4430/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004431///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004432/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4433/// [C] pointer[opt] direct-declarator
4434/// [C++] direct-declarator
4435/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004436///
4437/// pointer: [C99 6.7.5]
4438/// '*' type-qualifier-list[opt]
4439/// '*' type-qualifier-list[opt] pointer
4440///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004441/// ptr-operator:
4442/// '*' cv-qualifier-seq[opt]
4443/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004444/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004445/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004446/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004447/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004448void Parser::ParseDeclaratorInternal(Declarator &D,
4449 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004450 if (Diags.hasAllExtensionsSilenced())
4451 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004452
Sebastian Redlf30208a2009-01-24 21:16:55 +00004453 // C++ member pointers start with a '::' or a nested-name.
4454 // Member pointers get special handling, since there's no place for the
4455 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004456 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004457 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4458 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004459 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4460 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004461 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004462 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004463
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004464 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004465 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004466 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004467 if (D.mayHaveIdentifier())
4468 D.getCXXScopeSpec() = SS;
4469 else
4470 AnnotateScopeToken(SS, true);
4471
Sebastian Redlf30208a2009-01-24 21:16:55 +00004472 if (DirectDeclParser)
4473 (this->*DirectDeclParser)(D);
4474 return;
4475 }
4476
4477 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004478 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004479 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004480 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004481 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004482
4483 // Recurse to parse whatever is left.
4484 ParseDeclaratorInternal(D, DirectDeclParser);
4485
4486 // Sema will have to catch (syntactically invalid) pointers into global
4487 // scope. It has to catch pointers into namespace scope anyway.
4488 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004489 Loc),
4490 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004491 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004492 return;
4493 }
4494 }
4495
4496 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004497 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004498 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004499 if (DirectDeclParser)
4500 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004501 return;
4502 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004503
Sebastian Redl05532f22009-03-15 22:02:01 +00004504 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4505 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004506 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004507 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004508
Chris Lattner9af55002009-03-27 04:18:06 +00004509 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004510 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004511 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004512
Richard Smith6ee326a2012-04-10 01:32:12 +00004513 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004514 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004515 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004516
Reid Spencer5f016e22007-07-11 17:01:13 +00004517 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004518 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004519 if (Kind == tok::star)
4520 // Remember that we parsed a pointer type, and remember the type-quals.
4521 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004522 DS.getConstSpecLoc(),
4523 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004524 DS.getRestrictSpecLoc()),
4525 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004526 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004527 else
4528 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004529 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004530 Loc),
4531 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004532 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004533 } else {
4534 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004535 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004536
Sebastian Redl743de1f2009-03-23 00:00:23 +00004537 // Complain about rvalue references in C++03, but then go on and build
4538 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004539 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004540 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004541 diag::warn_cxx98_compat_rvalue_reference :
4542 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004543
Richard Smith6ee326a2012-04-10 01:32:12 +00004544 // GNU-style and C++11 attributes are allowed here, as is restrict.
4545 ParseTypeQualifierListOpt(DS);
4546 D.ExtendWithDeclSpec(DS);
4547
Reid Spencer5f016e22007-07-11 17:01:13 +00004548 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4549 // cv-qualifiers are introduced through the use of a typedef or of a
4550 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004551 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4552 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4553 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004554 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004555 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4556 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004557 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004558 // 'restrict' is permitted as an extension.
4559 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4560 Diag(DS.getAtomicSpecLoc(),
4561 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Reid Spencer5f016e22007-07-11 17:01:13 +00004562 }
4563
4564 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004565 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004566
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004567 if (D.getNumTypeObjects() > 0) {
4568 // C++ [dcl.ref]p4: There shall be no references to references.
4569 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4570 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004571 if (const IdentifierInfo *II = D.getIdentifier())
4572 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4573 << II;
4574 else
4575 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4576 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004577
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004578 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004579 // can go ahead and build the (technically ill-formed)
4580 // declarator: reference collapsing will take care of it.
4581 }
4582 }
4583
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004584 // Remember that we parsed a reference type.
Chris Lattner76549142008-02-21 01:32:26 +00004585 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004586 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004587 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004588 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004589 }
4590}
4591
Richard Smith9988f282012-03-29 01:16:42 +00004592static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4593 SourceLocation EllipsisLoc) {
4594 if (EllipsisLoc.isValid()) {
4595 FixItHint Insertion;
4596 if (!D.getEllipsisLoc().isValid()) {
4597 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4598 D.setEllipsisLoc(EllipsisLoc);
4599 }
4600 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4601 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4602 }
4603}
4604
Reid Spencer5f016e22007-07-11 17:01:13 +00004605/// ParseDirectDeclarator
4606/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004607/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004608/// '(' declarator ')'
4609/// [GNU] '(' attributes declarator ')'
4610/// [C90] direct-declarator '[' constant-expression[opt] ']'
4611/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4612/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4613/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4614/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004615/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4616/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004617/// direct-declarator '(' parameter-type-list ')'
4618/// direct-declarator '(' identifier-list[opt] ')'
4619/// [GNU] direct-declarator '(' parameter-forward-declarations
4620/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004621/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4622/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004623/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4624/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4625/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004626/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004627/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004628///
4629/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004630/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004631/// '::'[opt] nested-name-specifier[opt] type-name
4632///
4633/// id-expression: [C++ 5.1]
4634/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004635/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004636///
4637/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004638/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004639/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004640/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004641/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004642/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004643///
Richard Smith5d8388c2012-03-27 01:42:32 +00004644/// Note, any additional constructs added here may need corresponding changes
4645/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004646void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004647 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004648
David Blaikie4e4d0842012-03-11 07:00:24 +00004649 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004650 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004651 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004652 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4653 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004654 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004655 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004656 }
4657
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004658 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004659 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004660 // Change the declaration context for name lookup, until this function
4661 // is exited (and the declarator has been parsed).
4662 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004663 }
4664
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004665 // C++0x [dcl.fct]p14:
4666 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004667 // of a parameter-declaration-clause without a preceding comma. In
4668 // this case, the ellipsis is parsed as part of the
4669 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004670 // parameter pack that has not been expanded; otherwise, it is parsed
4671 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004672 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004673 !((D.getContext() == Declarator::PrototypeContext ||
4674 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004675 NextToken().is(tok::r_paren) &&
Richard Smith30f2a742013-02-20 20:19:27 +00004676 !D.hasGroupingParens() &&
Richard Smith9988f282012-03-29 01:16:42 +00004677 !Actions.containsUnexpandedParameterPacks(D))) {
4678 SourceLocation EllipsisLoc = ConsumeToken();
4679 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4680 // The ellipsis was put in the wrong place. Recover, and explain to
4681 // the user what they should have done.
4682 ParseDeclarator(D);
4683 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4684 return;
4685 } else
4686 D.setEllipsisLoc(EllipsisLoc);
4687
4688 // The ellipsis can't be followed by a parenthesized declarator. We
4689 // check for that in ParseParenDeclarator, after we have disambiguated
4690 // the l_paren token.
4691 }
4692
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004693 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4694 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4695 // We found something that indicates the start of an unqualified-id.
4696 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004697 bool AllowConstructorName;
4698 if (D.getDeclSpec().hasTypeSpecifier())
4699 AllowConstructorName = false;
4700 else if (D.getCXXScopeSpec().isSet())
4701 AllowConstructorName =
4702 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00004703 D.getContext() == Declarator::MemberContext);
John McCallba9d8532010-04-13 06:39:49 +00004704 else
4705 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4706
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004707 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004708 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4709 /*EnteringContext=*/true,
4710 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004711 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004712 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004713 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004714 D.getName()) ||
4715 // Once we're past the identifier, if the scope was bad, mark the
4716 // whole declarator bad.
4717 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004718 D.SetIdentifier(0, Tok.getLocation());
4719 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004720 } else {
4721 // Parsed the unqualified-id; update range information and move along.
4722 if (D.getSourceRange().getBegin().isInvalid())
4723 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4724 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004725 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004726 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004727 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004728 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004729 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004730 "There's a C++-specific check for tok::identifier above");
4731 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4732 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4733 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004734 goto PastIdentifier;
Richard Smitha38253c2013-07-11 05:10:21 +00004735 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
4736 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4737 << FixItHint::CreateRemoval(Tok.getLocation());
4738 D.SetIdentifier(0, Tok.getLocation());
4739 ConsumeToken();
4740 goto PastIdentifier;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004741 }
Richard Smith9988f282012-03-29 01:16:42 +00004742
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004743 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004744 // direct-declarator: '(' declarator ')'
4745 // direct-declarator: '(' attributes declarator ')'
4746 // Example: 'char (*X)' or 'int (*XX)(void)'
4747 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004748
4749 // If the declarator was parenthesized, we entered the declarator
4750 // scope when parsing the parenthesized declarator, then exited
4751 // the scope already. Re-enter the scope, if we need to.
4752 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004753 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004754 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004755 if (!D.isInvalidType() &&
4756 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004757 // Change the declaration context for name lookup, until this function
4758 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004759 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004760 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004761 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004762 // This could be something simple like "int" (in which case the declarator
4763 // portion is empty), if an abstract-declarator is allowed.
4764 D.SetIdentifier(0, Tok.getLocation());
Richard Smith30f2a742013-02-20 20:19:27 +00004765
4766 // The grammar for abstract-pack-declarator does not allow grouping parens.
4767 // FIXME: Revisit this once core issue 1488 is resolved.
4768 if (D.hasEllipsis() && D.hasGroupingParens())
4769 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4770 diag::ext_abstract_pack_declarator_parens);
Reid Spencer5f016e22007-07-11 17:01:13 +00004771 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004772 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004773 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004774 if (D.getContext() == Declarator::MemberContext)
4775 Diag(Tok, diag::err_expected_member_name_or_semi)
4776 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004777 else if (getLangOpts().CPlusPlus) {
4778 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4779 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4780 else
4781 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4782 } else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004783 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004784 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004785 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004786 }
Mike Stump1eb44332009-09-09 15:08:12 +00004787
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004788 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004789 assert(D.isPastIdentifier() &&
4790 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004791
Richard Smith6ee326a2012-04-10 01:32:12 +00004792 // Don't parse attributes unless we have parsed an unparenthesized name.
4793 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004794 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004795
Reid Spencer5f016e22007-07-11 17:01:13 +00004796 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004797 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004798 // Enter function-declaration scope, limiting any declarators to the
4799 // function prototype scope, including parameter declarators.
4800 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004801 Scope::FunctionPrototypeScope|Scope::DeclScope|
4802 (D.isFunctionDeclaratorAFunctionDeclaration()
4803 ? Scope::FunctionDeclarationScope : 0));
4804
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004805 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4806 // In such a case, check if we actually have a function declarator; if it
4807 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004808 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004809 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4810 // The name of the declarator, if any, is tentatively declared within
4811 // a possible direct initializer.
4812 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4813 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4814 TentativelyDeclaredIdentifiers.pop_back();
4815 if (!IsFunctionDecl)
4816 break;
4817 }
John McCall0b7e6782011-03-24 11:26:52 +00004818 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004819 BalancedDelimiterTracker T(*this, tok::l_paren);
4820 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004821 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004822 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004823 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004824 ParseBracketDeclarator(D);
4825 } else {
4826 break;
4827 }
4828 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004829}
Reid Spencer5f016e22007-07-11 17:01:13 +00004830
Chris Lattneref4715c2008-04-06 05:45:57 +00004831/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4832/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004833/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004834/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4835///
4836/// direct-declarator:
4837/// '(' declarator ')'
4838/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004839/// direct-declarator '(' parameter-type-list ')'
4840/// direct-declarator '(' identifier-list[opt] ')'
4841/// [GNU] direct-declarator '(' parameter-forward-declarations
4842/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004843///
4844void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004845 BalancedDelimiterTracker T(*this, tok::l_paren);
4846 T.consumeOpen();
4847
Chris Lattneref4715c2008-04-06 05:45:57 +00004848 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004849
Chris Lattner7399ee02008-10-20 02:05:46 +00004850 // Eat any attributes before we look at whether this is a grouping or function
4851 // declarator paren. If this is a grouping paren, the attribute applies to
4852 // the type being built up, for example:
4853 // int (__attribute__(()) *x)(long y)
4854 // If this ends up not being a grouping paren, the attribute applies to the
4855 // first argument, for example:
4856 // int (__attribute__(()) int x)
4857 // In either case, we need to eat any attributes to be able to determine what
4858 // sort of paren this is.
4859 //
John McCall0b7e6782011-03-24 11:26:52 +00004860 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004861 bool RequiresArg = false;
4862 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004863 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004864
Chris Lattner7399ee02008-10-20 02:05:46 +00004865 // We require that the argument list (if this is a non-grouping paren) be
4866 // present even if the attribute list was empty.
4867 RequiresArg = true;
4868 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004869
Steve Naroff239f0732008-12-25 14:16:32 +00004870 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004871 ParseMicrosoftTypeAttributes(attrs);
4872
Dawn Perchik52fc3142010-09-03 01:29:35 +00004873 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004874 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004875 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004876
Chris Lattneref4715c2008-04-06 05:45:57 +00004877 // If we haven't past the identifier yet (or where the identifier would be
4878 // stored, if this is an abstract declarator), then this is probably just
4879 // grouping parens. However, if this could be an abstract-declarator, then
4880 // this could also be the start of function arguments (consider 'void()').
4881 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004882
Chris Lattneref4715c2008-04-06 05:45:57 +00004883 if (!D.mayOmitIdentifier()) {
4884 // If this can't be an abstract-declarator, this *must* be a grouping
4885 // paren, because we haven't seen the identifier yet.
4886 isGrouping = true;
4887 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004888 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4889 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004890 isDeclarationSpecifier() || // 'int(int)' is a function.
4891 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004892 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4893 // considered to be a type, not a K&R identifier-list.
4894 isGrouping = false;
4895 } else {
4896 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4897 isGrouping = true;
4898 }
Mike Stump1eb44332009-09-09 15:08:12 +00004899
Chris Lattneref4715c2008-04-06 05:45:57 +00004900 // If this is a grouping paren, handle:
4901 // direct-declarator: '(' declarator ')'
4902 // direct-declarator: '(' attributes declarator ')'
4903 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004904 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4905 D.setEllipsisLoc(SourceLocation());
4906
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004907 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004908 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004909 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004910 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004911 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004912 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004913 T.getCloseLocation()),
4914 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004915
4916 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004917
4918 // An ellipsis cannot be placed outside parentheses.
4919 if (EllipsisLoc.isValid())
4920 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4921
Chris Lattneref4715c2008-04-06 05:45:57 +00004922 return;
4923 }
Mike Stump1eb44332009-09-09 15:08:12 +00004924
Chris Lattneref4715c2008-04-06 05:45:57 +00004925 // Okay, if this wasn't a grouping paren, it must be the start of a function
4926 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004927 // identifier (and remember where it would have been), then call into
4928 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004929 D.SetIdentifier(0, Tok.getLocation());
4930
David Blaikie42d6d0c2011-12-04 05:04:18 +00004931 // Enter function-declaration scope, limiting any declarators to the
4932 // function prototype scope, including parameter declarators.
4933 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004934 Scope::FunctionPrototypeScope | Scope::DeclScope |
4935 (D.isFunctionDeclaratorAFunctionDeclaration()
4936 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00004937 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004938 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004939}
4940
4941/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4942/// declarator D up to a paren, which indicates that we are parsing function
4943/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004944///
Richard Smith6ee326a2012-04-10 01:32:12 +00004945/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4946/// immediately after the open paren - they should be considered to be the
4947/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004948///
Richard Smith6ee326a2012-04-10 01:32:12 +00004949/// If RequiresArg is true, then the first argument of the function is required
4950/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004951///
Richard Smith6ee326a2012-04-10 01:32:12 +00004952/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4953/// (C++11) ref-qualifier[opt], exception-specification[opt],
4954/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4955///
4956/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004957/// dynamic-exception-specification
4958/// noexcept-specification
4959///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004960void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004961 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004962 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004963 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004964 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004965 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004966 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004967 // lparen is already consumed!
4968 assert(D.isPastIdentifier() && "Should not call before identifier!");
4969
4970 // This should be true when the function has typed arguments.
4971 // Otherwise, it is treated as a K&R-style function.
4972 bool HasProto = false;
4973 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004974 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004975 // Remember where we see an ellipsis, if any.
4976 SourceLocation EllipsisLoc;
4977
4978 DeclSpec DS(AttrFactory);
4979 bool RefQualifierIsLValueRef = true;
4980 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004981 SourceLocation ConstQualifierLoc;
4982 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004983 ExceptionSpecificationType ESpecType = EST_None;
4984 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004985 SmallVector<ParsedType, 2> DynamicExceptions;
4986 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004987 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004988 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004989 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004990
James Molloy16f1f712012-02-29 10:24:19 +00004991 Actions.ActOnStartFunctionDeclarator();
4992
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004993 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4994 EndLoc is the end location for the function declarator.
4995 They differ for trailing return types. */
4996 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004997 SourceLocation LParenLoc, RParenLoc;
4998 LParenLoc = Tracker.getOpenLocation();
4999 StartLoc = LParenLoc;
5000
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005001 if (isFunctionDeclaratorIdentifierList()) {
5002 if (RequiresArg)
5003 Diag(Tok, diag::err_argument_required_after_attribute);
5004
5005 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5006
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005007 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005008 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005009 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005010 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005011 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005012 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00005013 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005014 else if (RequiresArg)
5015 Diag(Tok, diag::err_argument_required_after_attribute);
5016
David Blaikie4e4d0842012-03-11 07:00:24 +00005017 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005018
5019 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005020 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005021 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005022 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005023 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005024
David Blaikie4e4d0842012-03-11 07:00:24 +00005025 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005026 // FIXME: Accept these components in any order, and produce fixits to
5027 // correct the order if the user gets it wrong. Ideally we should deal
5028 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005029
5030 // Parse cv-qualifier-seq[opt].
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005031 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5032 /*CXX11AttributesAllowed*/ false,
5033 /*AtomicAllowed*/ false);
Richard Smith6ee326a2012-04-10 01:32:12 +00005034 if (!DS.getSourceRange().getEnd().isInvalid()) {
5035 EndLoc = DS.getSourceRange().getEnd();
5036 ConstQualifierLoc = DS.getConstSpecLoc();
5037 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5038 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005039
5040 // Parse ref-qualifier[opt].
5041 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00005042 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00005043 diag::warn_cxx98_compat_ref_qualifier :
5044 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00005045
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005046 RefQualifierIsLValueRef = Tok.is(tok::amp);
5047 RefQualifierLoc = ConsumeToken();
5048 EndLoc = RefQualifierLoc;
5049 }
5050
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005051 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00005052 // If a declaration declares a member function or member function
5053 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005054 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00005055 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005056 // declarator.
Richard Smithd9227792013-03-15 00:41:52 +00005057 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosier8decdee2012-06-26 22:30:43 +00005058 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00005059 getLangOpts().CPlusPlus11 &&
Richard Smithd9227792013-03-15 00:41:52 +00005060 (D.getContext() == Declarator::MemberContext
5061 ? !D.getDeclSpec().isFriendSpecified()
5062 : D.getContext() == Declarator::FileContext &&
5063 D.getCXXScopeSpec().isValid() &&
5064 Actions.CurContext->isRecord());
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005065 Sema::CXXThisScopeRAII ThisScope(Actions,
5066 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00005067 DS.getTypeQualifiers() |
Richard Smith84046262013-04-21 01:08:50 +00005068 (D.getDeclSpec().isConstexprSpecified() &&
5069 !getLangOpts().CPlusPlus1y
Richard Smith7b19cb12013-01-14 01:55:13 +00005070 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005071 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00005072
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005073 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00005074 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00005075 DynamicExceptions,
5076 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00005077 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005078 if (ESpecType != EST_None)
5079 EndLoc = ESpecRange.getEnd();
5080
Richard Smith6ee326a2012-04-10 01:32:12 +00005081 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5082 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005083 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00005084
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005085 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005086 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00005087 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00005088 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005089 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5090 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005091 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00005092 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00005093 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005094 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005095 }
5096 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005097 }
5098
5099 // Remember that we parsed a function type, and remember the attributes.
5100 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005101 IsAmbiguous,
5102 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005103 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005104 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005105 DS.getTypeQualifiers(),
5106 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00005107 RefQualifierLoc, ConstQualifierLoc,
5108 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00005109 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005110 ESpecType, ESpecRange.getBegin(),
5111 DynamicExceptions.data(),
5112 DynamicExceptionRanges.data(),
5113 DynamicExceptions.size(),
5114 NoexceptExpr.isUsable() ?
5115 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005116 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005117 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00005118 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00005119
5120 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005121}
5122
5123/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5124/// identifier list form for a K&R-style function: void foo(a,b,c)
5125///
5126/// Note that identifier-lists are only allowed for normal declarators, not for
5127/// abstract-declarators.
5128bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00005129 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005130 && Tok.is(tok::identifier)
5131 && !TryAltiVecVectorToken()
5132 // K&R identifier lists can't have typedefs as identifiers, per C99
5133 // 6.7.5.3p11.
5134 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5135 // Identifier lists follow a really simple grammar: the identifiers can
5136 // be followed *only* by a ", identifier" or ")". However, K&R
5137 // identifier lists are really rare in the brave new modern world, and
5138 // it is very common for someone to typo a type in a non-K&R style
5139 // list. If we are presented with something like: "void foo(intptr x,
5140 // float y)", we don't want to start parsing the function declarator as
5141 // though it is a K&R style declarator just because intptr is an
5142 // invalid type.
5143 //
5144 // To handle this, we check to see if the token after the first
5145 // identifier is a "," or ")". Only then do we parse it as an
5146 // identifier list.
5147 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5148}
5149
5150/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5151/// we found a K&R-style identifier list instead of a typed parameter list.
5152///
5153/// After returning, ParamInfo will hold the parsed parameters.
5154///
5155/// identifier-list: [C99 6.7.5]
5156/// identifier
5157/// identifier-list ',' identifier
5158///
5159void Parser::ParseFunctionDeclaratorIdentifierList(
5160 Declarator &D,
Craig Topper6b9240e2013-07-05 19:34:19 +00005161 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005162 // If there was no identifier specified for the declarator, either we are in
5163 // an abstract-declarator, or we are in a parameter declarator which was found
5164 // to be abstract. In abstract-declarators, identifier lists are not valid:
5165 // diagnose this.
5166 if (!D.getIdentifier())
5167 Diag(Tok, diag::ext_ident_list_in_param);
5168
5169 // Maintain an efficient lookup of params we have seen so far.
5170 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5171
5172 while (1) {
5173 // If this isn't an identifier, report the error and skip until ')'.
5174 if (Tok.isNot(tok::identifier)) {
5175 Diag(Tok, diag::err_expected_ident);
5176 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
5177 // Forget we parsed anything.
5178 ParamInfo.clear();
5179 return;
5180 }
5181
5182 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5183
5184 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5185 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5186 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5187
5188 // Verify that the argument identifier has not already been mentioned.
5189 if (!ParamsSoFar.insert(ParmII)) {
5190 Diag(Tok, diag::err_param_redefinition) << ParmII;
5191 } else {
5192 // Remember this identifier in ParamInfo.
5193 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5194 Tok.getLocation(),
5195 0));
5196 }
5197
5198 // Eat the identifier.
5199 ConsumeToken();
5200
5201 // The list continues if we see a comma.
5202 if (Tok.isNot(tok::comma))
5203 break;
5204 ConsumeToken();
5205 }
5206}
5207
5208/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5209/// after the opening parenthesis. This function will not parse a K&R-style
5210/// identifier list.
5211///
Richard Smith6ce48a72012-04-11 04:01:28 +00005212/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5213/// caller parsed those arguments immediately after the open paren - they should
5214/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005215///
5216/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5217/// be the location of the ellipsis, if any was parsed.
5218///
Reid Spencer5f016e22007-07-11 17:01:13 +00005219/// parameter-type-list: [C99 6.7.5]
5220/// parameter-list
5221/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00005222/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00005223///
5224/// parameter-list: [C99 6.7.5]
5225/// parameter-declaration
5226/// parameter-list ',' parameter-declaration
5227///
5228/// parameter-declaration: [C99 6.7.5]
5229/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00005230/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00005231/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00005232/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00005233/// declaration-specifiers abstract-declarator[opt]
5234/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00005235/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00005236/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00005237/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00005238///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005239void Parser::ParseParameterDeclarationClause(
5240 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00005241 ParsedAttributes &FirstArgAttrs,
Craig Topper6b9240e2013-07-05 19:34:19 +00005242 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005243 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00005244
Chris Lattnerf97409f2008-04-06 06:57:35 +00005245 while (1) {
5246 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00005247 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5248 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00005249 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00005250 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00005251 }
Mike Stump1eb44332009-09-09 15:08:12 +00005252
Chris Lattnerf97409f2008-04-06 06:57:35 +00005253 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00005254 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00005255 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005256
Richard Smith6ce48a72012-04-11 04:01:28 +00005257 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005258 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00005259
John McCall7f040a92010-12-24 02:08:15 +00005260 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00005261 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00005262
5263 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00005264
5265 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00005266 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005267 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00005268 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5269 // too much hassle.
5270 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00005271
Chris Lattnere64c5492009-02-27 18:38:20 +00005272 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005273
Chris Lattnerf97409f2008-04-06 06:57:35 +00005274 // Parse the declarator. This is "PrototypeContext", because we must
5275 // accept either 'declarator' or 'abstract-declarator' here.
5276 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5277 ParseDeclarator(ParmDecl);
5278
5279 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00005280 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00005281
Chris Lattnerf97409f2008-04-06 06:57:35 +00005282 // Remember this parsed parameter in ParamInfo.
5283 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005284
Douglas Gregor72b505b2008-12-16 21:30:33 +00005285 // DefArgToks is used when the parsing of default arguments needs
5286 // to be delayed.
5287 CachedTokens *DefArgToks = 0;
5288
Chris Lattnerf97409f2008-04-06 06:57:35 +00005289 // If no parameter was specified, verify that *something* was specified,
5290 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00005291 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5292 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005293 // Completely missing, emit error.
5294 Diag(DSStart, diag::err_missing_param);
5295 } else {
5296 // Otherwise, we have something. Add it and let semantic analysis try
5297 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005298
Chris Lattnerf97409f2008-04-06 06:57:35 +00005299 // Inform the actions module about the parameter declarator, so it gets
5300 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00005301 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00005302
5303 // Parse the default argument, if any. We parse the default
5304 // arguments in all dialects; the semantic analysis in
5305 // ActOnParamDefaultArgument will reject the default argument in
5306 // C.
5307 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005308 SourceLocation EqualLoc = Tok.getLocation();
5309
Chris Lattner04421082008-04-08 04:40:51 +00005310 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005311 if (D.getContext() == Declarator::MemberContext) {
5312 // If we're inside a class definition, cache the tokens
5313 // corresponding to the default argument. We'll actually parse
5314 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005315 // FIXME: Can we use a smart pointer for Toks?
5316 DefArgToks = new CachedTokens;
5317
Mike Stump1eb44332009-09-09 15:08:12 +00005318 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00005319 /*StopAtSemi=*/true,
5320 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005321 delete DefArgToks;
5322 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005323 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005324 } else {
5325 // Mark the end of the default argument so that we know when to
5326 // stop when we parse it later on.
5327 Token DefArgEnd;
5328 DefArgEnd.startToken();
5329 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5330 DefArgEnd.setLocation(Tok.getLocation());
5331 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005332 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005333 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005334 }
Chris Lattner04421082008-04-08 04:40:51 +00005335 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005336 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005337 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005338
Chad Rosier8decdee2012-06-26 22:30:43 +00005339 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005340 // used.
5341 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005342 Sema::PotentiallyEvaluatedIfUsed,
5343 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005344
Sebastian Redl84407ba2012-03-14 15:54:00 +00005345 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005346 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005347 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005348 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005349 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005350 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005351 if (DefArgResult.isInvalid()) {
5352 Actions.ActOnParamDefaultArgumentError(Param);
5353 SkipUntil(tok::comma, tok::r_paren, true, true);
5354 } else {
5355 // Inform the actions module about the default argument
5356 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005357 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005358 }
Chris Lattner04421082008-04-08 04:40:51 +00005359 }
5360 }
Mike Stump1eb44332009-09-09 15:08:12 +00005361
5362 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5363 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00005364 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005365 }
5366
5367 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005368 if (Tok.isNot(tok::comma)) {
5369 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005370 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005371
David Blaikie4e4d0842012-03-11 07:00:24 +00005372 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005373 // We have ellipsis without a preceding ',', which is ill-formed
5374 // in C. Complain and provide the fix.
5375 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005376 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005377 }
5378 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005379
Douglas Gregored5d6512009-09-22 21:41:40 +00005380 break;
5381 }
Mike Stump1eb44332009-09-09 15:08:12 +00005382
Chris Lattnerf97409f2008-04-06 06:57:35 +00005383 // Consume the comma.
5384 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005385 }
Mike Stump1eb44332009-09-09 15:08:12 +00005386
Chris Lattner66d28652008-04-06 06:34:08 +00005387}
Chris Lattneref4715c2008-04-06 05:45:57 +00005388
Reid Spencer5f016e22007-07-11 17:01:13 +00005389/// [C90] direct-declarator '[' constant-expression[opt] ']'
5390/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5391/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5392/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5393/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005394/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5395/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005396void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005397 if (CheckProhibitedCXX11Attribute())
5398 return;
5399
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005400 BalancedDelimiterTracker T(*this, tok::l_square);
5401 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005402
Chris Lattner378c7e42008-12-18 07:27:21 +00005403 // C array syntax has many features, but by-far the most common is [] and [4].
5404 // This code does a fast path to handle some of the most obvious cases.
5405 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005406 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005407 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005408 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005409
Chris Lattner378c7e42008-12-18 07:27:21 +00005410 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005411 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005412 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005413 T.getOpenLocation(),
5414 T.getCloseLocation()),
5415 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005416 return;
5417 } else if (Tok.getKind() == tok::numeric_constant &&
5418 GetLookAheadToken(1).is(tok::r_square)) {
5419 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005420 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005421 ConsumeToken();
5422
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005423 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005424 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005425 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005426
Chris Lattner378c7e42008-12-18 07:27:21 +00005427 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005428 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005429 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005430 T.getOpenLocation(),
5431 T.getCloseLocation()),
5432 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005433 return;
5434 }
Mike Stump1eb44332009-09-09 15:08:12 +00005435
Reid Spencer5f016e22007-07-11 17:01:13 +00005436 // If valid, this location is the position where we read the 'static' keyword.
5437 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005438 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005439 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005440
Reid Spencer5f016e22007-07-11 17:01:13 +00005441 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005442 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005443 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005444 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005445
Reid Spencer5f016e22007-07-11 17:01:13 +00005446 // If we haven't already read 'static', check to see if there is one after the
5447 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005448 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005449 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005450
Reid Spencer5f016e22007-07-11 17:01:13 +00005451 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5452 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005453 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005454
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005455 // Handle the case where we have '[*]' as the array size. However, a leading
5456 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005457 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005458 // infrequent, use of lookahead is not costly here.
5459 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005460 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005461
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005462 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005463 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005464 StaticLoc = SourceLocation(); // Drop the static.
5465 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005466 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005467 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005468 // Note, in C89, this production uses the constant-expr production instead
5469 // of assignment-expr. The only difference is that assignment-expr allows
5470 // things like '=' and '*='. Sema rejects these in C89 mode because they
5471 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005472
Douglas Gregore0762c92009-06-19 23:52:42 +00005473 // Parse the constant-expression or assignment-expression now (depending
5474 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005475 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005476 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005477 } else {
5478 EnterExpressionEvaluationContext Unevaluated(Actions,
5479 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005480 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005481 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005482 }
Mike Stump1eb44332009-09-09 15:08:12 +00005483
Reid Spencer5f016e22007-07-11 17:01:13 +00005484 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005485 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005486 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005487 // If the expression was invalid, skip it.
5488 SkipUntil(tok::r_square);
5489 return;
5490 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005491
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005492 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005493
John McCall0b7e6782011-03-24 11:26:52 +00005494 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005495 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005496
Chris Lattner378c7e42008-12-18 07:27:21 +00005497 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005498 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005499 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005500 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005501 T.getOpenLocation(),
5502 T.getCloseLocation()),
5503 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005504}
5505
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005506/// [GNU] typeof-specifier:
5507/// typeof ( expressions )
5508/// typeof ( type-name )
5509/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005510///
5511void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005512 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005513 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005514 SourceLocation StartLoc = ConsumeToken();
5515
John McCallcfb708c2010-01-13 20:03:27 +00005516 const bool hasParens = Tok.is(tok::l_paren);
5517
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005518 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5519 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005520
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005521 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005522 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005523 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005524 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5525 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005526 if (hasParens)
5527 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005528
5529 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005530 // FIXME: Not accurate, the range gets one token more than it should.
5531 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005532 else
5533 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005534
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005535 if (isCastExpr) {
5536 if (!CastTy) {
5537 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005538 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005539 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005540
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005541 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005542 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005543 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5544 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005545 DiagID, CastTy))
5546 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005547 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005548 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005549
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005550 // If we get here, the operand to the typeof was an expresion.
5551 if (Operand.isInvalid()) {
5552 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005553 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005554 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005555
Eli Friedman71b8fb52012-01-21 01:01:51 +00005556 // We might need to transform the operand if it is potentially evaluated.
5557 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5558 if (Operand.isInvalid()) {
5559 DS.SetTypeSpecError();
5560 return;
5561 }
5562
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005563 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005564 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005565 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5566 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005567 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005568 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005569}
Chris Lattner1b492422010-02-28 18:33:55 +00005570
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005571/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005572/// _Atomic ( type-name )
5573///
5574void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005575 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5576 "Not an atomic specifier");
Eli Friedmanb001de72011-10-06 23:00:33 +00005577
5578 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005579 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005580 if (T.consumeOpen())
Eli Friedmanb001de72011-10-06 23:00:33 +00005581 return;
Eli Friedmanb001de72011-10-06 23:00:33 +00005582
5583 TypeResult Result = ParseTypeName();
5584 if (Result.isInvalid()) {
5585 SkipUntil(tok::r_paren);
5586 return;
5587 }
5588
5589 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005590 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005591
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005592 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005593 return;
5594
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005595 DS.setTypeofParensRange(T.getRange());
5596 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005597
5598 const char *PrevSpec = 0;
5599 unsigned DiagID;
5600 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5601 DiagID, Result.release()))
5602 Diag(StartLoc, DiagID) << PrevSpec;
5603}
5604
Chris Lattner1b492422010-02-28 18:33:55 +00005605
5606/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5607/// from TryAltiVecVectorToken.
5608bool Parser::TryAltiVecVectorTokenOutOfLine() {
5609 Token Next = NextToken();
5610 switch (Next.getKind()) {
5611 default: return false;
5612 case tok::kw_short:
5613 case tok::kw_long:
5614 case tok::kw_signed:
5615 case tok::kw_unsigned:
5616 case tok::kw_void:
5617 case tok::kw_char:
5618 case tok::kw_int:
5619 case tok::kw_float:
5620 case tok::kw_double:
5621 case tok::kw_bool:
5622 case tok::kw___pixel:
5623 Tok.setKind(tok::kw___vector);
5624 return true;
5625 case tok::identifier:
5626 if (Next.getIdentifierInfo() == Ident_pixel) {
5627 Tok.setKind(tok::kw___vector);
5628 return true;
5629 }
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005630 if (Next.getIdentifierInfo() == Ident_bool) {
5631 Tok.setKind(tok::kw___vector);
5632 return true;
5633 }
Chris Lattner1b492422010-02-28 18:33:55 +00005634 return false;
5635 }
5636}
5637
5638bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5639 const char *&PrevSpec, unsigned &DiagID,
5640 bool &isInvalid) {
5641 if (Tok.getIdentifierInfo() == Ident_vector) {
5642 Token Next = NextToken();
5643 switch (Next.getKind()) {
5644 case tok::kw_short:
5645 case tok::kw_long:
5646 case tok::kw_signed:
5647 case tok::kw_unsigned:
5648 case tok::kw_void:
5649 case tok::kw_char:
5650 case tok::kw_int:
5651 case tok::kw_float:
5652 case tok::kw_double:
5653 case tok::kw_bool:
5654 case tok::kw___pixel:
5655 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5656 return true;
5657 case tok::identifier:
5658 if (Next.getIdentifierInfo() == Ident_pixel) {
5659 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5660 return true;
5661 }
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005662 if (Next.getIdentifierInfo() == Ident_bool) {
5663 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5664 return true;
5665 }
Chris Lattner1b492422010-02-28 18:33:55 +00005666 break;
5667 default:
5668 break;
5669 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005670 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005671 DS.isTypeAltiVecVector()) {
5672 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5673 return true;
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005674 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5675 DS.isTypeAltiVecVector()) {
5676 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID);
5677 return true;
Chris Lattner1b492422010-02-28 18:33:55 +00005678 }
5679 return false;
5680}