blob: 331c5e42fdbe782a434829d1cd76ac5cd23d6a6d [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"
Stephen Hines651f13c2014-04-23 16:59:28 -070016#include "clang/AST/ASTContext.h"
Larisse Voufo7c64ef02013-06-21 00:08:46 +000017#include "clang/AST/DeclTemplate.h"
Benjamin Kramer9852f582012-12-01 16:35:25 +000018#include "clang/Basic/AddressSpaces.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070019#include "clang/Basic/Attributes.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000020#include "clang/Basic/CharInfo.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070021#include "clang/Basic/TargetInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000023#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000024#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000025#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000026#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000028#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000029#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000030using namespace clang;
31
32//===----------------------------------------------------------------------===//
33// C99 6.7: Declarations.
34//===----------------------------------------------------------------------===//
35
36/// ParseTypeName
37/// type-name: [C99 6.7.6]
38/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000039///
40/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000041TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000042 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000043 AccessSpecifier AS,
Richard Smith6b3d3e52013-02-20 19:22:51 +000044 Decl **OwnedType,
45 ParsedAttributes *Attrs) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000046 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000047 if (DSC == DSC_normal)
48 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000049
Reid Spencer5f016e22007-07-11 17:01:13 +000050 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000051 DeclSpec DS(AttrFactory);
Richard Smith6b3d3e52013-02-20 19:22:51 +000052 if (Attrs)
53 DS.addAttributes(Attrs->getList());
Richard Smith7796eb52012-03-12 08:56:40 +000054 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000055 if (OwnedType)
56 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000057
Reid Spencer5f016e22007-07-11 17:01:13 +000058 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000059 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000060 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000061 if (Range)
62 *Range = DeclaratorInfo.getSourceRange();
63
Chris Lattnereaaebc72009-04-25 08:06:05 +000064 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000065 return true;
66
Douglas Gregor23c94db2010-07-02 17:43:08 +000067 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000068}
69
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000070
71/// isAttributeLateParsed - Return true if the attribute has arguments that
72/// require late parsing.
73static bool isAttributeLateParsed(const IdentifierInfo &II) {
Stephen Hines651f13c2014-04-23 16:59:28 -070074#define CLANG_ATTR_LATE_PARSED_LIST
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000075 return llvm::StringSwitch<bool>(II.getName())
Stephen Hines651f13c2014-04-23 16:59:28 -070076#include "clang/Parse/AttrParserStringSwitches.inc"
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000077 .Default(false);
Stephen Hines651f13c2014-04-23 16:59:28 -070078#undef CLANG_ATTR_LATE_PARSED_LIST
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000079}
80
Sean Huntbbd37c62009-11-21 08:43:09 +000081/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000082///
83/// [GNU] attributes:
84/// attribute
85/// attributes attribute
86///
87/// [GNU] attribute:
88/// '__attribute__' '(' '(' attribute-list ')' ')'
89///
90/// [GNU] attribute-list:
91/// attrib
92/// attribute_list ',' attrib
93///
94/// [GNU] attrib:
95/// empty
96/// attrib-name
97/// attrib-name '(' identifier ')'
98/// attrib-name '(' identifier ',' nonempty-expr-list ')'
99/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
100///
101/// [GNU] attrib-name:
102/// identifier
103/// typespec
104/// typequal
105/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +0000106///
Richard Smith4c6c4112013-09-03 18:57:36 +0000107/// Whether an attribute takes an 'identifier' is determined by the
108/// attrib-name. GCC's behavior here is not worth imitating:
Reid Spencer5f016e22007-07-11 17:01:13 +0000109///
Richard Smith4c6c4112013-09-03 18:57:36 +0000110/// * In C mode, if the attribute argument list starts with an identifier
111/// followed by a ',' or an ')', and the identifier doesn't resolve to
112/// a type, it is parsed as an identifier. If the attribute actually
113/// wanted an expression, it's out of luck (but it turns out that no
114/// attributes work that way, because C constant expressions are very
115/// limited).
116/// * In C++ mode, if the attribute argument list starts with an identifier,
117/// and the attribute *wants* an identifier, it is parsed as an identifier.
118/// At block scope, any additional tokens between the identifier and the
119/// ',' or ')' are ignored, otherwise they produce a parse error.
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000120///
Richard Smith4c6c4112013-09-03 18:57:36 +0000121/// We follow the C++ model, but don't allow junk after the identifier.
John McCall7f040a92010-12-24 02:08:15 +0000122void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000123 SourceLocation *endLoc,
Stephen Hines651f13c2014-04-23 16:59:28 -0700124 LateParsedAttrList *LateAttrs,
125 Declarator *D) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000126 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Chris Lattner04d66662007-10-09 17:33:22 +0000128 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 ConsumeToken();
130 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
131 "attribute")) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000132 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000133 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 }
135 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000136 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000137 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 }
139 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Stephen Hines651f13c2014-04-23 16:59:28 -0700140 while (true) {
141 // Allow empty/non-empty attributes. ((__vector_size__(16),,,,))
142 if (TryConsumeToken(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 continue;
Stephen Hines651f13c2014-04-23 16:59:28 -0700144
145 // Expect an identifier or declaration specifier (const, int, etc.)
146 if (Tok.isNot(tok::identifier) && !isDeclarationSpecifier())
147 break;
148
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
150 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Stephen Hines651f13c2014-04-23 16:59:28 -0700152 if (Tok.isNot(tok::l_paren)) {
Aaron Ballman624421f2013-08-31 01:11:41 +0000153 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
154 AttributeList::AS_GNU);
Stephen Hines651f13c2014-04-23 16:59:28 -0700155 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700157
158 // Handle "parameterized" attributes
159 if (!LateAttrs || !isAttributeLateParsed(*AttrName)) {
160 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, 0,
161 SourceLocation(), AttributeList::AS_GNU, D);
162 continue;
163 }
164
165 // Handle attributes with arguments that require late parsing.
166 LateParsedAttribute *LA =
167 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
168 LateAttrs->push_back(LA);
169
170 // Attributes in a class are parsed at the end of the class, along
171 // with other late-parsed declarations.
172 if (!ClassStack.empty() && !LateAttrs->parseSoon())
173 getCurrentClass().LateParsedDeclarations.push_back(LA);
174
175 // consume everything up to and including the matching right parens
176 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
177
178 Token Eof;
179 Eof.startToken();
180 Eof.setLocation(Tok.getLocation());
181 LA->Toks.push_back(Eof);
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700183
184 if (ExpectAndConsume(tok::r_paren))
Alexey Bataev8fe24752013-11-18 08:17:37 +0000185 SkipUntil(tok::r_paren, StopAtSemi);
Sean Huntbbd37c62009-11-21 08:43:09 +0000186 SourceLocation Loc = Tok.getLocation();
Stephen Hines651f13c2014-04-23 16:59:28 -0700187 if (ExpectAndConsume(tok::r_paren))
Alexey Bataev8fe24752013-11-18 08:17:37 +0000188 SkipUntil(tok::r_paren, StopAtSemi);
John McCall7f040a92010-12-24 02:08:15 +0000189 if (endLoc)
190 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000192}
193
Aaron Ballman9feedb82013-11-04 12:55:56 +0000194/// \brief Normalizes an attribute name by dropping prefixed and suffixed __.
195static StringRef normalizeAttrName(StringRef Name) {
Richard Smithd92aa2d2013-10-24 01:07:54 +0000196 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
197 Name = Name.drop_front(2).drop_back(2);
Aaron Ballman9feedb82013-11-04 12:55:56 +0000198 return Name;
199}
200
201/// \brief Determine whether the given attribute has an identifier argument.
202static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700203#define CLANG_ATTR_IDENTIFIER_ARG_LIST
Aaron Ballman9feedb82013-11-04 12:55:56 +0000204 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Stephen Hines651f13c2014-04-23 16:59:28 -0700205#include "clang/Parse/AttrParserStringSwitches.inc"
Douglas Gregor92eb7d82013-05-02 23:25:32 +0000206 .Default(false);
Stephen Hines651f13c2014-04-23 16:59:28 -0700207#undef CLANG_ATTR_IDENTIFIER_ARG_LIST
Douglas Gregor92eb7d82013-05-02 23:25:32 +0000208}
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000209
Aaron Ballman9feedb82013-11-04 12:55:56 +0000210/// \brief Determine whether the given attribute parses a type argument.
211static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700212#define CLANG_ATTR_TYPE_ARG_LIST
Aaron Ballman9feedb82013-11-04 12:55:56 +0000213 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Stephen Hines651f13c2014-04-23 16:59:28 -0700214#include "clang/Parse/AttrParserStringSwitches.inc"
Aaron Ballman9feedb82013-11-04 12:55:56 +0000215 .Default(false);
Stephen Hines651f13c2014-04-23 16:59:28 -0700216#undef CLANG_ATTR_TYPE_ARG_LIST
217}
218
219/// \brief Determine whether the given attribute requires parsing its arguments
220/// in an unevaluated context or not.
221static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) {
222#define CLANG_ATTR_ARG_CONTEXT_LIST
223 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
224#include "clang/Parse/AttrParserStringSwitches.inc"
225 .Default(false);
226#undef CLANG_ATTR_ARG_CONTEXT_LIST
Aaron Ballman9feedb82013-11-04 12:55:56 +0000227}
228
Richard Smith8edabd92013-09-03 18:01:40 +0000229IdentifierLoc *Parser::ParseIdentifierLoc() {
230 assert(Tok.is(tok::identifier) && "expected an identifier");
231 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
232 Tok.getLocation(),
233 Tok.getIdentifierInfo());
234 ConsumeToken();
235 return IL;
236}
237
Richard Smithd386fef2013-10-31 01:56:18 +0000238void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
239 SourceLocation AttrNameLoc,
240 ParsedAttributes &Attrs,
241 SourceLocation *EndLoc) {
242 BalancedDelimiterTracker Parens(*this, tok::l_paren);
243 Parens.consumeOpen();
244
245 TypeResult T;
246 if (Tok.isNot(tok::r_paren))
247 T = ParseTypeName();
248
249 if (Parens.consumeClose())
250 return;
251
252 if (T.isInvalid())
253 return;
254
255 if (T.isUsable())
256 Attrs.addNewTypeAttr(&AttrName,
257 SourceRange(AttrNameLoc, Parens.getCloseLocation()), 0,
258 AttrNameLoc, T.get(), AttributeList::AS_GNU);
259 else
260 Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
261 0, AttrNameLoc, 0, 0, AttributeList::AS_GNU);
262}
263
Stephen Hines651f13c2014-04-23 16:59:28 -0700264void Parser::ParseAttributeArgsCommon(
265 IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
266 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
267 SourceLocation ScopeLoc, AttributeList::Syntax Syntax) {
Richard Smithd92aa2d2013-10-24 01:07:54 +0000268 // Ignore the left paren location for now.
269 ConsumeParen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000270
Aaron Ballman624421f2013-08-31 01:11:41 +0000271 ArgsVector ArgExprs;
Richard Smithd386fef2013-10-31 01:56:18 +0000272 if (Tok.is(tok::identifier)) {
Richard Smithd92aa2d2013-10-24 01:07:54 +0000273 // If this attribute wants an 'identifier' argument, make it so.
Richard Smithd386fef2013-10-31 01:56:18 +0000274 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName);
Stephen Hines651f13c2014-04-23 16:59:28 -0700275 AttributeList::Kind AttrKind =
276 AttributeList::getKind(AttrName, ScopeName, Syntax);
Richard Smithd92aa2d2013-10-24 01:07:54 +0000277
278 // If we don't know how to parse this attribute, but this is the only
279 // token in this argument, assume it's meant to be an identifier.
Bill Wendling307c92e2013-12-05 18:35:37 +0000280 if (AttrKind == AttributeList::UnknownAttribute ||
281 AttrKind == AttributeList::IgnoredAttribute) {
Richard Smithd92aa2d2013-10-24 01:07:54 +0000282 const Token &Next = NextToken();
Richard Smithd386fef2013-10-31 01:56:18 +0000283 IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma);
Richard Smithd92aa2d2013-10-24 01:07:54 +0000284 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000285
Richard Smithd386fef2013-10-31 01:56:18 +0000286 if (IsIdentifierArg)
287 ArgExprs.push_back(ParseIdentifierLoc());
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000288 }
289
Richard Smithd386fef2013-10-31 01:56:18 +0000290 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000291 // Eat the comma.
Aaron Ballman624421f2013-08-31 01:11:41 +0000292 if (!ArgExprs.empty())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000293 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000294
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000295 // Parse the non-empty comma-separated list of expressions.
Stephen Hines651f13c2014-04-23 16:59:28 -0700296 do {
297 std::unique_ptr<EnterExpressionEvaluationContext> Unevaluated;
298 if (attributeParsedArgsUnevaluated(*AttrName))
299 Unevaluated.reset(
300 new EnterExpressionEvaluationContext(Actions, Sema::Unevaluated));
301
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000302 ExprResult ArgExpr(ParseAssignmentExpression());
303 if (ArgExpr.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000304 SkipUntil(tok::r_paren, StopAtSemi);
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000305 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000306 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000307 ArgExprs.push_back(ArgExpr.release());
Stephen Hines651f13c2014-04-23 16:59:28 -0700308 // Eat the comma, move to the next argument
309 } while (TryConsumeToken(tok::comma));
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000310 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000311
312 SourceLocation RParen = Tok.getLocation();
Stephen Hines651f13c2014-04-23 16:59:28 -0700313 if (!ExpectAndConsume(tok::r_paren)) {
Michael Han45bed132012-10-04 16:42:52 +0000314 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithd386fef2013-10-31 01:56:18 +0000315 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
316 ArgExprs.data(), ArgExprs.size(), Syntax);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000317 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700318
319 if (EndLoc)
320 *EndLoc = RParen;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000321}
322
Stephen Hines651f13c2014-04-23 16:59:28 -0700323/// Parse the arguments to a parameterized GNU attribute or
324/// a C++11 attribute in "gnu" namespace.
325void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
326 SourceLocation AttrNameLoc,
327 ParsedAttributes &Attrs,
328 SourceLocation *EndLoc,
329 IdentifierInfo *ScopeName,
330 SourceLocation ScopeLoc,
331 AttributeList::Syntax Syntax,
332 Declarator *D) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000333
Stephen Hines651f13c2014-04-23 16:59:28 -0700334 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
335
336 AttributeList::Kind AttrKind =
337 AttributeList::getKind(AttrName, ScopeName, Syntax);
338
339 // Availability attributes have their own grammar.
340 // FIXME: All these cases fail to pass in the syntax and scope, and might be
341 // written as C++11 gnu:: attributes.
342 if (AttrKind == AttributeList::AT_Availability) {
343 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000344 return;
345 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000346
Stephen Hines651f13c2014-04-23 16:59:28 -0700347 if (AttrKind == AttributeList::AT_ObjCBridgeRelated) {
348 ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
349 return;
350 }
351
352 // Type safety attributes have their own grammar.
353 if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
354 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
355 return;
356 }
357
358 // Some attributes expect solely a type parameter.
359 if (attributeIsTypeArgAttr(*AttrName)) {
360 ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc);
361 return;
362 }
363
364 // These may refer to the function arguments, but need to be parsed early to
365 // participate in determining whether it's a redeclaration.
366 std::unique_ptr<ParseScope> PrototypeScope;
367 if (AttrName->isStr("enable_if") && D && D->isFunctionDeclarator()) {
368 DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo();
369 PrototypeScope.reset(new ParseScope(this, Scope::FunctionPrototypeScope |
370 Scope::FunctionDeclarationScope |
371 Scope::DeclScope));
372 for (unsigned i = 0; i != FTI.NumParams; ++i) {
373 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param);
374 Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param);
375 }
376 }
377
378 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
379 ScopeLoc, Syntax);
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000380}
381
Stephen Hines651f13c2014-04-23 16:59:28 -0700382bool Parser::ParseMicrosoftDeclSpecArgs(IdentifierInfo *AttrName,
383 SourceLocation AttrNameLoc,
384 ParsedAttributes &Attrs) {
385 // If the attribute isn't known, we will not attempt to parse any
386 // arguments.
387 if (!hasAttribute(AttrSyntax::Declspec, nullptr, AttrName,
388 getTargetInfo().getTriple(), getLangOpts())) {
389 // Eat the left paren, then skip to the ending right paren.
390 ConsumeParen();
391 SkipUntil(tok::r_paren);
392 return false;
393 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000394
Stephen Hines651f13c2014-04-23 16:59:28 -0700395 if (AttrName->getName() == "property") {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000396 // The property declspec is more complex in that it can take one or two
Chad Rosier8decdee2012-06-26 22:30:43 +0000397 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000398 // must be named get or put.
Stephen Hines651f13c2014-04-23 16:59:28 -0700399
John McCall76da55d2013-04-16 07:28:30 +0000400 BalancedDelimiterTracker T(*this, tok::l_paren);
401 T.expectAndConsume(diag::err_expected_lparen_after,
Stephen Hines651f13c2014-04-23 16:59:28 -0700402 AttrName->getNameStart(), tok::r_paren);
John McCall76da55d2013-04-16 07:28:30 +0000403
404 enum AccessorKind {
405 AK_Invalid = -1,
Stephen Hines651f13c2014-04-23 16:59:28 -0700406 AK_Put = 0,
407 AK_Get = 1 // indices into AccessorNames
John McCall76da55d2013-04-16 07:28:30 +0000408 };
Stephen Hines651f13c2014-04-23 16:59:28 -0700409 IdentifierInfo *AccessorNames[] = {0, 0};
John McCall76da55d2013-04-16 07:28:30 +0000410 bool HasInvalidAccessor = false;
411
412 // Parse the accessor specifications.
413 while (true) {
414 // Stop if this doesn't look like an accessor spec.
415 if (!Tok.is(tok::identifier)) {
416 // If the user wrote a completely empty list, use a special diagnostic.
417 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
418 AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700419 Diag(AttrNameLoc, diag::err_ms_property_no_getter_or_putter);
John McCall76da55d2013-04-16 07:28:30 +0000420 break;
421 }
422
423 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
424 break;
425 }
426
427 AccessorKind Kind;
428 SourceLocation KindLoc = Tok.getLocation();
429 StringRef KindStr = Tok.getIdentifierInfo()->getName();
430 if (KindStr == "get") {
431 Kind = AK_Get;
432 } else if (KindStr == "put") {
433 Kind = AK_Put;
434
Stephen Hines651f13c2014-04-23 16:59:28 -0700435 // Recover from the common mistake of using 'set' instead of 'put'.
John McCall76da55d2013-04-16 07:28:30 +0000436 } else if (KindStr == "set") {
437 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
Stephen Hines651f13c2014-04-23 16:59:28 -0700438 << FixItHint::CreateReplacement(KindLoc, "put");
John McCall76da55d2013-04-16 07:28:30 +0000439 Kind = AK_Put;
440
Stephen Hines651f13c2014-04-23 16:59:28 -0700441 // Handle the mistake of forgetting the accessor kind by skipping
442 // this accessor.
John McCall76da55d2013-04-16 07:28:30 +0000443 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
444 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
445 ConsumeToken();
446 HasInvalidAccessor = true;
447 goto next_property_accessor;
448
Stephen Hines651f13c2014-04-23 16:59:28 -0700449 // Otherwise, complain about the unknown accessor kind.
John McCall76da55d2013-04-16 07:28:30 +0000450 } else {
451 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
452 HasInvalidAccessor = true;
453 Kind = AK_Invalid;
454
455 // Try to keep parsing unless it doesn't look like an accessor spec.
Stephen Hines651f13c2014-04-23 16:59:28 -0700456 if (!NextToken().is(tok::equal))
457 break;
John McCall76da55d2013-04-16 07:28:30 +0000458 }
459
460 // Consume the identifier.
461 ConsumeToken();
462
463 // Consume the '='.
Stephen Hines651f13c2014-04-23 16:59:28 -0700464 if (!TryConsumeToken(tok::equal)) {
John McCall76da55d2013-04-16 07:28:30 +0000465 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
Stephen Hines651f13c2014-04-23 16:59:28 -0700466 << KindStr;
John McCall76da55d2013-04-16 07:28:30 +0000467 break;
468 }
469
470 // Expect the method name.
471 if (!Tok.is(tok::identifier)) {
472 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
473 break;
474 }
475
476 if (Kind == AK_Invalid) {
477 // Just drop invalid accessors.
478 } else if (AccessorNames[Kind] != NULL) {
479 // Complain about the repeated accessor, ignore it, and keep parsing.
480 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
481 } else {
482 AccessorNames[Kind] = Tok.getIdentifierInfo();
483 }
484 ConsumeToken();
485
486 next_property_accessor:
487 // Keep processing accessors until we run out.
Stephen Hines651f13c2014-04-23 16:59:28 -0700488 if (TryConsumeToken(tok::comma))
John McCall76da55d2013-04-16 07:28:30 +0000489 continue;
490
491 // If we run into the ')', stop without consuming it.
Stephen Hines651f13c2014-04-23 16:59:28 -0700492 if (Tok.is(tok::r_paren))
John McCall76da55d2013-04-16 07:28:30 +0000493 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700494
495 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
496 break;
John McCall76da55d2013-04-16 07:28:30 +0000497 }
498
499 // Only add the property attribute if it was well-formed.
Stephen Hines651f13c2014-04-23 16:59:28 -0700500 if (!HasInvalidAccessor)
501 Attrs.addNewPropertyAttr(AttrName, AttrNameLoc, 0, SourceLocation(),
John McCall76da55d2013-04-16 07:28:30 +0000502 AccessorNames[AK_Get], AccessorNames[AK_Put],
503 AttributeList::AS_Declspec);
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000504 T.skipToEnd();
Stephen Hines651f13c2014-04-23 16:59:28 -0700505 return !HasInvalidAccessor;
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000506 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700507
508 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, nullptr, nullptr,
509 SourceLocation(), AttributeList::AS_Declspec);
510 return true;
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000511}
512
Eli Friedmana23b4852009-06-08 07:21:15 +0000513/// [MS] decl-specifier:
514/// __declspec ( extended-decl-modifier-seq )
515///
516/// [MS] extended-decl-modifier-seq:
517/// extended-decl-modifier[opt]
518/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000519void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000520 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000521
Steve Narofff59e17e2008-12-24 20:59:21 +0000522 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000523 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000524 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000525 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000526 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000527
Chad Rosier8decdee2012-06-26 22:30:43 +0000528 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000529 // you can specify multiple attributes per declspec.
Stephen Hines651f13c2014-04-23 16:59:28 -0700530 while (Tok.isNot(tok::r_paren)) {
531 // Attribute not present.
532 if (TryConsumeToken(tok::comma))
533 continue;
534
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000535 // We expect either a well-known identifier or a generic string. Anything
536 // else is a malformed declspec.
537 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000538 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000539 Tok.getKind() != tok::kw_restrict) {
540 Diag(Tok, diag::err_ms_declspec_type);
541 T.skipToEnd();
542 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000543 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000544
545 IdentifierInfo *AttrName;
546 SourceLocation AttrNameLoc;
547 if (IsString) {
548 SmallString<8> StrBuffer;
549 bool Invalid = false;
550 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
551 if (Invalid) {
552 T.skipToEnd();
553 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000554 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000555 AttrName = PP.getIdentifierInfo(Str);
556 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000557 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000558 AttrName = Tok.getIdentifierInfo();
559 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000560 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000561
Stephen Hines651f13c2014-04-23 16:59:28 -0700562 bool AttrHandled = false;
563
564 // Parse attribute arguments.
565 if (Tok.is(tok::l_paren))
566 AttrHandled = ParseMicrosoftDeclSpecArgs(AttrName, AttrNameLoc, Attrs);
567 else if (AttrName->getName() == "property")
568 // The property attribute must have an argument list.
569 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
570 << AttrName->getName();
571
572 if (!AttrHandled)
Aaron Ballman624421f2013-08-31 01:11:41 +0000573 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
574 AttributeList::AS_Declspec);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000575 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000576 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000577}
578
John McCall7f040a92010-12-24 02:08:15 +0000579void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000580 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000581 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000582 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000583 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Aaron Ballmanaa9df092013-05-22 23:25:32 +0000584 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) ||
585 Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000586 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
587 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman624421f2013-08-31 01:11:41 +0000588 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
589 AttributeList::AS_Keyword);
Eli Friedman290eeb02009-06-08 23:27:34 +0000590 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000591}
592
John McCall7f040a92010-12-24 02:08:15 +0000593void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000594 // Treat these like attributes
595 while (Tok.is(tok::kw___pascal)) {
596 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
597 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman624421f2013-08-31 01:11:41 +0000598 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
599 AttributeList::AS_Keyword);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000600 }
John McCall7f040a92010-12-24 02:08:15 +0000601}
602
Peter Collingbournef315fa82011-02-14 01:42:53 +0000603void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
604 // Treat these like attributes
605 while (Tok.is(tok::kw___kernel)) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000606 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbournef315fa82011-02-14 01:42:53 +0000607 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman624421f2013-08-31 01:11:41 +0000608 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
609 AttributeList::AS_Keyword);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000610 }
611}
612
Stephen Hines651f13c2014-04-23 16:59:28 -0700613void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) {
614 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
615 SourceLocation AttrNameLoc = Tok.getLocation();
616 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
617 AttributeList::AS_Keyword);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000618}
619
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000620/// \brief Parse a version number.
621///
622/// version:
623/// simple-integer
624/// simple-integer ',' simple-integer
625/// simple-integer ',' simple-integer ',' simple-integer
626VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
627 Range = Tok.getLocation();
628
629 if (!Tok.is(tok::numeric_constant)) {
630 Diag(Tok, diag::err_expected_version);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000631 SkipUntil(tok::comma, tok::r_paren,
632 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000633 return VersionTuple();
634 }
635
636 // Parse the major (and possibly minor and subminor) versions, which
637 // are stored in the numeric constant. We utilize a quirk of the
638 // lexer, which is that it handles something like 1.2.3 as a single
639 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000640 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000641 Buffer.resize(Tok.getLength()+1);
642 const char *ThisTokBegin = &Buffer[0];
643
644 // Get the spelling of the token, which eliminates trigraphs, etc.
645 bool Invalid = false;
646 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
647 if (Invalid)
648 return VersionTuple();
649
650 // Parse the major version.
651 unsigned AfterMajor = 0;
652 unsigned Major = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000653 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000654 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
655 ++AfterMajor;
656 }
657
658 if (AfterMajor == 0) {
659 Diag(Tok, diag::err_expected_version);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000660 SkipUntil(tok::comma, tok::r_paren,
661 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000662 return VersionTuple();
663 }
664
665 if (AfterMajor == ActualLength) {
666 ConsumeToken();
667
668 // We only had a single version component.
669 if (Major == 0) {
670 Diag(Tok, diag::err_zero_version);
671 return VersionTuple();
672 }
673
674 return VersionTuple(Major);
675 }
676
677 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
678 Diag(Tok, diag::err_expected_version);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000679 SkipUntil(tok::comma, tok::r_paren,
680 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000681 return VersionTuple();
682 }
683
684 // Parse the minor version.
685 unsigned AfterMinor = AfterMajor + 1;
686 unsigned Minor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000687 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000688 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
689 ++AfterMinor;
690 }
691
692 if (AfterMinor == ActualLength) {
693 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000694
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000695 // We had major.minor.
696 if (Major == 0 && Minor == 0) {
697 Diag(Tok, diag::err_zero_version);
698 return VersionTuple();
699 }
700
Chad Rosier8decdee2012-06-26 22:30:43 +0000701 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000702 }
703
704 // If what follows is not a '.', we have a problem.
705 if (ThisTokBegin[AfterMinor] != '.') {
706 Diag(Tok, diag::err_expected_version);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000707 SkipUntil(tok::comma, tok::r_paren,
708 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Chad Rosier8decdee2012-06-26 22:30:43 +0000709 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000710 }
711
712 // Parse the subminor version.
713 unsigned AfterSubminor = AfterMinor + 1;
714 unsigned Subminor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000715 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000716 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
717 ++AfterSubminor;
718 }
719
720 if (AfterSubminor != ActualLength) {
721 Diag(Tok, diag::err_expected_version);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000722 SkipUntil(tok::comma, tok::r_paren,
723 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000724 return VersionTuple();
725 }
726 ConsumeToken();
727 return VersionTuple(Major, Minor, Subminor);
728}
729
730/// \brief Parse the contents of the "availability" attribute.
731///
732/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000733/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000734///
735/// platform:
736/// identifier
737///
738/// version-arg-list:
739/// version-arg
740/// version-arg ',' version-arg-list
741///
742/// version-arg:
743/// 'introduced' '=' version
744/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000745/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000746/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000747/// opt-message:
748/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000749void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
750 SourceLocation AvailabilityLoc,
751 ParsedAttributes &attrs,
752 SourceLocation *endLoc) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000753 enum { Introduced, Deprecated, Obsoleted, Unknown };
754 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000755 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000756
757 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000758 BalancedDelimiterTracker T(*this, tok::l_paren);
759 if (T.consumeOpen()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700760 Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000761 return;
762 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000763
764 // Parse the platform name,
765 if (Tok.isNot(tok::identifier)) {
766 Diag(Tok, diag::err_availability_expected_platform);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000767 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000768 return;
769 }
Richard Smith8edabd92013-09-03 18:01:40 +0000770 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000771
772 // Parse the ',' following the platform name.
Stephen Hines651f13c2014-04-23 16:59:28 -0700773 if (ExpectAndConsume(tok::comma)) {
774 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000775 return;
Stephen Hines651f13c2014-04-23 16:59:28 -0700776 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000777
778 // If we haven't grabbed the pointers for the identifiers
779 // "introduced", "deprecated", and "obsoleted", do so now.
780 if (!Ident_introduced) {
781 Ident_introduced = PP.getIdentifierInfo("introduced");
782 Ident_deprecated = PP.getIdentifierInfo("deprecated");
783 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000784 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000785 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000786 }
787
788 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000789 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000790 do {
791 if (Tok.isNot(tok::identifier)) {
792 Diag(Tok, diag::err_availability_expected_change);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000793 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000794 return;
795 }
796 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
797 SourceLocation KeywordLoc = ConsumeToken();
798
Douglas Gregorb53e4172011-03-26 03:35:55 +0000799 if (Keyword == Ident_unavailable) {
800 if (UnavailableLoc.isValid()) {
801 Diag(KeywordLoc, diag::err_availability_redundant)
802 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000803 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000804 UnavailableLoc = KeywordLoc;
Douglas Gregorb53e4172011-03-26 03:35:55 +0000805 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000806 }
807
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000808 if (Tok.isNot(tok::equal)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700809 Diag(Tok, diag::err_expected_after) << Keyword << tok::equal;
Alexey Bataev8fe24752013-11-18 08:17:37 +0000810 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000811 return;
812 }
813 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000814 if (Keyword == Ident_message) {
Benjamin Kramerc5617142013-09-13 17:31:48 +0000815 if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals.
Andy Gibbs97f84612012-11-17 19:16:52 +0000816 Diag(Tok, diag::err_expected_string_literal)
817 << /*Source='availability attribute'*/2;
Alexey Bataev8fe24752013-11-18 08:17:37 +0000818 SkipUntil(tok::r_paren, StopAtSemi);
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000819 return;
820 }
821 MessageExpr = ParseStringLiteralExpression();
822 break;
823 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000824
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000825 SourceRange VersionRange;
826 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000827
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000828 if (Version.empty()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000829 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000830 return;
831 }
832
833 unsigned Index;
834 if (Keyword == Ident_introduced)
835 Index = Introduced;
836 else if (Keyword == Ident_deprecated)
837 Index = Deprecated;
838 else if (Keyword == Ident_obsoleted)
839 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000840 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000841 Index = Unknown;
842
843 if (Index < Unknown) {
844 if (!Changes[Index].KeywordLoc.isInvalid()) {
845 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000846 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000847 << SourceRange(Changes[Index].KeywordLoc,
848 Changes[Index].VersionRange.getEnd());
849 }
850
851 Changes[Index].KeywordLoc = KeywordLoc;
852 Changes[Index].Version = Version;
853 Changes[Index].VersionRange = VersionRange;
854 } else {
855 Diag(KeywordLoc, diag::err_availability_unknown_change)
856 << Keyword << VersionRange;
857 }
858
Stephen Hines651f13c2014-04-23 16:59:28 -0700859 } while (TryConsumeToken(tok::comma));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000860
861 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000862 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000863 return;
864
865 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000866 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000867
Douglas Gregorb53e4172011-03-26 03:35:55 +0000868 // The 'unavailable' availability cannot be combined with any other
869 // availability changes. Make sure that hasn't happened.
870 if (UnavailableLoc.isValid()) {
871 bool Complained = false;
872 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
873 if (Changes[Index].KeywordLoc.isValid()) {
874 if (!Complained) {
875 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
876 << SourceRange(Changes[Index].KeywordLoc,
877 Changes[Index].VersionRange.getEnd());
878 Complained = true;
879 }
880
881 // Clear out the availability.
882 Changes[Index] = AvailabilityChange();
883 }
884 }
885 }
886
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000887 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000888 attrs.addNew(&Availability,
889 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000890 0, AvailabilityLoc,
Aaron Ballman624421f2013-08-31 01:11:41 +0000891 Platform,
John McCall0b7e6782011-03-24 11:26:52 +0000892 Changes[Introduced],
893 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000894 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000895 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000896 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000897}
898
Stephen Hines651f13c2014-04-23 16:59:28 -0700899/// \brief Parse the contents of the "objc_bridge_related" attribute.
900/// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
901/// related_class:
902/// Identifier
903///
904/// opt-class_method:
905/// Identifier: | <empty>
906///
907/// opt-instance_method:
908/// Identifier | <empty>
909///
910void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated,
911 SourceLocation ObjCBridgeRelatedLoc,
912 ParsedAttributes &attrs,
913 SourceLocation *endLoc) {
914 // Opening '('.
915 BalancedDelimiterTracker T(*this, tok::l_paren);
916 if (T.consumeOpen()) {
917 Diag(Tok, diag::err_expected) << tok::l_paren;
918 return;
919 }
920
921 // Parse the related class name.
922 if (Tok.isNot(tok::identifier)) {
923 Diag(Tok, diag::err_objcbridge_related_expected_related_class);
924 SkipUntil(tok::r_paren, StopAtSemi);
925 return;
926 }
927 IdentifierLoc *RelatedClass = ParseIdentifierLoc();
928 if (ExpectAndConsume(tok::comma)) {
929 SkipUntil(tok::r_paren, StopAtSemi);
930 return;
931 }
932
933 // Parse optional class method name.
934 IdentifierLoc *ClassMethod = 0;
935 if (Tok.is(tok::identifier)) {
936 ClassMethod = ParseIdentifierLoc();
937 if (!TryConsumeToken(tok::colon)) {
938 Diag(Tok, diag::err_objcbridge_related_selector_name);
939 SkipUntil(tok::r_paren, StopAtSemi);
940 return;
941 }
942 }
943 if (!TryConsumeToken(tok::comma)) {
944 if (Tok.is(tok::colon))
945 Diag(Tok, diag::err_objcbridge_related_selector_name);
946 else
947 Diag(Tok, diag::err_expected) << tok::comma;
948 SkipUntil(tok::r_paren, StopAtSemi);
949 return;
950 }
951
952 // Parse optional instance method name.
953 IdentifierLoc *InstanceMethod = 0;
954 if (Tok.is(tok::identifier))
955 InstanceMethod = ParseIdentifierLoc();
956 else if (Tok.isNot(tok::r_paren)) {
957 Diag(Tok, diag::err_expected) << tok::r_paren;
958 SkipUntil(tok::r_paren, StopAtSemi);
959 return;
960 }
961
962 // Closing ')'.
963 if (T.consumeClose())
964 return;
965
966 if (endLoc)
967 *endLoc = T.getCloseLocation();
968
969 // Record this attribute
970 attrs.addNew(&ObjCBridgeRelated,
971 SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
972 0, ObjCBridgeRelatedLoc,
973 RelatedClass,
974 ClassMethod,
975 InstanceMethod,
976 AttributeList::AS_GNU);
977
978}
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000979
Bill Wendlingad017fa2012-12-20 19:22:21 +0000980// Late Parsed Attributes:
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000981// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
982
983void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
984
985void Parser::LateParsedClass::ParseLexedAttributes() {
986 Self->ParseLexedAttributes(*Class);
987}
988
989void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000990 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000991}
992
993/// Wrapper class which calls ParseLexedAttribute, after setting up the
994/// scope appropriately.
995void Parser::ParseLexedAttributes(ParsingClass &Class) {
996 // Deal with templates
997 // FIXME: Test cases to make sure this does the right thing for templates.
998 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
999 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
1000 HasTemplateScope);
1001 if (HasTemplateScope)
1002 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
1003
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001004 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001005 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001006 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001007 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1008 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1009
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +00001010 // Enter the scope of nested classes
1011 if (!AlreadyHasClassScope)
1012 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1013 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +00001014 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001015 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1016 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1017 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001018 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001019
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +00001020 if (!AlreadyHasClassScope)
1021 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1022 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001023}
1024
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001025
1026/// \brief Parse all attributes in LAs, and attach them to Decl D.
1027void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1028 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +00001029 assert(LAs.parseSoon() &&
1030 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001031 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +00001032 if (D)
1033 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001034 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +00001035 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001036 }
1037 LAs.clear();
1038}
1039
1040
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001041/// \brief Finish parsing an attribute for which parsing was delayed.
1042/// This will be called at the end of parsing a class declaration
1043/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +00001044/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001045/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001046void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1047 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001048 // Save the current token position.
1049 SourceLocation OrigLoc = Tok.getLocation();
1050
1051 // Append the current token at the end of the new token stream so that it
1052 // doesn't get lost.
1053 LA.Toks.push_back(Tok);
1054 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1055 // Consume the previously pushed token.
Argyrios Kyrtzidisab2d09b2013-03-27 23:58:17 +00001056 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001057
1058 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,
Stephen Hines651f13c2014-04-23 16:59:28 -07001084 0, SourceLocation(), AttributeList::AS_GNU, 0);
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,
Stephen Hines651f13c2014-04-23 16:59:28 -07001097 0, SourceLocation(), AttributeList::AS_GNU, 0);
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
Stephen Hines651f13c2014-04-23 16:59:28 -07001103 const AttributeList *AL = Attrs.getList();
1104 if (OnDefinition && AL && !AL->isCXX11Attribute() &&
1105 AL->isKnownToGCC())
1106 Diag(Tok, diag::warn_attribute_on_function_definition)
1107 << &LA.AttrName;
1108
1109 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i)
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00001110 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001111
1112 if (Tok.getLocation() != OrigLoc) {
1113 // Due to a parsing error, we either went over the cached tokens or
1114 // there are still cached tokens left, so we skip the leftover tokens.
1115 // Since this is an uncommon situation that should be avoided, use the
1116 // expensive isBeforeInTranslationUnit call.
1117 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1118 OrigLoc))
1119 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +00001120 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001121 }
1122}
1123
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001124void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1125 SourceLocation AttrNameLoc,
1126 ParsedAttributes &Attrs,
1127 SourceLocation *EndLoc) {
1128 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1129
1130 BalancedDelimiterTracker T(*this, tok::l_paren);
1131 T.consumeOpen();
1132
1133 if (Tok.isNot(tok::identifier)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001134 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001135 T.skipToEnd();
1136 return;
1137 }
Richard Smith8edabd92013-09-03 18:01:40 +00001138 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001139
Stephen Hines651f13c2014-04-23 16:59:28 -07001140 if (ExpectAndConsume(tok::comma)) {
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001141 T.skipToEnd();
1142 return;
1143 }
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001144
1145 SourceRange MatchingCTypeRange;
1146 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1147 if (MatchingCType.isInvalid()) {
1148 T.skipToEnd();
1149 return;
1150 }
1151
1152 bool LayoutCompatible = false;
1153 bool MustBeNull = false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001154 while (TryConsumeToken(tok::comma)) {
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001155 if (Tok.isNot(tok::identifier)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001156 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001157 T.skipToEnd();
1158 return;
1159 }
1160 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1161 if (Flag->isStr("layout_compatible"))
1162 LayoutCompatible = true;
1163 else if (Flag->isStr("must_be_null"))
1164 MustBeNull = true;
1165 else {
1166 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1167 T.skipToEnd();
1168 return;
1169 }
1170 ConsumeToken(); // consume flag
1171 }
1172
1173 if (!T.consumeClose()) {
1174 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
Aaron Ballman624421f2013-08-31 01:11:41 +00001175 ArgumentKind, MatchingCType.release(),
1176 LayoutCompatible, MustBeNull,
1177 AttributeList::AS_GNU);
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001178 }
1179
1180 if (EndLoc)
1181 *EndLoc = T.getCloseLocation();
1182}
1183
Richard Smith6ee326a2012-04-10 01:32:12 +00001184/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1185/// of a C++11 attribute-specifier in a location where an attribute is not
1186/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1187/// situation.
1188///
1189/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1190/// this doesn't appear to actually be an attribute-specifier, and the caller
1191/// should try to parse it.
1192bool Parser::DiagnoseProhibitedCXX11Attribute() {
1193 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1194
1195 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1196 case CAK_NotAttributeSpecifier:
1197 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1198 return false;
1199
1200 case CAK_InvalidAttributeSpecifier:
1201 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1202 return false;
1203
1204 case CAK_AttributeSpecifier:
1205 // Parse and discard the attributes.
1206 SourceLocation BeginLoc = ConsumeBracket();
1207 ConsumeBracket();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001208 SkipUntil(tok::r_square);
Richard Smith6ee326a2012-04-10 01:32:12 +00001209 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1210 SourceLocation EndLoc = ConsumeBracket();
1211 Diag(BeginLoc, diag::err_attributes_not_allowed)
1212 << SourceRange(BeginLoc, EndLoc);
1213 return true;
1214 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001215 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001216}
1217
Richard Smith975d52c2013-02-20 01:17:14 +00001218/// \brief We have found the opening square brackets of a C++11
1219/// attribute-specifier in a location where an attribute is not permitted, but
1220/// we know where the attributes ought to be written. Parse them anyway, and
1221/// provide a fixit moving them to the right place.
Richard Smith05321402013-02-19 23:47:15 +00001222void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1223 SourceLocation CorrectLocation) {
1224 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1225 Tok.is(tok::kw_alignas));
1226
1227 // Consume the attributes.
1228 SourceLocation Loc = Tok.getLocation();
1229 ParseCXX11Attributes(Attrs);
1230 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1231
1232 Diag(Loc, diag::err_attributes_not_allowed)
1233 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1234 << FixItHint::CreateRemoval(AttrRange);
1235}
1236
John McCall7f040a92010-12-24 02:08:15 +00001237void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1238 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1239 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001240}
1241
Michael Hanf64231e2012-11-06 19:34:54 +00001242void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1243 AttributeList *AttrList = attrs.getList();
1244 while (AttrList) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001245 if (AttrList->isCXX11Attribute()) {
Richard Smithd03de6a2013-01-29 10:02:16 +00001246 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Hanf64231e2012-11-06 19:34:54 +00001247 << AttrList->getName();
1248 AttrList->setInvalid();
1249 }
1250 AttrList = AttrList->getNext();
1251 }
1252}
1253
Reid Spencer5f016e22007-07-11 17:01:13 +00001254/// ParseDeclaration - Parse a full 'declaration', which consists of
1255/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001256/// 'Context' should be a Declarator::TheContext value. This returns the
1257/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001258///
1259/// declaration: [C99 6.7]
1260/// block-declaration ->
1261/// simple-declaration
1262/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001263/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001264/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001265/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001266/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001267/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001268/// others... [FIXME]
1269///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001270Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1271 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001272 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001273 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001274 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001275 // Must temporarily exit the objective-c container scope for
1276 // parsing c none objective-c decls.
1277 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001278
John McCalld226f652010-08-21 09:40:31 +00001279 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001280 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001281 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001282 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001283 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001284 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001285 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001286 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001287 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001288 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001289 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001290 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001291 SourceLocation InlineLoc = ConsumeToken();
1292 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1293 break;
1294 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001295 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001296 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001297 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001298 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001299 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001300 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001301 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001302 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001303 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001304 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001305 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001306 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001307 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001308 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001309 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001310 default:
John McCall7f040a92010-12-24 02:08:15 +00001311 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001312 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001313
Chris Lattner682bf922009-03-29 16:50:03 +00001314 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001315 // single decl, convert it now. Alias declarations can also declare a type;
1316 // include that too if it is present.
1317 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001318}
1319
1320/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1321/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001322/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1323/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001324///[C90/C++]init-declarator-list ';' [TODO]
1325/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001326///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001327/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001328/// attribute-specifier-seq[opt] type-specifier-seq declarator
1329///
Chris Lattnercd147752009-03-29 17:27:48 +00001330/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001331/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001332///
1333/// If FRI is non-null, we might be parsing a for-range-declaration instead
1334/// of a simple-declaration. If we find that we are, we also parse the
1335/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001336Parser::DeclGroupPtrTy
1337Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1338 SourceLocation &DeclEnd,
Richard Smith68ea3ae2013-02-22 09:06:26 +00001339 ParsedAttributesWithRange &Attrs,
Sean Hunt2edf0a22012-06-23 05:07:58 +00001340 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001342 ParsingDeclSpec DS(*this);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001343
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00001344 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
1345 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
1346
1347 // If we had a free-standing type definition with a missing semicolon, we
1348 // may get this far before the problem becomes obvious.
1349 if (DS.hasTagDefinition() &&
1350 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
1351 return DeclGroupPtrTy();
Abramo Bagnara06284c12012-01-07 10:52:36 +00001352
Reid Spencer5f016e22007-07-11 17:01:13 +00001353 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1354 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001355 if (Tok.is(tok::semi)) {
Richard Smith68ea3ae2013-02-22 09:06:26 +00001356 ProhibitAttributes(Attrs);
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001357 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001358 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001359 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001360 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001361 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001362 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001363 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001364
Richard Smith68ea3ae2013-02-22 09:06:26 +00001365 DS.takeAttributesFrom(Attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00001366 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001367}
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Richard Smith0706df42011-10-19 21:33:05 +00001369/// Returns true if this might be the start of a declarator, or a common typo
1370/// for a declarator.
1371bool Parser::MightBeDeclarator(unsigned Context) {
1372 switch (Tok.getKind()) {
1373 case tok::annot_cxxscope:
1374 case tok::annot_template_id:
1375 case tok::caret:
1376 case tok::code_completion:
1377 case tok::coloncolon:
1378 case tok::ellipsis:
1379 case tok::kw___attribute:
1380 case tok::kw_operator:
1381 case tok::l_paren:
1382 case tok::star:
1383 return true;
1384
1385 case tok::amp:
1386 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001387 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001388
Richard Smith1c94c162012-01-09 22:31:44 +00001389 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001390 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001391 NextToken().is(tok::l_square);
1392
1393 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001394 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001395
Richard Smith0706df42011-10-19 21:33:05 +00001396 case tok::identifier:
1397 switch (NextToken().getKind()) {
1398 case tok::code_completion:
1399 case tok::coloncolon:
1400 case tok::comma:
1401 case tok::equal:
1402 case tok::equalequal: // Might be a typo for '='.
1403 case tok::kw_alignas:
1404 case tok::kw_asm:
1405 case tok::kw___attribute:
1406 case tok::l_brace:
1407 case tok::l_paren:
1408 case tok::l_square:
1409 case tok::less:
1410 case tok::r_brace:
1411 case tok::r_paren:
1412 case tok::r_square:
1413 case tok::semi:
1414 return true;
1415
1416 case tok::colon:
1417 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001418 // and in block scope it's probably a label. Inside a class definition,
1419 // this is a bit-field.
1420 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001421 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001422
1423 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001424 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001425
1426 default:
1427 return false;
1428 }
1429
1430 default:
1431 return false;
1432 }
1433}
1434
Richard Smith994d73f2012-04-11 20:59:20 +00001435/// Skip until we reach something which seems like a sensible place to pick
1436/// up parsing after a malformed declaration. This will sometimes stop sooner
1437/// than SkipUntil(tok::r_brace) would, but will never stop later.
1438void Parser::SkipMalformedDecl() {
1439 while (true) {
1440 switch (Tok.getKind()) {
1441 case tok::l_brace:
1442 // Skip until matching }, then stop. We've probably skipped over
1443 // a malformed class or function definition or similar.
1444 ConsumeBrace();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001445 SkipUntil(tok::r_brace);
Richard Smith994d73f2012-04-11 20:59:20 +00001446 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1447 // This declaration isn't over yet. Keep skipping.
1448 continue;
1449 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001450 TryConsumeToken(tok::semi);
Richard Smith994d73f2012-04-11 20:59:20 +00001451 return;
1452
1453 case tok::l_square:
1454 ConsumeBracket();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001455 SkipUntil(tok::r_square);
Richard Smith994d73f2012-04-11 20:59:20 +00001456 continue;
1457
1458 case tok::l_paren:
1459 ConsumeParen();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001460 SkipUntil(tok::r_paren);
Richard Smith994d73f2012-04-11 20:59:20 +00001461 continue;
1462
1463 case tok::r_brace:
1464 return;
1465
1466 case tok::semi:
1467 ConsumeToken();
1468 return;
1469
1470 case tok::kw_inline:
1471 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001472 // a good place to pick back up parsing, except in an Objective-C
1473 // @interface context.
1474 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1475 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001476 return;
1477 break;
1478
1479 case tok::kw_namespace:
1480 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001481 // place to pick back up parsing, except in an Objective-C
1482 // @interface context.
1483 if (Tok.isAtStartOfLine() &&
1484 (!ParsingInObjCContainer || CurParsedObjCImpl))
1485 return;
1486 break;
1487
1488 case tok::at:
1489 // @end is very much like } in Objective-C contexts.
1490 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1491 ParsingInObjCContainer)
1492 return;
1493 break;
1494
1495 case tok::minus:
1496 case tok::plus:
1497 // - and + probably start new method declarations in Objective-C contexts.
1498 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001499 return;
1500 break;
1501
1502 case tok::eof:
Stephen Hines651f13c2014-04-23 16:59:28 -07001503 case tok::annot_module_begin:
1504 case tok::annot_module_end:
1505 case tok::annot_module_include:
Richard Smith994d73f2012-04-11 20:59:20 +00001506 return;
1507
1508 default:
1509 break;
1510 }
1511
1512 ConsumeAnyToken();
1513 }
1514}
1515
John McCalld8ac0572009-11-03 19:26:08 +00001516/// ParseDeclGroup - Having concluded that this is either a function
1517/// definition or a group of object declarations, actually parse the
1518/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001519Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1520 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001521 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001522 SourceLocation *DeclEnd,
1523 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001524 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001525 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001526 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001527
John McCalld8ac0572009-11-03 19:26:08 +00001528 // Bail out if the first declarator didn't seem well-formed.
1529 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001530 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001531 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001532 }
Mike Stump1eb44332009-09-09 15:08:12 +00001533
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001534 // Save late-parsed attributes for now; they need to be parsed in the
1535 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001536 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1537 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001538 if (D.isFunctionDeclarator())
1539 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1540
Chris Lattnerc82daef2010-07-11 22:24:20 +00001541 // Check to see if we have a function *definition* which must have a body.
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001542 if (D.isFunctionDeclarator() &&
Chris Lattnerc82daef2010-07-11 22:24:20 +00001543 // Look at the next token to make sure that this isn't a function
1544 // declaration. We have to check this because __attribute__ might be the
1545 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001546 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001547
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001548 if (AllowFunctionDefinitions) {
1549 if (isStartOfFunctionDefinition(D)) {
1550 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1551 Diag(Tok, diag::err_function_declared_typedef);
John McCalld8ac0572009-11-03 19:26:08 +00001552
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001553 // Recover by treating the 'typedef' as spurious.
1554 DS.ClearStorageClassSpecs();
1555 }
1556
1557 Decl *TheDecl =
1558 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1559 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld8ac0572009-11-03 19:26:08 +00001560 }
1561
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001562 if (isDeclarationSpecifier()) {
1563 // If there is an invalid declaration specifier right after the function
1564 // prototype, then we must be in a missing semicolon case where this isn't
1565 // actually a body. Just fall through into the code that handles it as a
1566 // prototype, and let the top-level code handle the erroneous declspec
1567 // where it would otherwise expect a comma or semicolon.
1568 } else {
1569 Diag(Tok, diag::err_expected_fn_body);
1570 SkipUntil(tok::semi);
1571 return DeclGroupPtrTy();
1572 }
John McCalld8ac0572009-11-03 19:26:08 +00001573 } else {
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001574 if (Tok.is(tok::l_brace)) {
1575 Diag(Tok, diag::err_function_definition_not_allowed);
Stephen Hines651f13c2014-04-23 16:59:28 -07001576 SkipMalformedDecl();
1577 return DeclGroupPtrTy();
Douglas Gregorb004a8e2013-04-16 16:01:32 +00001578 }
John McCalld8ac0572009-11-03 19:26:08 +00001579 }
1580 }
1581
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001582 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001583 return DeclGroupPtrTy();
1584
1585 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1586 // must parse and analyze the for-range-initializer before the declaration is
1587 // analyzed.
Douglas Gregor12849d02013-04-08 20:52:24 +00001588 //
1589 // Handle the Objective-C for-in loop variable similarly, although we
1590 // don't need to parse the container in advance.
1591 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1592 bool IsForRangeLoop = false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001593 if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
Douglas Gregor12849d02013-04-08 20:52:24 +00001594 IsForRangeLoop = true;
Douglas Gregor12849d02013-04-08 20:52:24 +00001595 if (Tok.is(tok::l_brace))
1596 FRI->RangeExpr = ParseBraceInitializer();
1597 else
1598 FRI->RangeExpr = ParseExpression();
1599 }
1600
Richard Smithad762fc2011-04-14 22:09:26 +00001601 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor12849d02013-04-08 20:52:24 +00001602 if (IsForRangeLoop)
1603 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001604 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001605 D.complete(ThisDecl);
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001606 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001607 }
1608
Chris Lattner5f9e2722011-07-23 10:55:15 +00001609 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001610 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001611 if (LateParsedAttrs.size() > 0)
1612 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001613 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001614 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001615 DeclsInGroup.push_back(FirstDecl);
1616
Richard Smith0706df42011-10-19 21:33:05 +00001617 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001618
John McCalld8ac0572009-11-03 19:26:08 +00001619 // If we don't have a comma, it is either the end of the list (a ';') or an
1620 // error, bail out.
Stephen Hines651f13c2014-04-23 16:59:28 -07001621 SourceLocation CommaLoc;
1622 while (TryConsumeToken(tok::comma, CommaLoc)) {
Richard Smith0706df42011-10-19 21:33:05 +00001623 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1624 // This comma was followed by a line-break and something which can't be
1625 // the start of a declarator. The comma was probably a typo for a
1626 // semicolon.
1627 Diag(CommaLoc, diag::err_expected_semi_declaration)
1628 << FixItHint::CreateReplacement(CommaLoc, ";");
1629 ExpectSemi = false;
1630 break;
1631 }
John McCalld8ac0572009-11-03 19:26:08 +00001632
1633 // Parse the next declarator.
1634 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001635 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001636
1637 // Accept attributes in an init-declarator. In the first declarator in a
1638 // declaration, these would be part of the declspec. In subsequent
1639 // declarators, they become part of the declarator itself, so that they
1640 // don't apply to declarators after *this* one. Examples:
1641 // short __attribute__((common)) var; -> declspec
1642 // short var __attribute__((common)); -> declarator
1643 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001644 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001645
1646 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001647 if (!D.isInvalidType()) {
1648 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1649 D.complete(ThisDecl);
1650 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001651 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001652 }
John McCalld8ac0572009-11-03 19:26:08 +00001653 }
1654
1655 if (DeclEnd)
1656 *DeclEnd = Tok.getLocation();
1657
Richard Smith0706df42011-10-19 21:33:05 +00001658 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001659 ExpectAndConsumeSemi(Context == Declarator::FileContext
1660 ? diag::err_invalid_token_after_toplevel_declarator
1661 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001662 // Okay, there was no semicolon and one was expected. If we see a
1663 // declaration specifier, just assume it was missing and continue parsing.
1664 // Otherwise things are very confused and we skip to recover.
1665 if (!isDeclarationSpecifier()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00001666 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Stephen Hines651f13c2014-04-23 16:59:28 -07001667 TryConsumeToken(tok::semi);
Chris Lattner004659a2010-07-11 22:42:07 +00001668 }
John McCalld8ac0572009-11-03 19:26:08 +00001669 }
1670
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001671 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +00001672}
1673
Richard Smithad762fc2011-04-14 22:09:26 +00001674/// Parse an optional simple-asm-expr and attributes, and attach them to a
1675/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001676bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001677 // If a simple-asm-expr is present, parse it.
1678 if (Tok.is(tok::kw_asm)) {
1679 SourceLocation Loc;
1680 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1681 if (AsmLabel.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00001682 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smithad762fc2011-04-14 22:09:26 +00001683 return true;
1684 }
1685
1686 D.setAsmLabel(AsmLabel.release());
1687 D.SetRangeEnd(Loc);
1688 }
1689
1690 MaybeParseGNUAttributes(D);
1691 return false;
1692}
1693
Douglas Gregor1426e532009-05-12 21:31:51 +00001694/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1695/// declarator'. This method parses the remainder of the declaration
1696/// (including any attributes or initializer, among other things) and
1697/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001698///
Reid Spencer5f016e22007-07-11 17:01:13 +00001699/// init-declarator: [C99 6.7]
1700/// declarator
1701/// declarator '=' initializer
1702/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1703/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001704/// [C++] declarator initializer[opt]
1705///
1706/// [C++] initializer:
1707/// [C++] '=' initializer-clause
1708/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001709/// [C++0x] '=' 'default' [TODO]
1710/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001711/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001712///
1713/// According to the standard grammar, =default and =delete are function
1714/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001715///
John McCalld226f652010-08-21 09:40:31 +00001716Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001717 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001718 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001719 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Richard Smithad762fc2011-04-14 22:09:26 +00001721 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1722}
Mike Stump1eb44332009-09-09 15:08:12 +00001723
Richard Smithad762fc2011-04-14 22:09:26 +00001724Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1725 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001726 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001727 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001728 switch (TemplateInfo.Kind) {
1729 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001730 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001731 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001732
Douglas Gregord5a423b2009-09-25 18:43:00 +00001733 case ParsedTemplateInfo::Template:
Larisse Voufoef4579c2013-08-06 01:03:05 +00001734 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001735 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001736 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001737 D);
Larisse Voufo25218132013-08-06 07:33:00 +00001738 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufoef4579c2013-08-06 01:03:05 +00001739 // Re-direct this decl to refer to the templated decl so that we can
1740 // initialize it.
1741 ThisDecl = VT->getTemplatedDecl();
1742 break;
1743 }
1744 case ParsedTemplateInfo::ExplicitInstantiation: {
1745 if (Tok.is(tok::semi)) {
1746 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1747 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1748 if (ThisRes.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00001749 SkipUntil(tok::semi, StopBeforeMatch);
Larisse Voufoef4579c2013-08-06 01:03:05 +00001750 return 0;
1751 }
1752 ThisDecl = ThisRes.get();
1753 } else {
1754 // FIXME: This check should be for a variable template instantiation only.
1755
1756 // Check that this is a valid instantiation
1757 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1758 // If the declarator-id is not a template-id, issue a diagnostic and
1759 // recover by ignoring the 'template' keyword.
1760 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1761 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1762 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1763 } else {
1764 SourceLocation LAngleLoc =
1765 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1766 Diag(D.getIdentifierLoc(),
1767 diag::err_explicit_instantiation_with_definition)
1768 << SourceRange(TemplateInfo.TemplateLoc)
1769 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1770
1771 // Recover as if it were an explicit specialization.
1772 TemplateParameterLists FakedParamLists;
1773 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1774 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1775 LAngleLoc));
1776
1777 ThisDecl =
1778 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1779 }
1780 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00001781 break;
1782 }
1783 }
Mike Stump1eb44332009-09-09 15:08:12 +00001784
Richard Smitha2c36462013-04-26 16:15:35 +00001785 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith34b41d92011-02-20 03:19:35 +00001786
Douglas Gregor1426e532009-05-12 21:31:51 +00001787 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001788 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001789 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001790 ConsumeToken();
Larisse Voufoef4579c2013-08-06 01:03:05 +00001791
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001792 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001793 if (D.isFunctionDeclarator())
1794 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1795 << 1 /* delete */;
1796 else
1797 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001798 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001799 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001800 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1801 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001802 else
1803 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001804 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001805 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001806 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001807 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001808 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001809
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001810 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001811 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001812 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001813 cutOffParsing();
1814 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001815 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001816
John McCall60d7b3a2010-08-24 06:29:42 +00001817 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001818
David Blaikie4e4d0842012-03-11 07:00:24 +00001819 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001820 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001821 ExitScope();
1822 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001823
Douglas Gregor1426e532009-05-12 21:31:51 +00001824 if (Init.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00001825 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor00225542010-03-01 18:27:54 +00001826 Actions.ActOnInitializerError(ThisDecl);
1827 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001828 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1829 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001830 }
1831 } else if (Tok.is(tok::l_paren)) {
1832 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001833 BalancedDelimiterTracker T(*this, tok::l_paren);
1834 T.consumeOpen();
1835
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001836 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001837 CommaLocsTy CommaLocs;
1838
David Blaikie4e4d0842012-03-11 07:00:24 +00001839 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001840 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001841 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001842 }
1843
Douglas Gregor1426e532009-05-12 21:31:51 +00001844 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001845 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataev8fe24752013-11-18 08:17:37 +00001846 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001847
David Blaikie4e4d0842012-03-11 07:00:24 +00001848 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001849 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001850 ExitScope();
1851 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001852 } else {
1853 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001854 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001855
1856 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1857 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001858
David Blaikie4e4d0842012-03-11 07:00:24 +00001859 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001860 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001861 ExitScope();
1862 }
1863
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001864 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1865 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001866 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001867 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1868 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001869 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001870 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001871 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001872 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001873 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1874
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001875 if (D.getCXXScopeSpec().isSet()) {
1876 EnterScope(0);
1877 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1878 }
1879
1880 ExprResult Init(ParseBraceInitializer());
1881
1882 if (D.getCXXScopeSpec().isSet()) {
1883 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1884 ExitScope();
1885 }
1886
1887 if (Init.isInvalid()) {
1888 Actions.ActOnInitializerError(ThisDecl);
1889 } else
1890 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1891 /*DirectInit=*/true, TypeContainsAuto);
1892
Douglas Gregor1426e532009-05-12 21:31:51 +00001893 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001894 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001895 }
1896
Richard Smith483b9f32011-02-21 20:05:19 +00001897 Actions.FinalizeDeclaration(ThisDecl);
1898
Douglas Gregor1426e532009-05-12 21:31:51 +00001899 return ThisDecl;
1900}
1901
Reid Spencer5f016e22007-07-11 17:01:13 +00001902/// ParseSpecifierQualifierList
1903/// specifier-qualifier-list:
1904/// type-specifier specifier-qualifier-list[opt]
1905/// type-qualifier specifier-qualifier-list[opt]
1906/// [GNU] attributes specifier-qualifier-list[opt]
1907///
Richard Smith69730c12012-03-12 07:56:15 +00001908void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1909 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001910 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1911 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001912 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001913 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001914
Reid Spencer5f016e22007-07-11 17:01:13 +00001915 // Validate declspec for type-name.
1916 unsigned Specs = DS.getParsedSpecifiers();
Stephen Hines651f13c2014-04-23 16:59:28 -07001917 if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001918 Diag(Tok, diag::err_expected_type);
1919 DS.SetTypeSpecError();
1920 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1921 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001922 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001923 if (!DS.hasTypeSpecifier())
1924 DS.SetTypeSpecError();
1925 }
Mike Stump1eb44332009-09-09 15:08:12 +00001926
Reid Spencer5f016e22007-07-11 17:01:13 +00001927 // Issue diagnostic and remove storage class if present.
1928 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1929 if (DS.getStorageClassSpecLoc().isValid())
1930 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1931 else
Richard Smithec642442013-04-12 22:46:28 +00001932 Diag(DS.getThreadStorageClassSpecLoc(),
1933 diag::err_typename_invalid_storageclass);
Reid Spencer5f016e22007-07-11 17:01:13 +00001934 DS.ClearStorageClassSpecs();
1935 }
Mike Stump1eb44332009-09-09 15:08:12 +00001936
Reid Spencer5f016e22007-07-11 17:01:13 +00001937 // Issue diagnostic and remove function specfier if present.
1938 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001939 if (DS.isInlineSpecified())
1940 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1941 if (DS.isVirtualSpecified())
1942 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1943 if (DS.isExplicitSpecified())
1944 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001945 DS.ClearFunctionSpecs();
1946 }
Richard Smith69730c12012-03-12 07:56:15 +00001947
1948 // Issue diagnostic and remove constexpr specfier if present.
1949 if (DS.isConstexprSpecified()) {
1950 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1951 DS.ClearConstexprSpec();
1952 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001953}
1954
Chris Lattnerc199ab32009-04-12 20:42:31 +00001955/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1956/// specified token is valid after the identifier in a declarator which
1957/// immediately follows the declspec. For example, these things are valid:
1958///
1959/// int x [ 4]; // direct-declarator
1960/// int x ( int y); // direct-declarator
1961/// int(int x ) // direct-declarator
1962/// int x ; // simple-declaration
1963/// int x = 17; // init-declarator-list
1964/// int x , y; // init-declarator-list
1965/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001966/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001967/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001968///
1969/// This is not, because 'x' does not immediately follow the declspec (though
1970/// ')' happens to be valid anyway).
1971/// int (x)
1972///
1973static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1974 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1975 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001976 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001977}
1978
Chris Lattnere40c2952009-04-14 21:34:55 +00001979
1980/// ParseImplicitInt - This method is called when we have an non-typename
1981/// identifier in a declspec (which normally terminates the decl spec) when
1982/// the declspec has no type specifier. In this case, the declspec is either
1983/// malformed or is "implicit int" (in K&R and C89).
1984///
1985/// This method handles diagnosing this prettily and returns false if the
1986/// declspec is done being processed. If it recovers and thinks there may be
1987/// other pieces of declspec after it, it returns true.
1988///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001989bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001990 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00001991 AccessSpecifier AS, DeclSpecContext DSC,
1992 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001993 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001994
Chris Lattnere40c2952009-04-14 21:34:55 +00001995 SourceLocation Loc = Tok.getLocation();
1996 // If we see an identifier that is not a type name, we normally would
1997 // parse it as the identifer being declared. However, when a typename
1998 // is typo'd or the definition is not included, this will incorrectly
1999 // parse the typename as the identifier name and fall over misparsing
2000 // later parts of the diagnostic.
2001 //
2002 // As such, we try to do some look-ahead in cases where this would
2003 // otherwise be an "implicit-int" case to see if this is invalid. For
2004 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2005 // an identifier with implicit int, we'd get a parse error because the
2006 // next token is obviously invalid for a type. Parse these as a case
2007 // with an invalid type specifier.
2008 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00002009
Chris Lattnere40c2952009-04-14 21:34:55 +00002010 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00002011 // error, do lookahead to try to do better recovery. This never applies
2012 // within a type specifier. Outside of C++, we allow this even if the
2013 // language doesn't "officially" support implicit int -- we support
Richard Smith58eb3702013-04-30 22:43:51 +00002014 // implicit int as an extension in C99 and C11.
Stephen Hines651f13c2014-04-23 16:59:28 -07002015 if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus &&
Richard Smith69730c12012-03-12 07:56:15 +00002016 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00002017 // If this token is valid for implicit int, e.g. "static x = 4", then
2018 // we just avoid eating the identifier, so it will be parsed as the
2019 // identifier in the declarator.
2020 return false;
2021 }
Mike Stump1eb44332009-09-09 15:08:12 +00002022
Richard Smith827adaf2012-05-15 21:01:51 +00002023 if (getLangOpts().CPlusPlus &&
2024 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2025 // Don't require a type specifier if we have the 'auto' storage class
2026 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithb79b17b2013-10-15 00:00:26 +00002027 if (SS)
2028 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smith827adaf2012-05-15 21:01:51 +00002029 return false;
2030 }
2031
Chris Lattnere40c2952009-04-14 21:34:55 +00002032 // Otherwise, if we don't consume this token, we are going to emit an
2033 // error anyway. Try to recover from various common problems. Check
2034 // to see if this was a reference to a tag name without a tag specified.
2035 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00002036 //
2037 // C++ doesn't need this, and isTagName doesn't take SS.
2038 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002039 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002040 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00002041
Douglas Gregor23c94db2010-07-02 17:43:08 +00002042 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00002043 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002044 case DeclSpec::TST_enum:
2045 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2046 case DeclSpec::TST_union:
2047 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2048 case DeclSpec::TST_struct:
2049 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00002050 case DeclSpec::TST_interface:
2051 TagName="__interface"; FixitTagName = "__interface ";
2052 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00002053 case DeclSpec::TST_class:
2054 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00002055 }
Mike Stump1eb44332009-09-09 15:08:12 +00002056
Chris Lattnerf4382f52009-04-14 22:17:06 +00002057 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002058 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2059 LookupResult R(Actions, TokenName, SourceLocation(),
2060 Sema::LookupOrdinaryName);
2061
Chris Lattnerf4382f52009-04-14 22:17:06 +00002062 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002063 << TokenName << TagName << getLangOpts().CPlusPlus
2064 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2065
2066 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2067 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2068 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00002069 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00002070 << TokenName << TagName;
2071 }
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Chris Lattnerf4382f52009-04-14 22:17:06 +00002073 // Parse this as a tag as if the missing tag were present.
2074 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00002075 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00002076 else
Richard Smith69730c12012-03-12 07:56:15 +00002077 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00002078 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00002079 return true;
2080 }
Chris Lattnere40c2952009-04-14 21:34:55 +00002081 }
Mike Stump1eb44332009-09-09 15:08:12 +00002082
Richard Smith8f0a7e72012-05-15 21:29:55 +00002083 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00002084 // being declared (with a missing type).
Stephen Hines651f13c2014-04-23 16:59:28 -07002085 if (!isTypeSpecifier(DSC) &&
Richard Smith8f0a7e72012-05-15 21:29:55 +00002086 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00002087 // Look ahead to the next token to try to figure out what this declaration
2088 // was supposed to be.
2089 switch (NextToken().getKind()) {
Richard Smith827adaf2012-05-15 21:01:51 +00002090 case tok::l_paren: {
2091 // static x(4); // 'x' is not a type
2092 // x(int n); // 'x' is not a type
2093 // x (*p)[]; // 'x' is a type
2094 //
2095 // Since we're in an error case (or the rare 'implicit int in C++' MS
2096 // extension), we can afford to perform a tentative parse to determine
2097 // which case we're in.
2098 TentativeParsingAction PA(*this);
2099 ConsumeToken();
2100 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2101 PA.Revert();
Richard Smithb79b17b2013-10-15 00:00:26 +00002102
2103 if (TPR != TPResult::False()) {
2104 // The identifier is followed by a parenthesized declarator.
2105 // It's supposed to be a type.
2106 break;
2107 }
2108
2109 // If we're in a context where we could be declaring a constructor,
2110 // check whether this is a constructor declaration with a bogus name.
2111 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2112 IdentifierInfo *II = Tok.getIdentifierInfo();
2113 if (Actions.isCurrentClassNameTypo(II, SS)) {
2114 Diag(Loc, diag::err_constructor_bad_name)
2115 << Tok.getIdentifierInfo() << II
2116 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2117 Tok.setIdentifierInfo(II);
2118 }
2119 }
2120 // Fall through.
Richard Smith827adaf2012-05-15 21:01:51 +00002121 }
Richard Smithb79b17b2013-10-15 00:00:26 +00002122 case tok::comma:
2123 case tok::equal:
2124 case tok::kw_asm:
2125 case tok::l_brace:
2126 case tok::l_square:
2127 case tok::semi:
2128 // This looks like a variable or function declaration. The type is
2129 // probably missing. We're done parsing decl-specifiers.
2130 if (SS)
2131 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2132 return false;
Richard Smith827adaf2012-05-15 21:01:51 +00002133
2134 default:
2135 // This is probably supposed to be a type. This includes cases like:
2136 // int f(itn);
2137 // struct S { unsinged : 4; };
2138 break;
2139 }
2140 }
2141
Chad Rosier8decdee2012-06-26 22:30:43 +00002142 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00002143 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00002144 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002145 IdentifierInfo *II = Tok.getIdentifierInfo();
Stephen Hines651f13c2014-04-23 16:59:28 -07002146 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T,
2147 getLangOpts().CPlusPlus &&
2148 NextToken().is(tok::less))) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00002149 // The action emitted a diagnostic, so we don't have to.
2150 if (T) {
2151 // The action has suggested that the type T could be used. Set that as
2152 // the type in the declaration specifiers, consume the would-be type
2153 // name token, and we're done.
2154 const char *PrevSpec;
2155 unsigned DiagID;
Stephen Hines651f13c2014-04-23 16:59:28 -07002156 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
2157 Actions.getASTContext().getPrintingPolicy());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002158 DS.SetRangeEnd(Tok.getLocation());
2159 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002160 // There may be other declaration specifiers after this.
2161 return true;
2162 } else if (II != Tok.getIdentifierInfo()) {
2163 // If no type was suggested, the correction is to a keyword
2164 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002165 // There may be other declaration specifiers after this.
2166 return true;
2167 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002168
Douglas Gregora786fdb2009-10-13 23:27:22 +00002169 // Fall through; the action had no suggestion for us.
2170 } else {
2171 // The action did not emit a diagnostic, so emit one now.
2172 SourceRange R;
2173 if (SS) R = SS->getRange();
2174 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2175 }
Mike Stump1eb44332009-09-09 15:08:12 +00002176
Douglas Gregora786fdb2009-10-13 23:27:22 +00002177 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002178 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002179 DS.SetRangeEnd(Tok.getLocation());
2180 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002181
Chris Lattnere40c2952009-04-14 21:34:55 +00002182 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2183 // avoid rippling error messages on subsequent uses of the same type,
2184 // could be useful if #include was forgotten.
2185 return false;
2186}
2187
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002188/// \brief Determine the declaration specifier context from the declarator
2189/// context.
2190///
2191/// \param Context the declarator context, which is one of the
2192/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002193Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002194Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2195 if (Context == Declarator::MemberContext)
2196 return DSC_class;
2197 if (Context == Declarator::FileContext)
2198 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002199 if (Context == Declarator::TrailingReturnContext)
2200 return DSC_trailing;
Stephen Hines651f13c2014-04-23 16:59:28 -07002201 if (Context == Declarator::AliasDeclContext ||
2202 Context == Declarator::AliasTemplateContext)
2203 return DSC_alias_declaration;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002204 return DSC_normal;
2205}
2206
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002207/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2208///
2209/// FIXME: Simply returns an alignof() expression if the argument is a
2210/// type. Ideally, the type should be propagated directly into Sema.
2211///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002212/// [C11] type-id
2213/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002214/// [C++0x] type-id ...[opt]
2215/// [C++0x] assignment-expression ...[opt]
2216ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2217 SourceLocation &EllipsisLoc) {
2218 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002219 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002220 SourceLocation TypeLoc = Tok.getLocation();
2221 ParsedType Ty = ParseTypeName().get();
2222 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002223 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2224 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002225 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002226 ER = ParseConstantExpression();
2227
Stephen Hines651f13c2014-04-23 16:59:28 -07002228 if (getLangOpts().CPlusPlus11)
2229 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002230
2231 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002232}
2233
2234/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2235/// attribute to Attrs.
2236///
2237/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002238/// [C11] '_Alignas' '(' type-id ')'
2239/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002240/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2241/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002242void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smithf6565a92013-02-22 08:32:16 +00002243 SourceLocation *EndLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002244 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2245 "Not an alignment-specifier!");
2246
Richard Smith33f04a22013-01-29 01:48:07 +00002247 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2248 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002249
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002250 BalancedDelimiterTracker T(*this, tok::l_paren);
Stephen Hines651f13c2014-04-23 16:59:28 -07002251 if (T.expectAndConsume())
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002252 return;
2253
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002254 SourceLocation EllipsisLoc;
2255 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002256 if (ArgExpr.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002257 T.skipToEnd();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002258 return;
2259 }
2260
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002261 T.consumeClose();
Richard Smithf6565a92013-02-22 08:32:16 +00002262 if (EndLoc)
2263 *EndLoc = T.getCloseLocation();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002264
Aaron Ballman624421f2013-08-31 01:11:41 +00002265 ArgsVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002266 ArgExprs.push_back(ArgExpr.release());
Aaron Ballman624421f2013-08-31 01:11:41 +00002267 Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1,
2268 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002269}
2270
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002271/// Determine whether we're looking at something that might be a declarator
2272/// in a simple-declaration. If it can't possibly be a declarator, maybe
2273/// diagnose a missing semicolon after a prior tag definition in the decl
2274/// specifier.
2275///
2276/// \return \c true if an error occurred and this can't be any kind of
2277/// declaration.
2278bool
2279Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2280 DeclSpecContext DSContext,
2281 LateParsedAttrList *LateAttrs) {
2282 assert(DS.hasTagDefinition() && "shouldn't call this");
2283
2284 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002285
2286 if (getLangOpts().CPlusPlus &&
2287 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2288 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2289 TryAnnotateCXXScopeToken(EnteringContext)) {
2290 SkipMalformedDecl();
2291 return true;
2292 }
2293
Stephen Hines651f13c2014-04-23 16:59:28 -07002294 bool HasScope = Tok.is(tok::annot_cxxscope);
2295 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2296 Token AfterScope = HasScope ? NextToken() : Tok;
2297
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002298 // Determine whether the following tokens could possibly be a
2299 // declarator.
Stephen Hines651f13c2014-04-23 16:59:28 -07002300 bool MightBeDeclarator = true;
2301 if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2302 // A declarator-id can't start with 'typename'.
2303 MightBeDeclarator = false;
2304 } else if (AfterScope.is(tok::annot_template_id)) {
2305 // If we have a type expressed as a template-id, this cannot be a
2306 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2307 TemplateIdAnnotation *Annot =
2308 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2309 if (Annot->Kind == TNK_Type_template)
2310 MightBeDeclarator = false;
2311 } else if (AfterScope.is(tok::identifier)) {
2312 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2313
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002314 // These tokens cannot come after the declarator-id in a
2315 // simple-declaration, and are likely to come after a type-specifier.
Stephen Hines651f13c2014-04-23 16:59:28 -07002316 if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) ||
2317 Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) ||
2318 Next.is(tok::coloncolon)) {
2319 // Missing a semicolon.
2320 MightBeDeclarator = false;
2321 } else if (HasScope) {
2322 // If the declarator-id has a scope specifier, it must redeclare a
2323 // previously-declared entity. If that's a type (and this is not a
2324 // typedef), that's an error.
2325 CXXScopeSpec SS;
2326 Actions.RestoreNestedNameSpecifierAnnotation(
2327 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2328 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2329 Sema::NameClassification Classification = Actions.ClassifyName(
2330 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2331 /*IsAddressOfOperand*/false);
2332 switch (Classification.getKind()) {
2333 case Sema::NC_Error:
2334 SkipMalformedDecl();
2335 return true;
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002336
Stephen Hines651f13c2014-04-23 16:59:28 -07002337 case Sema::NC_Keyword:
2338 case Sema::NC_NestedNameSpecifier:
2339 llvm_unreachable("typo correction and nested name specifiers not "
2340 "possible here");
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002341
Stephen Hines651f13c2014-04-23 16:59:28 -07002342 case Sema::NC_Type:
2343 case Sema::NC_TypeTemplate:
2344 // Not a previously-declared non-type entity.
2345 MightBeDeclarator = false;
2346 break;
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002347
Stephen Hines651f13c2014-04-23 16:59:28 -07002348 case Sema::NC_Unknown:
2349 case Sema::NC_Expression:
2350 case Sema::NC_VarTemplate:
2351 case Sema::NC_FunctionTemplate:
2352 // Might be a redeclaration of a prior entity.
2353 break;
2354 }
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002355 }
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002356 }
2357
Stephen Hines651f13c2014-04-23 16:59:28 -07002358 if (MightBeDeclarator)
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002359 return false;
2360
Stephen Hines651f13c2014-04-23 16:59:28 -07002361 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002362 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
Stephen Hines651f13c2014-04-23 16:59:28 -07002363 diag::err_expected_after)
2364 << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi;
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002365
2366 // Try to recover from the typo, by dropping the tag definition and parsing
2367 // the problematic tokens as a type.
2368 //
2369 // FIXME: Split the DeclSpec into pieces for the standalone
2370 // declaration and pieces for the following declaration, instead
2371 // of assuming that all the other pieces attach to new declaration,
2372 // and call ParsedFreeStandingDeclSpec as appropriate.
2373 DS.ClearTypeSpecType();
2374 ParsedTemplateInfo NotATemplate;
2375 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2376 return false;
2377}
2378
Reid Spencer5f016e22007-07-11 17:01:13 +00002379/// ParseDeclarationSpecifiers
2380/// declaration-specifiers: [C99 6.7]
2381/// storage-class-specifier declaration-specifiers[opt]
2382/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002383/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002384/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002385/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002386/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002387///
2388/// storage-class-specifier: [C99 6.7.1]
2389/// 'typedef'
2390/// 'extern'
2391/// 'static'
2392/// 'auto'
2393/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002394/// [C++] 'mutable'
Richard Smithec642442013-04-12 22:46:28 +00002395/// [C++11] 'thread_local'
2396/// [C11] '_Thread_local'
Reid Spencer5f016e22007-07-11 17:01:13 +00002397/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002398/// function-specifier: [C99 6.7.4]
2399/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002400/// [C++] 'virtual'
2401/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002402/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002403/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002404/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002405
Reid Spencer5f016e22007-07-11 17:01:13 +00002406///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002407void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002408 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002409 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002410 DeclSpecContext DSContext,
2411 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002412 if (DS.getSourceRange().isInvalid()) {
2413 DS.SetRangeStart(Tok.getLocation());
2414 DS.SetRangeEnd(Tok.getLocation());
2415 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002416
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002417 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002418 bool AttrsLastTime = false;
2419 ParsedAttributesWithRange attrs(AttrFactory);
Stephen Hines651f13c2014-04-23 16:59:28 -07002420 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Reid Spencer5f016e22007-07-11 17:01:13 +00002421 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002422 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002423 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002424 unsigned DiagID = 0;
2425
Reid Spencer5f016e22007-07-11 17:01:13 +00002426 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002427
Reid Spencer5f016e22007-07-11 17:01:13 +00002428 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002429 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002430 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002431 if (!AttrsLastTime)
2432 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002433 else {
2434 // Reject C++11 attributes that appertain to decl specifiers as
2435 // we don't support any C++11 attributes that appertain to decl
2436 // specifiers. This also conforms to what g++ 4.8 is doing.
2437 ProhibitCXX11Attributes(attrs);
2438
Sean Hunt2edf0a22012-06-23 05:07:58 +00002439 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002440 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002441
Reid Spencer5f016e22007-07-11 17:01:13 +00002442 // If this is not a declaration specifier token, we're done reading decl
2443 // specifiers. First verify that DeclSpec's are consistent.
Stephen Hines651f13c2014-04-23 16:59:28 -07002444 DS.Finish(Diags, PP, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00002445 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002446
Sean Hunt2edf0a22012-06-23 05:07:58 +00002447 case tok::l_square:
2448 case tok::kw_alignas:
Richard Smith672edb02013-02-22 09:15:49 +00002449 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Sean Hunt2edf0a22012-06-23 05:07:58 +00002450 goto DoneWithDeclSpec;
2451
2452 ProhibitAttributes(attrs);
2453 // FIXME: It would be good to recover by accepting the attributes,
2454 // but attempting to do that now would cause serious
2455 // madness in terms of diagnostics.
2456 attrs.clear();
2457 attrs.Range = SourceRange();
2458
2459 ParseCXX11Attributes(attrs);
2460 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002461 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002462
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002463 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002464 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002465 if (DS.hasTypeSpecifier()) {
2466 bool AllowNonIdentifiers
2467 = (getCurScope()->getFlags() & (Scope::ControlScope |
2468 Scope::BlockScope |
2469 Scope::TemplateParamScope |
2470 Scope::FunctionPrototypeScope |
2471 Scope::AtCatchScope)) == 0;
2472 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002473 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002474 (DSContext == DSC_class && DS.isFriendSpecified());
2475
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002476 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002477 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002478 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002479 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002480 }
2481
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002482 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2483 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2484 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002485 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002486 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002487 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002488 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002489 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002490 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002491
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002492 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002493 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002494 }
2495
Chris Lattner5e02c472009-01-05 00:07:25 +00002496 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002497 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman5a428202013-08-15 23:59:20 +00002498 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall9ba61662010-02-26 08:45:28 +00002499 if (!DS.hasTypeSpecifier())
2500 DS.SetTypeSpecError();
2501 goto DoneWithDeclSpec;
2502 }
John McCall2e0a7152010-03-01 18:20:46 +00002503 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2504 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002505 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002506
2507 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002508 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002509 goto DoneWithDeclSpec;
2510
John McCallaa87d332009-12-12 11:40:51 +00002511 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002512 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2513 Tok.getAnnotationRange(),
2514 SS);
John McCallaa87d332009-12-12 11:40:51 +00002515
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002516 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002517 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002518 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002519 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002520 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002521 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002522
2523 // C++ [class.qual]p2:
2524 // In a lookup in which the constructor is an acceptable lookup
2525 // result and the nested-name-specifier nominates a class C:
2526 //
2527 // - if the name specified after the
2528 // nested-name-specifier, when looked up in C, is the
2529 // injected-class-name of C (Clause 9), or
2530 //
2531 // - if the name specified after the nested-name-specifier
2532 // is the same as the identifier or the
2533 // simple-template-id's template-name in the last
2534 // component of the nested-name-specifier,
2535 //
2536 // the name is instead considered to name the constructor of
2537 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002538 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002539 // Thus, if the template-name is actually the constructor
2540 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002541 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002542 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002543 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCallba9d8532010-04-13 06:39:49 +00002544 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002545 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002546 if (isConstructorDeclarator(/*Unqualified*/false)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002547 // The user meant this to be an out-of-line constructor
2548 // definition, but template arguments are not allowed
2549 // there. Just allow this as a constructor; we'll
2550 // complain about it later.
2551 goto DoneWithDeclSpec;
2552 }
2553
2554 // The user meant this to name a type, but it actually names
2555 // a constructor with some extraneous template
2556 // arguments. Complain, then parse it as a type as the user
2557 // intended.
2558 Diag(TemplateId->TemplateNameLoc,
2559 diag::err_out_of_line_template_id_names_constructor)
2560 << TemplateId->Name;
2561 }
2562
John McCallaa87d332009-12-12 11:40:51 +00002563 DS.getTypeSpecScope() = SS;
2564 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002565 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002566 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002567 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002568 continue;
2569 }
2570
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002571 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002572 DS.getTypeSpecScope() = SS;
2573 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002574 if (Tok.getAnnotationValue()) {
2575 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002576 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002577 Tok.getAnnotationEndLoc(),
Stephen Hines651f13c2014-04-23 16:59:28 -07002578 PrevSpec, DiagID, T, Policy);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002579 if (isInvalid)
2580 break;
John McCallb3d87482010-08-24 05:47:05 +00002581 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002582 else
2583 DS.SetTypeSpecError();
2584 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2585 ConsumeToken(); // The typename
2586 }
2587
Douglas Gregor9135c722009-03-25 15:40:00 +00002588 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002589 goto DoneWithDeclSpec;
2590
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002591 // If we're in a context where the identifier could be a class name,
2592 // check whether this is a constructor declaration.
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002593 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002594 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002595 &SS)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002596 if (isConstructorDeclarator(/*Unqualified*/false))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002597 goto DoneWithDeclSpec;
2598
2599 // As noted in C++ [class.qual]p2 (cited above), when the name
2600 // of the class is qualified in a context where it could name
2601 // a constructor, its a constructor name. However, we've
2602 // looked at the declarator, and the user probably meant this
2603 // to be a type. Complain that it isn't supposed to be treated
2604 // as a type, then proceed to parse it as a type.
2605 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2606 << Next.getIdentifierInfo();
2607 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002608
John McCallb3d87482010-08-24 05:47:05 +00002609 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2610 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002611 getCurScope(), &SS,
2612 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002613 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002614 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002615
Chris Lattnerf4382f52009-04-14 22:17:06 +00002616 // If the referenced identifier is not a type, then this declspec is
2617 // erroneous: We already checked about that it has no type specifier, and
2618 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002619 // typename.
David Blaikie7247c882013-05-15 07:37:26 +00002620 if (!TypeRep) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00002621 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002622 ParsedAttributesWithRange Attrs(AttrFactory);
2623 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2624 if (!Attrs.empty()) {
2625 AttrsLastTime = true;
2626 attrs.takeAllFrom(Attrs);
2627 }
2628 continue;
2629 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002630 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002631 }
Mike Stump1eb44332009-09-09 15:08:12 +00002632
John McCallaa87d332009-12-12 11:40:51 +00002633 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002634 ConsumeToken(); // The C++ scope.
2635
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002636 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002637 DiagID, TypeRep, Policy);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002638 if (isInvalid)
2639 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002640
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002641 DS.SetRangeEnd(Tok.getLocation());
2642 ConsumeToken(); // The typename.
2643
2644 continue;
2645 }
Mike Stump1eb44332009-09-09 15:08:12 +00002646
Chris Lattner80d0c892009-01-21 19:48:37 +00002647 case tok::annot_typename: {
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002648 // If we've previously seen a tag definition, we were almost surely
2649 // missing a semicolon after it.
2650 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2651 goto DoneWithDeclSpec;
2652
John McCallb3d87482010-08-24 05:47:05 +00002653 if (Tok.getAnnotationValue()) {
2654 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002655 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002656 DiagID, T, Policy);
John McCallb3d87482010-08-24 05:47:05 +00002657 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002658 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002659
Chris Lattner5c5db552010-04-05 18:18:31 +00002660 if (isInvalid)
2661 break;
2662
Chris Lattner80d0c892009-01-21 19:48:37 +00002663 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2664 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002665
Chris Lattner80d0c892009-01-21 19:48:37 +00002666 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2667 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002668 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002669 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002670 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002671
Chris Lattner80d0c892009-01-21 19:48:37 +00002672 continue;
2673 }
Mike Stump1eb44332009-09-09 15:08:12 +00002674
Douglas Gregorbfad9152011-04-28 15:48:45 +00002675 case tok::kw___is_signed:
2676 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2677 // typically treats it as a trait. If we see __is_signed as it appears
2678 // in libstdc++, e.g.,
2679 //
2680 // static const bool __is_signed;
2681 //
2682 // then treat __is_signed as an identifier rather than as a keyword.
2683 if (DS.getTypeSpecType() == TST_bool &&
2684 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
Bill Wendling03e463e2013-12-16 02:32:55 +00002685 DS.getStorageClassSpec() == DeclSpec::SCS_static)
2686 TryKeywordIdentFallback(true);
Douglas Gregorbfad9152011-04-28 15:48:45 +00002687
2688 // We're done with the declaration-specifiers.
2689 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002690
Chris Lattner3bd934a2008-07-26 01:18:38 +00002691 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002692 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002693 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002694 // In C++, check to see if this is a scope specifier like foo::bar::, if
2695 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002696 if (getLangOpts().CPlusPlus) {
Eli Friedman5a428202013-08-15 23:59:20 +00002697 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall9ba61662010-02-26 08:45:28 +00002698 if (!DS.hasTypeSpecifier())
2699 DS.SetTypeSpecError();
2700 goto DoneWithDeclSpec;
2701 }
2702 if (!Tok.is(tok::identifier))
2703 continue;
2704 }
Mike Stump1eb44332009-09-09 15:08:12 +00002705
Chris Lattner3bd934a2008-07-26 01:18:38 +00002706 // This identifier can only be a typedef name if we haven't already seen
2707 // a type-specifier. Without this check we misparse:
2708 // typedef int X; struct Y { short X; }; as 'short int'.
2709 if (DS.hasTypeSpecifier())
2710 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002711
John Thompson82287d12010-02-05 00:12:22 +00002712 // Check for need to substitute AltiVec keyword tokens.
2713 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2714 break;
2715
Richard Smithf63eee72012-05-09 18:56:43 +00002716 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2717 // allow the use of a typedef name as a type specifier.
2718 if (DS.isTypeAltiVecVector())
2719 goto DoneWithDeclSpec;
2720
John McCallb3d87482010-08-24 05:47:05 +00002721 ParsedType TypeRep =
2722 Actions.getTypeName(*Tok.getIdentifierInfo(),
2723 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002724
Chris Lattnerc199ab32009-04-12 20:42:31 +00002725 // If this is not a typedef name, don't parse it as part of the declspec,
2726 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002727 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002728 ParsedAttributesWithRange Attrs(AttrFactory);
2729 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2730 if (!Attrs.empty()) {
2731 AttrsLastTime = true;
2732 attrs.takeAllFrom(Attrs);
2733 }
2734 continue;
2735 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002736 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002737 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002738
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002739 // If we're in a context where the identifier could be a class name,
2740 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002741 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002742 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002743 isConstructorDeclarator(/*Unqualified*/true))
Douglas Gregorb48fe382008-10-31 09:07:45 +00002744 goto DoneWithDeclSpec;
2745
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002746 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002747 DiagID, TypeRep, Policy);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002748 if (isInvalid)
2749 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002750
Chris Lattner3bd934a2008-07-26 01:18:38 +00002751 DS.SetRangeEnd(Tok.getLocation());
2752 ConsumeToken(); // The identifier
2753
2754 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2755 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002756 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002757 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002758 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002759
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002760 // Need to support trailing type qualifiers (e.g. "id<p> const").
2761 // If a type specifier follows, it will be diagnosed elsewhere.
2762 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002763 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002764
2765 // type-name
2766 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002767 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002768 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002769 // This template-id does not refer to a type name, so we're
2770 // done with the type-specifiers.
2771 goto DoneWithDeclSpec;
2772 }
2773
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002774 // If we're in a context where the template-id could be a
2775 // constructor name or specialization, check whether this is a
2776 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002777 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002778 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002779 isConstructorDeclarator(TemplateId->SS.isEmpty()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002780 goto DoneWithDeclSpec;
2781
Douglas Gregor39a8de12009-02-25 19:37:18 +00002782 // Turn the template-id annotation token into a type annotation
2783 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002784 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002785 continue;
2786 }
2787
Reid Spencer5f016e22007-07-11 17:01:13 +00002788 // GNU attributes support.
2789 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002790 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002791 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002792
2793 // Microsoft declspec support.
2794 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002795 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002796 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002797
Steve Naroff239f0732008-12-25 14:16:32 +00002798 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002799 case tok::kw___forceinline: {
Serge Pavlovd1fa81c2013-11-13 06:57:53 +00002800 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002801 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002802 SourceLocation AttrNameLoc = Tok.getLocation();
Aaron Ballman624421f2013-08-31 01:11:41 +00002803 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
Stephen Hines651f13c2014-04-23 16:59:28 -07002804 AttributeList::AS_Keyword);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002805 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002806 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002807
Aaron Ballmanaa9df092013-05-22 23:25:32 +00002808 case tok::kw___sptr:
2809 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00002810 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002811 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002812 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002813 case tok::kw___cdecl:
2814 case tok::kw___stdcall:
2815 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002816 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002817 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002818 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002819 continue;
2820
Dawn Perchik52fc3142010-09-03 01:29:35 +00002821 // Borland single token adornments.
2822 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002823 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002824 continue;
2825
Peter Collingbournef315fa82011-02-14 01:42:53 +00002826 // OpenCL single token adornments.
2827 case tok::kw___kernel:
2828 ParseOpenCLAttributes(DS.getAttributes());
2829 continue;
2830
Reid Spencer5f016e22007-07-11 17:01:13 +00002831 // storage-class-specifier
2832 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002833 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
Stephen Hines651f13c2014-04-23 16:59:28 -07002834 PrevSpec, DiagID, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00002835 break;
2836 case tok::kw_extern:
Richard Smithec642442013-04-12 22:46:28 +00002837 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002838 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002839 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
Stephen Hines651f13c2014-04-23 16:59:28 -07002840 PrevSpec, DiagID, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00002841 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002842 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002843 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
Stephen Hines651f13c2014-04-23 16:59:28 -07002844 Loc, PrevSpec, DiagID, Policy);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002845 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002846 case tok::kw_static:
Richard Smithec642442013-04-12 22:46:28 +00002847 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner1ab3b962008-11-18 07:48:38 +00002848 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002849 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
Stephen Hines651f13c2014-04-23 16:59:28 -07002850 PrevSpec, DiagID, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00002851 break;
2852 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002853 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002854 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002855 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
Stephen Hines651f13c2014-04-23 16:59:28 -07002856 PrevSpec, DiagID, Policy);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002857 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002858 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002859 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002860 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002861 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002862 DiagID, Policy);
Richard Smith8f4fb192011-09-04 19:54:14 +00002863 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002864 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
Stephen Hines651f13c2014-04-23 16:59:28 -07002865 PrevSpec, DiagID, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00002866 break;
2867 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002868 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
Stephen Hines651f13c2014-04-23 16:59:28 -07002869 PrevSpec, DiagID, Policy);
Reid Spencer5f016e22007-07-11 17:01:13 +00002870 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002871 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002872 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
Stephen Hines651f13c2014-04-23 16:59:28 -07002873 PrevSpec, DiagID, Policy);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002874 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002875 case tok::kw___thread:
Richard Smithec642442013-04-12 22:46:28 +00002876 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2877 PrevSpec, DiagID);
2878 break;
2879 case tok::kw_thread_local:
2880 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2881 PrevSpec, DiagID);
2882 break;
2883 case tok::kw__Thread_local:
2884 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2885 Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002886 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002887
Reid Spencer5f016e22007-07-11 17:01:13 +00002888 // function-specifier
2889 case tok::kw_inline:
Serge Pavlovd1fa81c2013-11-13 06:57:53 +00002890 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002891 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002892 case tok::kw_virtual:
Serge Pavlovd1fa81c2013-11-13 06:57:53 +00002893 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002894 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002895 case tok::kw_explicit:
Serge Pavlovd1fa81c2013-11-13 06:57:53 +00002896 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002897 break;
Richard Smithde03c152013-01-17 22:16:11 +00002898 case tok::kw__Noreturn:
2899 if (!getLangOpts().C11)
2900 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlovd1fa81c2013-11-13 06:57:53 +00002901 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smithde03c152013-01-17 22:16:11 +00002902 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002903
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002904 // alignment-specifier
2905 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002906 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002907 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002908 ParseAlignmentSpecifier(DS.getAttributes());
2909 continue;
2910
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002911 // friend
2912 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002913 if (DSContext == DSC_class)
2914 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2915 else {
2916 PrevSpec = ""; // not actually used by the diagnostic
2917 DiagID = diag::err_friend_invalid_in_context;
2918 isInvalid = true;
2919 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002920 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002921
Douglas Gregor8d267c52011-09-09 02:06:17 +00002922 // Modules
2923 case tok::kw___module_private__:
2924 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2925 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002926
Sebastian Redl2ac67232009-11-05 15:47:02 +00002927 // constexpr
2928 case tok::kw_constexpr:
2929 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2930 break;
2931
Chris Lattner80d0c892009-01-21 19:48:37 +00002932 // type-specifier
2933 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002934 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002935 DiagID, Policy);
Chris Lattner80d0c892009-01-21 19:48:37 +00002936 break;
2937 case tok::kw_long:
2938 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002939 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002940 DiagID, Policy);
Chris Lattner80d0c892009-01-21 19:48:37 +00002941 else
John McCallfec54012009-08-03 20:12:06 +00002942 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002943 DiagID, Policy);
Chris Lattner80d0c892009-01-21 19:48:37 +00002944 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002945 case tok::kw___int64:
2946 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002947 DiagID, Policy);
Francois Pichet338d7f72011-04-28 01:59:37 +00002948 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002949 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002950 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2951 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002952 break;
2953 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002954 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2955 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002956 break;
2957 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002958 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2959 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002960 break;
2961 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002962 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2963 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002964 break;
2965 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002966 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002967 DiagID, Policy);
Chris Lattner80d0c892009-01-21 19:48:37 +00002968 break;
2969 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002970 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002971 DiagID, Policy);
Chris Lattner80d0c892009-01-21 19:48:37 +00002972 break;
2973 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002974 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002975 DiagID, Policy);
Chris Lattner80d0c892009-01-21 19:48:37 +00002976 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002977 case tok::kw___int128:
2978 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002979 DiagID, Policy);
Richard Smith5a5a9712012-04-04 06:24:32 +00002980 break;
2981 case tok::kw_half:
2982 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002983 DiagID, Policy);
Richard Smith5a5a9712012-04-04 06:24:32 +00002984 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002985 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002986 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002987 DiagID, Policy);
Chris Lattner80d0c892009-01-21 19:48:37 +00002988 break;
2989 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002990 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002991 DiagID, Policy);
Chris Lattner80d0c892009-01-21 19:48:37 +00002992 break;
2993 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002994 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002995 DiagID, Policy);
Chris Lattner80d0c892009-01-21 19:48:37 +00002996 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002997 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002998 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07002999 DiagID, Policy);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003000 break;
3001 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00003002 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07003003 DiagID, Policy);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003004 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00003005 case tok::kw_bool:
3006 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00003007 if (Tok.is(tok::kw_bool) &&
3008 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3009 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3010 PrevSpec = ""; // Not used by the diagnostic.
3011 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00003012 // For better error recovery.
3013 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00003014 isInvalid = true;
3015 } else {
3016 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07003017 DiagID, Policy);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00003018 }
Chris Lattner80d0c892009-01-21 19:48:37 +00003019 break;
3020 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00003021 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07003022 DiagID, Policy);
Chris Lattner80d0c892009-01-21 19:48:37 +00003023 break;
3024 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00003025 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07003026 DiagID, Policy);
Chris Lattner80d0c892009-01-21 19:48:37 +00003027 break;
3028 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00003029 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07003030 DiagID, Policy);
Chris Lattner80d0c892009-01-21 19:48:37 +00003031 break;
John Thompson82287d12010-02-05 00:12:22 +00003032 case tok::kw___vector:
Stephen Hines651f13c2014-04-23 16:59:28 -07003033 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
John Thompson82287d12010-02-05 00:12:22 +00003034 break;
3035 case tok::kw___pixel:
Stephen Hines651f13c2014-04-23 16:59:28 -07003036 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
Guy Benyeie6b9d802013-01-20 12:31:11 +00003037 break;
John McCalla5fc4722011-04-09 22:50:59 +00003038 case tok::kw___unknown_anytype:
3039 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
Stephen Hines651f13c2014-04-23 16:59:28 -07003040 PrevSpec, DiagID, Policy);
John McCalla5fc4722011-04-09 22:50:59 +00003041 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00003042
3043 // class-specifier:
3044 case tok::kw_class:
3045 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003046 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00003047 case tok::kw_union: {
3048 tok::TokenKind Kind = Tok.getKind();
3049 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00003050
3051 // These are attributes following class specifiers.
3052 // To produce better diagnostic, we parse them when
3053 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00003054 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00003055 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00003056 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00003057
3058 // If there are attributes following class specifier,
3059 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00003060 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00003061 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00003062 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00003063 }
Chris Lattner80d0c892009-01-21 19:48:37 +00003064 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00003065 }
Chris Lattner80d0c892009-01-21 19:48:37 +00003066
3067 // enum-specifier:
3068 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00003069 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00003070 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00003071 continue;
3072
3073 // cv-qualifier:
3074 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003075 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003076 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003077 break;
3078 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003079 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003080 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003081 break;
3082 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003083 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00003084 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00003085 break;
3086
Douglas Gregord57959a2009-03-27 23:10:48 +00003087 // C++ typename-specifier:
3088 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00003089 if (TryAnnotateTypeOrScopeToken()) {
3090 DS.SetTypeSpecError();
3091 goto DoneWithDeclSpec;
3092 }
3093 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00003094 continue;
3095 break;
3096
Chris Lattner80d0c892009-01-21 19:48:37 +00003097 // GNU typeof support.
3098 case tok::kw_typeof:
3099 ParseTypeofSpecifier(DS);
3100 continue;
3101
David Blaikie42d6d0c2011-12-04 05:04:18 +00003102 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00003103 ParseDecltypeSpecifier(DS);
3104 continue;
3105
Sean Huntdb5d44b2011-05-19 05:37:45 +00003106 case tok::kw___underlying_type:
3107 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00003108 continue;
3109
3110 case tok::kw__Atomic:
Richard Smith4cf4a5e2013-03-28 01:55:44 +00003111 // C11 6.7.2.4/4:
3112 // If the _Atomic keyword is immediately followed by a left parenthesis,
3113 // it is interpreted as a type specifier (with a type name), not as a
3114 // type qualifier.
3115 if (NextToken().is(tok::l_paren)) {
3116 ParseAtomicSpecifier(DS);
3117 continue;
3118 }
3119 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3120 getLangOpts());
3121 break;
Sean Huntdb5d44b2011-05-19 05:37:45 +00003122
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003123 // OpenCL qualifiers:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003124 case tok::kw___private:
3125 case tok::kw___global:
3126 case tok::kw___local:
3127 case tok::kw___constant:
3128 case tok::kw___read_only:
3129 case tok::kw___write_only:
3130 case tok::kw___read_write:
Stephen Hines651f13c2014-04-23 16:59:28 -07003131 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003132 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00003133
Steve Naroffd3ded1f2008-06-05 00:02:44 +00003134 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00003135 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00003136 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3137 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00003138 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00003139 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00003140
Douglas Gregor46f936e2010-11-19 17:10:50 +00003141 if (!ParseObjCProtocolQualifiers(DS))
3142 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3143 << FixItHint::CreateInsertion(Loc, "id")
3144 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00003145
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00003146 // Need to support trailing type qualifiers (e.g. "id<p> const").
3147 // If a type specifier follows, it will be diagnosed elsewhere.
3148 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00003149 }
John McCallfec54012009-08-03 20:12:06 +00003150 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00003151 if (isInvalid) {
3152 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00003153 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00003154
Douglas Gregorae2fb142010-08-23 14:34:43 +00003155 if (DiagID == diag::ext_duplicate_declspec)
3156 Diag(Tok, DiagID)
3157 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3158 else
3159 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003160 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00003161
Chris Lattner81c018d2008-03-13 06:29:04 +00003162 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00003163 if (DiagID != diag::err_bool_redeclaration)
3164 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00003165
3166 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003167 }
3168}
Douglas Gregoradcac882008-12-01 23:54:00 +00003169
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003170/// ParseStructDeclaration - Parse a struct declaration without the terminating
3171/// semicolon.
3172///
Reid Spencer5f016e22007-07-11 17:01:13 +00003173/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003174/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00003175/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003176/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00003177/// struct-declarator-list:
3178/// struct-declarator
3179/// struct-declarator-list ',' struct-declarator
3180/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3181/// struct-declarator:
3182/// declarator
3183/// [GNU] declarator attributes[opt]
3184/// declarator[opt] ':' constant-expression
3185/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3186///
Chris Lattnere1359422008-04-10 06:46:29 +00003187void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003188ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003189
Chris Lattnerc46d1a12008-10-20 06:45:43 +00003190 if (Tok.is(tok::kw___extension__)) {
3191 // __extension__ silences extension warnings in the subexpression.
3192 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003193 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00003194 return ParseStructDeclaration(DS, Fields);
3195 }
Mike Stump1eb44332009-09-09 15:08:12 +00003196
Steve Naroff28a7ca82007-08-20 22:28:22 +00003197 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00003198 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003199
Douglas Gregor4920f1f2009-01-12 22:49:06 +00003200 // If there are no declarators, this is a free-standing declaration
3201 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00003202 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003203 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3204 DS);
3205 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00003206 return;
3207 }
3208
3209 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00003210 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00003211 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003212 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003213 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00003214 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00003215
Bill Wendlingad017fa2012-12-20 19:22:21 +00003216 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00003217 if (!FirstDeclarator)
3218 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00003219
Steve Naroff28a7ca82007-08-20 22:28:22 +00003220 /// struct-declarator: declarator
3221 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003222 if (Tok.isNot(tok::colon)) {
3223 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3224 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00003225 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00003226 }
Mike Stump1eb44332009-09-09 15:08:12 +00003227
Stephen Hines651f13c2014-04-23 16:59:28 -07003228 if (TryConsumeToken(tok::colon)) {
John McCall60d7b3a2010-08-24 06:29:42 +00003229 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003230 if (Res.isInvalid())
Alexey Bataev8fe24752013-11-18 08:17:37 +00003231 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00003232 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00003233 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00003234 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003235
Steve Naroff28a7ca82007-08-20 22:28:22 +00003236 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003237 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003238
John McCallbdd563e2009-11-03 02:38:08 +00003239 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00003240 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00003241
Steve Naroff28a7ca82007-08-20 22:28:22 +00003242 // If we don't have a comma, it is either the end of the list (a ';')
3243 // or an error, bail out.
Stephen Hines651f13c2014-04-23 16:59:28 -07003244 if (!TryConsumeToken(tok::comma, CommaLoc))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003245 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00003246
John McCallbdd563e2009-11-03 02:38:08 +00003247 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003248 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003249}
3250
3251/// ParseStructUnionBody
3252/// struct-contents:
3253/// struct-declaration-list
3254/// [EXT] empty
3255/// [GNU] "struct-declaration-list" without terminatoring ';'
3256/// struct-declaration-list:
3257/// struct-declaration
3258/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003259/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003260///
Reid Spencer5f016e22007-07-11 17:01:13 +00003261void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003262 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003263 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3264 "parsing struct/union body");
Andy Gibbsf50f3f72013-04-03 09:31:19 +00003265 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump1eb44332009-09-09 15:08:12 +00003266
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003267 BalancedDelimiterTracker T(*this, tok::l_brace);
3268 if (T.consumeOpen())
3269 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003270
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003271 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003272 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003273
Chris Lattner5f9e2722011-07-23 10:55:15 +00003274 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003275
Reid Spencer5f016e22007-07-11 17:01:13 +00003276 // While we still have something to read, read the declarations in the struct.
Stephen Hines651f13c2014-04-23 16:59:28 -07003277 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003278 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003279
Reid Spencer5f016e22007-07-11 17:01:13 +00003280 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003281 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003282 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003283 continue;
3284 }
Chris Lattnere1359422008-04-10 06:46:29 +00003285
Andy Gibbs74b9fa12013-04-03 09:46:04 +00003286 // Parse _Static_assert declaration.
3287 if (Tok.is(tok::kw__Static_assert)) {
3288 SourceLocation DeclEnd;
3289 ParseStaticAssertDeclaration(DeclEnd);
3290 continue;
3291 }
3292
Argyrios Kyrtzidisbd957452013-04-18 01:42:35 +00003293 if (Tok.is(tok::annot_pragma_pack)) {
3294 HandlePragmaPack();
3295 continue;
3296 }
3297
3298 if (Tok.is(tok::annot_pragma_align)) {
3299 HandlePragmaAlign();
3300 continue;
3301 }
3302
John McCallbdd563e2009-11-03 02:38:08 +00003303 if (!Tok.is(tok::at)) {
3304 struct CFieldCallback : FieldCallback {
3305 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003306 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003307 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003308
John McCalld226f652010-08-21 09:40:31 +00003309 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003310 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003311 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3312
Stephen Hines651f13c2014-04-23 16:59:28 -07003313 void invoke(ParsingFieldDeclarator &FD) override {
John McCallbdd563e2009-11-03 02:38:08 +00003314 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003315 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003316 FD.D.getDeclSpec().getSourceRange().getBegin(),
3317 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003318 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003319 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003320 }
John McCallbdd563e2009-11-03 02:38:08 +00003321 } Callback(*this, TagDecl, FieldDecls);
3322
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003323 // Parse all the comma separated declarators.
3324 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003325 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003326 } else { // Handle @defs
3327 ConsumeToken();
3328 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3329 Diag(Tok, diag::err_unexpected_at);
Alexey Bataev8fe24752013-11-18 08:17:37 +00003330 SkipUntil(tok::semi);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003331 continue;
3332 }
3333 ConsumeToken();
Stephen Hines651f13c2014-04-23 16:59:28 -07003334 ExpectAndConsume(tok::l_paren);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003335 if (!Tok.is(tok::identifier)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003336 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev8fe24752013-11-18 08:17:37 +00003337 SkipUntil(tok::semi);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003338 continue;
3339 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003340 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003341 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003342 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003343 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3344 ConsumeToken();
Stephen Hines651f13c2014-04-23 16:59:28 -07003345 ExpectAndConsume(tok::r_paren);
Mike Stump1eb44332009-09-09 15:08:12 +00003346 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003347
Stephen Hines651f13c2014-04-23 16:59:28 -07003348 if (TryConsumeToken(tok::semi))
3349 continue;
3350
3351 if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003352 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003353 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003354 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003355
3356 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3357 // Skip to end of block or statement to avoid ext-warning on extra ';'.
3358 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
3359 // If we stopped at a ';', eat it.
3360 TryConsumeToken(tok::semi);
Reid Spencer5f016e22007-07-11 17:01:13 +00003361 }
Mike Stump1eb44332009-09-09 15:08:12 +00003362
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003363 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003364
John McCall0b7e6782011-03-24 11:26:52 +00003365 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003366 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003367 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003368
Douglas Gregor23c94db2010-07-02 17:43:08 +00003369 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003370 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003371 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003372 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003373 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003374 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3375 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003376}
3377
Reid Spencer5f016e22007-07-11 17:01:13 +00003378/// ParseEnumSpecifier
3379/// enum-specifier: [C99 6.7.2.2]
3380/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003381///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003382/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3383/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003384/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3385/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003386/// 'enum' identifier
3387/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003388///
Richard Smith1af83c42012-03-23 03:33:32 +00003389/// [C++11] enum-head '{' enumerator-list[opt] '}'
3390/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003391///
Richard Smith1af83c42012-03-23 03:33:32 +00003392/// enum-head: [C++11]
3393/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3394/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3395/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003396///
Richard Smith1af83c42012-03-23 03:33:32 +00003397/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003398/// 'enum'
3399/// 'enum' 'class'
3400/// 'enum' 'struct'
3401///
Richard Smith1af83c42012-03-23 03:33:32 +00003402/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003403/// ':' type-specifier-seq
3404///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003405/// [C++] elaborated-type-specifier:
3406/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3407///
Chris Lattner4c97d762009-04-12 21:49:30 +00003408void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003409 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003410 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003411 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003412 if (Tok.is(tok::code_completion)) {
3413 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003414 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003415 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003416 }
John McCall57c13002011-07-06 05:58:41 +00003417
Sean Hunt2edf0a22012-06-23 05:07:58 +00003418 // If attributes exist after tag, parse them.
3419 ParsedAttributesWithRange attrs(AttrFactory);
3420 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003421 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003422
3423 // If declspecs exist after tag, parse them.
3424 while (Tok.is(tok::kw___declspec))
3425 ParseMicrosoftDeclSpec(attrs);
3426
Richard Smithbdad7a22012-01-10 01:33:14 +00003427 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003428 bool IsScopedUsingClassTag = false;
3429
John McCall1e12b3d2012-06-23 22:30:04 +00003430 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieued5a2922013-04-23 02:47:36 +00003431 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3432 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3433 : diag::ext_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003434 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003435 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003436
Bill Wendlingad017fa2012-12-20 19:22:21 +00003437 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003438 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003439 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003440
3441 // They are allowed afterwards, though.
3442 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003443 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003444 while (Tok.is(tok::kw___declspec))
3445 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003446 }
Richard Smith1af83c42012-03-23 03:33:32 +00003447
John McCall13489672012-05-07 06:16:58 +00003448 // C++11 [temp.explicit]p12:
3449 // The usual access controls do not apply to names used to specify
3450 // explicit instantiations.
3451 // We extend this to also cover explicit specializations. Note that
3452 // we don't suppress if this turns out to be an elaborated type
3453 // specifier.
3454 bool shouldDelayDiagsInTag =
3455 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3456 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3457 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003458
Richard Smith7796eb52012-03-12 08:56:40 +00003459 // Enum definitions should not be parsed in a trailing-return-type.
3460 bool AllowDeclaration = DSC != DSC_trailing;
3461
3462 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003463 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003464 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003465
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003466 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003467 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003468 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3469 // if a fixed underlying type is allowed.
3470 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003471
3472 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith725fe0e2013-04-01 21:43:41 +00003473 /*EnteringContext=*/true))
John McCall9ba61662010-02-26 08:45:28 +00003474 return;
3475
3476 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003477 Diag(Tok, diag::err_expected) << tok::identifier;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003478 if (Tok.isNot(tok::l_brace)) {
3479 // Has no name and is not a definition.
3480 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataev8fe24752013-11-18 08:17:37 +00003481 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003482 return;
3483 }
3484 }
3485 }
Mike Stump1eb44332009-09-09 15:08:12 +00003486
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003487 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003488 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003489 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003490 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Mike Stump1eb44332009-09-09 15:08:12 +00003491
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003492 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataev8fe24752013-11-18 08:17:37 +00003493 SkipUntil(tok::comma, StopAtSemi);
Reid Spencer5f016e22007-07-11 17:01:13 +00003494 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003495 }
Mike Stump1eb44332009-09-09 15:08:12 +00003496
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003497 // If an identifier is present, consume and remember it.
3498 IdentifierInfo *Name = 0;
3499 SourceLocation NameLoc;
3500 if (Tok.is(tok::identifier)) {
3501 Name = Tok.getIdentifierInfo();
3502 NameLoc = ConsumeToken();
3503 }
Mike Stump1eb44332009-09-09 15:08:12 +00003504
Richard Smithbdad7a22012-01-10 01:33:14 +00003505 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003506 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3507 // declaration of a scoped enumeration.
3508 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003509 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003510 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003511 }
3512
John McCall13489672012-05-07 06:16:58 +00003513 // Okay, end the suppression area. We'll decide whether to emit the
3514 // diagnostics in a second.
3515 if (shouldDelayDiagsInTag)
3516 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003517
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003518 TypeResult BaseType;
3519
Douglas Gregora61b3e72010-12-01 17:42:47 +00003520 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003521 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003522 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003523 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003524 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003525 // If we're in class scope, this can either be an enum declaration with
3526 // an underlying type, or a declaration of a bitfield member. We try to
3527 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003528 // (integer literal, sizeof); if it's still ambiguous, we then consider
3529 // anything that's a simple-type-specifier followed by '(' as an
3530 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003531 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003532 EnterExpressionEvaluationContext Unevaluated(Actions,
3533 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003534 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003535 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003536 // bit-field. This is the common case.
3537 if (TPR == TPResult::True())
3538 PossibleBitfield = true;
3539 // If the next token starts a type-specifier-seq, it may be either a
3540 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003541 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003542 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003543 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003544 GetLookAheadToken(2).getKind() == tok::semi) {
3545 // Consume the ':'.
3546 ConsumeToken();
3547 } else {
3548 // We have the start of a type-specifier-seq, so we have to perform
3549 // tentative parsing to determine whether we have an expression or a
3550 // type.
3551 TentativeParsingAction TPA(*this);
3552
3553 // Consume the ':'.
3554 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003555
3556 // If we see a type specifier followed by an open-brace, we have an
3557 // ambiguity between an underlying type and a C++11 braced
3558 // function-style cast. Resolve this by always treating it as an
3559 // underlying type.
3560 // FIXME: The standard is not entirely clear on how to disambiguate in
3561 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003562 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003563 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003564 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003565 // We'll parse this as a bitfield later.
3566 PossibleBitfield = true;
3567 TPA.Revert();
3568 } else {
3569 // We have a type-specifier-seq.
3570 TPA.Commit();
3571 }
3572 }
3573 } else {
3574 // Consume the ':'.
3575 ConsumeToken();
3576 }
3577
3578 if (!PossibleBitfield) {
3579 SourceRange Range;
3580 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003581
Richard Smith80ad52f2013-01-02 11:42:31 +00003582 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003583 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003584 } else if (!getLangOpts().ObjC2) {
3585 if (getLangOpts().CPlusPlus)
3586 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3587 else
3588 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3589 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003590 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003591 }
3592
Richard Smithbdad7a22012-01-10 01:33:14 +00003593 // There are four options here. If we have 'friend enum foo;' then this is a
3594 // friend declaration, and cannot have an accompanying definition. If we have
3595 // 'enum foo;', then this is a forward declaration. If we have
3596 // 'enum foo {...' then this is a definition. Otherwise we have something
3597 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003598 //
3599 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3600 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3601 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3602 //
John McCallf312b1e2010-08-26 23:41:50 +00003603 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003604 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003605 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003606 } else if (Tok.is(tok::l_brace)) {
3607 if (DS.isFriendSpecified()) {
3608 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3609 << SourceRange(DS.getFriendSpecLoc());
3610 ConsumeBrace();
Alexey Bataev8fe24752013-11-18 08:17:37 +00003611 SkipUntil(tok::r_brace, StopAtSemi);
John McCall13489672012-05-07 06:16:58 +00003612 TUK = Sema::TUK_Friend;
3613 } else {
3614 TUK = Sema::TUK_Definition;
3615 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003616 } else if (!isTypeSpecifier(DSC) &&
Richard Smithc9f35172012-06-25 21:37:02 +00003617 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003618 (Tok.isAtStartOfLine() &&
3619 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003620 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3621 if (Tok.isNot(tok::semi)) {
3622 // A semicolon was missing after this declaration. Diagnose and recover.
Stephen Hines651f13c2014-04-23 16:59:28 -07003623 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smithc9f35172012-06-25 21:37:02 +00003624 PP.EnterToken(Tok);
3625 Tok.setKind(tok::semi);
3626 }
John McCall13489672012-05-07 06:16:58 +00003627 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003628 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003629 }
3630
3631 // If this is an elaborated type specifier, and we delayed
3632 // diagnostics before, just merge them into the current pool.
3633 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3634 diagsFromTag.redelay();
3635 }
Richard Smith1af83c42012-03-23 03:33:32 +00003636
3637 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003638 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003639 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003640 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003641 // Skip the rest of this declarator, up until the comma or semicolon.
3642 Diag(Tok, diag::err_enum_template);
Alexey Bataev8fe24752013-11-18 08:17:37 +00003643 SkipUntil(tok::comma, StopAtSemi);
Richard Smith1af83c42012-03-23 03:33:32 +00003644 return;
3645 }
3646
3647 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3648 // Enumerations can't be explicitly instantiated.
3649 DS.SetTypeSpecError();
3650 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3651 return;
3652 }
3653
3654 assert(TemplateInfo.TemplateParams && "no template parameters");
3655 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3656 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003657 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003658
Sean Hunt2edf0a22012-06-23 05:07:58 +00003659 if (TUK == Sema::TUK_Reference)
3660 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003661
Douglas Gregorb9075602011-02-22 02:55:24 +00003662 if (!Name && TUK != Sema::TUK_Definition) {
3663 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003664
Douglas Gregorb9075602011-02-22 02:55:24 +00003665 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataev8fe24752013-11-18 08:17:37 +00003666 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregorb9075602011-02-22 02:55:24 +00003667 return;
3668 }
Richard Smith1af83c42012-03-23 03:33:32 +00003669
Douglas Gregor402abb52009-05-28 23:31:59 +00003670 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003671 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003672 const char *PrevSpec = 0;
3673 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003674 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003675 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003676 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003677 Owned, IsDependent, ScopedEnumKWLoc,
Stephen Hines651f13c2014-04-23 16:59:28 -07003678 IsScopedUsingClassTag, BaseType,
3679 DSC == DSC_type_specifier);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003680
Douglas Gregor48c89f42010-04-24 16:38:41 +00003681 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003682 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003683 // dependent tag.
3684 if (!Name) {
3685 DS.SetTypeSpecError();
3686 Diag(Tok, diag::err_expected_type_name_after_typename);
3687 return;
3688 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003689
Douglas Gregor23c94db2010-07-02 17:43:08 +00003690 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003691 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003692 NameLoc);
3693 if (Type.isInvalid()) {
3694 DS.SetTypeSpecError();
3695 return;
3696 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003697
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003698 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3699 NameLoc.isValid() ? NameLoc : StartLoc,
Stephen Hines651f13c2014-04-23 16:59:28 -07003700 PrevSpec, DiagID, Type.get(),
3701 Actions.getASTContext().getPrintingPolicy()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003702 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003703
Douglas Gregor48c89f42010-04-24 16:38:41 +00003704 return;
3705 }
Mike Stump1eb44332009-09-09 15:08:12 +00003706
John McCalld226f652010-08-21 09:40:31 +00003707 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003708 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003709 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003710 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003711 ConsumeBrace();
Alexey Bataev8fe24752013-11-18 08:17:37 +00003712 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor48c89f42010-04-24 16:38:41 +00003713 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003714
Douglas Gregor48c89f42010-04-24 16:38:41 +00003715 DS.SetTypeSpecError();
3716 return;
3717 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003718
Richard Smithc9f35172012-06-25 21:37:02 +00003719 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003720 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003721
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003722 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3723 NameLoc.isValid() ? NameLoc : StartLoc,
Stephen Hines651f13c2014-04-23 16:59:28 -07003724 PrevSpec, DiagID, TagDecl, Owned,
3725 Actions.getASTContext().getPrintingPolicy()))
John McCallfec54012009-08-03 20:12:06 +00003726 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003727}
3728
3729/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3730/// enumerator-list:
3731/// enumerator
3732/// enumerator-list ',' enumerator
3733/// enumerator:
3734/// enumeration-constant
3735/// enumeration-constant '=' constant-expression
3736/// enumeration-constant:
3737/// identifier
3738///
John McCalld226f652010-08-21 09:40:31 +00003739void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003740 // Enter the scope of the enum body and start the definition.
3741 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003742 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003743
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003744 BalancedDelimiterTracker T(*this, tok::l_brace);
3745 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003746
Chris Lattner7946dd32007-08-27 17:24:30 +00003747 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003748 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003749 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003750
Chris Lattner5f9e2722011-07-23 10:55:15 +00003751 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003752
John McCalld226f652010-08-21 09:40:31 +00003753 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003754
Reid Spencer5f016e22007-07-11 17:01:13 +00003755 // Parse the enumerator-list.
Stephen Hines651f13c2014-04-23 16:59:28 -07003756 while (Tok.isNot(tok::r_brace)) {
3757 // Parse enumerator. If failed, try skipping till the start of the next
3758 // enumerator definition.
3759 if (Tok.isNot(tok::identifier)) {
3760 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
3761 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
3762 TryConsumeToken(tok::comma))
3763 continue;
3764 break;
3765 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003766 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3767 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003768
John McCall5b629aa2010-10-22 23:36:17 +00003769 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003770 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003771 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003772 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003773 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003774
Reid Spencer5f016e22007-07-11 17:01:13 +00003775 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003776 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003777 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003778
Stephen Hines651f13c2014-04-23 16:59:28 -07003779 if (TryConsumeToken(tok::equal, EqualLoc)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003780 AssignedVal = ParseConstantExpression();
3781 if (AssignedVal.isInvalid())
Stephen Hines651f13c2014-04-23 16:59:28 -07003782 SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
Reid Spencer5f016e22007-07-11 17:01:13 +00003783 }
Mike Stump1eb44332009-09-09 15:08:12 +00003784
Reid Spencer5f016e22007-07-11 17:01:13 +00003785 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003786 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3787 LastEnumConstDecl,
3788 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003789 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003790 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003791 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003792
Reid Spencer5f016e22007-07-11 17:01:13 +00003793 EnumConstantDecls.push_back(EnumConstDecl);
3794 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003795
Douglas Gregor751f6922010-09-07 14:51:08 +00003796 if (Tok.is(tok::identifier)) {
3797 // We're missing a comma between enumerators.
3798 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003799 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003800 << FixItHint::CreateInsertion(Loc, ", ");
3801 continue;
3802 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003803
Stephen Hines651f13c2014-04-23 16:59:28 -07003804 // Emumerator definition must be finished, only comma or r_brace are
3805 // allowed here.
3806 SourceLocation CommaLoc;
3807 if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
3808 if (EqualLoc.isValid())
3809 Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
3810 << tok::comma;
3811 else
3812 Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
3813 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
3814 if (TryConsumeToken(tok::comma, CommaLoc))
3815 continue;
3816 } else {
3817 break;
3818 }
3819 }
Mike Stump1eb44332009-09-09 15:08:12 +00003820
Stephen Hines651f13c2014-04-23 16:59:28 -07003821 // If comma is followed by r_brace, emit appropriate warning.
3822 if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003823 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003824 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3825 diag::ext_enumerator_list_comma_cxx :
3826 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003827 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003828 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003829 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3830 << FixItHint::CreateRemoval(CommaLoc);
Stephen Hines651f13c2014-04-23 16:59:28 -07003831 break;
Richard Smith7fe62082011-10-15 05:09:34 +00003832 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003833 }
Mike Stump1eb44332009-09-09 15:08:12 +00003834
Reid Spencer5f016e22007-07-11 17:01:13 +00003835 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003836 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003837
Reid Spencer5f016e22007-07-11 17:01:13 +00003838 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003839 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003840 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003841
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003842 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenko9ff2b422013-04-27 20:23:52 +00003843 EnumDecl, EnumConstantDecls,
3844 getCurScope(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003845 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003846
Douglas Gregor72de6672009-01-08 20:45:30 +00003847 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003848 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3849 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003850
3851 // The next token must be valid after an enum definition. If not, a ';'
3852 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003853 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3854 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003855 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smithc9f35172012-06-25 21:37:02 +00003856 // Push this token back into the preprocessor and change our current token
3857 // to ';' so that the rest of the code recovers as though there were an
3858 // ';' after the definition.
3859 PP.EnterToken(Tok);
3860 Tok.setKind(tok::semi);
3861 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003862}
3863
3864/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003865/// start of a type-qualifier-list.
3866bool Parser::isTypeQualifier() const {
3867 switch (Tok.getKind()) {
3868 default: return false;
Stephen Hines651f13c2014-04-23 16:59:28 -07003869 // type-qualifier
Steve Naroff5f8aa692008-02-11 23:15:56 +00003870 case tok::kw_const:
3871 case tok::kw_volatile:
3872 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003873 case tok::kw___private:
3874 case tok::kw___local:
3875 case tok::kw___global:
3876 case tok::kw___constant:
3877 case tok::kw___read_only:
3878 case tok::kw___read_write:
3879 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003880 return true;
3881 }
3882}
3883
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003884/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3885/// is definitely a type-specifier. Return false if it isn't part of a type
3886/// specifier or if we're not sure.
3887bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3888 switch (Tok.getKind()) {
3889 default: return false;
3890 // type-specifiers
3891 case tok::kw_short:
3892 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003893 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003894 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003895 case tok::kw_signed:
3896 case tok::kw_unsigned:
3897 case tok::kw__Complex:
3898 case tok::kw__Imaginary:
3899 case tok::kw_void:
3900 case tok::kw_char:
3901 case tok::kw_wchar_t:
3902 case tok::kw_char16_t:
3903 case tok::kw_char32_t:
3904 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003905 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003906 case tok::kw_float:
3907 case tok::kw_double:
3908 case tok::kw_bool:
3909 case tok::kw__Bool:
3910 case tok::kw__Decimal32:
3911 case tok::kw__Decimal64:
3912 case tok::kw__Decimal128:
3913 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003914
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003915 // struct-or-union-specifier (C99) or class-specifier (C++)
3916 case tok::kw_class:
3917 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003918 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003919 case tok::kw_union:
3920 // enum-specifier
3921 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003922
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003923 // typedef-name
3924 case tok::annot_typename:
3925 return true;
3926 }
3927}
3928
Steve Naroff5f8aa692008-02-11 23:15:56 +00003929/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003930/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003931bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003932 switch (Tok.getKind()) {
3933 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003934
Chris Lattner166a8fc2009-01-04 23:41:41 +00003935 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003936 if (TryAltiVecVectorToken())
3937 return true;
3938 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003939 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003940 // Annotate typenames and C++ scope specifiers. If we get one, just
3941 // recurse to handle whatever we get.
3942 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003943 return true;
3944 if (Tok.is(tok::identifier))
3945 return false;
3946 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003947
Chris Lattner166a8fc2009-01-04 23:41:41 +00003948 case tok::coloncolon: // ::foo::bar
3949 if (NextToken().is(tok::kw_new) || // ::new
3950 NextToken().is(tok::kw_delete)) // ::delete
3951 return false;
3952
Chris Lattner166a8fc2009-01-04 23:41:41 +00003953 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003954 return true;
3955 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003956
Reid Spencer5f016e22007-07-11 17:01:13 +00003957 // GNU attributes support.
3958 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003959 // GNU typeof support.
3960 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003961
Reid Spencer5f016e22007-07-11 17:01:13 +00003962 // type-specifiers
3963 case tok::kw_short:
3964 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003965 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003966 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003967 case tok::kw_signed:
3968 case tok::kw_unsigned:
3969 case tok::kw__Complex:
3970 case tok::kw__Imaginary:
3971 case tok::kw_void:
3972 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003973 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003974 case tok::kw_char16_t:
3975 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003976 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003977 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003978 case tok::kw_float:
3979 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003980 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003981 case tok::kw__Bool:
3982 case tok::kw__Decimal32:
3983 case tok::kw__Decimal64:
3984 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003985 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003986
Chris Lattner99dc9142008-04-13 18:59:07 +00003987 // struct-or-union-specifier (C99) or class-specifier (C++)
3988 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003989 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003990 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003991 case tok::kw_union:
3992 // enum-specifier
3993 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003994
Reid Spencer5f016e22007-07-11 17:01:13 +00003995 // type-qualifier
3996 case tok::kw_const:
3997 case tok::kw_volatile:
3998 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003999
John McCallb8a8de32012-11-14 00:49:39 +00004000 // Debugger support.
4001 case tok::kw___unknown_anytype:
4002
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004003 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00004004 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00004005 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00004006
Chris Lattner7c186be2008-10-20 00:25:30 +00004007 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4008 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00004009 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00004010
Steve Naroff239f0732008-12-25 14:16:32 +00004011 case tok::kw___cdecl:
4012 case tok::kw___stdcall:
4013 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004014 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00004015 case tok::kw___w64:
4016 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004017 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004018 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004019 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004020
4021 case tok::kw___private:
4022 case tok::kw___local:
4023 case tok::kw___global:
4024 case tok::kw___constant:
4025 case tok::kw___read_only:
4026 case tok::kw___read_write:
4027 case tok::kw___write_only:
4028
Eli Friedman290eeb02009-06-08 23:27:34 +00004029 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004030
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004031 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00004032 case tok::kw__Atomic:
4033 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00004034 }
4035}
4036
4037/// isDeclarationSpecifier() - Return true if the current token is part of a
4038/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00004039///
4040/// \param DisambiguatingWithExpression True to indicate that the purpose of
4041/// this check is to disambiguate between an expression and a declaration.
4042bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004043 switch (Tok.getKind()) {
4044 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004045
Chris Lattner166a8fc2009-01-04 23:41:41 +00004046 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00004047 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00004048 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00004049 return false;
John Thompson82287d12010-02-05 00:12:22 +00004050 if (TryAltiVecVectorToken())
4051 return true;
4052 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00004053 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00004054 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00004055 // Annotate typenames and C++ scope specifiers. If we get one, just
4056 // recurse to handle whatever we get.
4057 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004058 return true;
4059 if (Tok.is(tok::identifier))
4060 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00004061
Douglas Gregor9497a732010-09-16 01:51:54 +00004062 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00004063 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00004064 // expression is permitted, then this is probably a class message send
4065 // missing the initial '['. In this case, we won't consider this to be
4066 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00004067 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00004068 isStartOfObjCClassMessageMissingOpenBracket())
4069 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00004070
John McCall9ba61662010-02-26 08:45:28 +00004071 return isDeclarationSpecifier();
4072
Chris Lattner166a8fc2009-01-04 23:41:41 +00004073 case tok::coloncolon: // ::foo::bar
4074 if (NextToken().is(tok::kw_new) || // ::new
4075 NextToken().is(tok::kw_delete)) // ::delete
4076 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00004077
Chris Lattner166a8fc2009-01-04 23:41:41 +00004078 // Annotate typenames and C++ scope specifiers. If we get one, just
4079 // recurse to handle whatever we get.
4080 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00004081 return true;
4082 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004083
Reid Spencer5f016e22007-07-11 17:01:13 +00004084 // storage-class-specifier
4085 case tok::kw_typedef:
4086 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00004087 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00004088 case tok::kw_static:
4089 case tok::kw_auto:
4090 case tok::kw_register:
4091 case tok::kw___thread:
Richard Smithec642442013-04-12 22:46:28 +00004092 case tok::kw_thread_local:
4093 case tok::kw__Thread_local:
Mike Stump1eb44332009-09-09 15:08:12 +00004094
Douglas Gregor8d267c52011-09-09 02:06:17 +00004095 // Modules
4096 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00004097
John McCallb8a8de32012-11-14 00:49:39 +00004098 // Debugger support
4099 case tok::kw___unknown_anytype:
4100
Reid Spencer5f016e22007-07-11 17:01:13 +00004101 // type-specifiers
4102 case tok::kw_short:
4103 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00004104 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00004105 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00004106 case tok::kw_signed:
4107 case tok::kw_unsigned:
4108 case tok::kw__Complex:
4109 case tok::kw__Imaginary:
4110 case tok::kw_void:
4111 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00004112 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00004113 case tok::kw_char16_t:
4114 case tok::kw_char32_t:
4115
Reid Spencer5f016e22007-07-11 17:01:13 +00004116 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004117 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00004118 case tok::kw_float:
4119 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00004120 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00004121 case tok::kw__Bool:
4122 case tok::kw__Decimal32:
4123 case tok::kw__Decimal64:
4124 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00004125 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00004126
Chris Lattner99dc9142008-04-13 18:59:07 +00004127 // struct-or-union-specifier (C99) or class-specifier (C++)
4128 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00004129 case tok::kw_struct:
4130 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00004131 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00004132 // enum-specifier
4133 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00004134
Reid Spencer5f016e22007-07-11 17:01:13 +00004135 // type-qualifier
4136 case tok::kw_const:
4137 case tok::kw_volatile:
4138 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00004139
Reid Spencer5f016e22007-07-11 17:01:13 +00004140 // function-specifier
4141 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00004142 case tok::kw_virtual:
4143 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00004144 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00004145
Richard Smith4cd81c52013-01-29 09:02:09 +00004146 // alignment-specifier
4147 case tok::kw__Alignas:
4148
Richard Smith53aec2a2012-10-25 00:00:53 +00004149 // friend keyword.
4150 case tok::kw_friend:
4151
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00004152 // static_assert-declaration
4153 case tok::kw__Static_assert:
4154
Chris Lattner1ef08762007-08-09 17:01:07 +00004155 // GNU typeof support.
4156 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00004157
Chris Lattner1ef08762007-08-09 17:01:07 +00004158 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00004159 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00004160
Richard Smith53aec2a2012-10-25 00:00:53 +00004161 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00004162 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00004163 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00004164
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004165 // C11 _Atomic
Eli Friedmanb001de72011-10-06 23:00:33 +00004166 case tok::kw__Atomic:
4167 return true;
4168
Chris Lattnerf3948c42008-07-26 03:38:44 +00004169 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4170 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00004171 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00004172
Douglas Gregord9d75e52011-04-27 05:41:15 +00004173 // typedef-name
4174 case tok::annot_typename:
4175 return !DisambiguatingWithExpression ||
4176 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00004177
Steve Naroff47f52092009-01-06 19:34:12 +00004178 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00004179 case tok::kw___cdecl:
4180 case tok::kw___stdcall:
4181 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004182 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00004183 case tok::kw___w64:
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004184 case tok::kw___sptr:
4185 case tok::kw___uptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00004186 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004187 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00004188 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004189 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004190 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004191
4192 case tok::kw___private:
4193 case tok::kw___local:
4194 case tok::kw___global:
4195 case tok::kw___constant:
4196 case tok::kw___read_only:
4197 case tok::kw___read_write:
4198 case tok::kw___write_only:
4199
Eli Friedman290eeb02009-06-08 23:27:34 +00004200 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00004201 }
4202}
4203
Stephen Hines651f13c2014-04-23 16:59:28 -07004204bool Parser::isConstructorDeclarator(bool IsUnqualified) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004205 TentativeParsingAction TPA(*this);
4206
4207 // Parse the C++ scope specifier.
4208 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00004209 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004210 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00004211 TPA.Revert();
4212 return false;
4213 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004214
4215 // Parse the constructor name.
4216 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4217 // We already know that we have a constructor name; just consume
4218 // the token.
4219 ConsumeToken();
4220 } else {
4221 TPA.Revert();
4222 return false;
4223 }
4224
Richard Smith22592862012-03-27 23:05:05 +00004225 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004226 if (Tok.isNot(tok::l_paren)) {
4227 TPA.Revert();
4228 return false;
4229 }
4230 ConsumeParen();
4231
Richard Smith22592862012-03-27 23:05:05 +00004232 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4233 // that we have a constructor.
4234 if (Tok.is(tok::r_paren) ||
4235 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004236 TPA.Revert();
4237 return true;
4238 }
4239
Richard Smith9ec28912013-09-06 00:12:20 +00004240 // A C++11 attribute here signals that we have a constructor, and is an
4241 // attribute on the first constructor parameter.
4242 if (getLangOpts().CPlusPlus11 &&
4243 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4244 /*OuterMightBeMessageSend*/ true)) {
4245 TPA.Revert();
4246 return true;
4247 }
4248
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004249 // If we need to, enter the specified scope.
4250 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00004251 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004252 DeclScopeObj.EnterDeclaratorScope();
4253
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004254 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00004255 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004256 MaybeParseMicrosoftAttributes(Attrs);
4257
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004258 // Check whether the next token(s) are part of a declaration
4259 // specifier, in which case we have the start of a parameter and,
4260 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004261 bool IsConstructor = false;
4262 if (isDeclarationSpecifier())
4263 IsConstructor = true;
4264 else if (Tok.is(tok::identifier) ||
4265 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4266 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4267 // This might be a parenthesized member name, but is more likely to
4268 // be a constructor declaration with an invalid argument type. Keep
4269 // looking.
4270 if (Tok.is(tok::annot_cxxscope))
4271 ConsumeToken();
4272 ConsumeToken();
4273
4274 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004275 // which must have one of the following syntactic forms (see the
4276 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004277 switch (Tok.getKind()) {
4278 case tok::l_paren:
4279 // C(X ( int));
4280 case tok::l_square:
4281 // C(X [ 5]);
4282 // C(X [ [attribute]]);
4283 case tok::coloncolon:
4284 // C(X :: Y);
4285 // C(X :: *p);
Richard Smith412e0cc2012-03-27 00:56:56 +00004286 // Assume this isn't a constructor, rather than assuming it's a
4287 // constructor with an unnamed parameter of an ill-formed type.
4288 break;
4289
Stephen Hines651f13c2014-04-23 16:59:28 -07004290 case tok::r_paren:
4291 // C(X )
4292 if (NextToken().is(tok::colon) || NextToken().is(tok::kw_try)) {
4293 // Assume these were meant to be constructors:
4294 // C(X) : (the name of a bit-field cannot be parenthesized).
4295 // C(X) try (this is otherwise ill-formed).
4296 IsConstructor = true;
4297 }
4298 if (NextToken().is(tok::semi) || NextToken().is(tok::l_brace)) {
4299 // If we have a constructor name within the class definition,
4300 // assume these were meant to be constructors:
4301 // C(X) {
4302 // C(X) ;
4303 // ... because otherwise we would be declaring a non-static data
4304 // member that is ill-formed because it's of the same type as its
4305 // surrounding class.
4306 //
4307 // FIXME: We can actually do this whether or not the name is qualified,
4308 // because if it is qualified in this context it must be being used as
4309 // a constructor name. However, we do not implement that rule correctly
4310 // currently, so we're somewhat conservative here.
4311 IsConstructor = IsUnqualified;
4312 }
4313 break;
4314
Richard Smith412e0cc2012-03-27 00:56:56 +00004315 default:
4316 IsConstructor = true;
4317 break;
4318 }
4319 }
4320
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004321 TPA.Revert();
4322 return IsConstructor;
4323}
Reid Spencer5f016e22007-07-11 17:01:13 +00004324
4325/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004326/// type-qualifier-list: [C99 6.7.5]
4327/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004328/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004329/// [ only if VendorAttributesAllowed=true ]
4330/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004331/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004332/// [ only if VendorAttributesAllowed=true ]
4333/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004334/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004335/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004336///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004337void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4338 bool VendorAttributesAllowed,
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004339 bool CXX11AttributesAllowed,
Bill Wendling7f3ec662013-11-26 04:10:07 +00004340 bool AtomicAllowed,
4341 bool IdentifierRequired) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004342 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004343 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004344 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004345 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004346 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004347 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004348
4349 SourceLocation EndLoc;
4350
Reid Spencer5f016e22007-07-11 17:01:13 +00004351 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004352 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004353 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004354 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004355 SourceLocation Loc = Tok.getLocation();
4356
4357 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004358 case tok::code_completion:
4359 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004360 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004361
Reid Spencer5f016e22007-07-11 17:01:13 +00004362 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004363 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004364 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004365 break;
4366 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004367 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004368 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004369 break;
4370 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004371 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004372 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004373 break;
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004374 case tok::kw__Atomic:
4375 if (!AtomicAllowed)
4376 goto DoneWithTypeQuals;
4377 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4378 getLangOpts());
4379 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004380
4381 // OpenCL qualifiers:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004382 case tok::kw___private:
4383 case tok::kw___global:
4384 case tok::kw___local:
4385 case tok::kw___constant:
4386 case tok::kw___read_only:
4387 case tok::kw___write_only:
4388 case tok::kw___read_write:
Stephen Hines651f13c2014-04-23 16:59:28 -07004389 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004390 break;
4391
Aaron Ballmanaa9df092013-05-22 23:25:32 +00004392 case tok::kw___uptr:
Bill Wendling7f3ec662013-11-26 04:10:07 +00004393 // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
4394 // with the MS modifier keyword.
4395 if (VendorAttributesAllowed && !getLangOpts().CPlusPlus &&
Bill Wendling03e463e2013-12-16 02:32:55 +00004396 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
4397 if (TryKeywordIdentFallback(false))
4398 continue;
Bill Wendling7f3ec662013-11-26 04:10:07 +00004399 }
4400 case tok::kw___sptr:
Eli Friedman290eeb02009-06-08 23:27:34 +00004401 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004402 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004403 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004404 case tok::kw___cdecl:
4405 case tok::kw___stdcall:
4406 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004407 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004408 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004409 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004410 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004411 continue;
4412 }
4413 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004414 case tok::kw___pascal:
4415 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004416 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004417 continue;
4418 }
4419 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004420 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004421 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004422 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004423 continue; // do *not* consume the next token!
4424 }
4425 // otherwise, FALL THROUGH!
4426 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004427 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004428 // If this is not a type-qualifier token, we're done reading type
4429 // qualifiers. First verify that DeclSpec's are consistent.
Stephen Hines651f13c2014-04-23 16:59:28 -07004430 DS.Finish(Diags, PP, Actions.getASTContext().getPrintingPolicy());
Abramo Bagnara796aa442011-03-12 11:17:06 +00004431 if (EndLoc.isValid())
4432 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004433 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004434 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004435
Reid Spencer5f016e22007-07-11 17:01:13 +00004436 // If the specifier combination wasn't legal, issue a diagnostic.
4437 if (isInvalid) {
4438 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004439 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004440 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004441 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004442 }
4443}
4444
4445
4446/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4447///
4448void Parser::ParseDeclarator(Declarator &D) {
4449 /// This implements the 'declarator' production in the C grammar, then checks
4450 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004451 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004452}
4453
Richard Smith9988f282012-03-29 01:16:42 +00004454static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4455 if (Kind == tok::star || Kind == tok::caret)
4456 return true;
4457
4458 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4459 if (!Lang.CPlusPlus)
4460 return false;
4461
4462 return Kind == tok::amp || Kind == tok::ampamp;
4463}
4464
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004465/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4466/// is parsed by the function passed to it. Pass null, and the direct-declarator
4467/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004468/// ptr-operator production.
4469///
Richard Smith0706df42011-10-19 21:33:05 +00004470/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004471/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4472/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004473///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004474/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4475/// [C] pointer[opt] direct-declarator
4476/// [C++] direct-declarator
4477/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004478///
4479/// pointer: [C99 6.7.5]
4480/// '*' type-qualifier-list[opt]
4481/// '*' type-qualifier-list[opt] pointer
4482///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004483/// ptr-operator:
4484/// '*' cv-qualifier-seq[opt]
4485/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004486/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004487/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004488/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004489/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004490void Parser::ParseDeclaratorInternal(Declarator &D,
4491 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004492 if (Diags.hasAllExtensionsSilenced())
4493 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004494
Sebastian Redlf30208a2009-01-24 21:16:55 +00004495 // C++ member pointers start with a '::' or a nested-name.
4496 // Member pointers get special handling, since there's no place for the
4497 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004498 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004499 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4500 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004501 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4502 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004503 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004504 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004505
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004506 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004507 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004508 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004509 if (D.mayHaveIdentifier())
4510 D.getCXXScopeSpec() = SS;
4511 else
4512 AnnotateScopeToken(SS, true);
4513
Sebastian Redlf30208a2009-01-24 21:16:55 +00004514 if (DirectDeclParser)
4515 (this->*DirectDeclParser)(D);
4516 return;
4517 }
4518
4519 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004520 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004521 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004522 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004523 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004524
4525 // Recurse to parse whatever is left.
4526 ParseDeclaratorInternal(D, DirectDeclParser);
4527
4528 // Sema will have to catch (syntactically invalid) pointers into global
4529 // scope. It has to catch pointers into namespace scope anyway.
4530 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004531 Loc),
4532 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004533 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004534 return;
4535 }
4536 }
4537
4538 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004539 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004540 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004541 if (DirectDeclParser)
4542 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004543 return;
4544 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004545
Sebastian Redl05532f22009-03-15 22:02:01 +00004546 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4547 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004548 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004549 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004550
Chris Lattner9af55002009-03-27 04:18:06 +00004551 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004552 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004553 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004554
Richard Smith6ee326a2012-04-10 01:32:12 +00004555 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling7f3ec662013-11-26 04:10:07 +00004556 ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier());
Sebastian Redlab197ba2009-02-09 18:23:29 +00004557 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004558
Reid Spencer5f016e22007-07-11 17:01:13 +00004559 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004560 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004561 if (Kind == tok::star)
4562 // Remember that we parsed a pointer type, and remember the type-quals.
4563 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004564 DS.getConstSpecLoc(),
4565 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004566 DS.getRestrictSpecLoc()),
4567 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004568 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004569 else
4570 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004571 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004572 Loc),
4573 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004574 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004575 } else {
4576 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004577 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004578
Sebastian Redl743de1f2009-03-23 00:00:23 +00004579 // Complain about rvalue references in C++03, but then go on and build
4580 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004581 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004582 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004583 diag::warn_cxx98_compat_rvalue_reference :
4584 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004585
Richard Smith6ee326a2012-04-10 01:32:12 +00004586 // GNU-style and C++11 attributes are allowed here, as is restrict.
4587 ParseTypeQualifierListOpt(DS);
4588 D.ExtendWithDeclSpec(DS);
4589
Reid Spencer5f016e22007-07-11 17:01:13 +00004590 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4591 // cv-qualifiers are introduced through the use of a typedef or of a
4592 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004593 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4594 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4595 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004596 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004597 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4598 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004599 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004600 // 'restrict' is permitted as an extension.
4601 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4602 Diag(DS.getAtomicSpecLoc(),
4603 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Reid Spencer5f016e22007-07-11 17:01:13 +00004604 }
4605
4606 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004607 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004608
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004609 if (D.getNumTypeObjects() > 0) {
4610 // C++ [dcl.ref]p4: There shall be no references to references.
4611 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4612 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004613 if (const IdentifierInfo *II = D.getIdentifier())
4614 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4615 << II;
4616 else
4617 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4618 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004619
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004620 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004621 // can go ahead and build the (technically ill-formed)
4622 // declarator: reference collapsing will take care of it.
4623 }
4624 }
4625
Richard Smith4cf4a5e2013-03-28 01:55:44 +00004626 // Remember that we parsed a reference type.
Chris Lattner76549142008-02-21 01:32:26 +00004627 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004628 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004629 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004630 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004631 }
4632}
4633
Richard Smith9988f282012-03-29 01:16:42 +00004634static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4635 SourceLocation EllipsisLoc) {
4636 if (EllipsisLoc.isValid()) {
4637 FixItHint Insertion;
4638 if (!D.getEllipsisLoc().isValid()) {
4639 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4640 D.setEllipsisLoc(EllipsisLoc);
4641 }
4642 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4643 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4644 }
4645}
4646
Reid Spencer5f016e22007-07-11 17:01:13 +00004647/// ParseDirectDeclarator
4648/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004649/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004650/// '(' declarator ')'
4651/// [GNU] '(' attributes declarator ')'
4652/// [C90] direct-declarator '[' constant-expression[opt] ']'
4653/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4654/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4655/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4656/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004657/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4658/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004659/// direct-declarator '(' parameter-type-list ')'
4660/// direct-declarator '(' identifier-list[opt] ')'
4661/// [GNU] direct-declarator '(' parameter-forward-declarations
4662/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004663/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4664/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004665/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4666/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4667/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004668/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004669/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004670///
4671/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004672/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004673/// '::'[opt] nested-name-specifier[opt] type-name
4674///
4675/// id-expression: [C++ 5.1]
4676/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004677/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004678///
4679/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004680/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004681/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004682/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004683/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004684/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004685///
Richard Smith5d8388c2012-03-27 01:42:32 +00004686/// Note, any additional constructs added here may need corresponding changes
4687/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004688void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004689 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004690
David Blaikie4e4d0842012-03-11 07:00:24 +00004691 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004692 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004693 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004694 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4695 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004696 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004697 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004698 }
4699
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004700 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004701 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004702 // Change the declaration context for name lookup, until this function
4703 // is exited (and the declarator has been parsed).
4704 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004705 }
4706
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004707 // C++0x [dcl.fct]p14:
4708 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004709 // of a parameter-declaration-clause without a preceding comma. In
4710 // this case, the ellipsis is parsed as part of the
4711 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004712 // parameter pack that has not been expanded; otherwise, it is parsed
4713 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004714 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004715 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Valifad9e132013-09-26 19:54:12 +00004716 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004717 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004718 NextToken().is(tok::r_paren) &&
Richard Smith30f2a742013-02-20 20:19:27 +00004719 !D.hasGroupingParens() &&
Richard Smith9988f282012-03-29 01:16:42 +00004720 !Actions.containsUnexpandedParameterPacks(D))) {
4721 SourceLocation EllipsisLoc = ConsumeToken();
4722 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4723 // The ellipsis was put in the wrong place. Recover, and explain to
4724 // the user what they should have done.
4725 ParseDeclarator(D);
4726 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4727 return;
4728 } else
4729 D.setEllipsisLoc(EllipsisLoc);
4730
4731 // The ellipsis can't be followed by a parenthesized declarator. We
4732 // check for that in ParseParenDeclarator, after we have disambiguated
4733 // the l_paren token.
4734 }
4735
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004736 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4737 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4738 // We found something that indicates the start of an unqualified-id.
4739 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004740 bool AllowConstructorName;
4741 if (D.getDeclSpec().hasTypeSpecifier())
4742 AllowConstructorName = false;
4743 else if (D.getCXXScopeSpec().isSet())
4744 AllowConstructorName =
4745 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00004746 D.getContext() == Declarator::MemberContext);
John McCallba9d8532010-04-13 06:39:49 +00004747 else
4748 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4749
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004750 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004751 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4752 /*EnteringContext=*/true,
4753 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004754 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004755 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004756 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004757 D.getName()) ||
4758 // Once we're past the identifier, if the scope was bad, mark the
4759 // whole declarator bad.
4760 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004761 D.SetIdentifier(0, Tok.getLocation());
4762 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004763 } else {
4764 // Parsed the unqualified-id; update range information and move along.
4765 if (D.getSourceRange().getBegin().isInvalid())
4766 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4767 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004768 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004769 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004770 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004771 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004772 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004773 "There's a C++-specific check for tok::identifier above");
4774 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4775 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4776 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004777 goto PastIdentifier;
Richard Smitha38253c2013-07-11 05:10:21 +00004778 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smith8d1ab8a2013-10-13 22:12:28 +00004779 // A virt-specifier isn't treated as an identifier if it appears after a
4780 // trailing-return-type.
4781 if (D.getContext() != Declarator::TrailingReturnContext ||
4782 !isCXX11VirtSpecifier(Tok)) {
4783 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4784 << FixItHint::CreateRemoval(Tok.getLocation());
4785 D.SetIdentifier(0, Tok.getLocation());
4786 ConsumeToken();
4787 goto PastIdentifier;
4788 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004789 }
Richard Smith9988f282012-03-29 01:16:42 +00004790
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004791 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004792 // direct-declarator: '(' declarator ')'
4793 // direct-declarator: '(' attributes declarator ')'
4794 // Example: 'char (*X)' or 'int (*XX)(void)'
4795 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004796
4797 // If the declarator was parenthesized, we entered the declarator
4798 // scope when parsing the parenthesized declarator, then exited
4799 // the scope already. Re-enter the scope, if we need to.
4800 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004801 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004802 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004803 if (!D.isInvalidType() &&
4804 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004805 // Change the declaration context for name lookup, until this function
4806 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004807 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004808 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004809 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004810 // This could be something simple like "int" (in which case the declarator
4811 // portion is empty), if an abstract-declarator is allowed.
4812 D.SetIdentifier(0, Tok.getLocation());
Richard Smith30f2a742013-02-20 20:19:27 +00004813
4814 // The grammar for abstract-pack-declarator does not allow grouping parens.
4815 // FIXME: Revisit this once core issue 1488 is resolved.
4816 if (D.hasEllipsis() && D.hasGroupingParens())
4817 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4818 diag::ext_abstract_pack_declarator_parens);
Reid Spencer5f016e22007-07-11 17:01:13 +00004819 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004820 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004821 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004822 if (D.getContext() == Declarator::MemberContext)
4823 Diag(Tok, diag::err_expected_member_name_or_semi)
4824 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004825 else if (getLangOpts().CPlusPlus) {
4826 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4827 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieuefb288c2013-09-05 02:31:33 +00004828 else {
4829 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4830 if (Tok.isAtStartOfLine() && Loc.isValid())
4831 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4832 << getLangOpts().CPlusPlus;
4833 else
4834 Diag(Tok, diag::err_expected_unqualified_id)
4835 << getLangOpts().CPlusPlus;
4836 }
Richard Trieudb55c04c2013-01-26 02:31:38 +00004837 } else
Stephen Hines651f13c2014-04-23 16:59:28 -07004838 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00004839 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004840 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004841 }
Mike Stump1eb44332009-09-09 15:08:12 +00004842
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004843 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004844 assert(D.isPastIdentifier() &&
4845 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004846
Richard Smith6ee326a2012-04-10 01:32:12 +00004847 // Don't parse attributes unless we have parsed an unparenthesized name.
4848 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004849 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004850
Reid Spencer5f016e22007-07-11 17:01:13 +00004851 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004852 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004853 // Enter function-declaration scope, limiting any declarators to the
4854 // function prototype scope, including parameter declarators.
4855 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004856 Scope::FunctionPrototypeScope|Scope::DeclScope|
4857 (D.isFunctionDeclaratorAFunctionDeclaration()
4858 ? Scope::FunctionDeclarationScope : 0));
4859
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004860 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4861 // In such a case, check if we actually have a function declarator; if it
4862 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004863 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004864 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4865 // The name of the declarator, if any, is tentatively declared within
4866 // a possible direct initializer.
4867 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4868 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4869 TentativelyDeclaredIdentifiers.pop_back();
4870 if (!IsFunctionDecl)
4871 break;
4872 }
John McCall0b7e6782011-03-24 11:26:52 +00004873 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004874 BalancedDelimiterTracker T(*this, tok::l_paren);
4875 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004876 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004877 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004878 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004879 ParseBracketDeclarator(D);
4880 } else {
4881 break;
4882 }
4883 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004884}
Reid Spencer5f016e22007-07-11 17:01:13 +00004885
Chris Lattneref4715c2008-04-06 05:45:57 +00004886/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4887/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004888/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004889/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4890///
4891/// direct-declarator:
4892/// '(' declarator ')'
4893/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004894/// direct-declarator '(' parameter-type-list ')'
4895/// direct-declarator '(' identifier-list[opt] ')'
4896/// [GNU] direct-declarator '(' parameter-forward-declarations
4897/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004898///
4899void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004900 BalancedDelimiterTracker T(*this, tok::l_paren);
4901 T.consumeOpen();
4902
Chris Lattneref4715c2008-04-06 05:45:57 +00004903 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004904
Chris Lattner7399ee02008-10-20 02:05:46 +00004905 // Eat any attributes before we look at whether this is a grouping or function
4906 // declarator paren. If this is a grouping paren, the attribute applies to
4907 // the type being built up, for example:
4908 // int (__attribute__(()) *x)(long y)
4909 // If this ends up not being a grouping paren, the attribute applies to the
4910 // first argument, for example:
4911 // int (__attribute__(()) int x)
4912 // In either case, we need to eat any attributes to be able to determine what
4913 // sort of paren this is.
4914 //
John McCall0b7e6782011-03-24 11:26:52 +00004915 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004916 bool RequiresArg = false;
4917 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004918 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004919
Chris Lattner7399ee02008-10-20 02:05:46 +00004920 // We require that the argument list (if this is a non-grouping paren) be
4921 // present even if the attribute list was empty.
4922 RequiresArg = true;
4923 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004924
Steve Naroff239f0732008-12-25 14:16:32 +00004925 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004926 ParseMicrosoftTypeAttributes(attrs);
4927
Dawn Perchik52fc3142010-09-03 01:29:35 +00004928 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004929 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004930 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004931
Chris Lattneref4715c2008-04-06 05:45:57 +00004932 // If we haven't past the identifier yet (or where the identifier would be
4933 // stored, if this is an abstract declarator), then this is probably just
4934 // grouping parens. However, if this could be an abstract-declarator, then
4935 // this could also be the start of function arguments (consider 'void()').
4936 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004937
Chris Lattneref4715c2008-04-06 05:45:57 +00004938 if (!D.mayOmitIdentifier()) {
4939 // If this can't be an abstract-declarator, this *must* be a grouping
4940 // paren, because we haven't seen the identifier yet.
4941 isGrouping = true;
4942 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004943 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4944 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004945 isDeclarationSpecifier() || // 'int(int)' is a function.
4946 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004947 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4948 // considered to be a type, not a K&R identifier-list.
4949 isGrouping = false;
4950 } else {
4951 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4952 isGrouping = true;
4953 }
Mike Stump1eb44332009-09-09 15:08:12 +00004954
Chris Lattneref4715c2008-04-06 05:45:57 +00004955 // If this is a grouping paren, handle:
4956 // direct-declarator: '(' declarator ')'
4957 // direct-declarator: '(' attributes declarator ')'
4958 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004959 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4960 D.setEllipsisLoc(SourceLocation());
4961
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004962 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004963 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004964 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004965 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004966 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004967 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004968 T.getCloseLocation()),
4969 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004970
4971 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004972
4973 // An ellipsis cannot be placed outside parentheses.
4974 if (EllipsisLoc.isValid())
4975 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4976
Chris Lattneref4715c2008-04-06 05:45:57 +00004977 return;
4978 }
Mike Stump1eb44332009-09-09 15:08:12 +00004979
Chris Lattneref4715c2008-04-06 05:45:57 +00004980 // Okay, if this wasn't a grouping paren, it must be the start of a function
4981 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004982 // identifier (and remember where it would have been), then call into
4983 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004984 D.SetIdentifier(0, Tok.getLocation());
4985
David Blaikie42d6d0c2011-12-04 05:04:18 +00004986 // Enter function-declaration scope, limiting any declarators to the
4987 // function prototype scope, including parameter declarators.
4988 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004989 Scope::FunctionPrototypeScope | Scope::DeclScope |
4990 (D.isFunctionDeclaratorAFunctionDeclaration()
4991 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00004992 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004993 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004994}
4995
4996/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4997/// declarator D up to a paren, which indicates that we are parsing function
4998/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004999///
Richard Smith6ee326a2012-04-10 01:32:12 +00005000/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5001/// immediately after the open paren - they should be considered to be the
5002/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00005003///
Richard Smith6ee326a2012-04-10 01:32:12 +00005004/// If RequiresArg is true, then the first argument of the function is required
5005/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005006///
Richard Smith6ee326a2012-04-10 01:32:12 +00005007/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5008/// (C++11) ref-qualifier[opt], exception-specification[opt],
5009/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5010///
5011/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005012/// dynamic-exception-specification
5013/// noexcept-specification
5014///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005015void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00005016 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005017 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00005018 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005019 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00005020 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00005021 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005022 // lparen is already consumed!
5023 assert(D.isPastIdentifier() && "Should not call before identifier!");
5024
5025 // This should be true when the function has typed arguments.
5026 // Otherwise, it is treated as a K&R-style function.
5027 bool HasProto = false;
5028 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005029 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005030 // Remember where we see an ellipsis, if any.
5031 SourceLocation EllipsisLoc;
5032
5033 DeclSpec DS(AttrFactory);
5034 bool RefQualifierIsLValueRef = true;
5035 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00005036 SourceLocation ConstQualifierLoc;
5037 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005038 ExceptionSpecificationType ESpecType = EST_None;
5039 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005040 SmallVector<ParsedType, 2> DynamicExceptions;
5041 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005042 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00005043 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00005044 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00005045
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005046 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5047 EndLoc is the end location for the function declarator.
5048 They differ for trailing return types. */
5049 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005050 SourceLocation LParenLoc, RParenLoc;
5051 LParenLoc = Tracker.getOpenLocation();
5052 StartLoc = LParenLoc;
5053
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005054 if (isFunctionDeclaratorIdentifierList()) {
5055 if (RequiresArg)
5056 Diag(Tok, diag::err_argument_required_after_attribute);
5057
5058 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5059
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005060 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005061 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005062 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005063 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005064 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005065 if (Tok.isNot(tok::r_paren))
Faisal Valifad9e132013-09-26 19:54:12 +00005066 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5067 EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005068 else if (RequiresArg)
5069 Diag(Tok, diag::err_argument_required_after_attribute);
5070
David Blaikie4e4d0842012-03-11 07:00:24 +00005071 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005072
5073 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005074 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005075 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005076 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005077 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005078
David Blaikie4e4d0842012-03-11 07:00:24 +00005079 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005080 // FIXME: Accept these components in any order, and produce fixits to
5081 // correct the order if the user gets it wrong. Ideally we should deal
5082 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005083
5084 // Parse cv-qualifier-seq[opt].
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005085 ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false,
5086 /*CXX11AttributesAllowed*/ false,
5087 /*AtomicAllowed*/ false);
Richard Smith6ee326a2012-04-10 01:32:12 +00005088 if (!DS.getSourceRange().getEnd().isInvalid()) {
5089 EndLoc = DS.getSourceRange().getEnd();
5090 ConstQualifierLoc = DS.getConstSpecLoc();
5091 VolatileQualifierLoc = DS.getVolatileSpecLoc();
5092 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005093
5094 // Parse ref-qualifier[opt].
5095 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00005096 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00005097 diag::warn_cxx98_compat_ref_qualifier :
5098 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00005099
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005100 RefQualifierIsLValueRef = Tok.is(tok::amp);
5101 RefQualifierLoc = ConsumeToken();
5102 EndLoc = RefQualifierLoc;
5103 }
5104
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005105 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00005106 // If a declaration declares a member function or member function
5107 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005108 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00005109 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005110 // declarator.
Richard Smithd9227792013-03-15 00:41:52 +00005111 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosier8decdee2012-06-26 22:30:43 +00005112 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00005113 getLangOpts().CPlusPlus11 &&
Stephen Hines651f13c2014-04-23 16:59:28 -07005114 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Richard Smithd9227792013-03-15 00:41:52 +00005115 (D.getContext() == Declarator::MemberContext
5116 ? !D.getDeclSpec().isFriendSpecified()
5117 : D.getContext() == Declarator::FileContext &&
5118 D.getCXXScopeSpec().isValid() &&
5119 Actions.CurContext->isRecord());
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005120 Sema::CXXThisScopeRAII ThisScope(Actions,
5121 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00005122 DS.getTypeQualifiers() |
Richard Smith84046262013-04-21 01:08:50 +00005123 (D.getDeclSpec().isConstexprSpecified() &&
5124 !getLangOpts().CPlusPlus1y
Richard Smith7b19cb12013-01-14 01:55:13 +00005125 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00005126 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00005127
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005128 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00005129 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00005130 DynamicExceptions,
5131 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00005132 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005133 if (ESpecType != EST_None)
5134 EndLoc = ESpecRange.getEnd();
5135
Richard Smith6ee326a2012-04-10 01:32:12 +00005136 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5137 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005138 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00005139
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005140 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005141 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00005142 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00005143 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005144 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5145 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005146 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00005147 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00005148 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005149 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005150 }
5151 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005152 }
5153
5154 // Remember that we parsed a function type, and remember the attributes.
5155 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005156 IsAmbiguous,
5157 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005158 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00005159 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005160 DS.getTypeQualifiers(),
5161 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00005162 RefQualifierLoc, ConstQualifierLoc,
5163 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00005164 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005165 ESpecType, ESpecRange.getBegin(),
5166 DynamicExceptions.data(),
5167 DynamicExceptionRanges.data(),
5168 DynamicExceptions.size(),
5169 NoexceptExpr.isUsable() ?
5170 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00005171 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005172 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00005173 FnAttrs, EndLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005174}
5175
5176/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5177/// identifier list form for a K&R-style function: void foo(a,b,c)
5178///
5179/// Note that identifier-lists are only allowed for normal declarators, not for
5180/// abstract-declarators.
5181bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00005182 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005183 && Tok.is(tok::identifier)
5184 && !TryAltiVecVectorToken()
5185 // K&R identifier lists can't have typedefs as identifiers, per C99
5186 // 6.7.5.3p11.
5187 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5188 // Identifier lists follow a really simple grammar: the identifiers can
5189 // be followed *only* by a ", identifier" or ")". However, K&R
5190 // identifier lists are really rare in the brave new modern world, and
5191 // it is very common for someone to typo a type in a non-K&R style
5192 // list. If we are presented with something like: "void foo(intptr x,
5193 // float y)", we don't want to start parsing the function declarator as
5194 // though it is a K&R style declarator just because intptr is an
5195 // invalid type.
5196 //
5197 // To handle this, we check to see if the token after the first
5198 // identifier is a "," or ")". Only then do we parse it as an
5199 // identifier list.
5200 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5201}
5202
5203/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5204/// we found a K&R-style identifier list instead of a typed parameter list.
5205///
5206/// After returning, ParamInfo will hold the parsed parameters.
5207///
5208/// identifier-list: [C99 6.7.5]
5209/// identifier
5210/// identifier-list ',' identifier
5211///
5212void Parser::ParseFunctionDeclaratorIdentifierList(
5213 Declarator &D,
Craig Topper6b9240e2013-07-05 19:34:19 +00005214 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005215 // If there was no identifier specified for the declarator, either we are in
5216 // an abstract-declarator, or we are in a parameter declarator which was found
5217 // to be abstract. In abstract-declarators, identifier lists are not valid:
5218 // diagnose this.
5219 if (!D.getIdentifier())
5220 Diag(Tok, diag::ext_ident_list_in_param);
5221
5222 // Maintain an efficient lookup of params we have seen so far.
5223 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5224
Stephen Hines651f13c2014-04-23 16:59:28 -07005225 do {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005226 // If this isn't an identifier, report the error and skip until ')'.
5227 if (Tok.isNot(tok::identifier)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07005228 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataev8fe24752013-11-18 08:17:37 +00005229 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005230 // Forget we parsed anything.
5231 ParamInfo.clear();
5232 return;
5233 }
5234
5235 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5236
5237 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5238 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5239 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5240
5241 // Verify that the argument identifier has not already been mentioned.
5242 if (!ParamsSoFar.insert(ParmII)) {
5243 Diag(Tok, diag::err_param_redefinition) << ParmII;
5244 } else {
5245 // Remember this identifier in ParamInfo.
5246 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5247 Tok.getLocation(),
5248 0));
5249 }
5250
5251 // Eat the identifier.
5252 ConsumeToken();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005253 // The list continues if we see a comma.
Stephen Hines651f13c2014-04-23 16:59:28 -07005254 } while (TryConsumeToken(tok::comma));
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005255}
5256
5257/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5258/// after the opening parenthesis. This function will not parse a K&R-style
5259/// identifier list.
5260///
Richard Smith6ce48a72012-04-11 04:01:28 +00005261/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5262/// caller parsed those arguments immediately after the open paren - they should
5263/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005264///
5265/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5266/// be the location of the ellipsis, if any was parsed.
5267///
Reid Spencer5f016e22007-07-11 17:01:13 +00005268/// parameter-type-list: [C99 6.7.5]
5269/// parameter-list
5270/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00005271/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00005272///
5273/// parameter-list: [C99 6.7.5]
5274/// parameter-declaration
5275/// parameter-list ',' parameter-declaration
5276///
5277/// parameter-declaration: [C99 6.7.5]
5278/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00005279/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00005280/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00005281/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00005282/// declaration-specifiers abstract-declarator[opt]
5283/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00005284/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00005285/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00005286/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00005287///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005288void Parser::ParseParameterDeclarationClause(
5289 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00005290 ParsedAttributes &FirstArgAttrs,
Craig Topper6b9240e2013-07-05 19:34:19 +00005291 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005292 SourceLocation &EllipsisLoc) {
Stephen Hines651f13c2014-04-23 16:59:28 -07005293 do {
5294 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5295 // before deciding this was a parameter-declaration-clause.
5296 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Chris Lattnerf97409f2008-04-06 06:57:35 +00005297 break;
Mike Stump1eb44332009-09-09 15:08:12 +00005298
Chris Lattnerf97409f2008-04-06 06:57:35 +00005299 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00005300 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00005301 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005302
Richard Smith6ce48a72012-04-11 04:01:28 +00005303 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005304 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00005305
John McCall7f040a92010-12-24 02:08:15 +00005306 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00005307 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00005308
5309 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00005310
5311 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00005312 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005313 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00005314 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5315 // too much hassle.
5316 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00005317
Chris Lattnere64c5492009-02-27 18:38:20 +00005318 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005319
Faisal Valifad9e132013-09-26 19:54:12 +00005320
5321 // Parse the declarator. This is "PrototypeContext" or
5322 // "LambdaExprParameterContext", because we must accept either
5323 // 'declarator' or 'abstract-declarator' here.
5324 Declarator ParmDeclarator(DS,
5325 D.getContext() == Declarator::LambdaExprContext ?
5326 Declarator::LambdaExprParameterContext :
5327 Declarator::PrototypeContext);
5328 ParseDeclarator(ParmDeclarator);
Chris Lattnerf97409f2008-04-06 06:57:35 +00005329
5330 // Parse GNU attributes, if present.
Faisal Valifad9e132013-09-26 19:54:12 +00005331 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump1eb44332009-09-09 15:08:12 +00005332
Chris Lattnerf97409f2008-04-06 06:57:35 +00005333 // Remember this parsed parameter in ParamInfo.
Faisal Valifad9e132013-09-26 19:54:12 +00005334 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005335
Douglas Gregor72b505b2008-12-16 21:30:33 +00005336 // DefArgToks is used when the parsing of default arguments needs
5337 // to be delayed.
5338 CachedTokens *DefArgToks = 0;
5339
Chris Lattnerf97409f2008-04-06 06:57:35 +00005340 // If no parameter was specified, verify that *something* was specified,
5341 // otherwise we have a missing type and identifier.
Faisal Valifad9e132013-09-26 19:54:12 +00005342 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 &&
5343 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005344 // Completely missing, emit error.
5345 Diag(DSStart, diag::err_missing_param);
5346 } else {
5347 // Otherwise, we have something. Add it and let semantic analysis try
5348 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005349
Chris Lattnerf97409f2008-04-06 06:57:35 +00005350 // Inform the actions module about the parameter declarator, so it gets
5351 // added to the current scope.
Faisal Valifad9e132013-09-26 19:54:12 +00005352 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(),
5353 ParmDeclarator);
Chris Lattner04421082008-04-08 04:40:51 +00005354 // Parse the default argument, if any. We parse the default
5355 // arguments in all dialects; the semantic analysis in
5356 // ActOnParamDefaultArgument will reject the default argument in
5357 // C.
5358 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005359 SourceLocation EqualLoc = Tok.getLocation();
5360
Chris Lattner04421082008-04-08 04:40:51 +00005361 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005362 if (D.getContext() == Declarator::MemberContext) {
5363 // If we're inside a class definition, cache the tokens
5364 // corresponding to the default argument. We'll actually parse
5365 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005366 // FIXME: Can we use a smart pointer for Toks?
5367 DefArgToks = new CachedTokens;
5368
Richard Smith9bd3cdc2013-09-12 23:28:08 +00005369 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005370 delete DefArgToks;
5371 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005372 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005373 } else {
5374 // Mark the end of the default argument so that we know when to
5375 // stop when we parse it later on.
5376 Token DefArgEnd;
5377 DefArgEnd.startToken();
5378 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5379 DefArgEnd.setLocation(Tok.getLocation());
5380 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005381 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005382 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005383 }
Chris Lattner04421082008-04-08 04:40:51 +00005384 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005385 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005386 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005387
Chad Rosier8decdee2012-06-26 22:30:43 +00005388 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005389 // used.
5390 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005391 Sema::PotentiallyEvaluatedIfUsed,
5392 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005393
Sebastian Redl84407ba2012-03-14 15:54:00 +00005394 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005395 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005396 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005397 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005398 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005399 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005400 if (DefArgResult.isInvalid()) {
5401 Actions.ActOnParamDefaultArgumentError(Param);
Alexey Bataev8fe24752013-11-18 08:17:37 +00005402 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor72b505b2008-12-16 21:30:33 +00005403 } else {
5404 // Inform the actions module about the default argument
5405 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005406 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005407 }
Chris Lattner04421082008-04-08 04:40:51 +00005408 }
5409 }
Mike Stump1eb44332009-09-09 15:08:12 +00005410
5411 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Valifad9e132013-09-26 19:54:12 +00005412 ParmDeclarator.getIdentifierLoc(),
5413 Param, DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005414 }
5415
Stephen Hines651f13c2014-04-23 16:59:28 -07005416 if (TryConsumeToken(tok::ellipsis, EllipsisLoc) &&
5417 !getLangOpts().CPlusPlus) {
5418 // We have ellipsis without a preceding ',', which is ill-formed
5419 // in C. Complain and provide the fix.
5420 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
5421 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005422 break;
5423 }
Mike Stump1eb44332009-09-09 15:08:12 +00005424
Stephen Hines651f13c2014-04-23 16:59:28 -07005425 // If the next token is a comma, consume it and keep reading arguments.
5426 } while (TryConsumeToken(tok::comma));
Chris Lattner66d28652008-04-06 06:34:08 +00005427}
Chris Lattneref4715c2008-04-06 05:45:57 +00005428
Reid Spencer5f016e22007-07-11 17:01:13 +00005429/// [C90] direct-declarator '[' constant-expression[opt] ']'
5430/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5431/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5432/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5433/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005434/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5435/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005436void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005437 if (CheckProhibitedCXX11Attribute())
5438 return;
5439
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005440 BalancedDelimiterTracker T(*this, tok::l_square);
5441 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005442
Chris Lattner378c7e42008-12-18 07:27:21 +00005443 // C array syntax has many features, but by-far the most common is [] and [4].
5444 // This code does a fast path to handle some of the most obvious cases.
5445 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005446 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005447 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005448 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005449
Chris Lattner378c7e42008-12-18 07:27:21 +00005450 // Remember that we parsed the empty array type.
John McCall0b7e6782011-03-24 11:26:52 +00005451 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005452 T.getOpenLocation(),
5453 T.getCloseLocation()),
5454 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005455 return;
5456 } else if (Tok.getKind() == tok::numeric_constant &&
5457 GetLookAheadToken(1).is(tok::r_square)) {
5458 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005459 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005460 ConsumeToken();
5461
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005462 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005463 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005464 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005465
Chris Lattner378c7e42008-12-18 07:27:21 +00005466 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005467 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005468 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005469 T.getOpenLocation(),
5470 T.getCloseLocation()),
5471 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005472 return;
5473 }
Mike Stump1eb44332009-09-09 15:08:12 +00005474
Reid Spencer5f016e22007-07-11 17:01:13 +00005475 // If valid, this location is the position where we read the 'static' keyword.
5476 SourceLocation StaticLoc;
Stephen Hines651f13c2014-04-23 16:59:28 -07005477 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005478
Reid Spencer5f016e22007-07-11 17:01:13 +00005479 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005480 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005481 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005482 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005483
Reid Spencer5f016e22007-07-11 17:01:13 +00005484 // If we haven't already read 'static', check to see if there is one after the
5485 // type-qualifier-list.
Stephen Hines651f13c2014-04-23 16:59:28 -07005486 if (!StaticLoc.isValid())
5487 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005488
Reid Spencer5f016e22007-07-11 17:01:13 +00005489 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5490 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005491 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005492
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005493 // Handle the case where we have '[*]' as the array size. However, a leading
5494 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005495 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005496 // infrequent, use of lookahead is not costly here.
5497 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005498 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005499
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005500 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005501 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005502 StaticLoc = SourceLocation(); // Drop the static.
5503 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005504 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005505 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005506 // Note, in C89, this production uses the constant-expr production instead
5507 // of assignment-expr. The only difference is that assignment-expr allows
5508 // things like '=' and '*='. Sema rejects these in C89 mode because they
5509 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005510
Douglas Gregore0762c92009-06-19 23:52:42 +00005511 // Parse the constant-expression or assignment-expression now (depending
5512 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005513 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005514 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005515 } else {
5516 EnterExpressionEvaluationContext Unevaluated(Actions,
5517 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005518 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005519 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005520 }
Mike Stump1eb44332009-09-09 15:08:12 +00005521
Reid Spencer5f016e22007-07-11 17:01:13 +00005522 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005523 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005524 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005525 // If the expression was invalid, skip it.
Alexey Bataev8fe24752013-11-18 08:17:37 +00005526 SkipUntil(tok::r_square, StopAtSemi);
Reid Spencer5f016e22007-07-11 17:01:13 +00005527 return;
5528 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005529
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005530 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005531
John McCall0b7e6782011-03-24 11:26:52 +00005532 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005533 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005534
Chris Lattner378c7e42008-12-18 07:27:21 +00005535 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005536 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005537 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005538 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005539 T.getOpenLocation(),
5540 T.getCloseLocation()),
5541 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005542}
5543
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005544/// [GNU] typeof-specifier:
5545/// typeof ( expressions )
5546/// typeof ( type-name )
5547/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005548///
5549void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005550 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005551 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005552 SourceLocation StartLoc = ConsumeToken();
5553
John McCallcfb708c2010-01-13 20:03:27 +00005554 const bool hasParens = Tok.is(tok::l_paren);
5555
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005556 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5557 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005558
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005559 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005560 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005561 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005562 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5563 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005564 if (hasParens)
5565 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005566
5567 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005568 // FIXME: Not accurate, the range gets one token more than it should.
5569 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005570 else
5571 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005572
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005573 if (isCastExpr) {
5574 if (!CastTy) {
5575 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005576 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005577 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005578
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005579 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005580 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005581 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5582 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07005583 DiagID, CastTy,
5584 Actions.getASTContext().getPrintingPolicy()))
John McCallfec54012009-08-03 20:12:06 +00005585 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005586 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005587 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005588
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005589 // If we get here, the operand to the typeof was an expresion.
5590 if (Operand.isInvalid()) {
5591 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005592 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005593 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005594
Eli Friedman71b8fb52012-01-21 01:01:51 +00005595 // We might need to transform the operand if it is potentially evaluated.
5596 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5597 if (Operand.isInvalid()) {
5598 DS.SetTypeSpecError();
5599 return;
5600 }
5601
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005602 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005603 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005604 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5605 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07005606 DiagID, Operand.get(),
5607 Actions.getASTContext().getPrintingPolicy()))
John McCallfec54012009-08-03 20:12:06 +00005608 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005609}
Chris Lattner1b492422010-02-28 18:33:55 +00005610
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005611/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005612/// _Atomic ( type-name )
5613///
5614void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005615 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5616 "Not an atomic specifier");
Eli Friedmanb001de72011-10-06 23:00:33 +00005617
5618 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005619 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith4cf4a5e2013-03-28 01:55:44 +00005620 if (T.consumeOpen())
Eli Friedmanb001de72011-10-06 23:00:33 +00005621 return;
Eli Friedmanb001de72011-10-06 23:00:33 +00005622
5623 TypeResult Result = ParseTypeName();
5624 if (Result.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00005625 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedmanb001de72011-10-06 23:00:33 +00005626 return;
5627 }
5628
5629 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005630 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005631
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005632 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005633 return;
5634
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005635 DS.setTypeofParensRange(T.getRange());
5636 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005637
5638 const char *PrevSpec = 0;
5639 unsigned DiagID;
5640 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -07005641 DiagID, Result.release(),
5642 Actions.getASTContext().getPrintingPolicy()))
Eli Friedmanb001de72011-10-06 23:00:33 +00005643 Diag(StartLoc, DiagID) << PrevSpec;
5644}
5645
Chris Lattner1b492422010-02-28 18:33:55 +00005646
5647/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5648/// from TryAltiVecVectorToken.
5649bool Parser::TryAltiVecVectorTokenOutOfLine() {
5650 Token Next = NextToken();
5651 switch (Next.getKind()) {
5652 default: return false;
5653 case tok::kw_short:
5654 case tok::kw_long:
5655 case tok::kw_signed:
5656 case tok::kw_unsigned:
5657 case tok::kw_void:
5658 case tok::kw_char:
5659 case tok::kw_int:
5660 case tok::kw_float:
5661 case tok::kw_double:
5662 case tok::kw_bool:
5663 case tok::kw___pixel:
5664 Tok.setKind(tok::kw___vector);
5665 return true;
5666 case tok::identifier:
5667 if (Next.getIdentifierInfo() == Ident_pixel) {
5668 Tok.setKind(tok::kw___vector);
5669 return true;
5670 }
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005671 if (Next.getIdentifierInfo() == Ident_bool) {
5672 Tok.setKind(tok::kw___vector);
5673 return true;
5674 }
Chris Lattner1b492422010-02-28 18:33:55 +00005675 return false;
5676 }
5677}
5678
5679bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5680 const char *&PrevSpec, unsigned &DiagID,
5681 bool &isInvalid) {
Stephen Hines651f13c2014-04-23 16:59:28 -07005682 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Chris Lattner1b492422010-02-28 18:33:55 +00005683 if (Tok.getIdentifierInfo() == Ident_vector) {
5684 Token Next = NextToken();
5685 switch (Next.getKind()) {
5686 case tok::kw_short:
5687 case tok::kw_long:
5688 case tok::kw_signed:
5689 case tok::kw_unsigned:
5690 case tok::kw_void:
5691 case tok::kw_char:
5692 case tok::kw_int:
5693 case tok::kw_float:
5694 case tok::kw_double:
5695 case tok::kw_bool:
5696 case tok::kw___pixel:
Stephen Hines651f13c2014-04-23 16:59:28 -07005697 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner1b492422010-02-28 18:33:55 +00005698 return true;
5699 case tok::identifier:
5700 if (Next.getIdentifierInfo() == Ident_pixel) {
Stephen Hines651f13c2014-04-23 16:59:28 -07005701 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Chris Lattner1b492422010-02-28 18:33:55 +00005702 return true;
5703 }
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005704 if (Next.getIdentifierInfo() == Ident_bool) {
Stephen Hines651f13c2014-04-23 16:59:28 -07005705 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005706 return true;
5707 }
Chris Lattner1b492422010-02-28 18:33:55 +00005708 break;
5709 default:
5710 break;
5711 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005712 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005713 DS.isTypeAltiVecVector()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07005714 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner1b492422010-02-28 18:33:55 +00005715 return true;
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005716 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5717 DS.isTypeAltiVecVector()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07005718 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
Bill Schmidt3e3d20b2013-07-03 20:54:09 +00005719 return true;
Chris Lattner1b492422010-02-28 18:33:55 +00005720 }
5721 return false;
5722}