blob: f8e8e4b0f1548fa18440a1466f71b2cc9ff8bf56 [file] [log] [blame]
Hans Wennborgdcfba332015-10-06 23:40:43 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -------------*- C++ -*-===//
Chris Lattnera5235172007-08-25 06:57:03 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera5235172007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor423984d2008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Erik Verbruggen888d52a2014-01-15 09:15:43 +000016#include "clang/AST/ASTContext.h"
Chandler Carruth757fcd62014-03-04 10:05:20 +000017#include "clang/AST/DeclTemplate.h"
Aaron Ballmanb8e20392014-03-31 17:32:39 +000018#include "clang/Basic/Attributes.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000019#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Basic/OperatorKinds.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000021#include "clang/Basic/TargetInfo.h"
Chris Lattner60f36222009-01-29 05:15:15 +000022#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000023#include "clang/Sema/DeclSpec.h"
John McCall8b0666c2010-08-20 18:27:03 +000024#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000025#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "clang/Sema/Scope.h"
John McCalldb632ac2012-09-25 07:32:39 +000027#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000028#include "llvm/ADT/SmallString.h"
Hans Wennborgdcfba332015-10-06 23:40:43 +000029
Chris Lattnera5235172007-08-25 06:57:03 +000030using namespace clang;
31
32/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redl67667942010-08-27 23:12:46 +000033/// may either be a top level namespace or a block-level namespace alias. If
34/// there was an inline keyword, it has already been parsed.
Chris Lattnera5235172007-08-25 06:57:03 +000035///
36/// namespace-definition: [C++ 7.3: basic.namespace]
37/// named-namespace-definition
38/// unnamed-namespace-definition
39///
40/// unnamed-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000041/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000042///
43/// named-namespace-definition:
44/// original-namespace-definition
45/// extension-namespace-definition
46///
47/// original-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000048/// 'inline'[opt] 'namespace' identifier attributes[opt]
49/// '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000050///
51/// extension-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000052/// 'inline'[opt] 'namespace' original-namespace-name
53/// '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000054///
Chris Lattnera5235172007-08-25 06:57:03 +000055/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
56/// 'namespace' identifier '=' qualified-namespace-specifier ';'
57///
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +000058Parser::DeclGroupPtrTy Parser::ParseNamespace(unsigned Context,
59 SourceLocation &DeclEnd,
60 SourceLocation InlineLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000061 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000062 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian4bf82622011-08-22 17:59:19 +000063 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000064
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000065 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000066 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000067 cutOffParsing();
David Blaikie0403cb12016-01-15 23:43:25 +000068 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000069 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000070
Chris Lattnera5235172007-08-25 06:57:03 +000071 SourceLocation IdentLoc;
Craig Topper161e4db2014-05-21 06:02:52 +000072 IdentifierInfo *Ident = nullptr;
Richard Trieu61384cb2011-05-26 20:11:09 +000073 std::vector<SourceLocation> ExtraIdentLoc;
74 std::vector<IdentifierInfo*> ExtraIdent;
75 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000076
Aaron Ballman730476b2014-11-08 15:33:35 +000077 ParsedAttributesWithRange attrs(AttrFactory);
78 SourceLocation attrLoc;
79 if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) {
80 if (!getLangOpts().CPlusPlus1z)
Aaron Ballmanc0ae7df2014-11-08 17:07:15 +000081 Diag(Tok.getLocation(), diag::warn_cxx14_compat_attribute)
82 << 0 /*namespace*/;
Aaron Ballman730476b2014-11-08 15:33:35 +000083 attrLoc = Tok.getLocation();
84 ParseCXX11Attributes(attrs);
85 }
Mike Stump11289f42009-09-09 15:08:12 +000086
Chris Lattner76c72282007-10-09 17:33:22 +000087 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000088 Ident = Tok.getIdentifierInfo();
89 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieu61384cb2011-05-26 20:11:09 +000090 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
91 ExtraNamespaceLoc.push_back(ConsumeToken());
92 ExtraIdent.push_back(Tok.getIdentifierInfo());
93 ExtraIdentLoc.push_back(ConsumeToken());
94 }
Chris Lattnera5235172007-08-25 06:57:03 +000095 }
Mike Stump11289f42009-09-09 15:08:12 +000096
Aaron Ballmanc0ae7df2014-11-08 17:07:15 +000097 // A nested namespace definition cannot have attributes.
98 if (!ExtraNamespaceLoc.empty() && attrLoc.isValid())
99 Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute);
100
Chris Lattnera5235172007-08-25 06:57:03 +0000101 // Read label attributes, if present.
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000102 if (Tok.is(tok::kw___attribute)) {
Aaron Ballman730476b2014-11-08 15:33:35 +0000103 attrLoc = Tok.getLocation();
John McCall53fa7142010-12-24 02:08:15 +0000104 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000105 }
Mike Stump11289f42009-09-09 15:08:12 +0000106
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000107 if (Tok.is(tok::equal)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000108 if (!Ident) {
Alp Tokerec543272013-12-24 09:48:30 +0000109 Diag(Tok, diag::err_expected) << tok::identifier;
Nico Weber729f1e22012-10-27 23:44:27 +0000110 // Skip to end of the definition and eat the ';'.
111 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +0000112 return nullptr;
Nico Weber729f1e22012-10-27 23:44:27 +0000113 }
Aaron Ballman730476b2014-11-08 15:33:35 +0000114 if (attrLoc.isValid())
115 Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +0000116 if (InlineLoc.isValid())
117 Diag(InlineLoc, diag::err_inline_namespace_alias)
118 << FixItHint::CreateRemoval(InlineLoc);
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +0000119 Decl *NSAlias = ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
120 return Actions.ConvertDeclToDeclGroup(NSAlias);
121}
Mike Stump11289f42009-09-09 15:08:12 +0000122
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000123 BalancedDelimiterTracker T(*this, tok::l_brace);
124 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000125 if (Ident)
126 Diag(Tok, diag::err_expected) << tok::l_brace;
127 else
128 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
David Blaikie0403cb12016-01-15 23:43:25 +0000129 return nullptr;
Chris Lattnera5235172007-08-25 06:57:03 +0000130 }
Mike Stump11289f42009-09-09 15:08:12 +0000131
Douglas Gregor0be31a22010-07-02 17:43:08 +0000132 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
133 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
134 getCurScope()->getFnParent()) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000135 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000136 SkipUntil(tok::r_brace);
David Blaikie0403cb12016-01-15 23:43:25 +0000137 return nullptr;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000138 }
139
Richard Smith13307f52014-11-08 05:37:34 +0000140 if (ExtraIdent.empty()) {
141 // Normal namespace definition, not a nested-namespace-definition.
142 } else if (InlineLoc.isValid()) {
143 Diag(InlineLoc, diag::err_inline_nested_namespace_definition);
144 } else if (getLangOpts().CPlusPlus1z) {
145 Diag(ExtraNamespaceLoc[0],
146 diag::warn_cxx14_compat_nested_namespace_definition);
147 } else {
Richard Trieu61384cb2011-05-26 20:11:09 +0000148 TentativeParsingAction TPA(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000149 SkipUntil(tok::r_brace, StopBeforeMatch);
Richard Trieu61384cb2011-05-26 20:11:09 +0000150 Token rBraceToken = Tok;
151 TPA.Revert();
152
153 if (!rBraceToken.is(tok::r_brace)) {
Richard Smith13307f52014-11-08 05:37:34 +0000154 Diag(ExtraNamespaceLoc[0], diag::ext_nested_namespace_definition)
Richard Trieu61384cb2011-05-26 20:11:09 +0000155 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
156 } else {
Benjamin Kramerf546f412011-05-26 21:32:30 +0000157 std::string NamespaceFix;
Richard Trieu61384cb2011-05-26 20:11:09 +0000158 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
159 E = ExtraIdent.end(); I != E; ++I) {
160 NamespaceFix += " { namespace ";
161 NamespaceFix += (*I)->getName();
162 }
Benjamin Kramerf546f412011-05-26 21:32:30 +0000163
Richard Trieu61384cb2011-05-26 20:11:09 +0000164 std::string RBraces;
Benjamin Kramerf546f412011-05-26 21:32:30 +0000165 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieu61384cb2011-05-26 20:11:09 +0000166 RBraces += "} ";
Benjamin Kramerf546f412011-05-26 21:32:30 +0000167
Richard Smith13307f52014-11-08 05:37:34 +0000168 Diag(ExtraNamespaceLoc[0], diag::ext_nested_namespace_definition)
Richard Trieu61384cb2011-05-26 20:11:09 +0000169 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
170 ExtraIdentLoc.back()),
171 NamespaceFix)
172 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
173 }
174 }
175
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000176 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith5d164bc2011-10-15 05:09:34 +0000177 if (InlineLoc.isValid())
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000178 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000179 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000180
Chris Lattner4de55aa2009-03-29 14:02:43 +0000181 // Enter a scope for the namespace.
182 ParseScope NamespaceScope(this, Scope::DeclScope);
183
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +0000184 UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr;
John McCall48871652010-08-21 09:40:31 +0000185 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000186 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000187 IdentLoc, Ident, T.getOpenLocation(),
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +0000188 attrs.getList(), ImplicitUsingDirectiveDecl);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000189
John McCallfaf5fb42010-08-26 23:41:50 +0000190 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
191 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000192
Richard Trieu61384cb2011-05-26 20:11:09 +0000193 // Parse the contents of the namespace. This includes parsing recovery on
194 // any improperly nested namespaces.
195 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000196 InlineLoc, attrs, T);
Mike Stump11289f42009-09-09 15:08:12 +0000197
Chris Lattner4de55aa2009-03-29 14:02:43 +0000198 // Leave the namespace scope.
199 NamespaceScope.Exit();
200
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000201 DeclEnd = T.getCloseLocation();
202 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +0000203
204 return Actions.ConvertDeclToDeclGroup(NamespcDecl,
205 ImplicitUsingDirectiveDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000206}
Chris Lattner38376f12008-01-12 07:05:38 +0000207
Richard Trieu61384cb2011-05-26 20:11:09 +0000208/// ParseInnerNamespace - Parse the contents of a namespace.
Richard Smith13307f52014-11-08 05:37:34 +0000209void Parser::ParseInnerNamespace(std::vector<SourceLocation> &IdentLoc,
210 std::vector<IdentifierInfo *> &Ident,
211 std::vector<SourceLocation> &NamespaceLoc,
212 unsigned int index, SourceLocation &InlineLoc,
213 ParsedAttributes &attrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000214 BalancedDelimiterTracker &Tracker) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000215 if (index == Ident.size()) {
Richard Smith752ada82015-11-17 23:32:01 +0000216 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
217 Tok.isNot(tok::eof)) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000218 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000219 MaybeParseCXX11Attributes(attrs);
Richard Trieu61384cb2011-05-26 20:11:09 +0000220 ParseExternalDeclaration(attrs);
221 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000222
223 // The caller is what called check -- we are simply calling
224 // the close for it.
225 Tracker.consumeClose();
Richard Trieu61384cb2011-05-26 20:11:09 +0000226
227 return;
228 }
229
Richard Smith13307f52014-11-08 05:37:34 +0000230 // Handle a nested namespace definition.
231 // FIXME: Preserve the source information through to the AST rather than
232 // desugaring it here.
Richard Trieu61384cb2011-05-26 20:11:09 +0000233 ParseScope NamespaceScope(this, Scope::DeclScope);
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +0000234 UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr;
Richard Trieu61384cb2011-05-26 20:11:09 +0000235 Decl *NamespcDecl =
236 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
237 NamespaceLoc[index], IdentLoc[index],
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000238 Ident[index], Tracker.getOpenLocation(),
Ekaterina Romanova9218a3b2015-12-10 18:52:50 +0000239 attrs.getList(), ImplicitUsingDirectiveDecl);
240 assert(!ImplicitUsingDirectiveDecl &&
241 "nested namespace definition cannot define anonymous namespace");
Richard Trieu61384cb2011-05-26 20:11:09 +0000242
243 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000244 attrs, Tracker);
Richard Trieu61384cb2011-05-26 20:11:09 +0000245
246 NamespaceScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000247 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieu61384cb2011-05-26 20:11:09 +0000248}
249
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000250/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
251/// alias definition.
252///
John McCall48871652010-08-21 09:40:31 +0000253Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000254 SourceLocation AliasLoc,
255 IdentifierInfo *Alias,
256 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000257 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000258
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000259 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000260
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000261 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000262 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000263 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000264 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000265 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000266
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000267 CXXScopeSpec SS;
268 // Parse (optional) nested-name-specifier.
David Blaikieefdccaa2016-01-15 23:43:34 +0000269 ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000270
271 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
272 Diag(Tok, diag::err_expected_namespace_name);
273 // Skip to end of the definition and eat the ';'.
274 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000275 return nullptr;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000276 }
277
278 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000279 IdentifierInfo *Ident = Tok.getIdentifierInfo();
280 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000281
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000282 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000283 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000284 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
285 SkipUntil(tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000286
Craig Topperff354282015-11-14 18:16:00 +0000287 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc,
288 Alias, SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000289}
290
Chris Lattner38376f12008-01-12 07:05:38 +0000291/// ParseLinkage - We know that the current token is a string_literal
292/// and just before that, that extern was seen.
293///
294/// linkage-specification: [C++ 7.5p2: dcl.link]
295/// 'extern' string-literal '{' declaration-seq[opt] '}'
296/// 'extern' string-literal declaration
297///
Chris Lattner8ea64422010-11-09 20:15:55 +0000298Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Richard Smith4ee696d2014-02-17 23:25:27 +0000299 assert(isTokenStringLiteral() && "Not a string literal!");
300 ExprResult Lang = ParseStringLiteralExpression(false);
Chris Lattner38376f12008-01-12 07:05:38 +0000301
Douglas Gregor07665a62009-01-05 19:45:36 +0000302 ParseScope LinkageScope(this, Scope::DeclScope);
Richard Smith4ee696d2014-02-17 23:25:27 +0000303 Decl *LinkageSpec =
304 Lang.isInvalid()
Craig Topper161e4db2014-05-21 06:02:52 +0000305 ? nullptr
Richard Smith4ee696d2014-02-17 23:25:27 +0000306 : Actions.ActOnStartLinkageSpecification(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000307 getCurScope(), DS.getSourceRange().getBegin(), Lang.get(),
Richard Smith4ee696d2014-02-17 23:25:27 +0000308 Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
Douglas Gregor07665a62009-01-05 19:45:36 +0000309
John McCall084e83d2011-03-24 11:26:52 +0000310 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000311 MaybeParseCXX11Attributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000312
Douglas Gregor07665a62009-01-05 19:45:36 +0000313 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara4d423992011-05-01 16:25:54 +0000314 // Reset the source range in DS, as the leading "extern"
315 // does not really belong to the inner declaration ...
316 DS.SetRangeStart(SourceLocation());
317 DS.SetRangeEnd(SourceLocation());
318 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000319 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000320 ParseExternalDeclaration(attrs, &DS);
Richard Smith4ee696d2014-02-17 23:25:27 +0000321 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
322 getCurScope(), LinkageSpec, SourceLocation())
Craig Topper161e4db2014-05-21 06:02:52 +0000323 : nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000324 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000325
Douglas Gregorb65a9132010-02-07 08:38:28 +0000326 DS.abort();
327
John McCall53fa7142010-12-24 02:08:15 +0000328 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000329
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000330 BalancedDelimiterTracker T(*this, tok::l_brace);
331 T.consumeOpen();
Richard Smith77944862014-03-02 05:58:18 +0000332
333 unsigned NestedModules = 0;
334 while (true) {
335 switch (Tok.getKind()) {
336 case tok::annot_module_begin:
337 ++NestedModules;
338 ParseTopLevelDecl();
339 continue;
340
341 case tok::annot_module_end:
342 if (!NestedModules)
343 break;
344 --NestedModules;
345 ParseTopLevelDecl();
346 continue;
347
348 case tok::annot_module_include:
349 ParseTopLevelDecl();
350 continue;
351
352 case tok::eof:
353 break;
354
355 case tok::r_brace:
356 if (!NestedModules)
357 break;
358 // Fall through.
359 default:
360 ParsedAttributesWithRange attrs(AttrFactory);
361 MaybeParseCXX11Attributes(attrs);
Richard Smith77944862014-03-02 05:58:18 +0000362 ParseExternalDeclaration(attrs);
363 continue;
364 }
365
366 break;
Chris Lattner38376f12008-01-12 07:05:38 +0000367 }
368
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000369 T.consumeClose();
Richard Smith4ee696d2014-02-17 23:25:27 +0000370 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
371 getCurScope(), LinkageSpec, T.getCloseLocation())
Craig Topper161e4db2014-05-21 06:02:52 +0000372 : nullptr;
Chris Lattner38376f12008-01-12 07:05:38 +0000373}
Douglas Gregor556877c2008-04-13 21:30:24 +0000374
Richard Smith8df390f2016-09-08 23:14:54 +0000375/// Parse a C++ Modules TS export-declaration.
376///
377/// export-declaration:
378/// 'export' declaration
379/// 'export' '{' declaration-seq[opt] '}'
380///
381Decl *Parser::ParseExportDeclaration() {
382 assert(Tok.is(tok::kw_export));
383 SourceLocation ExportLoc = ConsumeToken();
384
385 ParseScope ExportScope(this, Scope::DeclScope);
386 Decl *ExportDecl = Actions.ActOnStartExportDecl(
387 getCurScope(), ExportLoc,
388 Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
389
390 if (Tok.isNot(tok::l_brace)) {
391 // FIXME: Factor out a ParseExternalDeclarationWithAttrs.
392 ParsedAttributesWithRange Attrs(AttrFactory);
393 MaybeParseCXX11Attributes(Attrs);
394 MaybeParseMicrosoftAttributes(Attrs);
395 ParseExternalDeclaration(Attrs);
396 return Actions.ActOnFinishExportDecl(getCurScope(), ExportDecl,
397 SourceLocation());
398 }
399
400 BalancedDelimiterTracker T(*this, tok::l_brace);
401 T.consumeOpen();
402
403 // The Modules TS draft says "An export-declaration shall declare at least one
404 // entity", but the intent is that it shall contain at least one declaration.
405 if (Tok.is(tok::r_brace))
406 Diag(ExportLoc, diag::err_export_empty)
407 << SourceRange(ExportLoc, Tok.getLocation());
408
409 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
410 Tok.isNot(tok::eof)) {
411 ParsedAttributesWithRange Attrs(AttrFactory);
412 MaybeParseCXX11Attributes(Attrs);
413 MaybeParseMicrosoftAttributes(Attrs);
414 ParseExternalDeclaration(Attrs);
415 }
416
417 T.consumeClose();
418 return Actions.ActOnFinishExportDecl(getCurScope(), ExportDecl,
419 T.getCloseLocation());
420}
421
Douglas Gregord7c4d982008-12-30 03:27:21 +0000422/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
423/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000424Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000425 const ParsedTemplateInfo &TemplateInfo,
426 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000427 ParsedAttributesWithRange &attrs,
428 Decl **OwnedType) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000429 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000430 ObjCDeclContextSwitch ObjCDC(*this);
431
Douglas Gregord7c4d982008-12-30 03:27:21 +0000432 // Eat 'using'.
433 SourceLocation UsingLoc = ConsumeToken();
434
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000435 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000436 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000437 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000438 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000439 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000440
John McCall9b72f892010-11-10 02:40:36 +0000441 // 'using namespace' means this is a using-directive.
442 if (Tok.is(tok::kw_namespace)) {
443 // Template parameters are always an error here.
444 if (TemplateInfo.Kind) {
445 SourceRange R = TemplateInfo.getSourceRange();
Craig Topper54a6a682015-11-14 18:16:08 +0000446 Diag(UsingLoc, diag::err_templated_using_directive_declaration)
447 << 0 /* directive */ << R << FixItHint::CreateRemoval(R);
John McCall9b72f892010-11-10 02:40:36 +0000448 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000449
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000450 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000451 }
452
Richard Smithdda56e42011-04-15 14:24:37 +0000453 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000454
455 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000456 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000457
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000458 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000459 AS_none, OwnedType);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000460}
461
462/// ParseUsingDirective - Parse C++ using-directive, assumes
463/// that current token is 'namespace' and 'using' was already parsed.
464///
465/// using-directive: [C++ 7.3.p4: namespace.udir]
466/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
467/// namespace-name ;
468/// [GNU] using-directive:
469/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
470/// namespace-name attributes[opt] ;
471///
John McCall48871652010-08-21 09:40:31 +0000472Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000473 SourceLocation UsingLoc,
474 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000475 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000476 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
477
478 // Eat 'namespace'.
479 SourceLocation NamespcLoc = ConsumeToken();
480
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000481 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000482 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000483 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000484 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000485 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000486
Douglas Gregord7c4d982008-12-30 03:27:21 +0000487 CXXScopeSpec SS;
488 // Parse (optional) nested-name-specifier.
David Blaikieefdccaa2016-01-15 23:43:34 +0000489 ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000490
Craig Topper161e4db2014-05-21 06:02:52 +0000491 IdentifierInfo *NamespcName = nullptr;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000492 SourceLocation IdentLoc = SourceLocation();
493
494 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000495 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000496 Diag(Tok, diag::err_expected_namespace_name);
497 // If there was invalid namespace name, skip to end of decl, and eat ';'.
498 SkipUntil(tok::semi);
499 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Craig Topper161e4db2014-05-21 06:02:52 +0000500 return nullptr;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000501 }
Mike Stump11289f42009-09-09 15:08:12 +0000502
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000503 // Parse identifier.
504 NamespcName = Tok.getIdentifierInfo();
505 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000506
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000507 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000508 bool GNUAttr = false;
509 if (Tok.is(tok::kw___attribute)) {
510 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000511 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000512 }
Mike Stump11289f42009-09-09 15:08:12 +0000513
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000514 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000515 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000516 if (ExpectAndConsume(tok::semi,
517 GNUAttr ? diag::err_expected_semi_after_attribute_list
518 : diag::err_expected_semi_after_namespace_name))
519 SkipUntil(tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000520
Douglas Gregor0be31a22010-07-02 17:43:08 +0000521 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000522 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000523}
524
Richard Smithdda56e42011-04-15 14:24:37 +0000525/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
526/// Assumes that 'using' was already seen.
Douglas Gregord7c4d982008-12-30 03:27:21 +0000527///
528/// using-declaration: [C++ 7.3.p3: namespace.udecl]
529/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000530/// unqualified-id
531/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000532///
Richard Smith810ad3e2013-01-29 10:02:16 +0000533/// alias-declaration: C++11 [dcl.dcl]p1
534/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smithdda56e42011-04-15 14:24:37 +0000535///
John McCall48871652010-08-21 09:40:31 +0000536Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000537 const ParsedTemplateInfo &TemplateInfo,
538 SourceLocation UsingLoc,
539 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000540 AccessSpecifier AS,
541 Decl **OwnedType) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000542 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000543 SourceLocation TypenameLoc;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000544 bool HasTypenameKeyword = false;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000545
Richard Smithc2c8bb82013-10-15 01:34:54 +0000546 // Check for misplaced attributes before the identifier in an
547 // alias-declaration.
548 ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
549 MaybeParseCXX11Attributes(MisplacedAttrs);
Douglas Gregorfec52632009-06-20 00:51:54 +0000550
551 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000552 // FIXME: This is wrong; we should parse this as a typename-specifier.
Alp Toker97650562014-01-10 11:19:30 +0000553 if (TryConsumeToken(tok::kw_typename, TypenameLoc))
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000554 HasTypenameKeyword = true;
Douglas Gregorfec52632009-06-20 00:51:54 +0000555
Nikola Smiljanic67860242014-09-26 00:28:20 +0000556 if (Tok.is(tok::kw___super)) {
557 Diag(Tok.getLocation(), diag::err_super_in_using_declaration);
558 SkipUntil(tok::semi);
559 return nullptr;
560 }
561
Douglas Gregorfec52632009-06-20 00:51:54 +0000562 // Parse nested-name-specifier.
Craig Topper161e4db2014-05-21 06:02:52 +0000563 IdentifierInfo *LastII = nullptr;
David Blaikieefdccaa2016-01-15 23:43:34 +0000564 ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false,
Craig Topper161e4db2014-05-21 06:02:52 +0000565 /*MayBePseudoDtor=*/nullptr,
566 /*IsTypename=*/false,
Richard Smith7447af42013-03-26 01:15:19 +0000567 /*LastII=*/&LastII);
Douglas Gregorfec52632009-06-20 00:51:54 +0000568
Douglas Gregorfec52632009-06-20 00:51:54 +0000569 // Check nested-name specifier.
570 if (SS.isInvalid()) {
571 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000572 return nullptr;
Douglas Gregorfec52632009-06-20 00:51:54 +0000573 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000574
Richard Smith7447af42013-03-26 01:15:19 +0000575 SourceLocation TemplateKWLoc;
576 UnqualifiedId Name;
577
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000578 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000579 // destructor names and allow the action module to diagnose any semantic
580 // errors.
Richard Smith7447af42013-03-26 01:15:19 +0000581 //
582 // C++11 [class.qual]p2:
583 // [...] in a using-declaration that is a member-declaration, if the name
584 // specified after the nested-name-specifier is the same as the identifier
585 // or the simple-template-id's template-name in the last component of the
586 // nested-name-specifier, the name is [...] considered to name the
587 // constructor.
588 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
589 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
590 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
591 !SS.getScopeRep()->getAsNamespace() &&
592 !SS.getScopeRep()->getAsNamespaceAlias()) {
593 SourceLocation IdLoc = ConsumeToken();
594 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
595 Name.setConstructorName(Type, IdLoc, IdLoc);
Richard Smith88fe69c2015-07-06 01:45:27 +0000596 } else if (ParseUnqualifiedId(
597 SS, /*EnteringContext=*/false,
598 /*AllowDestructorName=*/true,
Richard Smithc7ae3e02015-07-21 00:23:34 +0000599 /*AllowConstructorName=*/!(Tok.is(tok::identifier) &&
600 NextToken().is(tok::equal)),
David Blaikieefdccaa2016-01-15 23:43:34 +0000601 nullptr, TemplateKWLoc, Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000602 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000603 return nullptr;
Douglas Gregorfec52632009-06-20 00:51:54 +0000604 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000605
Richard Smithc2c8bb82013-10-15 01:34:54 +0000606 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith37a45dd2013-10-24 01:21:09 +0000607 MaybeParseGNUAttributes(Attrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000608 MaybeParseCXX11Attributes(Attrs);
Richard Smithdda56e42011-04-15 14:24:37 +0000609
610 // Maybe this is an alias-declaration.
Richard Smithdda56e42011-04-15 14:24:37 +0000611 TypeResult TypeAlias;
Richard Smithc2c8bb82013-10-15 01:34:54 +0000612 bool IsAliasDecl = Tok.is(tok::equal);
David Majnemerf9bde282015-03-11 06:45:39 +0000613 Decl *DeclFromDeclSpec = nullptr;
Richard Smithdda56e42011-04-15 14:24:37 +0000614 if (IsAliasDecl) {
Richard Smithc2c8bb82013-10-15 01:34:54 +0000615 // If we had any misplaced attributes from earlier, this is where they
616 // should have been written.
617 if (MisplacedAttrs.Range.isValid()) {
618 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
619 << FixItHint::CreateInsertionFromRange(
620 Tok.getLocation(),
621 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
622 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
623 Attrs.takeAllFrom(MisplacedAttrs);
624 }
625
Richard Smithdda56e42011-04-15 14:24:37 +0000626 ConsumeToken();
627
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000628 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000629 diag::warn_cxx98_compat_alias_declaration :
630 diag::ext_alias_declaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000631
Richard Smith3f1b5d02011-05-05 21:57:07 +0000632 // Type alias templates cannot be specialized.
633 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000634 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
635 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000636 SpecKind = 0;
637 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
638 SpecKind = 1;
639 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
640 SpecKind = 2;
641 if (SpecKind != -1) {
642 SourceRange Range;
643 if (SpecKind == 0)
644 Range = SourceRange(Name.TemplateId->LAngleLoc,
645 Name.TemplateId->RAngleLoc);
646 else
647 Range = TemplateInfo.getSourceRange();
648 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
649 << SpecKind << Range;
650 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000651 return nullptr;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000652 }
653
Richard Smithdda56e42011-04-15 14:24:37 +0000654 // Name must be an identifier.
655 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
656 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
657 // No removal fixit: can't recover from this.
658 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000659 return nullptr;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000660 } else if (HasTypenameKeyword)
Richard Smithdda56e42011-04-15 14:24:37 +0000661 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
662 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
663 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
664 else if (SS.isNotEmpty())
665 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
666 << FixItHint::CreateRemoval(SS.getRange());
667
David Majnemerf9bde282015-03-11 06:45:39 +0000668 TypeAlias = ParseTypeName(nullptr, TemplateInfo.Kind
669 ? Declarator::AliasTemplateContext
670 : Declarator::AliasDeclContext,
671 AS, &DeclFromDeclSpec, &Attrs);
672 if (OwnedType)
673 *OwnedType = DeclFromDeclSpec;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000674 } else {
675 // C++11 attributes are not allowed on a using-declaration, but GNU ones
676 // are.
Richard Smithc2c8bb82013-10-15 01:34:54 +0000677 ProhibitAttributes(MisplacedAttrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000678 ProhibitAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000679
Richard Smithdda56e42011-04-15 14:24:37 +0000680 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith54ecd982013-02-20 19:22:51 +0000681 MaybeParseGNUAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000682 }
Mike Stump11289f42009-09-09 15:08:12 +0000683
Douglas Gregorfec52632009-06-20 00:51:54 +0000684 // Eat ';'.
685 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000686 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
687 !Attrs.empty() ? "attributes list"
688 : IsAliasDecl ? "alias declaration"
689 : "using declaration"))
690 SkipUntil(tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000691
John McCall9b72f892010-11-10 02:40:36 +0000692 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000693 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000694 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000695 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-11-10 02:40:36 +0000696 SourceRange R = TemplateInfo.getSourceRange();
Craig Topper54a6a682015-11-14 18:16:08 +0000697 Diag(UsingLoc, diag::err_templated_using_directive_declaration)
698 << 1 /* declaration */ << R << FixItHint::CreateRemoval(R);
John McCall9b72f892010-11-10 02:40:36 +0000699
700 // Unfortunately, we have to bail out instead of recovering by
701 // ignoring the parameters, just in case the nested name specifier
702 // depends on the parameters.
Craig Topper161e4db2014-05-21 06:02:52 +0000703 return nullptr;
John McCall9b72f892010-11-10 02:40:36 +0000704 }
705
Douglas Gregor882a61a2011-09-26 14:30:28 +0000706 // "typename" keyword is allowed for identifiers only,
707 // because it may be a type definition.
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000708 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor882a61a2011-09-26 14:30:28 +0000709 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
710 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000711 // Proceed parsing, but reset the HasTypenameKeyword flag.
712 HasTypenameKeyword = false;
Douglas Gregor882a61a2011-09-26 14:30:28 +0000713 }
714
Richard Smith3f1b5d02011-05-05 21:57:07 +0000715 if (IsAliasDecl) {
716 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000717 MultiTemplateParamsArg TemplateParamsArg(
Craig Topper161e4db2014-05-21 06:02:52 +0000718 TemplateParams ? TemplateParams->data() : nullptr,
Richard Smith3f1b5d02011-05-05 21:57:07 +0000719 TemplateParams ? TemplateParams->size() : 0);
720 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith54ecd982013-02-20 19:22:51 +0000721 UsingLoc, Name, Attrs.getList(),
David Majnemerf9bde282015-03-11 06:45:39 +0000722 TypeAlias, DeclFromDeclSpec);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000723 }
Richard Smithdda56e42011-04-15 14:24:37 +0000724
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000725 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
726 /* HasUsingKeyword */ true, UsingLoc,
727 SS, Name, Attrs.getList(),
728 HasTypenameKeyword, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000729}
730
Benjamin Kramere56f3932011-12-23 17:00:35 +0000731/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000732///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000733/// [C++0x] static_assert-declaration:
734/// static_assert ( constant-expression , string-literal ) ;
735///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000736/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000737/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000738///
John McCall48871652010-08-21 09:40:31 +0000739Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000740 assert(Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert) &&
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000741 "Not a static_assert declaration");
742
David Blaikiebbafb8a2012-03-11 07:00:24 +0000743 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000744 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000745 if (Tok.is(tok::kw_static_assert))
746 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000747
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000748 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000749
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000750 BalancedDelimiterTracker T(*this, tok::l_paren);
751 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000752 Diag(Tok, diag::err_expected) << tok::l_paren;
Richard Smith76965712012-09-13 19:12:50 +0000753 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000754 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000755 }
Mike Stump11289f42009-09-09 15:08:12 +0000756
John McCalldadc5752010-08-24 06:29:42 +0000757 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000758 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000759 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000760 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000761 }
Mike Stump11289f42009-09-09 15:08:12 +0000762
Richard Smith085a64f2014-06-20 19:57:12 +0000763 ExprResult AssertMessage;
764 if (Tok.is(tok::r_paren)) {
765 Diag(Tok, getLangOpts().CPlusPlus1z
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000766 ? diag::warn_cxx14_compat_static_assert_no_message
Richard Smith085a64f2014-06-20 19:57:12 +0000767 : diag::ext_static_assert_no_message)
768 << (getLangOpts().CPlusPlus1z
769 ? FixItHint()
770 : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\""));
771 } else {
772 if (ExpectAndConsume(tok::comma)) {
773 SkipUntil(tok::semi);
774 return nullptr;
775 }
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000776
Richard Smith085a64f2014-06-20 19:57:12 +0000777 if (!isTokenStringLiteral()) {
778 Diag(Tok, diag::err_expected_string_literal)
779 << /*Source='static_assert'*/1;
780 SkipMalformedDecl();
781 return nullptr;
782 }
Mike Stump11289f42009-09-09 15:08:12 +0000783
Richard Smith085a64f2014-06-20 19:57:12 +0000784 AssertMessage = ParseStringLiteralExpression();
785 if (AssertMessage.isInvalid()) {
786 SkipMalformedDecl();
787 return nullptr;
788 }
Richard Smithd67aea22012-03-06 03:21:47 +0000789 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000790
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000791 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000792
Chris Lattner49836b42009-04-02 04:16:50 +0000793 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000794 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000795
John McCallb268a282010-08-23 23:25:46 +0000796 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000797 AssertExpr.get(),
798 AssertMessage.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000799 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000800}
801
Richard Smith74aeef52013-04-26 16:15:35 +0000802/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000803///
804/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000805/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000806///
David Blaikie15a430a2011-12-04 05:04:18 +0000807SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000808 assert(Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)
David Blaikie15a430a2011-12-04 05:04:18 +0000809 && "Not a decltype specifier");
810
David Blaikie15a430a2011-12-04 05:04:18 +0000811 ExprResult Result;
812 SourceLocation StartLoc = Tok.getLocation();
813 SourceLocation EndLoc;
814
815 if (Tok.is(tok::annot_decltype)) {
816 Result = getExprAnnotation(Tok);
817 EndLoc = Tok.getAnnotationEndLoc();
818 ConsumeToken();
819 if (Result.isInvalid()) {
820 DS.SetTypeSpecError();
821 return EndLoc;
822 }
823 } else {
Richard Smith324df552012-02-24 22:30:04 +0000824 if (Tok.getIdentifierInfo()->isStr("decltype"))
825 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000826
David Blaikie15a430a2011-12-04 05:04:18 +0000827 ConsumeToken();
828
829 BalancedDelimiterTracker T(*this, tok::l_paren);
830 if (T.expectAndConsume(diag::err_expected_lparen_after,
831 "decltype", tok::r_paren)) {
832 DS.SetTypeSpecError();
833 return T.getOpenLocation() == Tok.getLocation() ?
834 StartLoc : T.getOpenLocation();
835 }
836
Richard Smith74aeef52013-04-26 16:15:35 +0000837 // Check for C++1y 'decltype(auto)'.
838 if (Tok.is(tok::kw_auto)) {
839 // No need to disambiguate here: an expression can't start with 'auto',
840 // because the typename-specifier in a function-style cast operation can't
841 // be 'auto'.
842 Diag(Tok.getLocation(),
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000843 getLangOpts().CPlusPlus14
Richard Smith74aeef52013-04-26 16:15:35 +0000844 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
845 : diag::ext_decltype_auto_type_specifier);
846 ConsumeToken();
847 } else {
848 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000849
Richard Smith74aeef52013-04-26 16:15:35 +0000850 // C++11 [dcl.type.simple]p4:
851 // The operand of the decltype specifier is an unevaluated operand.
852 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
Craig Topper161e4db2014-05-21 06:02:52 +0000853 nullptr,/*IsDecltype=*/true);
Kaelyn Takata5cc85352015-04-10 19:16:46 +0000854 Result =
855 Actions.CorrectDelayedTyposInExpr(ParseExpression(), [](Expr *E) {
856 return E->hasPlaceholderType() ? ExprError() : E;
857 });
Richard Smith74aeef52013-04-26 16:15:35 +0000858 if (Result.isInvalid()) {
859 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000860 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smith74aeef52013-04-26 16:15:35 +0000861 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000862 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000863 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
864 // Backtrack to get the location of the last token before the semi.
865 PP.RevertCachedTokens(2);
866 ConsumeToken(); // the semi.
867 EndLoc = ConsumeAnyToken();
868 assert(Tok.is(tok::semi));
869 } else {
870 EndLoc = Tok.getLocation();
871 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000872 }
Richard Smith74aeef52013-04-26 16:15:35 +0000873 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000874 }
Richard Smith74aeef52013-04-26 16:15:35 +0000875
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000876 Result = Actions.ActOnDecltypeExpression(Result.get());
David Blaikie15a430a2011-12-04 05:04:18 +0000877 }
878
879 // Match the ')'
880 T.consumeClose();
881 if (T.getCloseLocation().isInvalid()) {
882 DS.SetTypeSpecError();
883 // FIXME: this should return the location of the last token
884 // that was consumed (by "consumeClose()")
885 return T.getCloseLocation();
886 }
887
Richard Smithfd555f62012-02-22 02:04:18 +0000888 if (Result.isInvalid()) {
889 DS.SetTypeSpecError();
890 return T.getCloseLocation();
891 }
892
David Blaikie15a430a2011-12-04 05:04:18 +0000893 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000894 }
Richard Smith74aeef52013-04-26 16:15:35 +0000895 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000896
Craig Topper161e4db2014-05-21 06:02:52 +0000897 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +0000898 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000899 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Anders Carlsson74948d02009-06-24 17:47:40 +0000900 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000901 if (Result.get()
902 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000903 DiagID, Result.get(), Policy)
Richard Smith74aeef52013-04-26 16:15:35 +0000904 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000905 DiagID, Policy)) {
John McCall49bfce42009-08-03 20:12:06 +0000906 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000907 DS.SetTypeSpecError();
908 }
909 return EndLoc;
910}
911
912void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
913 SourceLocation StartLoc,
914 SourceLocation EndLoc) {
915 // make sure we have a token we can turn into an annotation token
916 if (PP.isBacktrackEnabled())
917 PP.RevertCachedTokens(1);
918 else
919 PP.EnterToken(Tok);
920
921 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000922 setExprAnnotation(Tok,
923 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
924 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
925 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000926 Tok.setAnnotationEndLoc(EndLoc);
927 Tok.setLocation(StartLoc);
928 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000929}
930
Alexis Hunt4a257072011-05-19 05:37:45 +0000931void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
932 assert(Tok.is(tok::kw___underlying_type) &&
933 "Not an underlying type specifier");
934
935 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000936 BalancedDelimiterTracker T(*this, tok::l_paren);
937 if (T.expectAndConsume(diag::err_expected_lparen_after,
938 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +0000939 return;
940 }
941
942 TypeResult Result = ParseTypeName();
943 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000944 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt4a257072011-05-19 05:37:45 +0000945 return;
946 }
947
948 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000949 T.consumeClose();
950 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000951 return;
952
Craig Topper161e4db2014-05-21 06:02:52 +0000953 const char *PrevSpec = nullptr;
Alexis Hunt4a257072011-05-19 05:37:45 +0000954 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000955 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000956 DiagID, Result.get(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000957 Actions.getASTContext().getPrintingPolicy()))
Alexis Hunt4a257072011-05-19 05:37:45 +0000958 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanellaa90af722013-07-06 18:54:58 +0000959 DS.setTypeofParensRange(T.getRange());
Alexis Hunt4a257072011-05-19 05:37:45 +0000960}
961
David Blaikie00ee7a082011-10-25 15:01:20 +0000962/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
963/// class name or decltype-specifier. Note that we only check that the result
964/// names a type; semantic analysis will need to verify that the type names a
965/// class. The result is either a type or null, depending on whether a type
966/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +0000967///
Richard Smith4c96e992013-02-19 23:47:15 +0000968/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000969/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +0000970/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000971/// nested-name-specifier[opt] class-name
972/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +0000973/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +0000974/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000975/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000976///
Richard Smith4c96e992013-02-19 23:47:15 +0000977/// In C++98, instead of base-type-specifier, we have:
978///
979/// ::[opt] nested-name-specifier[opt] class-name
Craig Topper9ad7e262014-10-31 06:57:07 +0000980TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
981 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +0000982 // Ignore attempts to use typename
983 if (Tok.is(tok::kw_typename)) {
984 Diag(Tok, diag::err_expected_class_name_not_template)
985 << FixItHint::CreateRemoval(Tok.getLocation());
986 ConsumeToken();
987 }
988
David Blaikieafa155f2011-10-25 18:17:58 +0000989 // Parse optional nested-name-specifier
990 CXXScopeSpec SS;
David Blaikieefdccaa2016-01-15 23:43:34 +0000991 ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false);
David Blaikieafa155f2011-10-25 18:17:58 +0000992
993 BaseLoc = Tok.getLocation();
994
David Blaikie1cd50022011-10-25 17:10:12 +0000995 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +0000996 // tok == kw_decltype is just error recovery, it can only happen when SS
997 // isn't empty
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000998 if (Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +0000999 if (SS.isNotEmpty())
1000 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
1001 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +00001002 // Fake up a Declarator to use with ActOnTypeName.
1003 DeclSpec DS(AttrFactory);
1004
David Blaikie7491e732011-12-08 04:53:15 +00001005 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +00001006
1007 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1008 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1009 }
1010
Douglas Gregord54dfb82009-02-25 23:52:28 +00001011 // Check whether we have a template-id that names a type.
1012 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001013 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00001014 if (TemplateId->Kind == TNK_Type_template ||
1015 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00001016 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +00001017
1018 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00001019 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +00001020 EndLocation = Tok.getAnnotationEndLoc();
1021 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001022
1023 if (Type)
1024 return Type;
1025 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +00001026 }
1027
1028 // Fall through to produce an error below.
1029 }
1030
Douglas Gregor831c93f2008-11-05 20:51:48 +00001031 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001032 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001033 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +00001034 }
1035
Douglas Gregor18473f32010-01-12 21:28:44 +00001036 IdentifierInfo *Id = Tok.getIdentifierInfo();
1037 SourceLocation IdLoc = ConsumeToken();
1038
1039 if (Tok.is(tok::less)) {
1040 // It looks the user intended to write a template-id here, but the
1041 // template-name was wrong. Try to fix that.
1042 TemplateNameKind TNK = TNK_Type_template;
1043 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001044 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +00001045 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +00001046 Diag(IdLoc, diag::err_unknown_template_name)
1047 << Id;
1048 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001049
Serge Pavlovb716b3c2013-08-10 05:54:47 +00001050 if (!Template) {
1051 TemplateArgList TemplateArgs;
1052 SourceLocation LAngleLoc, RAngleLoc;
David Blaikiee20506d2016-01-15 23:43:28 +00001053 ParseTemplateIdAfterTemplateName(nullptr, IdLoc, SS, true, LAngleLoc,
1054 TemplateArgs, RAngleLoc);
Douglas Gregor18473f32010-01-12 21:28:44 +00001055 return true;
Serge Pavlovb716b3c2013-08-10 05:54:47 +00001056 }
Douglas Gregor18473f32010-01-12 21:28:44 +00001057
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001058 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +00001059 UnqualifiedId TemplateName;
1060 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001061
Douglas Gregor18473f32010-01-12 21:28:44 +00001062 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001063 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1064 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +00001065 return true;
1066 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +00001067 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001068
Douglas Gregor18473f32010-01-12 21:28:44 +00001069 // If we didn't end up with a typename token, there's nothing more we
1070 // can do.
1071 if (Tok.isNot(tok::annot_typename))
1072 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001073
Douglas Gregor18473f32010-01-12 21:28:44 +00001074 // Retrieve the type from the annotation token, consume that token, and
1075 // return.
1076 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +00001077 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +00001078 ConsumeToken();
1079 return Type;
1080 }
1081
Douglas Gregor831c93f2008-11-05 20:51:48 +00001082 // We have an identifier; check whether it is actually a type.
Craig Topper161e4db2014-05-21 06:02:52 +00001083 IdentifierInfo *CorrectedII = nullptr;
David Blaikieefdccaa2016-01-15 23:43:34 +00001084 ParsedType Type =
1085 Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true, false, nullptr,
1086 /*IsCtorOrDtorName=*/false,
1087 /*NonTrivialTypeSourceInfo=*/true, &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001088 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001089 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001090 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +00001091 }
1092
1093 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +00001094 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001095
1096 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +00001097 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +00001098 DS.SetRangeStart(IdLoc);
1099 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +00001100 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001101
Craig Topper161e4db2014-05-21 06:02:52 +00001102 const char *PrevSpec = nullptr;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001103 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001104 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
1105 Actions.getASTContext().getPrintingPolicy());
Nick Lewycky19b9f952010-07-26 16:56:01 +00001106
1107 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1108 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001109}
1110
John McCall8d32c052012-05-22 21:28:12 +00001111void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001112 while (Tok.isOneOf(tok::kw___single_inheritance,
1113 tok::kw___multiple_inheritance,
1114 tok::kw___virtual_inheritance)) {
John McCall8d32c052012-05-22 21:28:12 +00001115 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1116 SourceLocation AttrNameLoc = ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001117 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Aaron Ballman8edb5c22013-12-18 23:44:18 +00001118 AttributeList::AS_Keyword);
John McCall8d32c052012-05-22 21:28:12 +00001119 }
1120}
1121
Richard Smith369b9f92012-06-25 21:37:02 +00001122/// Determine whether the following tokens are valid after a type-specifier
1123/// which could be a standalone declaration. This will conservatively return
1124/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +00001125bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +00001126 // This switch enumerates the valid "follow" set for type-specifiers.
1127 switch (Tok.getKind()) {
1128 default: break;
1129 case tok::semi: // struct foo {...} ;
1130 case tok::star: // struct foo {...} * P;
1131 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +00001132 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +00001133 case tok::identifier: // struct foo {...} V ;
1134 case tok::r_paren: //(struct foo {...} ) {4}
1135 case tok::annot_cxxscope: // struct foo {...} a:: b;
1136 case tok::annot_typename: // struct foo {...} a ::b;
1137 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1138 case tok::l_paren: // struct foo {...} ( x);
1139 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001140 case tok::kw_operator: // struct foo operator ++() {...}
Alp Tokerd3f79c52013-11-24 20:24:54 +00001141 case tok::kw___declspec: // struct foo {...} __declspec(...)
Richard Smith843f18f2014-08-13 02:13:15 +00001142 case tok::l_square: // void f(struct f [ 3])
1143 case tok::ellipsis: // void f(struct f ... [Ns])
Abramo Bagnara152eb392014-08-16 08:29:27 +00001144 // FIXME: we should emit semantic diagnostic when declaration
1145 // attribute is in type attribute position.
1146 case tok::kw___attribute: // struct foo __attribute__((used)) x;
David Majnemer15b311c2016-06-14 03:20:28 +00001147 case tok::annot_pragma_pack: // struct foo {...} _Pragma(pack(pop));
1148 // struct foo {...} _Pragma(section(...));
1149 case tok::annot_pragma_ms_pragma:
1150 // struct foo {...} _Pragma(vtordisp(pop));
1151 case tok::annot_pragma_ms_vtordisp:
1152 // struct foo {...} _Pragma(pointers_to_members(...));
1153 case tok::annot_pragma_ms_pointers_to_members:
Richard Smith369b9f92012-06-25 21:37:02 +00001154 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001155 case tok::colon:
1156 return CouldBeBitfield; // enum E { ... } : 2;
Reid Klecknercfa91552016-03-21 16:08:49 +00001157 // Microsoft compatibility
1158 case tok::kw___cdecl: // struct foo {...} __cdecl x;
1159 case tok::kw___fastcall: // struct foo {...} __fastcall x;
1160 case tok::kw___stdcall: // struct foo {...} __stdcall x;
1161 case tok::kw___thiscall: // struct foo {...} __thiscall x;
1162 case tok::kw___vectorcall: // struct foo {...} __vectorcall x;
1163 // We will diagnose these calling-convention specifiers on non-function
1164 // declarations later, so claim they are valid after a type specifier.
1165 return getLangOpts().MicrosoftExt;
Richard Smith369b9f92012-06-25 21:37:02 +00001166 // Type qualifiers
1167 case tok::kw_const: // struct foo {...} const x;
1168 case tok::kw_volatile: // struct foo {...} volatile x;
1169 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith843f18f2014-08-13 02:13:15 +00001170 case tok::kw__Atomic: // struct foo {...} _Atomic x;
Nico Rieck3e1ee832014-12-04 23:30:25 +00001171 case tok::kw___unaligned: // struct foo {...} __unaligned *x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001172 // Function specifiers
1173 // Note, no 'explicit'. An explicit function must be either a conversion
1174 // operator or a constructor. Either way, it can't have a return type.
1175 case tok::kw_inline: // struct foo inline f();
1176 case tok::kw_virtual: // struct foo virtual f();
1177 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001178 // Storage-class specifiers
1179 case tok::kw_static: // struct foo {...} static x;
1180 case tok::kw_extern: // struct foo {...} extern x;
1181 case tok::kw_typedef: // struct foo {...} typedef x;
1182 case tok::kw_register: // struct foo {...} register x;
1183 case tok::kw_auto: // struct foo {...} auto x;
1184 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001185 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001186 case tok::kw_constexpr: // struct foo {...} constexpr x;
1187 // As shown above, type qualifiers and storage class specifiers absolutely
1188 // can occur after class specifiers according to the grammar. However,
1189 // almost no one actually writes code like this. If we see one of these,
1190 // it is much more likely that someone missed a semi colon and the
1191 // type/storage class specifier we're seeing is part of the *next*
1192 // intended declaration, as in:
1193 //
1194 // struct foo { ... }
1195 // typedef int X;
1196 //
1197 // We'd really like to emit a missing semicolon error instead of emitting
1198 // an error on the 'int' saying that you can't have two type specifiers in
1199 // the same declaration of X. Because of this, we look ahead past this
1200 // token to see if it's a type specifier. If so, we know the code is
1201 // otherwise invalid, so we can produce the expected semi error.
1202 if (!isKnownToBeTypeSpecifier(NextToken()))
1203 return true;
1204 break;
1205 case tok::r_brace: // struct bar { struct foo {...} }
1206 // Missing ';' at end of struct is accepted as an extension in C mode.
1207 if (!getLangOpts().CPlusPlus)
1208 return true;
1209 break;
Richard Smith52c5b872013-01-29 04:13:32 +00001210 case tok::greater:
1211 // template<class T = class X>
1212 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001213 }
1214 return false;
1215}
1216
Douglas Gregor556877c2008-04-13 21:30:24 +00001217/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1218/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1219/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001220/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001221///
1222/// class-specifier: [C++ class]
1223/// class-head '{' member-specification[opt] '}'
1224/// class-head '{' member-specification[opt] '}' attributes[opt]
1225/// class-head:
1226/// class-key identifier[opt] base-clause[opt]
1227/// class-key nested-name-specifier identifier base-clause[opt]
1228/// class-key nested-name-specifier[opt] simple-template-id
1229/// base-clause[opt]
1230/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001231/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001232/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001233/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001234/// simple-template-id base-clause[opt]
1235/// class-key:
1236/// 'class'
1237/// 'struct'
1238/// 'union'
1239///
1240/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001241/// class-key ::[opt] nested-name-specifier[opt] identifier
1242/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1243/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001244///
1245/// Note that the C++ class-specifier and elaborated-type-specifier,
1246/// together, subsume the C99 struct-or-union-specifier:
1247///
1248/// struct-or-union-specifier: [C99 6.7.2.1]
1249/// struct-or-union identifier[opt] '{' struct-contents '}'
1250/// struct-or-union identifier
1251/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1252/// '}' attributes[opt]
1253/// [GNU] struct-or-union attributes[opt] identifier
1254/// struct-or-union:
1255/// 'struct'
1256/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001257void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1258 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001259 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001260 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001261 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001262 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001263 DeclSpec::TST TagType;
1264 if (TagTokKind == tok::kw_struct)
1265 TagType = DeclSpec::TST_struct;
1266 else if (TagTokKind == tok::kw___interface)
1267 TagType = DeclSpec::TST_interface;
1268 else if (TagTokKind == tok::kw_class)
1269 TagType = DeclSpec::TST_class;
1270 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001271 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1272 TagType = DeclSpec::TST_union;
1273 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001274
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001275 if (Tok.is(tok::code_completion)) {
1276 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001277 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001278 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001279 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001280
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001281 // C++03 [temp.explicit] 14.7.2/8:
1282 // The usual access checking rules do not apply to names used to specify
1283 // explicit instantiations.
1284 //
1285 // As an extension we do not perform access checking on the names used to
1286 // specify explicit specializations either. This is important to allow
1287 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001288 //
1289 // Note that we don't suppress if this turns out to be an elaborated
1290 // type specifier.
1291 bool shouldDelayDiagsInTag =
1292 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1293 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1294 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001295
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001296 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001297 // If attributes exist after tag, parse them.
Richard Smith37a45dd2013-10-24 01:21:09 +00001298 MaybeParseGNUAttributes(attrs);
Aaron Ballman068aa512015-05-20 20:58:33 +00001299 MaybeParseMicrosoftDeclSpecs(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001300
John McCall8d32c052012-05-22 21:28:12 +00001301 // Parse inheritance specifiers.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001302 if (Tok.isOneOf(tok::kw___single_inheritance,
1303 tok::kw___multiple_inheritance,
1304 tok::kw___virtual_inheritance))
Richard Smith37a45dd2013-10-24 01:21:09 +00001305 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCall8d32c052012-05-22 21:28:12 +00001306
Alexis Hunt96d5c762009-11-21 08:43:09 +00001307 // If C++0x attributes exist here, parse them.
1308 // FIXME: Are we consistent with the ordering of parsing of different
1309 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001310 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001311
Michael Han309af292013-01-07 16:57:11 +00001312 // Source location used by FIXIT to insert misplaced
1313 // C++11 attributes
1314 SourceLocation AttrFixitLoc = Tok.getLocation();
1315
Nico Weber7c3c5be2014-09-23 04:09:56 +00001316 if (TagType == DeclSpec::TST_struct &&
David Majnemer86330af2014-12-29 02:14:26 +00001317 Tok.isNot(tok::identifier) &&
1318 !Tok.isAnnotation() &&
Nico Weber7c3c5be2014-09-23 04:09:56 +00001319 Tok.getIdentifierInfo() &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001320 Tok.isOneOf(tok::kw___is_abstract,
1321 tok::kw___is_arithmetic,
1322 tok::kw___is_array,
David Majnemerb3d96882016-05-23 17:21:55 +00001323 tok::kw___is_assignable,
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001324 tok::kw___is_base_of,
1325 tok::kw___is_class,
1326 tok::kw___is_complete_type,
1327 tok::kw___is_compound,
1328 tok::kw___is_const,
1329 tok::kw___is_constructible,
1330 tok::kw___is_convertible,
1331 tok::kw___is_convertible_to,
1332 tok::kw___is_destructible,
1333 tok::kw___is_empty,
1334 tok::kw___is_enum,
1335 tok::kw___is_floating_point,
1336 tok::kw___is_final,
1337 tok::kw___is_function,
1338 tok::kw___is_fundamental,
1339 tok::kw___is_integral,
1340 tok::kw___is_interface_class,
1341 tok::kw___is_literal,
1342 tok::kw___is_lvalue_expr,
1343 tok::kw___is_lvalue_reference,
1344 tok::kw___is_member_function_pointer,
1345 tok::kw___is_member_object_pointer,
1346 tok::kw___is_member_pointer,
1347 tok::kw___is_nothrow_assignable,
1348 tok::kw___is_nothrow_constructible,
1349 tok::kw___is_nothrow_destructible,
1350 tok::kw___is_object,
1351 tok::kw___is_pod,
1352 tok::kw___is_pointer,
1353 tok::kw___is_polymorphic,
1354 tok::kw___is_reference,
1355 tok::kw___is_rvalue_expr,
1356 tok::kw___is_rvalue_reference,
1357 tok::kw___is_same,
1358 tok::kw___is_scalar,
1359 tok::kw___is_sealed,
1360 tok::kw___is_signed,
1361 tok::kw___is_standard_layout,
1362 tok::kw___is_trivial,
1363 tok::kw___is_trivially_assignable,
1364 tok::kw___is_trivially_constructible,
1365 tok::kw___is_trivially_copyable,
1366 tok::kw___is_union,
1367 tok::kw___is_unsigned,
1368 tok::kw___is_void,
1369 tok::kw___is_volatile))
Nico Weber7c3c5be2014-09-23 04:09:56 +00001370 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
1371 // name of struct templates, but some are keywords in GCC >= 4.3
1372 // and Clang. Therefore, when we see the token sequence "struct
1373 // X", make X into a normal identifier rather than a keyword, to
1374 // allow libstdc++ 4.2 and libc++ to work properly.
1375 TryKeywordIdentFallback(true);
Mike Stump11289f42009-09-09 15:08:12 +00001376
David Majnemer51fd8a02015-07-22 23:46:18 +00001377 struct PreserveAtomicIdentifierInfoRAII {
1378 PreserveAtomicIdentifierInfoRAII(Token &Tok, bool Enabled)
1379 : AtomicII(nullptr) {
1380 if (!Enabled)
1381 return;
1382 assert(Tok.is(tok::kw__Atomic));
1383 AtomicII = Tok.getIdentifierInfo();
1384 AtomicII->revertTokenIDToIdentifier();
1385 Tok.setKind(tok::identifier);
1386 }
1387 ~PreserveAtomicIdentifierInfoRAII() {
1388 if (!AtomicII)
1389 return;
1390 AtomicII->revertIdentifierToTokenID(tok::kw__Atomic);
1391 }
1392 IdentifierInfo *AtomicII;
1393 };
1394
1395 // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
1396 // implementation for VS2013 uses _Atomic as an identifier for one of the
1397 // classes in <atomic>. When we are parsing 'struct _Atomic', don't consider
1398 // '_Atomic' to be a keyword. We are careful to undo this so that clang can
1399 // use '_Atomic' in its own header files.
1400 bool ShouldChangeAtomicToIdentifier = getLangOpts().MSVCCompat &&
1401 Tok.is(tok::kw__Atomic) &&
1402 TagType == DeclSpec::TST_struct;
1403 PreserveAtomicIdentifierInfoRAII AtomicTokenGuard(
1404 Tok, ShouldChangeAtomicToIdentifier);
1405
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001406 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001407 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001408 if (getLangOpts().CPlusPlus) {
Serge Pavlov458ea762014-07-16 05:16:52 +00001409 // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it
1410 // is a base-specifier-list.
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001411 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001412
Nico Webercfaa4cd2015-02-15 07:26:13 +00001413 CXXScopeSpec Spec;
1414 bool HasValidSpec = true;
David Blaikieefdccaa2016-01-15 23:43:34 +00001415 if (ParseOptionalCXXScopeSpecifier(Spec, nullptr, EnteringContext)) {
John McCall413021a2010-07-30 06:26:29 +00001416 DS.SetTypeSpecError();
Nico Webercfaa4cd2015-02-15 07:26:13 +00001417 HasValidSpec = false;
1418 }
1419 if (Spec.isSet())
1420 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) {
Alp Tokerec543272013-12-24 09:48:30 +00001421 Diag(Tok, diag::err_expected) << tok::identifier;
Nico Webercfaa4cd2015-02-15 07:26:13 +00001422 HasValidSpec = false;
1423 }
1424 if (HasValidSpec)
1425 SS = Spec;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001426 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001427
Douglas Gregor916462b2009-10-30 21:46:58 +00001428 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1429
Douglas Gregor67a65642009-02-17 23:15:12 +00001430 // Parse the (optional) class name or simple-template-id.
Craig Topper161e4db2014-05-21 06:02:52 +00001431 IdentifierInfo *Name = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001432 SourceLocation NameLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001433 TemplateIdAnnotation *TemplateId = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001434 if (Tok.is(tok::identifier)) {
1435 Name = Tok.getIdentifierInfo();
1436 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001437
David Blaikiebbafb8a2012-03-11 07:00:24 +00001438 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001439 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001440 // Eat the template argument list and try to continue parsing this as
1441 // a class (or template thereof).
1442 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001443 SourceLocation LAngleLoc, RAngleLoc;
David Blaikiee20506d2016-01-15 23:43:28 +00001444 if (ParseTemplateIdAfterTemplateName(
1445 nullptr, NameLoc, SS, true, LAngleLoc, TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001446 // We couldn't parse the template argument list at all, so don't
1447 // try to give any location information for the list.
1448 LAngleLoc = RAngleLoc = SourceLocation();
1449 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001450
Douglas Gregor916462b2009-10-30 21:46:58 +00001451 Diag(NameLoc, diag::err_explicit_spec_non_template)
Alp Toker01d65e12014-01-06 12:54:41 +00001452 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1453 << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
Joao Matose9a3ed42012-08-31 22:18:20 +00001454
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001455 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001456 // we've removed its template argument list.
1457 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
Hubert Tong97b06632016-04-13 18:41:03 +00001458 if (TemplateParams->size() > 1) {
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001459 TemplateParams->pop_back();
1460 } else {
Craig Topper161e4db2014-05-21 06:02:52 +00001461 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001462 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001463 = ParsedTemplateInfo::NonTemplate;
1464 }
1465 } else if (TemplateInfo.Kind
1466 == ParsedTemplateInfo::ExplicitInstantiation) {
1467 // Pretend this is just a forward declaration.
Craig Topper161e4db2014-05-21 06:02:52 +00001468 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001469 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001470 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001471 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001472 = SourceLocation();
1473 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1474 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001475 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001476 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001477 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001478 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001479 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001480
Douglas Gregore7c20652011-03-02 00:47:37 +00001481 if (TemplateId->Kind != TNK_Type_template &&
1482 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001483 // The template-name in the simple-template-id refers to
1484 // something other than a class template. Give an appropriate
1485 // error message and skip to the ';'.
1486 SourceRange Range(NameLoc);
1487 if (SS.isNotEmpty())
1488 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001489
Richard Smith72bfbd82013-12-04 00:28:23 +00001490 // FIXME: Name may be null here.
Douglas Gregor7f741122009-02-25 19:37:18 +00001491 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001492 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001493
Douglas Gregor7f741122009-02-25 19:37:18 +00001494 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001495 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor7f741122009-02-25 19:37:18 +00001496 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001497 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001498 }
1499
Richard Smithbfdb1082012-03-12 08:56:40 +00001500 // There are four options here.
1501 // - If we are in a trailing return type, this is always just a reference,
1502 // and we must not try to parse a definition. For instance,
1503 // [] () -> struct S { };
1504 // does not define a type.
1505 // - If we have 'struct foo {...', 'struct foo :...',
1506 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1507 // - If we have 'struct foo;', then this is either a forward declaration
1508 // or a friend declaration, which have to be treated differently.
1509 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001510 //
1511 // We also detect these erroneous cases to provide better diagnostic for
1512 // C++11 attributes parsing.
1513 // - attributes follow class name:
1514 // struct foo [[]] {};
1515 // - attributes appear before or after 'final':
1516 // struct foo [[]] final [[]] {};
1517 //
Richard Smithc5b05522012-03-12 07:56:15 +00001518 // However, in type-specifier-seq's, things look like declarations but are
1519 // just references, e.g.
1520 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001521 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001522 // &T::operator struct s;
Richard Smith649c7b062014-01-08 00:56:48 +00001523 // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
Michael Han9407e502012-11-26 22:54:45 +00001524
1525 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001526 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001527
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001528 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
John McCallfaf5fb42010-08-26 23:41:50 +00001529 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001530 if (DSC == DSC_trailing)
1531 TUK = Sema::TUK_Reference;
1532 else if (Tok.is(tok::l_brace) ||
1533 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001534 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001535 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001536 if (DS.isFriendSpecified()) {
1537 // C++ [class.friend]p2:
1538 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001539 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001540 << SourceRange(DS.getFriendSpecLoc());
1541
1542 // Skip everything up to the semicolon, so that this looks like a proper
1543 // friend class (or template thereof) declaration.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001544 SkipUntil(tok::semi, StopBeforeMatch);
John McCallfaf5fb42010-08-26 23:41:50 +00001545 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001546 } else {
1547 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001548 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001549 }
Richard Smith434516c2013-02-22 06:46:23 +00001550 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1551 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001552 // We can't tell if this is a definition or reference
1553 // until we skipped the 'final' and C++11 attribute specifiers.
1554 TentativeParsingAction PA(*this);
1555
1556 // Skip the 'final' keyword.
1557 ConsumeToken();
1558
1559 // Skip C++11 attribute specifiers.
1560 while (true) {
1561 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1562 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001563 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001564 break;
Richard Smith434516c2013-02-22 06:46:23 +00001565 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001566 ConsumeToken();
1567 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001568 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001569 break;
1570 } else {
1571 break;
1572 }
1573 }
1574
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001575 if (Tok.isOneOf(tok::l_brace, tok::colon))
Michael Han9407e502012-11-26 22:54:45 +00001576 TUK = Sema::TUK_Definition;
1577 else
1578 TUK = Sema::TUK_Reference;
1579
1580 PA.Revert();
Richard Smith649c7b062014-01-08 00:56:48 +00001581 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00001582 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001583 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001584 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001585 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001586 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Joao Matose9a3ed42012-08-31 22:18:20 +00001587 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00001588 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001589 DeclSpec::getSpecifierName(TagType, PPol));
Joao Matose9a3ed42012-08-31 22:18:20 +00001590 PP.EnterToken(Tok);
1591 Tok.setKind(tok::semi);
1592 }
Richard Smith369b9f92012-06-25 21:37:02 +00001593 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001594 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001595
Michael Han9407e502012-11-26 22:54:45 +00001596 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1597 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001598 if (TUK != Sema::TUK_Reference) {
1599 // If this is not a reference, then the only possible
1600 // valid place for C++11 attributes to appear here
1601 // is between class-key and class-name. If there are
1602 // any attributes after class-name, we try a fixit to move
1603 // them to the right place.
1604 SourceRange AttrRange = Attributes.Range;
1605 if (AttrRange.isValid()) {
1606 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1607 << AttrRange
1608 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1609 CharSourceRange(AttrRange, true))
1610 << FixItHint::CreateRemoval(AttrRange);
1611
1612 // Recover by adding misplaced attributes to the attribute list
1613 // of the class so they can be applied on the class later.
1614 attrs.takeAllFrom(Attributes);
1615 }
1616 }
Michael Han9407e502012-11-26 22:54:45 +00001617
John McCall6347b682012-05-07 06:16:58 +00001618 // If this is an elaborated type specifier, and we delayed
1619 // diagnostics before, just merge them into the current pool.
1620 if (shouldDelayDiagsInTag) {
1621 diagsFromTag.done();
1622 if (TUK == Sema::TUK_Reference)
1623 diagsFromTag.redelay();
1624 }
1625
John McCall413021a2010-07-30 06:26:29 +00001626 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001627 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001628 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1629 // We have a declaration or reference to an anonymous class.
1630 Diag(StartLoc, diag::err_anon_type_definition)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001631 << DeclSpec::getSpecifierName(TagType, Policy);
John McCall413021a2010-07-30 06:26:29 +00001632 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001633
David Majnemer3252fd02013-12-05 01:36:53 +00001634 // If we are parsing a definition and stop at a base-clause, continue on
1635 // until the semicolon. Continuing from the comma will just trick us into
1636 // thinking we are seeing a variable declaration.
1637 if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1638 SkipUntil(tok::semi, StopBeforeMatch);
1639 else
1640 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor556877c2008-04-13 21:30:24 +00001641 return;
1642 }
1643
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001644 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001645 DeclResult TagOrTempResult = true; // invalid
1646 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001647
Douglas Gregord6ab8742009-05-28 23:31:59 +00001648 bool Owned = false;
Richard Smithd9ba2242015-05-07 03:54:19 +00001649 Sema::SkipBodyInfo SkipBody;
John McCall06f6fe8d2009-09-04 01:14:41 +00001650 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001651 // Explicit specialization, class template partial specialization,
1652 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001653 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001654 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001655 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001656 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001657 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001658 ProhibitAttributes(attrs);
1659
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001660 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001661 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001662 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001663 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001664 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001665 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001666 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001667 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001668 TemplateId->TemplateNameLoc,
1669 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001670 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001671 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001672 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001673
1674 // Friend template-ids are treated as references unless
1675 // they have template headers, in which case they're ill-formed
1676 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1677 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001678 } else if (TUK == Sema::TUK_Reference ||
1679 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001680 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001681 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001682 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001683 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001684 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001685 TemplateId->Template,
1686 TemplateId->TemplateNameLoc,
1687 TemplateId->LAngleLoc,
1688 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001689 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001690 } else {
1691 // This is an explicit specialization or a class template
1692 // partial specialization.
1693 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001694 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1695 // This looks like an explicit instantiation, because we have
1696 // something like
1697 //
1698 // template class Foo<X>
1699 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001700 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001701 // meant to be an explicit specialization, but the user forgot
1702 // the '<>' after 'template'.
Richard Smith003c5e12013-11-08 19:03:29 +00001703 // It this is friend declaration however, since it cannot have a
1704 // template header, it is most likely that the user meant to
1705 // remove the 'template' keyword.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001706 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith003c5e12013-11-08 19:03:29 +00001707 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001708
Richard Smith003c5e12013-11-08 19:03:29 +00001709 if (TUK == Sema::TUK_Friend) {
1710 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
Craig Topper161e4db2014-05-21 06:02:52 +00001711 TemplateParams = nullptr;
Richard Smith003c5e12013-11-08 19:03:29 +00001712 } else {
1713 SourceLocation LAngleLoc =
1714 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1715 Diag(TemplateId->TemplateNameLoc,
1716 diag::err_explicit_instantiation_with_definition)
1717 << SourceRange(TemplateInfo.TemplateLoc)
1718 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1719
1720 // Create a fake template parameter list that contains only
1721 // "template<>", so that we treat this construct as a class
1722 // template specialization.
1723 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
Craig Topper96225a52015-12-24 23:58:25 +00001724 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None,
Hubert Tongf608c052016-04-29 18:05:37 +00001725 LAngleLoc, nullptr));
Richard Smith003c5e12013-11-08 19:03:29 +00001726 TemplateParams = &FakedParamLists;
1727 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001728 }
1729
1730 // Build the class template specialization.
Richard Smith4b55a9c2014-04-17 03:29:33 +00001731 TagOrTempResult = Actions.ActOnClassTemplateSpecialization(
1732 getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(),
1733 *TemplateId, attrs.getList(),
Craig Topper161e4db2014-05-21 06:02:52 +00001734 MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0]
1735 : nullptr,
Richard Smithc7e6ff02015-05-18 20:36:47 +00001736 TemplateParams ? TemplateParams->size() : 0),
1737 &SkipBody);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001738 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001739 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001740 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001741 // Explicit instantiation of a member of a class template
1742 // specialization, e.g.,
1743 //
1744 // template struct Outer<int>::Inner;
1745 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001746 ProhibitAttributes(attrs);
1747
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001748 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001749 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001750 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001751 TemplateInfo.TemplateLoc,
1752 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001753 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001754 } else if (TUK == Sema::TUK_Friend &&
1755 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001756 ProhibitAttributes(attrs);
1757
John McCallace48cd2010-10-19 01:40:49 +00001758 TagOrTempResult =
1759 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1760 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001761 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001762 MultiTemplateParamsArg(
Craig Topper161e4db2014-05-21 06:02:52 +00001763 TemplateParams? &(*TemplateParams)[0]
1764 : nullptr,
John McCallace48cd2010-10-19 01:40:49 +00001765 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001766 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001767 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1768 ProhibitAttributes(attrs);
Richard Smith003c5e12013-11-08 19:03:29 +00001769
Larisse Voufo725de3e2013-06-21 00:08:46 +00001770 if (TUK == Sema::TUK_Definition &&
1771 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1772 // If the declarator-id is not a template-id, issue a diagnostic and
1773 // recover by ignoring the 'template' keyword.
1774 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1775 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Craig Topper161e4db2014-05-21 06:02:52 +00001776 TemplateParams = nullptr;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001777 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001778
John McCall7f41d982009-09-11 04:59:25 +00001779 bool IsDependent = false;
1780
John McCall32723e92010-10-19 18:40:57 +00001781 // Don't pass down template parameter lists if this is just a tag
1782 // reference. For example, we don't need the template parameters here:
1783 // template <class T> class A *makeA(T t);
1784 MultiTemplateParamsArg TParams;
1785 if (TUK != Sema::TUK_Reference && TemplateParams)
1786 TParams =
1787 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1788
Nico Weber32a0fc72016-09-03 03:01:32 +00001789 stripTypeAttributesOffDeclSpec(attrs, DS, TUK);
David Majnemer936b4112015-04-19 07:53:29 +00001790
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001791 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001792 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001793 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001794 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001795 TParams, Owned, IsDependent,
1796 SourceLocation(), false,
Richard Smith649c7b062014-01-08 00:56:48 +00001797 clang::TypeResult(),
Richard Smith65ebb4a2015-03-26 04:09:53 +00001798 DSC == DSC_type_specifier,
1799 &SkipBody);
John McCall7f41d982009-09-11 04:59:25 +00001800
1801 // If ActOnTag said the type was dependent, try again with the
1802 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001803 if (IsDependent) {
1804 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001805 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001806 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001807 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001808 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001809
Douglas Gregor556877c2008-04-13 21:30:24 +00001810 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001811 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001812 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001813 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001814 isCXX11FinalKeyword());
Richard Smithd9ba2242015-05-07 03:54:19 +00001815 if (SkipBody.ShouldSkip)
Richard Smith65ebb4a2015-03-26 04:09:53 +00001816 SkipCXXMemberSpecification(StartLoc, AttrFixitLoc, TagType,
1817 TagOrTempResult.get());
1818 else if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001819 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1820 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001821 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001822 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001823 }
1824
Craig Topper161e4db2014-05-21 06:02:52 +00001825 const char *PrevSpec = nullptr;
John McCallba7bf592010-08-24 05:47:05 +00001826 unsigned DiagID;
1827 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001828 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001829 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1830 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001831 PrevSpec, DiagID, TypeResult.get(), Policy);
John McCall7f41d982009-09-11 04:59:25 +00001832 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001833 Result = DS.SetTypeSpecType(TagType, StartLoc,
1834 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001835 PrevSpec, DiagID, TagOrTempResult.get(), Owned,
1836 Policy);
John McCall7f41d982009-09-11 04:59:25 +00001837 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001838 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001839 return;
1840 }
Mike Stump11289f42009-09-09 15:08:12 +00001841
John McCallba7bf592010-08-24 05:47:05 +00001842 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001843 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001844
Chris Lattnercf251412010-02-02 01:23:29 +00001845 // At this point, we've successfully parsed a class-specifier in 'definition'
1846 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1847 // going to look at what comes after it to improve error recovery. If an
1848 // impossible token occurs next, we assume that the programmer forgot a ; at
1849 // the end of the declaration and recover that way.
1850 //
Richard Smith369b9f92012-06-25 21:37:02 +00001851 // Also enforce C++ [temp]p3:
1852 // In a template-declaration which defines a class, no declarator
1853 // is permitted.
Richard Smith843f18f2014-08-13 02:13:15 +00001854 //
1855 // After a type-specifier, we don't expect a semicolon. This only happens in
1856 // C, since definitions are not permitted in this context in C++.
Joao Matose9a3ed42012-08-31 22:18:20 +00001857 if (TUK == Sema::TUK_Definition &&
Richard Smith843f18f2014-08-13 02:13:15 +00001858 (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) &&
Joao Matose9a3ed42012-08-31 22:18:20 +00001859 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001860 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001861 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Alp Toker383d2c42014-01-01 03:08:43 +00001862 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001863 DeclSpec::getSpecifierName(TagType, PPol));
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001864 // Push this token back into the preprocessor and change our current token
1865 // to ';' so that the rest of the code recovers as though there were an
1866 // ';' after the definition.
1867 PP.EnterToken(Tok);
1868 Tok.setKind(tok::semi);
1869 }
Chris Lattnercf251412010-02-02 01:23:29 +00001870 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001871}
1872
Mike Stump11289f42009-09-09 15:08:12 +00001873/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001874///
1875/// base-clause : [C++ class.derived]
1876/// ':' base-specifier-list
1877/// base-specifier-list:
1878/// base-specifier '...'[opt]
1879/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001880void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001881 assert(Tok.is(tok::colon) && "Not a base clause");
1882 ConsumeToken();
1883
Douglas Gregor29a92472008-10-22 17:49:05 +00001884 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001885 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001886
Douglas Gregor556877c2008-04-13 21:30:24 +00001887 while (true) {
1888 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001889 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001890 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001891 // Skip the rest of this base specifier, up until the comma or
1892 // opening brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001893 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor29a92472008-10-22 17:49:05 +00001894 } else {
1895 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001896 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001897 }
1898
1899 // If the next token is a comma, consume it and keep reading
1900 // base-specifiers.
Alp Toker97650562014-01-10 11:19:30 +00001901 if (!TryConsumeToken(tok::comma))
1902 break;
Douglas Gregor556877c2008-04-13 21:30:24 +00001903 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001904
1905 // Attach the base specifiers
Craig Topperaa700cb2015-12-27 21:55:19 +00001906 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo);
Douglas Gregor556877c2008-04-13 21:30:24 +00001907}
1908
1909/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1910/// one entry in the base class list of a class specifier, for example:
1911/// class foo : public bar, virtual private baz {
1912/// 'public bar' and 'virtual private baz' are each base-specifiers.
1913///
1914/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001915/// attribute-specifier-seq[opt] base-type-specifier
1916/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1917/// base-type-specifier
1918/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1919/// base-type-specifier
Craig Topper9ad7e262014-10-31 06:57:07 +00001920BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001921 bool IsVirtual = false;
1922 SourceLocation StartLoc = Tok.getLocation();
1923
Richard Smith4c96e992013-02-19 23:47:15 +00001924 ParsedAttributesWithRange Attributes(AttrFactory);
1925 MaybeParseCXX11Attributes(Attributes);
1926
Douglas Gregor556877c2008-04-13 21:30:24 +00001927 // Parse the 'virtual' keyword.
Alp Toker97650562014-01-10 11:19:30 +00001928 if (TryConsumeToken(tok::kw_virtual))
Douglas Gregor556877c2008-04-13 21:30:24 +00001929 IsVirtual = true;
Douglas Gregor556877c2008-04-13 21:30:24 +00001930
Richard Smith4c96e992013-02-19 23:47:15 +00001931 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1932
Douglas Gregor556877c2008-04-13 21:30:24 +00001933 // Parse an (optional) access specifier.
1934 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001935 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001936 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001937
Richard Smith4c96e992013-02-19 23:47:15 +00001938 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1939
Douglas Gregor556877c2008-04-13 21:30:24 +00001940 // Parse the 'virtual' keyword (again!), in case it came after the
1941 // access specifier.
1942 if (Tok.is(tok::kw_virtual)) {
1943 SourceLocation VirtualLoc = ConsumeToken();
1944 if (IsVirtual) {
1945 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001946 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001947 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001948 }
1949
1950 IsVirtual = true;
1951 }
1952
Richard Smith4c96e992013-02-19 23:47:15 +00001953 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1954
Douglas Gregor831c93f2008-11-05 20:51:48 +00001955 // Parse the class-name.
David Majnemer51fd8a02015-07-22 23:46:18 +00001956
1957 // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
1958 // implementation for VS2013 uses _Atomic as an identifier for one of the
1959 // classes in <atomic>. Treat '_Atomic' to be an identifier when we are
1960 // parsing the class-name for a base specifier.
1961 if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) &&
1962 NextToken().is(tok::less))
1963 Tok.setKind(tok::identifier);
1964
Douglas Gregord54dfb82009-02-25 23:52:28 +00001965 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001966 SourceLocation BaseLoc;
1967 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001968 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001969 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001970
Douglas Gregor752a5952011-01-03 22:36:02 +00001971 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1972 // actually part of the base-specifier-list grammar productions, but we
1973 // parse it here for convenience.
1974 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00001975 TryConsumeToken(tok::ellipsis, EllipsisLoc);
1976
Mike Stump11289f42009-09-09 15:08:12 +00001977 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001978 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001979
Douglas Gregor556877c2008-04-13 21:30:24 +00001980 // Notify semantic analysis that we have parsed a complete
1981 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00001982 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1983 Access, BaseType.get(), BaseLoc,
1984 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001985}
1986
1987/// getAccessSpecifierIfPresent - Determine whether the next token is
1988/// a C++ access-specifier.
1989///
1990/// access-specifier: [C++ class.derived]
1991/// 'private'
1992/// 'protected'
1993/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001994AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001995 switch (Tok.getKind()) {
1996 default: return AS_none;
1997 case tok::kw_private: return AS_private;
1998 case tok::kw_protected: return AS_protected;
1999 case tok::kw_public: return AS_public;
2000 }
2001}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002002
Douglas Gregor433e0532012-04-16 18:27:27 +00002003/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith0b3a4622014-11-13 20:01:57 +00002004/// delayed, e.g., default arguments or an exception-specification, create a
2005/// late-parsed method declaration record to handle the parsing at the end of
2006/// the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00002007void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
2008 Decl *ThisDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002009 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00002010 = DeclaratorInfo.getFunctionTypeInfo();
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002011 // If there was a late-parsed exception-specification, we'll need a
2012 // late parse
2013 bool NeedLateParse = FTI.getExceptionSpecType() == EST_Unparsed;
Douglas Gregor433e0532012-04-16 18:27:27 +00002014
Nathan Sidwell5bb231c2015-02-19 14:03:22 +00002015 if (!NeedLateParse) {
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002016 // Look ahead to see if there are any default args
Nathan Sidwell5bb231c2015-02-19 14:03:22 +00002017 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
2018 auto Param = cast<ParmVarDecl>(FTI.Params[ParamIdx].Param);
2019 if (Param->hasUnparsedDefaultArg()) {
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002020 NeedLateParse = true;
2021 break;
2022 }
Nathan Sidwell5bb231c2015-02-19 14:03:22 +00002023 }
2024 }
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002025
2026 if (NeedLateParse) {
Richard Smith0b3a4622014-11-13 20:01:57 +00002027 // Push this method onto the stack of late-parsed method
2028 // declarations.
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002029 auto LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
Richard Smith0b3a4622014-11-13 20:01:57 +00002030 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
2031 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
2032
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002033 // Stash the exception-specification tokens in the late-pased method.
Richard Smith0b3a4622014-11-13 20:01:57 +00002034 LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens;
Hans Wennborgdcfba332015-10-06 23:40:43 +00002035 FTI.ExceptionSpecTokens = nullptr;
Richard Smith0b3a4622014-11-13 20:01:57 +00002036
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002037 // Push tokens for each parameter. Those that do not have
2038 // defaults will be NULL.
Richard Smith0b3a4622014-11-13 20:01:57 +00002039 LateMethod->DefaultArgs.reserve(FTI.NumParams);
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002040 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx)
Alp Tokerc5350722014-02-26 22:27:52 +00002041 LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002042 FTI.Params[ParamIdx].Param, FTI.Params[ParamIdx].DefaultArgTokens));
Eli Friedman3af2a772009-07-22 21:45:50 +00002043 }
2044}
2045
Richard Smith89645bc2013-01-02 12:01:23 +00002046/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002047/// virt-specifier.
2048///
2049/// virt-specifier:
2050/// override
2051/// final
Andrey Bokhanko276055b2016-07-29 10:42:48 +00002052/// __final
Richard Smith89645bc2013-01-02 12:01:23 +00002053VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002054 if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002055 return VirtSpecifiers::VS_None;
2056
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002057 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002058
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002059 // Initialize the contextual keywords.
2060 if (!Ident_final) {
2061 Ident_final = &PP.getIdentifierTable().get("final");
Andrey Bokhanko276055b2016-07-29 10:42:48 +00002062 if (getLangOpts().GNUKeywords)
2063 Ident_GNU_final = &PP.getIdentifierTable().get("__final");
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002064 if (getLangOpts().MicrosoftExt)
2065 Ident_sealed = &PP.getIdentifierTable().get("sealed");
2066 Ident_override = &PP.getIdentifierTable().get("override");
Anders Carlsson56104902011-01-17 03:05:47 +00002067 }
2068
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002069 if (II == Ident_override)
2070 return VirtSpecifiers::VS_Override;
2071
2072 if (II == Ident_sealed)
2073 return VirtSpecifiers::VS_Sealed;
2074
2075 if (II == Ident_final)
2076 return VirtSpecifiers::VS_Final;
2077
Andrey Bokhanko276055b2016-07-29 10:42:48 +00002078 if (II == Ident_GNU_final)
2079 return VirtSpecifiers::VS_GNU_Final;
2080
Anders Carlsson56104902011-01-17 03:05:47 +00002081 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002082}
2083
Richard Smith89645bc2013-01-02 12:01:23 +00002084/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002085///
2086/// virt-specifier-seq:
2087/// virt-specifier
2088/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00002089void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
Richard Smith3d1a94c2014-08-12 00:22:39 +00002090 bool IsInterface,
2091 SourceLocation FriendLoc) {
Anders Carlsson56104902011-01-17 03:05:47 +00002092 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00002093 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00002094 if (Specifier == VirtSpecifiers::VS_None)
2095 return;
2096
Richard Smith3d1a94c2014-08-12 00:22:39 +00002097 if (FriendLoc.isValid()) {
2098 Diag(Tok.getLocation(), diag::err_friend_decl_spec)
2099 << VirtSpecifiers::getSpecifierName(Specifier)
2100 << FixItHint::CreateRemoval(Tok.getLocation())
2101 << SourceRange(FriendLoc, FriendLoc);
2102 ConsumeToken();
2103 continue;
2104 }
2105
Anders Carlsson56104902011-01-17 03:05:47 +00002106 // C++ [class.mem]p8:
2107 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Craig Topper161e4db2014-05-21 06:02:52 +00002108 const char *PrevSpec = nullptr;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00002109 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00002110 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
2111 << PrevSpec
2112 << FixItHint::CreateRemoval(Tok.getLocation());
2113
David Majnemera5433082013-10-18 00:33:31 +00002114 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
2115 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalldb632ac2012-09-25 07:32:39 +00002116 Diag(Tok.getLocation(), diag::err_override_control_interface)
2117 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemera5433082013-10-18 00:33:31 +00002118 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
2119 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
Andrey Bokhanko276055b2016-07-29 10:42:48 +00002120 } else if (Specifier == VirtSpecifiers::VS_GNU_Final) {
2121 Diag(Tok.getLocation(), diag::ext_warn_gnu_final);
John McCalldb632ac2012-09-25 07:32:39 +00002122 } else {
David Majnemera5433082013-10-18 00:33:31 +00002123 Diag(Tok.getLocation(),
2124 getLangOpts().CPlusPlus11
2125 ? diag::warn_cxx98_compat_override_control_keyword
2126 : diag::ext_override_control_keyword)
2127 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalldb632ac2012-09-25 07:32:39 +00002128 }
Anders Carlsson56104902011-01-17 03:05:47 +00002129 ConsumeToken();
2130 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002131}
2132
Richard Smith89645bc2013-01-02 12:01:23 +00002133/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002134/// 'final' or Microsoft 'sealed' contextual keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00002135bool Parser::isCXX11FinalKeyword() const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002136 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
2137 return Specifier == VirtSpecifiers::VS_Final ||
Andrey Bokhanko276055b2016-07-29 10:42:48 +00002138 Specifier == VirtSpecifiers::VS_GNU_Final ||
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002139 Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002140}
2141
Richard Smith72553fc2014-01-23 23:53:27 +00002142/// \brief Parse a C++ member-declarator up to, but not including, the optional
2143/// brace-or-equal-initializer or pure-specifier.
Nico Weberd89e6f72015-01-16 19:34:13 +00002144bool Parser::ParseCXXMemberDeclaratorBeforeInitializer(
Richard Smith72553fc2014-01-23 23:53:27 +00002145 Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
2146 LateParsedAttrList &LateParsedAttrs) {
2147 // member-declarator:
2148 // declarator pure-specifier[opt]
2149 // declarator brace-or-equal-initializer[opt]
2150 // identifier[opt] ':' constant-expression
Serge Pavlov458ea762014-07-16 05:16:52 +00002151 if (Tok.isNot(tok::colon))
Richard Smith72553fc2014-01-23 23:53:27 +00002152 ParseDeclarator(DeclaratorInfo);
Richard Smith3d1a94c2014-08-12 00:22:39 +00002153 else
2154 DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation());
Richard Smith72553fc2014-01-23 23:53:27 +00002155
2156 if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002157 assert(DeclaratorInfo.isPastIdentifier() &&
2158 "don't know where identifier would go yet?");
Richard Smith72553fc2014-01-23 23:53:27 +00002159 BitfieldSize = ParseConstantExpression();
2160 if (BitfieldSize.isInvalid())
2161 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002162 } else {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002163 ParseOptionalCXX11VirtSpecifierSeq(
2164 VS, getCurrentClass().IsInterface,
2165 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002166 if (!VS.isUnset())
2167 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
2168 }
Richard Smith72553fc2014-01-23 23:53:27 +00002169
2170 // If a simple-asm-expr is present, parse it.
2171 if (Tok.is(tok::kw_asm)) {
2172 SourceLocation Loc;
2173 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
2174 if (AsmLabel.isInvalid())
2175 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2176
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002177 DeclaratorInfo.setAsmLabel(AsmLabel.get());
Richard Smith72553fc2014-01-23 23:53:27 +00002178 DeclaratorInfo.SetRangeEnd(Loc);
2179 }
2180
2181 // If attributes exist after the declarator, but before an '{', parse them.
2182 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Richard Smith4b5a9492014-01-24 22:34:35 +00002183
2184 // For compatibility with code written to older Clang, also accept a
2185 // virt-specifier *after* the GNU attributes.
Aaron Ballman5d153e32014-08-04 17:03:51 +00002186 if (BitfieldSize.isUnset() && VS.isUnset()) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002187 ParseOptionalCXX11VirtSpecifierSeq(
2188 VS, getCurrentClass().IsInterface,
2189 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Aaron Ballman5d153e32014-08-04 17:03:51 +00002190 if (!VS.isUnset()) {
2191 // If we saw any GNU-style attributes that are known to GCC followed by a
2192 // virt-specifier, issue a GCC-compat warning.
2193 const AttributeList *Attr = DeclaratorInfo.getAttributes();
2194 while (Attr) {
2195 if (Attr->isKnownToGCC() && !Attr->isCXX11Attribute())
2196 Diag(Attr->getLoc(), diag::warn_gcc_attribute_location);
2197 Attr = Attr->getNext();
2198 }
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002199 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
Aaron Ballman5d153e32014-08-04 17:03:51 +00002200 }
2201 }
Nico Weberd89e6f72015-01-16 19:34:13 +00002202
2203 // If this has neither a name nor a bit width, something has gone seriously
2204 // wrong. Skip until the semi-colon or }.
2205 if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
2206 // If so, skip until the semi-colon or a }.
2207 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2208 return true;
2209 }
2210 return false;
Richard Smith72553fc2014-01-23 23:53:27 +00002211}
2212
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002213/// \brief Look for declaration specifiers possibly occurring after C++11
2214/// virt-specifier-seq and diagnose them.
2215void Parser::MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(
2216 Declarator &D,
2217 VirtSpecifiers &VS) {
2218 DeclSpec DS(AttrFactory);
2219
2220 // GNU-style and C++11 attributes are not allowed here, but they will be
2221 // handled by the caller. Diagnose everything else.
2222 ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed, false);
2223 D.ExtendWithDeclSpec(DS);
2224
2225 if (D.isFunctionDeclarator()) {
Ehsan Akhgaric07d1e22015-03-25 00:53:33 +00002226 auto &Function = D.getFunctionTypeInfo();
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002227 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2228 auto DeclSpecCheck = [&] (DeclSpec::TQ TypeQual,
2229 const char *FixItName,
2230 SourceLocation SpecLoc,
2231 unsigned* QualifierLoc) {
2232 FixItHint Insertion;
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002233 if (DS.getTypeQualifiers() & TypeQual) {
2234 if (!(Function.TypeQuals & TypeQual)) {
2235 std::string Name(FixItName);
2236 Name += " ";
Malcolm Parsonsf76f6502016-11-02 10:39:27 +00002237 Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name);
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002238 Function.TypeQuals |= TypeQual;
2239 *QualifierLoc = SpecLoc.getRawEncoding();
2240 }
2241 Diag(SpecLoc, diag::err_declspec_after_virtspec)
2242 << FixItName
2243 << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
2244 << FixItHint::CreateRemoval(SpecLoc)
2245 << Insertion;
2246 }
2247 };
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002248 DeclSpecCheck(DeclSpec::TQ_const, "const", DS.getConstSpecLoc(),
2249 &Function.ConstQualifierLoc);
2250 DeclSpecCheck(DeclSpec::TQ_volatile, "volatile", DS.getVolatileSpecLoc(),
2251 &Function.VolatileQualifierLoc);
2252 DeclSpecCheck(DeclSpec::TQ_restrict, "restrict", DS.getRestrictSpecLoc(),
2253 &Function.RestrictQualifierLoc);
2254 }
Ehsan Akhgaric07d1e22015-03-25 00:53:33 +00002255
2256 // Parse ref-qualifiers.
2257 bool RefQualifierIsLValueRef = true;
2258 SourceLocation RefQualifierLoc;
2259 if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) {
2260 const char *Name = (RefQualifierIsLValueRef ? "& " : "&& ");
2261 FixItHint Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name);
2262 Function.RefQualifierIsLValueRef = RefQualifierIsLValueRef;
2263 Function.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
2264
2265 Diag(RefQualifierLoc, diag::err_declspec_after_virtspec)
2266 << (RefQualifierIsLValueRef ? "&" : "&&")
2267 << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
2268 << FixItHint::CreateRemoval(RefQualifierLoc)
2269 << Insertion;
2270 D.SetRangeEnd(RefQualifierLoc);
2271 }
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002272 }
2273}
2274
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002275/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
2276///
2277/// member-declaration:
2278/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
2279/// function-definition ';'[opt]
2280/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
2281/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00002282/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002283/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002284/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002285///
2286/// member-declarator-list:
2287/// member-declarator
2288/// member-declarator-list ',' member-declarator
2289///
2290/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002291/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002292/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00002293/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002294/// identifier[opt] ':' constant-expression
2295///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002296/// virt-specifier-seq:
2297/// virt-specifier
2298/// virt-specifier-seq virt-specifier
2299///
2300/// virt-specifier:
2301/// override
2302/// final
David Majnemera5433082013-10-18 00:33:31 +00002303/// [MS] sealed
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002304///
Sebastian Redl42e92c42009-04-12 17:16:29 +00002305/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002306/// '= 0'
2307///
2308/// constant-initializer:
2309/// '=' constant-expression
2310///
Alexey Bataev05c25d62015-07-31 08:42:25 +00002311Parser::DeclGroupPtrTy
2312Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
2313 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00002314 const ParsedTemplateInfo &TemplateInfo,
2315 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00002316 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002317 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00002318 Diag(Tok, diag::err_at_defs_cxx);
2319 else
2320 Diag(Tok, diag::err_at_in_class);
Richard Smithda35e962013-11-09 04:52:51 +00002321
Douglas Gregor23c84762011-04-14 17:21:19 +00002322 ConsumeToken();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002323 SkipUntil(tok::r_brace, StopAtSemi);
David Blaikie0403cb12016-01-15 23:43:25 +00002324 return nullptr;
Douglas Gregor23c84762011-04-14 17:21:19 +00002325 }
Richard Smithda35e962013-11-09 04:52:51 +00002326
Serge Pavlov458ea762014-07-16 05:16:52 +00002327 // Turn on colon protection early, while parsing declspec, although there is
2328 // nothing to protect there. It prevents from false errors if error recovery
2329 // incorrectly determines where the declspec ends, as in the example:
2330 // struct A { enum class B { C }; };
2331 // const int C = 4;
2332 // struct D { A::B : C; };
2333 ColonProtectionRAIIObject X(*this);
2334
John McCalla0097262009-12-11 02:10:03 +00002335 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00002336 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00002337 if (!TemplateInfo.Kind &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002338 Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw___super)) {
Richard Smith45855df2012-05-09 08:23:23 +00002339 if (TryAnnotateCXXScopeToken())
2340 MalformedTypeSpec = true;
2341
2342 bool isAccessDecl;
2343 if (Tok.isNot(tok::annot_cxxscope))
2344 isAccessDecl = false;
2345 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00002346 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
2347 else
2348 isAccessDecl = NextToken().is(tok::kw_operator);
2349
2350 if (isAccessDecl) {
2351 // Collect the scope specifier token we annotated earlier.
2352 CXXScopeSpec SS;
David Blaikieefdccaa2016-01-15 23:43:34 +00002353 ParseOptionalCXXScopeSpecifier(SS, nullptr,
Douglas Gregordf593fb2011-11-07 17:33:42 +00002354 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00002355
Nico Weberef03e702014-09-10 00:59:37 +00002356 if (SS.isInvalid()) {
2357 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002358 return nullptr;
Nico Weberef03e702014-09-10 00:59:37 +00002359 }
2360
John McCalla0097262009-12-11 02:10:03 +00002361 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00002362 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00002363 UnqualifiedId Name;
David Blaikieefdccaa2016-01-15 23:43:34 +00002364 if (ParseUnqualifiedId(SS, false, true, true, nullptr, TemplateKWLoc,
2365 Name)) {
John McCalla0097262009-12-11 02:10:03 +00002366 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002367 return nullptr;
John McCalla0097262009-12-11 02:10:03 +00002368 }
2369
2370 // TODO: recover from mistakenly-qualified operator declarations.
Alp Toker383d2c42014-01-01 03:08:43 +00002371 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2372 "access declaration")) {
2373 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002374 return nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002375 }
John McCalla0097262009-12-11 02:10:03 +00002376
Alexey Bataev05c25d62015-07-31 08:42:25 +00002377 return DeclGroupPtrTy::make(DeclGroupRef(Actions.ActOnUsingDeclaration(
2378 getCurScope(), AS,
2379 /* HasUsingKeyword */ false, SourceLocation(), SS, Name,
2380 /* AttrList */ nullptr,
2381 /* HasTypenameKeyword */ false, SourceLocation())));
John McCalla0097262009-12-11 02:10:03 +00002382 }
2383 }
2384
Aaron Ballmane7c544d2014-08-04 20:28:35 +00002385 // static_assert-declaration. A templated static_assert declaration is
2386 // diagnosed in Parser::ParseSingleDeclarationAfterTemplate.
2387 if (!TemplateInfo.Kind &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002388 Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert)) {
Chris Lattner49836b42009-04-02 04:16:50 +00002389 SourceLocation DeclEnd;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002390 return DeclGroupPtrTy::make(
2391 DeclGroupRef(ParseStaticAssertDeclaration(DeclEnd)));
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002392 }
Mike Stump11289f42009-09-09 15:08:12 +00002393
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002394 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00002395 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00002396 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00002397 SourceLocation DeclEnd;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002398 return DeclGroupPtrTy::make(
2399 DeclGroupRef(ParseDeclarationStartingWithTemplate(
2400 Declarator::MemberContext, DeclEnd, AS, AccessAttrs)));
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002401 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002402
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002403 // Handle: member-declaration ::= '__extension__' member-declaration
2404 if (Tok.is(tok::kw___extension__)) {
2405 // __extension__ silences extension warnings in the subexpression.
2406 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2407 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002408 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2409 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002410 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002411
John McCall084e83d2011-03-24 11:26:52 +00002412 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002413 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002414 // Optional C++11 attribute-specifier
2415 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002416 // We need to keep these attributes for future diagnostic
2417 // before they are taken over by declaration specifier.
2418 FnAttrs.addAll(attrs.getList());
2419 FnAttrs.Range = attrs.Range;
2420
John McCall53fa7142010-12-24 02:08:15 +00002421 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002422
Douglas Gregorfec52632009-06-20 00:51:54 +00002423 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002424 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002425
Douglas Gregorfec52632009-06-20 00:51:54 +00002426 // Eat 'using'.
2427 SourceLocation UsingLoc = ConsumeToken();
2428
2429 if (Tok.is(tok::kw_namespace)) {
2430 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002431 SkipUntil(tok::semi, StopBeforeMatch);
David Blaikie0403cb12016-01-15 23:43:25 +00002432 return nullptr;
Douglas Gregorfec52632009-06-20 00:51:54 +00002433 }
Alexey Bataev05c25d62015-07-31 08:42:25 +00002434 SourceLocation DeclEnd;
2435 // Otherwise, it must be a using-declaration or an alias-declaration.
2436 return DeclGroupPtrTy::make(DeclGroupRef(ParseUsingDeclaration(
2437 Declarator::MemberContext, TemplateInfo, UsingLoc, DeclEnd, AS)));
Douglas Gregorfec52632009-06-20 00:51:54 +00002438 }
2439
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002440 // Hold late-parsed attributes so we can attach a Decl to them later.
2441 LateParsedAttrList CommonLateParsedAttrs;
2442
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002443 // decl-specifier-seq:
2444 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002445 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002446 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002447 if (MalformedTypeSpec)
2448 DS.SetTypeSpecError();
Richard Smith72553fc2014-01-23 23:53:27 +00002449
Serge Pavlov458ea762014-07-16 05:16:52 +00002450 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2451 &CommonLateParsedAttrs);
2452
2453 // Turn off colon protection that was set for declspec.
2454 X.restore();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002455
Richard Smith404dfb42013-11-19 22:47:36 +00002456 // If we had a free-standing type definition with a missing semicolon, we
2457 // may get this far before the problem becomes obvious.
2458 if (DS.hasTagDefinition() &&
2459 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2460 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2461 &CommonLateParsedAttrs))
David Blaikie0403cb12016-01-15 23:43:25 +00002462 return nullptr;
Richard Smith404dfb42013-11-19 22:47:36 +00002463
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002464 MultiTemplateParamsArg TemplateParams(
Craig Topper161e4db2014-05-21 06:02:52 +00002465 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data()
2466 : nullptr,
John McCall11083da2009-09-16 22:47:08 +00002467 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2468
Alp Toker35d87032013-12-30 23:29:50 +00002469 if (TryConsumeToken(tok::semi)) {
Michael Handdc016d2012-11-28 23:17:40 +00002470 if (DS.isFriendSpecified())
2471 ProhibitAttributes(FnAttrs);
2472
Nico Weber7b837f52016-01-28 19:25:00 +00002473 RecordDecl *AnonRecord = nullptr;
2474 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(
2475 getCurScope(), AS, DS, TemplateParams, false, AnonRecord);
John McCall796c2a52010-07-16 08:13:16 +00002476 DS.complete(TheDecl);
Nico Weber7b837f52016-01-28 19:25:00 +00002477 if (AnonRecord) {
2478 Decl* decls[] = {AnonRecord, TheDecl};
2479 return Actions.BuildDeclaratorGroup(decls, /*TypeMayContainAuto=*/false);
2480 }
2481 return Actions.ConvertDeclToDeclGroup(TheDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002482 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002483
John McCall28a6aea2009-11-04 02:18:39 +00002484 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002485 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002486
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002487 // Hold late-parsed attributes so we can attach a Decl to them later.
2488 LateParsedAttrList LateParsedAttrs;
2489
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002490 SourceLocation EqualLoc;
Richard Smith9ba0fec2015-06-30 01:28:56 +00002491 SourceLocation PureSpecLoc;
2492
Yaron Keren180c1672015-06-30 07:35:19 +00002493 auto TryConsumePureSpecifier = [&] (bool AllowDefinition) {
Richard Smith9ba0fec2015-06-30 01:28:56 +00002494 if (Tok.isNot(tok::equal))
2495 return false;
2496
2497 auto &Zero = NextToken();
2498 SmallString<8> Buffer;
2499 if (Zero.isNot(tok::numeric_constant) || Zero.getLength() != 1 ||
2500 PP.getSpelling(Zero, Buffer) != "0")
2501 return false;
2502
2503 auto &After = GetLookAheadToken(2);
2504 if (!After.isOneOf(tok::semi, tok::comma) &&
2505 !(AllowDefinition &&
2506 After.isOneOf(tok::l_brace, tok::colon, tok::kw_try)))
2507 return false;
2508
2509 EqualLoc = ConsumeToken();
2510 PureSpecLoc = ConsumeToken();
2511 return true;
2512 };
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002513
Richard Smith72553fc2014-01-23 23:53:27 +00002514 SmallVector<Decl *, 8> DeclsInGroup;
2515 ExprResult BitfieldSize;
2516 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002517
Richard Smith72553fc2014-01-23 23:53:27 +00002518 // Parse the first declarator.
Nico Weberd89e6f72015-01-16 19:34:13 +00002519 if (ParseCXXMemberDeclaratorBeforeInitializer(
2520 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) {
Richard Smith72553fc2014-01-23 23:53:27 +00002521 TryConsumeToken(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002522 return nullptr;
Richard Smith72553fc2014-01-23 23:53:27 +00002523 }
John Thompson5bc5cbe2009-11-25 22:58:06 +00002524
Richard Smith72553fc2014-01-23 23:53:27 +00002525 // Check for a member function definition.
Richard Smith4b5a9492014-01-24 22:34:35 +00002526 if (BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002527 // MSVC permits pure specifier on inline functions defined at class scope.
Francois Pichet3abc9b82011-05-11 02:14:46 +00002528 // Hence check for =0 before checking for function definition.
Richard Smith9ba0fec2015-06-30 01:28:56 +00002529 if (getLangOpts().MicrosoftExt && DeclaratorInfo.isDeclarationOfFunction())
2530 TryConsumePureSpecifier(/*AllowDefinition*/ true);
Francois Pichet3abc9b82011-05-11 02:14:46 +00002531
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002532 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002533 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002534 //
2535 // In C++11, a non-function declarator followed by an open brace is a
2536 // braced-init-list for an in-class member initialization, not an
2537 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002538 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002539 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002540 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002541 if (Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002542 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002543 } else if (Tok.is(tok::equal)) {
2544 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002545 if (KW.is(tok::kw_default))
2546 DefinitionKind = FDK_Defaulted;
2547 else if (KW.is(tok::kw_delete))
2548 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002549 }
2550 }
Eli Bendersky41842222015-03-23 23:49:41 +00002551 DeclaratorInfo.setFunctionDefinitionKind(DefinitionKind);
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002552
Michael Handdc016d2012-11-28 23:17:40 +00002553 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2554 // to a friend declaration, that declaration shall be a definition.
2555 if (DeclaratorInfo.isFunctionDeclarator() &&
2556 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2557 // Diagnose attributes that appear before decl specifier:
2558 // [[]] friend int foo();
2559 ProhibitAttributes(FnAttrs);
2560 }
2561
Nico Webera7f137d2015-01-16 19:35:01 +00002562 if (DefinitionKind != FDK_Declaration) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002563 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002564 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002565 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002566 SkipUntil(tok::r_brace);
Michael Handdc016d2012-11-28 23:17:40 +00002567
Douglas Gregor8a4db832011-01-19 16:41:58 +00002568 // Consume the optional ';'
Alp Toker35d87032013-12-30 23:29:50 +00002569 TryConsumeToken(tok::semi);
2570
David Blaikie0403cb12016-01-15 23:43:25 +00002571 return nullptr;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002572 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002573
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002574 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002575 Diag(DeclaratorInfo.getIdentifierLoc(),
2576 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002577
Richard Smith2603b092012-11-15 22:54:20 +00002578 // Recover by treating the 'typedef' as spurious.
2579 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002580 }
2581
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002582 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002583 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Richard Smith9ba0fec2015-06-30 01:28:56 +00002584 VS, PureSpecLoc);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002585
David Majnemer23252a32013-08-01 04:22:55 +00002586 if (FunDecl) {
2587 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2588 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2589 }
2590 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2591 LateParsedAttrs[i]->addDecl(FunDecl);
2592 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002593 }
2594 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002595
2596 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002597 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002598 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002599
Alexey Bataev05c25d62015-07-31 08:42:25 +00002600 return DeclGroupPtrTy::make(DeclGroupRef(FunDecl));
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002601 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002602 }
2603
2604 // member-declarator-list:
2605 // member-declarator
2606 // member-declarator-list ',' member-declarator
2607
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002608 while (1) {
Richard Smith2b013182012-06-10 03:12:00 +00002609 InClassInitStyle HasInClassInit = ICIS_NoInit;
Richard Smith9ba0fec2015-06-30 01:28:56 +00002610 bool HasStaticInitializer = false;
2611 if (Tok.isOneOf(tok::equal, tok::l_brace) && PureSpecLoc.isInvalid()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002612 if (BitfieldSize.get()) {
2613 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002614 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith9ba0fec2015-06-30 01:28:56 +00002615 } else if (DeclaratorInfo.isDeclarationOfFunction()) {
2616 // It's a pure-specifier.
2617 if (!TryConsumePureSpecifier(/*AllowFunctionDefinition*/ false))
2618 // Parse it as an expression so that Sema can diagnose it.
2619 HasStaticInitializer = true;
2620 } else if (DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2621 DeclSpec::SCS_static &&
2622 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2623 DeclSpec::SCS_typedef &&
2624 !DS.isFriendSpecified()) {
2625 // It's a default member initializer.
2626 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002627 } else {
Richard Smith9ba0fec2015-06-30 01:28:56 +00002628 HasStaticInitializer = true;
Richard Smith938f40b2011-06-11 17:19:42 +00002629 }
2630 }
2631
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002632 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002633 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002634 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002635
Craig Topper161e4db2014-05-21 06:02:52 +00002636 NamedDecl *ThisDecl = nullptr;
John McCall07e91c02009-08-06 02:15:43 +00002637 if (DS.isFriendSpecified()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002638 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
Michael Handdc016d2012-11-28 23:17:40 +00002639 // to a friend declaration, that declaration shall be a definition.
2640 //
Richard Smith72553fc2014-01-23 23:53:27 +00002641 // Diagnose attributes that appear in a friend member function declarator:
2642 // friend int foo [[]] ();
Michael Handdc016d2012-11-28 23:17:40 +00002643 SmallVector<SourceRange, 4> Ranges;
2644 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
Richard Smith72553fc2014-01-23 23:53:27 +00002645 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
2646 E = Ranges.end(); I != E; ++I)
2647 Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
Michael Handdc016d2012-11-28 23:17:40 +00002648
Douglas Gregor0be31a22010-07-02 17:43:08 +00002649 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002650 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002651 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002652 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002653 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002654 TemplateParams,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002655 BitfieldSize.get(),
Richard Smith2b013182012-06-10 03:12:00 +00002656 VS, HasInClassInit);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002657
2658 if (VarTemplateDecl *VT =
Craig Topper161e4db2014-05-21 06:02:52 +00002659 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr)
Larisse Voufo39a1e502013-08-06 01:03:05 +00002660 // Re-direct this decl to refer to the templated decl so that we can
2661 // initialize it.
2662 ThisDecl = VT->getTemplatedDecl();
2663
David Majnemer23252a32013-08-01 04:22:55 +00002664 if (ThisDecl && AccessAttrs)
Richard Smithf8a75c32013-08-29 00:47:48 +00002665 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00002666 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002667
Richard Smith9ba0fec2015-06-30 01:28:56 +00002668 // Error recovery might have converted a non-static member into a static
2669 // member.
David Blaikie35506f82013-01-30 01:22:18 +00002670 if (HasInClassInit != ICIS_NoInit &&
Richard Smith9ba0fec2015-06-30 01:28:56 +00002671 DeclaratorInfo.getDeclSpec().getStorageClassSpec() ==
2672 DeclSpec::SCS_static) {
2673 HasInClassInit = ICIS_NoInit;
2674 HasStaticInitializer = true;
2675 }
2676
2677 if (ThisDecl && PureSpecLoc.isValid())
2678 Actions.ActOnPureSpecifier(ThisDecl, PureSpecLoc);
2679
2680 // Handle the initializer.
2681 if (HasInClassInit != ICIS_NoInit) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002682 // The initializer was deferred; parse it and cache the tokens.
David Majnemer23252a32013-08-01 04:22:55 +00002683 Diag(Tok, getLangOpts().CPlusPlus11
2684 ? diag::warn_cxx98_compat_nonstatic_member_init
2685 : diag::ext_nonstatic_member_init);
Richard Smith5d164bc2011-10-15 05:09:34 +00002686
Richard Smith938f40b2011-06-11 17:19:42 +00002687 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002688 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2689 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002690 //
2691 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002692 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002693 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002694 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemer23252a32013-08-01 04:22:55 +00002695
2696 // Avoid later warnings about a class member of incomplete type.
David Blaikiecdd91db2012-02-14 09:00:46 +00002697 if (ThisDecl)
David Blaikiecdd91db2012-02-14 09:00:46 +00002698 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002699 } else
2700 ParseCXXNonStaticMemberInitializer(ThisDecl);
Richard Smith9ba0fec2015-06-30 01:28:56 +00002701 } else if (HasStaticInitializer) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002702 // Normal initializer.
Richard Smith9ba0fec2015-06-30 01:28:56 +00002703 ExprResult Init = ParseCXXMemberInitializer(
2704 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
David Majnemer23252a32013-08-01 04:22:55 +00002705
Douglas Gregor728d00b2011-10-10 14:49:18 +00002706 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002707 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002708 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002709 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smith74aeef52013-04-26 16:15:35 +00002710 DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002711 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor728d00b2011-10-10 14:49:18 +00002712 // No initializer.
Richard Smith74aeef52013-04-26 16:15:35 +00002713 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002714
Douglas Gregor728d00b2011-10-10 14:49:18 +00002715 if (ThisDecl) {
David Majnemer23252a32013-08-01 04:22:55 +00002716 if (!ThisDecl->isInvalidDecl()) {
2717 // Set the Decl for any late parsed attributes
2718 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2719 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2720
2721 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2722 LateParsedAttrs[i]->addDecl(ThisDecl);
2723 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002724 Actions.FinalizeDeclaration(ThisDecl);
2725 DeclsInGroup.push_back(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002726
2727 if (DeclaratorInfo.isFunctionDeclarator() &&
2728 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2729 DeclSpec::SCS_typedef)
2730 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002731 }
David Majnemer23252a32013-08-01 04:22:55 +00002732 LateParsedAttrs.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002733
2734 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002735
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002736 // If we don't have a comma, it is either the end of the list (a ';')
2737 // or an error, bail out.
Alp Toker094e5212014-01-05 03:27:11 +00002738 SourceLocation CommaLoc;
2739 if (!TryConsumeToken(tok::comma, CommaLoc))
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002740 break;
Mike Stump11289f42009-09-09 15:08:12 +00002741
Richard Smithc8a79032012-01-09 22:31:44 +00002742 if (Tok.isAtStartOfLine() &&
2743 !MightBeDeclarator(Declarator::MemberContext)) {
2744 // This comma was followed by a line-break and something which can't be
2745 // the start of a declarator. The comma was probably a typo for a
2746 // semicolon.
2747 Diag(CommaLoc, diag::err_expected_semi_declaration)
2748 << FixItHint::CreateReplacement(CommaLoc, ";");
2749 ExpectSemi = false;
2750 break;
2751 }
Mike Stump11289f42009-09-09 15:08:12 +00002752
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002753 // Parse the next declarator.
2754 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002755 VS.clear();
Nico Weberf56c85b2015-01-17 02:26:40 +00002756 BitfieldSize = ExprResult(/*Invalid=*/false);
Richard Smith9ba0fec2015-06-30 01:28:56 +00002757 EqualLoc = PureSpecLoc = SourceLocation();
Richard Smith8d06f422012-01-12 23:53:29 +00002758 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002759
Richard Smith72553fc2014-01-23 23:53:27 +00002760 // GNU attributes are allowed before the second and subsequent declarator.
John McCall53fa7142010-12-24 02:08:15 +00002761 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002762
Nico Weberd89e6f72015-01-16 19:34:13 +00002763 if (ParseCXXMemberDeclaratorBeforeInitializer(
2764 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs))
2765 break;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002766 }
2767
Richard Smithc8a79032012-01-09 22:31:44 +00002768 if (ExpectSemi &&
2769 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002770 // Skip to end of block or statement.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002771 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002772 // If we stopped at a ';', eat it.
Alp Toker35d87032013-12-30 23:29:50 +00002773 TryConsumeToken(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002774 return nullptr;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002775 }
2776
Alexey Bataev05c25d62015-07-31 08:42:25 +00002777 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002778}
2779
Richard Smith9ba0fec2015-06-30 01:28:56 +00002780/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer.
2781/// Also detect and reject any attempted defaulted/deleted function definition.
2782/// The location of the '=', if any, will be placed in EqualLoc.
Richard Smith938f40b2011-06-11 17:19:42 +00002783///
Richard Smith9ba0fec2015-06-30 01:28:56 +00002784/// This does not check for a pure-specifier; that's handled elsewhere.
Sebastian Redleef474c2012-02-22 10:50:08 +00002785///
Richard Smith938f40b2011-06-11 17:19:42 +00002786/// brace-or-equal-initializer:
2787/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002788/// braced-init-list
2789///
Richard Smith938f40b2011-06-11 17:19:42 +00002790/// initializer-clause:
2791/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002792/// braced-init-list
2793///
Richard Smithda35e962013-11-09 04:52:51 +00002794/// defaulted/deleted function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002795/// '=' 'default'
2796/// '=' 'delete'
2797///
2798/// Prior to C++0x, the assignment-expression in an initializer-clause must
2799/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002800ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002801 SourceLocation &EqualLoc) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002802 assert(Tok.isOneOf(tok::equal, tok::l_brace)
Richard Smith938f40b2011-06-11 17:19:42 +00002803 && "Data member initializer not starting with '=' or '{'");
2804
Douglas Gregor926410d2012-02-21 02:22:07 +00002805 EnterExpressionEvaluationContext Context(Actions,
2806 Sema::PotentiallyEvaluated,
2807 D);
Alp Toker094e5212014-01-05 03:27:11 +00002808 if (TryConsumeToken(tok::equal, EqualLoc)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002809 if (Tok.is(tok::kw_delete)) {
2810 // In principle, an initializer of '= delete p;' is legal, but it will
2811 // never type-check. It's better to diagnose it as an ill-formed expression
2812 // than as an ill-formed deleted non-function member.
2813 // An initializer of '= delete p, foo' will never be parsed, because
2814 // a top-level comma always ends the initializer expression.
2815 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002816 if (IsFunction || Next.isOneOf(tok::semi, tok::comma, tok::eof)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002817 if (IsFunction)
2818 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2819 << 1 /* delete */;
2820 else
2821 Diag(ConsumeToken(), diag::err_deleted_non_function);
Richard Smithedcb26e2014-06-11 00:49:52 +00002822 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002823 }
2824 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002825 if (IsFunction)
2826 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2827 << 0 /* default */;
2828 else
2829 Diag(ConsumeToken(), diag::err_default_special_members);
Richard Smithedcb26e2014-06-11 00:49:52 +00002830 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002831 }
David Majnemer87ff66c2014-12-13 11:34:16 +00002832 }
2833 if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) {
2834 Diag(Tok, diag::err_ms_property_initializer) << PD;
2835 return ExprError();
Sebastian Redleef474c2012-02-22 10:50:08 +00002836 }
2837 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002838}
2839
Richard Smith65ebb4a2015-03-26 04:09:53 +00002840void Parser::SkipCXXMemberSpecification(SourceLocation RecordLoc,
2841 SourceLocation AttrFixitLoc,
2842 unsigned TagType, Decl *TagDecl) {
2843 // Skip the optional 'final' keyword.
2844 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
2845 assert(isCXX11FinalKeyword() && "not a class definition");
2846 ConsumeToken();
2847
2848 // Diagnose any C++11 attributes after 'final' keyword.
2849 // We deliberately discard these attributes.
2850 ParsedAttributesWithRange Attrs(AttrFactory);
2851 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
2852
2853 // This can only happen if we had malformed misplaced attributes;
2854 // we only get called if there is a colon or left-brace after the
2855 // attributes.
2856 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_brace))
2857 return;
2858 }
2859
2860 // Skip the base clauses. This requires actually parsing them, because
2861 // otherwise we can't be sure where they end (a left brace may appear
2862 // within a template argument).
2863 if (Tok.is(tok::colon)) {
2864 // Enter the scope of the class so that we can correctly parse its bases.
2865 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
2866 ParsingClassDefinition ParsingDef(*this, TagDecl, /*NonNestedClass*/ true,
2867 TagType == DeclSpec::TST_interface);
Richard Smith0f192e82015-06-11 22:48:25 +00002868 auto OldContext =
2869 Actions.ActOnTagStartSkippedDefinition(getCurScope(), TagDecl);
Richard Smith65ebb4a2015-03-26 04:09:53 +00002870
2871 // Parse the bases but don't attach them to the class.
2872 ParseBaseClause(nullptr);
2873
Richard Smith0f192e82015-06-11 22:48:25 +00002874 Actions.ActOnTagFinishSkippedDefinition(OldContext);
Richard Smith65ebb4a2015-03-26 04:09:53 +00002875
2876 if (!Tok.is(tok::l_brace)) {
2877 Diag(PP.getLocForEndOfToken(PrevTokLocation),
2878 diag::err_expected_lbrace_after_base_specifiers);
2879 return;
2880 }
2881 }
2882
2883 // Skip the body.
2884 assert(Tok.is(tok::l_brace));
2885 BalancedDelimiterTracker T(*this, tok::l_brace);
2886 T.consumeOpen();
2887 T.skipToEnd();
Richard Smith04c6c1f2015-07-01 18:56:50 +00002888
2889 // Parse and discard any trailing attributes.
2890 ParsedAttributes Attrs(AttrFactory);
2891 if (Tok.is(tok::kw___attribute))
2892 MaybeParseGNUAttributes(Attrs);
Richard Smith65ebb4a2015-03-26 04:09:53 +00002893}
2894
Alexey Bataev05c25d62015-07-31 08:42:25 +00002895Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclarationWithPragmas(
2896 AccessSpecifier &AS, ParsedAttributesWithRange &AccessAttrs,
2897 DeclSpec::TST TagType, Decl *TagDecl) {
2898 if (getLangOpts().MicrosoftExt &&
2899 Tok.isOneOf(tok::kw___if_exists, tok::kw___if_not_exists)) {
2900 ParseMicrosoftIfExistsClassDeclaration(TagType, AS);
David Blaikie0403cb12016-01-15 23:43:25 +00002901 return nullptr;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002902 }
2903
2904 // Check for extraneous top-level semicolon.
2905 if (Tok.is(tok::semi)) {
2906 ConsumeExtraSemi(InsideStruct, TagType);
David Blaikie0403cb12016-01-15 23:43:25 +00002907 return nullptr;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002908 }
2909
2910 if (Tok.is(tok::annot_pragma_vis)) {
2911 HandlePragmaVisibility();
David Blaikie0403cb12016-01-15 23:43:25 +00002912 return nullptr;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002913 }
2914
2915 if (Tok.is(tok::annot_pragma_pack)) {
2916 HandlePragmaPack();
David Blaikie0403cb12016-01-15 23:43:25 +00002917 return nullptr;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002918 }
2919
2920 if (Tok.is(tok::annot_pragma_align)) {
2921 HandlePragmaAlign();
David Blaikie0403cb12016-01-15 23:43:25 +00002922 return nullptr;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002923 }
2924
2925 if (Tok.is(tok::annot_pragma_ms_pointers_to_members)) {
2926 HandlePragmaMSPointersToMembers();
David Blaikie0403cb12016-01-15 23:43:25 +00002927 return nullptr;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002928 }
2929
2930 if (Tok.is(tok::annot_pragma_ms_pragma)) {
2931 HandlePragmaMSPragma();
David Blaikie0403cb12016-01-15 23:43:25 +00002932 return nullptr;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002933 }
2934
Alexey Bataev3d42f342015-11-20 07:02:57 +00002935 if (Tok.is(tok::annot_pragma_ms_vtordisp)) {
2936 HandlePragmaMSVtorDisp();
David Blaikie0403cb12016-01-15 23:43:25 +00002937 return nullptr;
Alexey Bataev3d42f342015-11-20 07:02:57 +00002938 }
2939
Alexey Bataev05c25d62015-07-31 08:42:25 +00002940 // If we see a namespace here, a close brace was missing somewhere.
2941 if (Tok.is(tok::kw_namespace)) {
2942 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
David Blaikie0403cb12016-01-15 23:43:25 +00002943 return nullptr;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002944 }
2945
2946 AccessSpecifier NewAS = getAccessSpecifierIfPresent();
2947 if (NewAS != AS_none) {
2948 // Current token is a C++ access specifier.
2949 AS = NewAS;
2950 SourceLocation ASLoc = Tok.getLocation();
2951 unsigned TokLength = Tok.getLength();
2952 ConsumeToken();
2953 AccessAttrs.clear();
2954 MaybeParseGNUAttributes(AccessAttrs);
2955
2956 SourceLocation EndLoc;
2957 if (TryConsumeToken(tok::colon, EndLoc)) {
2958 } else if (TryConsumeToken(tok::semi, EndLoc)) {
2959 Diag(EndLoc, diag::err_expected)
2960 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
2961 } else {
2962 EndLoc = ASLoc.getLocWithOffset(TokLength);
2963 Diag(EndLoc, diag::err_expected)
2964 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
2965 }
2966
2967 // The Microsoft extension __interface does not permit non-public
2968 // access specifiers.
2969 if (TagType == DeclSpec::TST_interface && AS != AS_public) {
2970 Diag(ASLoc, diag::err_access_specifier_interface) << (AS == AS_protected);
2971 }
2972
2973 if (Actions.ActOnAccessSpecifier(NewAS, ASLoc, EndLoc,
2974 AccessAttrs.getList())) {
2975 // found another attribute than only annotations
2976 AccessAttrs.clear();
2977 }
2978
David Blaikie0403cb12016-01-15 23:43:25 +00002979 return nullptr;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002980 }
2981
2982 if (Tok.is(tok::annot_pragma_openmp))
Alexey Bataev587e1de2016-03-30 10:43:55 +00002983 return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, AccessAttrs, TagType,
2984 TagDecl);
Alexey Bataev05c25d62015-07-31 08:42:25 +00002985
2986 // Parse all the comma separated declarators.
2987 return ParseCXXClassMemberDeclaration(AS, AccessAttrs.getList());
2988}
2989
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002990/// ParseCXXMemberSpecification - Parse the class definition.
2991///
2992/// member-specification:
2993/// member-declaration member-specification[opt]
2994/// access-specifier ':' member-specification[opt]
2995///
Joao Matose9a3ed42012-08-31 22:18:20 +00002996void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002997 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00002998 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002999 unsigned TagType, Decl *TagDecl) {
3000 assert((TagType == DeclSpec::TST_struct ||
3001 TagType == DeclSpec::TST_interface ||
3002 TagType == DeclSpec::TST_union ||
3003 TagType == DeclSpec::TST_class) && "Invalid TagType!");
3004
John McCallfaf5fb42010-08-26 23:41:50 +00003005 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3006 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00003007
Douglas Gregoredf8f392010-01-16 20:52:59 +00003008 // Determine whether this is a non-nested class. Note that local
3009 // classes are *not* considered to be nested classes.
3010 bool NonNestedClass = true;
3011 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003012 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003013 if (S->isClassScope()) {
3014 // We're inside a class scope, so this is a nested class.
3015 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00003016
3017 // The Microsoft extension __interface does not permit nested classes.
3018 if (getCurrentClass().IsInterface) {
3019 Diag(RecordLoc, diag::err_invalid_member_in_interface)
3020 << /*ErrorType=*/6
3021 << (isa<NamedDecl>(TagDecl)
3022 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
David Blaikieabe1a392014-04-02 05:58:29 +00003023 : "(anonymous)");
John McCalldb632ac2012-09-25 07:32:39 +00003024 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00003025 break;
3026 }
3027
Serge Pavlovd9c0bcf2015-07-14 10:02:10 +00003028 if ((S->getFlags() & Scope::FnScope))
3029 // If we're in a function or function template then this is a local
3030 // class rather than a nested class.
3031 break;
Douglas Gregoredf8f392010-01-16 20:52:59 +00003032 }
3033 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003034
3035 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00003036 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003037
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003038 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00003039 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
3040 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003041
Douglas Gregorcd72ba92009-02-06 22:42:48 +00003042 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003043 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00003044
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00003045 SourceLocation FinalLoc;
David Majnemera5433082013-10-18 00:33:31 +00003046 bool IsFinalSpelledSealed = false;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00003047
3048 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003049 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemera5433082013-10-18 00:33:31 +00003050 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
3051 assert((Specifier == VirtSpecifiers::VS_Final ||
Andrey Bokhanko276055b2016-07-29 10:42:48 +00003052 Specifier == VirtSpecifiers::VS_GNU_Final ||
David Majnemera5433082013-10-18 00:33:31 +00003053 Specifier == VirtSpecifiers::VS_Sealed) &&
3054 "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00003055 FinalLoc = ConsumeToken();
David Majnemera5433082013-10-18 00:33:31 +00003056 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00003057
David Majnemera5433082013-10-18 00:33:31 +00003058 if (TagType == DeclSpec::TST_interface)
John McCalldb632ac2012-09-25 07:32:39 +00003059 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemera5433082013-10-18 00:33:31 +00003060 << VirtSpecifiers::getSpecifierName(Specifier);
3061 else if (Specifier == VirtSpecifiers::VS_Final)
3062 Diag(FinalLoc, getLangOpts().CPlusPlus11
3063 ? diag::warn_cxx98_compat_override_control_keyword
3064 : diag::ext_override_control_keyword)
3065 << VirtSpecifiers::getSpecifierName(Specifier);
3066 else if (Specifier == VirtSpecifiers::VS_Sealed)
3067 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Andrey Bokhanko276055b2016-07-29 10:42:48 +00003068 else if (Specifier == VirtSpecifiers::VS_GNU_Final)
3069 Diag(FinalLoc, diag::ext_warn_gnu_final);
Michael Han9407e502012-11-26 22:54:45 +00003070
Michael Han309af292013-01-07 16:57:11 +00003071 // Parse any C++11 attributes after 'final' keyword.
3072 // These attributes are not allowed to appear here,
3073 // and the only possible place for them to appertain
3074 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00003075 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Nico Weber4b4be842014-12-29 06:56:50 +00003076
3077 // ParseClassSpecifier() does only a superficial check for attributes before
3078 // deciding to call this method. For example, for
3079 // `class C final alignas ([l) {` it will decide that this looks like a
3080 // misplaced attribute since it sees `alignas '(' ')'`. But the actual
3081 // attribute parsing code will try to parse the '[' as a constexpr lambda
3082 // and consume enough tokens that the alignas parsing code will eat the
3083 // opening '{'. So bail out if the next token isn't one we expect.
Nico Weber36de3a22014-12-29 21:56:22 +00003084 if (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)) {
3085 if (TagDecl)
3086 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
Nico Weber4b4be842014-12-29 06:56:50 +00003087 return;
Nico Weber36de3a22014-12-29 21:56:22 +00003088 }
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00003089 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00003090
John McCall2d814c32009-12-19 21:48:58 +00003091 if (Tok.is(tok::colon)) {
3092 ParseBaseClause(TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00003093 if (!Tok.is(tok::l_brace)) {
Ismail Pazarbasi129c44c2014-09-25 21:13:02 +00003094 bool SuggestFixIt = false;
3095 SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation);
3096 if (Tok.isAtStartOfLine()) {
3097 switch (Tok.getKind()) {
3098 case tok::kw_private:
3099 case tok::kw_protected:
3100 case tok::kw_public:
3101 SuggestFixIt = NextToken().getKind() == tok::colon;
3102 break;
3103 case tok::kw_static_assert:
3104 case tok::r_brace:
3105 case tok::kw_using:
3106 // base-clause can have simple-template-id; 'template' can't be there
3107 case tok::kw_template:
3108 SuggestFixIt = true;
3109 break;
3110 case tok::identifier:
3111 SuggestFixIt = isConstructorDeclarator(true);
3112 break;
3113 default:
3114 SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
3115 break;
3116 }
3117 }
3118 DiagnosticBuilder LBraceDiag =
3119 Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers);
3120 if (SuggestFixIt) {
3121 LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {");
3122 // Try recovering from missing { after base-clause.
3123 PP.EnterToken(Tok);
3124 Tok.setKind(tok::l_brace);
3125 } else {
3126 if (TagDecl)
3127 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
3128 return;
3129 }
John McCall2d814c32009-12-19 21:48:58 +00003130 }
3131 }
3132
3133 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003134 BalancedDelimiterTracker T(*this, tok::l_brace);
3135 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00003136
John McCall08bede42010-05-28 08:11:17 +00003137 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00003138 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemera5433082013-10-18 00:33:31 +00003139 IsFinalSpelledSealed,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003140 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00003141
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003142 // C++ 11p3: Members of a class defined with the keyword class are private
3143 // by default. Members of a class defined with the keywords struct or union
3144 // are public by default.
3145 AccessSpecifier CurAS;
3146 if (TagType == DeclSpec::TST_class)
3147 CurAS = AS_private;
3148 else
3149 CurAS = AS_public;
Alexey Bataev05c25d62015-07-31 08:42:25 +00003150 ParsedAttributesWithRange AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003151
Douglas Gregor9377c822010-06-21 22:31:09 +00003152 if (TagDecl) {
3153 // While we still have something to read, read the member-declarations.
Richard Smith752ada82015-11-17 23:32:01 +00003154 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
3155 Tok.isNot(tok::eof)) {
Douglas Gregor9377c822010-06-21 22:31:09 +00003156 // Each iteration of this loop reads one member-declaration.
Alexey Bataev05c25d62015-07-31 08:42:25 +00003157 ParseCXXClassMemberDeclarationWithPragmas(
3158 CurAS, AccessAttrs, static_cast<DeclSpec::TST>(TagType), TagDecl);
Serge Pavlovc4e04a22015-09-19 05:32:57 +00003159 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003160 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00003161 } else {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003162 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003163 }
Mike Stump11289f42009-09-09 15:08:12 +00003164
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003165 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003166 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003167 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003168
John McCall08bede42010-05-28 08:11:17 +00003169 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003170 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003171 T.getOpenLocation(),
3172 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003173 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003174
Douglas Gregor433e0532012-04-16 18:27:27 +00003175 // C++11 [class.mem]p2:
3176 // Within the class member-specification, the class is regarded as complete
Richard Smith0b3a4622014-11-13 20:01:57 +00003177 // within function bodies, default arguments, exception-specifications, and
Douglas Gregor433e0532012-04-16 18:27:27 +00003178 // brace-or-equal-initializers for non-static data members (including such
3179 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00003180 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003181 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00003182 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00003183 // declarations and the lexed inline method definitions, along with any
3184 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00003185 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00003186 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003187 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00003188
3189 // We've finished with all pending member declarations.
3190 Actions.ActOnFinishCXXMemberDecls();
3191
Richard Smith938f40b2011-06-11 17:19:42 +00003192 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003193 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00003194 PrevTokLocation = SavedPrevTokLocation;
Reid Klecknerbba3cb92015-03-17 19:00:50 +00003195
3196 // We've finished parsing everything, including default argument
3197 // initializers.
Hans Wennborg99000c22015-08-15 01:18:16 +00003198 Actions.ActOnFinishCXXNonNestedClass(TagDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003199 }
3200
John McCall08bede42010-05-28 08:11:17 +00003201 if (TagDecl)
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00003202 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange());
John McCall2ff380a2010-03-17 00:38:33 +00003203
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003204 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003205 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00003206 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003207}
Douglas Gregore8381c02008-11-05 04:29:56 +00003208
Richard Smith2ac43ad2013-11-15 23:00:02 +00003209void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithda35e962013-11-09 04:52:51 +00003210 assert(Tok.is(tok::kw_namespace));
3211
3212 // FIXME: Suggest where the close brace should have gone by looking
3213 // at indentation changes within the definition body.
Richard Smith2ac43ad2013-11-15 23:00:02 +00003214 Diag(D->getLocation(),
3215 diag::err_missing_end_of_definition) << D;
Richard Smithda35e962013-11-09 04:52:51 +00003216 Diag(Tok.getLocation(),
Richard Smith2ac43ad2013-11-15 23:00:02 +00003217 diag::note_missing_end_of_definition_before) << D;
Richard Smithda35e962013-11-09 04:52:51 +00003218
3219 // Push '};' onto the token stream to recover.
3220 PP.EnterToken(Tok);
3221
3222 Tok.startToken();
3223 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
3224 Tok.setKind(tok::semi);
3225 PP.EnterToken(Tok);
3226
3227 Tok.setKind(tok::r_brace);
3228}
3229
Douglas Gregore8381c02008-11-05 04:29:56 +00003230/// ParseConstructorInitializer - Parse a C++ constructor initializer,
3231/// which explicitly initializes the members or base classes of a
3232/// class (C++ [class.base.init]). For example, the three initializers
3233/// after the ':' in the Derived constructor below:
3234///
3235/// @code
3236/// class Base { };
3237/// class Derived : Base {
3238/// int x;
3239/// float f;
3240/// public:
3241/// Derived(float f) : Base(), x(17), f(f) { }
3242/// };
3243/// @endcode
3244///
Mike Stump11289f42009-09-09 15:08:12 +00003245/// [C++] ctor-initializer:
3246/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00003247///
Mike Stump11289f42009-09-09 15:08:12 +00003248/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00003249/// mem-initializer ...[opt]
3250/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00003251void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Nico Weber3b00fdc2015-03-07 19:52:39 +00003252 assert(Tok.is(tok::colon) &&
3253 "Constructor initializer always starts with ':'");
Douglas Gregore8381c02008-11-05 04:29:56 +00003254
Nico Weber3b00fdc2015-03-07 19:52:39 +00003255 // Poison the SEH identifiers so they are flagged as illegal in constructor
3256 // initializers.
John Wiegley1c0675e2011-04-28 01:08:34 +00003257 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00003258 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003259
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003260 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00003261 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003262
Douglas Gregore8381c02008-11-05 04:29:56 +00003263 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00003264 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00003265 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
3266 MemInitializers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003267 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00003268 }
Alexey Bataev79de17d2016-01-20 05:25:51 +00003269
3270 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
3271 if (!MemInit.isInvalid())
3272 MemInitializers.push_back(MemInit.get());
3273 else
3274 AnyErrors = true;
3275
Douglas Gregore8381c02008-11-05 04:29:56 +00003276 if (Tok.is(tok::comma))
3277 ConsumeToken();
3278 else if (Tok.is(tok::l_brace))
3279 break;
Alexey Bataev79de17d2016-01-20 05:25:51 +00003280 // If the previous initializer was valid and the next token looks like a
3281 // base or member initializer, assume that we're just missing a comma.
3282 else if (!MemInit.isInvalid() &&
3283 Tok.isOneOf(tok::identifier, tok::coloncolon)) {
Douglas Gregorce66d022010-09-07 14:51:08 +00003284 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3285 Diag(Loc, diag::err_ctor_init_missing_comma)
3286 << FixItHint::CreateInsertion(Loc, ", ");
3287 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00003288 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataev79de17d2016-01-20 05:25:51 +00003289 if (!MemInit.isInvalid())
3290 Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
3291 << tok::comma;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003292 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregore8381c02008-11-05 04:29:56 +00003293 break;
3294 }
3295 } while (true);
3296
David Blaikie3fc2f912013-01-17 05:26:25 +00003297 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00003298 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00003299}
3300
3301/// ParseMemInitializer - Parse a C++ member initializer, which is
3302/// part of a constructor initializer that explicitly initializes one
3303/// member or base class (C++ [class.base.init]). See
3304/// ParseConstructorInitializer for an example.
3305///
3306/// [C++] mem-initializer:
3307/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00003308/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00003309///
Douglas Gregore8381c02008-11-05 04:29:56 +00003310/// [C++] mem-initializer-id:
3311/// '::'[opt] nested-name-specifier[opt] class-name
3312/// identifier
Craig Topper9ad7e262014-10-31 06:57:07 +00003313MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00003314 // parse '::'[opt] nested-name-specifier[opt]
3315 CXXScopeSpec SS;
David Blaikieefdccaa2016-01-15 23:43:34 +00003316 ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00003317 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003318 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00003319 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00003320 if (TemplateId->Kind == TNK_Type_template ||
3321 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00003322 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003323 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00003324 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003325 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003326 }
David Blaikie186a8892012-01-24 06:03:59 +00003327 // Uses of decltype will already have been converted to annot_decltype by
3328 // ParseOptionalCXXScopeSpecifier at this point.
3329 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
3330 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00003331 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00003332 return true;
3333 }
Mike Stump11289f42009-09-09 15:08:12 +00003334
Craig Topper161e4db2014-05-21 06:02:52 +00003335 IdentifierInfo *II = nullptr;
David Blaikie186a8892012-01-24 06:03:59 +00003336 DeclSpec DS(AttrFactory);
3337 SourceLocation IdLoc = Tok.getLocation();
3338 if (Tok.is(tok::annot_decltype)) {
3339 // Get the decltype expression, if there is one.
3340 ParseDecltypeSpecifier(DS);
3341 } else {
3342 if (Tok.is(tok::identifier))
3343 // Get the identifier. This may be a member name or a class name,
3344 // but we'll let the semantic analysis determine which it is.
3345 II = Tok.getIdentifierInfo();
3346 ConsumeToken();
3347 }
3348
Douglas Gregore8381c02008-11-05 04:29:56 +00003349
3350 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003351 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003352 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3353
Sebastian Redla74948d2011-09-24 17:48:25 +00003354 ExprResult InitList = ParseBraceInitializer();
3355 if (InitList.isInvalid())
3356 return true;
3357
3358 SourceLocation EllipsisLoc;
Alp Toker094e5212014-01-05 03:27:11 +00003359 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redla74948d2011-09-24 17:48:25 +00003360
3361 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00003362 TemplateTypeTy, DS, IdLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003363 InitList.get(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00003364 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003365 BalancedDelimiterTracker T(*this, tok::l_paren);
3366 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00003367
Sebastian Redl3da34892011-06-05 12:23:16 +00003368 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00003369 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00003370 CommaLocsTy CommaLocs;
3371 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003372 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00003373 return true;
3374 }
3375
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003376 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00003377
3378 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00003379 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00003380
3381 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00003382 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00003383 T.getOpenLocation(), ArgExprs,
3384 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00003385 }
3386
Alp Tokerec543272013-12-24 09:48:30 +00003387 if (getLangOpts().CPlusPlus11)
3388 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
3389 else
3390 return Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregore8381c02008-11-05 04:29:56 +00003391}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003392
Sebastian Redl965b0e32011-03-05 14:45:16 +00003393/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003394///
Douglas Gregor356513d2008-12-01 18:00:20 +00003395/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00003396/// dynamic-exception-specification
3397/// noexcept-specification
3398///
3399/// noexcept-specification:
3400/// 'noexcept'
3401/// 'noexcept' '(' constant-expression ')'
3402ExceptionSpecificationType
Richard Smith0b3a4622014-11-13 20:01:57 +00003403Parser::tryParseExceptionSpecification(bool Delayed,
Douglas Gregor433e0532012-04-16 18:27:27 +00003404 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003405 SmallVectorImpl<ParsedType> &DynamicExceptions,
3406 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith0b3a4622014-11-13 20:01:57 +00003407 ExprResult &NoexceptExpr,
3408 CachedTokens *&ExceptionSpecTokens) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003409 ExceptionSpecificationType Result = EST_None;
Hans Wennborgdcfba332015-10-06 23:40:43 +00003410 ExceptionSpecTokens = nullptr;
Richard Smith0b3a4622014-11-13 20:01:57 +00003411
3412 // Handle delayed parsing of exception-specifications.
3413 if (Delayed) {
3414 if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept))
3415 return EST_None;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003416
Richard Smith0b3a4622014-11-13 20:01:57 +00003417 // Consume and cache the starting token.
3418 bool IsNoexcept = Tok.is(tok::kw_noexcept);
3419 Token StartTok = Tok;
3420 SpecificationRange = SourceRange(ConsumeToken());
3421
3422 // Check for a '('.
3423 if (!Tok.is(tok::l_paren)) {
3424 // If this is a bare 'noexcept', we're done.
3425 if (IsNoexcept) {
3426 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
Hans Wennborgdcfba332015-10-06 23:40:43 +00003427 NoexceptExpr = nullptr;
Richard Smith0b3a4622014-11-13 20:01:57 +00003428 return EST_BasicNoexcept;
3429 }
3430
3431 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3432 return EST_DynamicNone;
3433 }
3434
3435 // Cache the tokens for the exception-specification.
3436 ExceptionSpecTokens = new CachedTokens;
3437 ExceptionSpecTokens->push_back(StartTok); // 'throw' or 'noexcept'
3438 ExceptionSpecTokens->push_back(Tok); // '('
3439 SpecificationRange.setEnd(ConsumeParen()); // '('
Richard Smithb1c217e2015-01-13 02:24:58 +00003440
3441 ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens,
3442 /*StopAtSemi=*/true,
3443 /*ConsumeFinalToken=*/true);
Aaron Ballman580ccaf2016-01-12 21:04:22 +00003444 SpecificationRange.setEnd(ExceptionSpecTokens->back().getLocation());
3445
Richard Smith0b3a4622014-11-13 20:01:57 +00003446 return EST_Unparsed;
3447 }
3448
Sebastian Redl965b0e32011-03-05 14:45:16 +00003449 // See if there's a dynamic specification.
3450 if (Tok.is(tok::kw_throw)) {
3451 Result = ParseDynamicExceptionSpecification(SpecificationRange,
3452 DynamicExceptions,
3453 DynamicExceptionRanges);
3454 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
3455 "Produced different number of exception types and ranges.");
3456 }
3457
3458 // If there's no noexcept specification, we're done.
3459 if (Tok.isNot(tok::kw_noexcept))
3460 return Result;
3461
Richard Smithb15c11c2011-10-17 23:06:20 +00003462 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3463
Sebastian Redl965b0e32011-03-05 14:45:16 +00003464 // If we already had a dynamic specification, parse the noexcept for,
3465 // recovery, but emit a diagnostic and don't store the results.
3466 SourceRange NoexceptRange;
3467 ExceptionSpecificationType NoexceptType = EST_None;
3468
3469 SourceLocation KeywordLoc = ConsumeToken();
3470 if (Tok.is(tok::l_paren)) {
3471 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003472 BalancedDelimiterTracker T(*this, tok::l_paren);
3473 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003474 NoexceptType = EST_ComputedNoexcept;
3475 NoexceptExpr = ParseConstantExpression();
Serge Pavlov3739f5e72015-06-29 17:50:19 +00003476 T.consumeClose();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003477 // The argument must be contextually convertible to bool. We use
Richard Smith03a4aa32016-06-23 19:02:52 +00003478 // CheckBooleanCondition for this purpose.
3479 // FIXME: Add a proper Sema entry point for this.
Serge Pavlov3739f5e72015-06-29 17:50:19 +00003480 if (!NoexceptExpr.isInvalid()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00003481 NoexceptExpr =
3482 Actions.CheckBooleanCondition(KeywordLoc, NoexceptExpr.get());
Serge Pavlov3739f5e72015-06-29 17:50:19 +00003483 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
3484 } else {
3485 NoexceptType = EST_None;
3486 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003487 } else {
3488 // There is no argument.
3489 NoexceptType = EST_BasicNoexcept;
3490 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
3491 }
3492
3493 if (Result == EST_None) {
3494 SpecificationRange = NoexceptRange;
3495 Result = NoexceptType;
3496
3497 // If there's a dynamic specification after a noexcept specification,
3498 // parse that and ignore the results.
3499 if (Tok.is(tok::kw_throw)) {
3500 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3501 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
3502 DynamicExceptionRanges);
3503 }
3504 } else {
3505 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3506 }
3507
3508 return Result;
3509}
3510
Richard Smith8ca78a12013-06-13 02:02:51 +00003511static void diagnoseDynamicExceptionSpecification(
Craig Toppere335f252015-10-04 04:53:55 +00003512 Parser &P, SourceRange Range, bool IsNoexcept) {
Richard Smith8ca78a12013-06-13 02:02:51 +00003513 if (P.getLangOpts().CPlusPlus11) {
3514 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
3515 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
3516 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
3517 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
3518 }
3519}
3520
Sebastian Redl965b0e32011-03-05 14:45:16 +00003521/// ParseDynamicExceptionSpecification - Parse a C++
3522/// dynamic-exception-specification (C++ [except.spec]).
3523///
3524/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00003525/// 'throw' '(' type-id-list [opt] ')'
3526/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00003527///
Douglas Gregor356513d2008-12-01 18:00:20 +00003528/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00003529/// type-id ... [opt]
3530/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003531///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003532ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
3533 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003534 SmallVectorImpl<ParsedType> &Exceptions,
3535 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003536 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00003537
Sebastian Redl965b0e32011-03-05 14:45:16 +00003538 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003539 BalancedDelimiterTracker T(*this, tok::l_paren);
3540 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003541 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3542 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003543 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003544 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003545
Douglas Gregor356513d2008-12-01 18:00:20 +00003546 // Parse throw(...), a Microsoft extension that means "this function
3547 // can throw anything".
3548 if (Tok.is(tok::ellipsis)) {
3549 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003550 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00003551 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003552 T.consumeClose();
3553 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003554 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003555 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00003556 }
3557
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003558 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00003559 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003560 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00003561 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00003562
Douglas Gregor830837d2010-12-20 23:57:46 +00003563 if (Tok.is(tok::ellipsis)) {
3564 // C++0x [temp.variadic]p5:
3565 // - In a dynamic-exception-specification (15.4); the pattern is a
3566 // type-id.
3567 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003568 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00003569 if (!Res.isInvalid())
3570 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3571 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003572
Sebastian Redld6434562009-05-29 18:02:33 +00003573 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003574 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00003575 Ranges.push_back(Range);
3576 }
Alp Toker97650562014-01-10 11:19:30 +00003577
3578 if (!TryConsumeToken(tok::comma))
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003579 break;
3580 }
3581
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003582 T.consumeClose();
3583 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003584 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3585 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003586 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003587}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003588
Douglas Gregor7fb25412010-10-01 18:44:50 +00003589/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3590/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003591TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00003592 assert(Tok.is(tok::arrow) && "expected arrow");
3593
3594 ConsumeToken();
3595
Richard Smithbfdb1082012-03-12 08:56:40 +00003596 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003597}
3598
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003599/// \brief We have just started parsing the definition of a new class,
3600/// so push that class onto our stack of classes that is currently
3601/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003602Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003603Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3604 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003605 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003606 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003607 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003608 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003609}
3610
3611/// \brief Deallocate the given parsed class and all of its nested
3612/// classes.
3613void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003614 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3615 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003616 delete Class;
3617}
3618
3619/// \brief Pop the top class of the stack of classes that are
3620/// currently being parsed.
3621///
3622/// This routine should be called when we have finished parsing the
3623/// definition of a class, but have not yet popped the Scope
3624/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003625void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003626 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003627
John McCallc1465822011-02-14 07:13:47 +00003628 Actions.PopParsingClass(state);
3629
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003630 ParsingClass *Victim = ClassStack.top();
3631 ClassStack.pop();
3632 if (Victim->TopLevelClass) {
3633 // Deallocate all of the nested classes of this class,
3634 // recursively: we don't need to keep any of this information.
3635 DeallocateParsedClasses(Victim);
3636 return;
Mike Stump11289f42009-09-09 15:08:12 +00003637 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003638 assert(!ClassStack.empty() && "Missing top-level class?");
3639
Douglas Gregorefc46952010-10-12 16:25:54 +00003640 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003641 // The victim is a nested class, but we will not need to perform
3642 // any processing after the definition of this class since it has
3643 // no members whose handling was delayed. Therefore, we can just
3644 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003645 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003646 return;
3647 }
3648
3649 // This nested class has some members that will need to be processed
3650 // after the top-level class is completely defined. Therefore, add
3651 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003652 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003653 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003654 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003655}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003656
Richard Smith3dff2512012-04-10 03:25:07 +00003657/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3658///
3659/// \return the parsed identifier on success, and 0 if the next token is not an
3660/// attribute-token.
3661///
3662/// C++11 [dcl.attr.grammar]p3:
3663/// If a keyword or an alternative token that satisfies the syntactic
3664/// requirements of an identifier is contained in an attribute-token,
3665/// it is considered an identifier.
3666IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3667 switch (Tok.getKind()) {
3668 default:
3669 // Identifiers and keywords have identifier info attached.
David Majnemerd5271992015-01-09 18:09:39 +00003670 if (!Tok.isAnnotation()) {
3671 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3672 Loc = ConsumeToken();
3673 return II;
3674 }
Richard Smith3dff2512012-04-10 03:25:07 +00003675 }
Craig Topper161e4db2014-05-21 06:02:52 +00003676 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003677
3678 case tok::ampamp: // 'and'
3679 case tok::pipe: // 'bitor'
3680 case tok::pipepipe: // 'or'
3681 case tok::caret: // 'xor'
3682 case tok::tilde: // 'compl'
3683 case tok::amp: // 'bitand'
3684 case tok::ampequal: // 'and_eq'
3685 case tok::pipeequal: // 'or_eq'
3686 case tok::caretequal: // 'xor_eq'
3687 case tok::exclaim: // 'not'
3688 case tok::exclaimequal: // 'not_eq'
3689 // Alternative tokens do not have identifier info, but their spelling
3690 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003691 SmallString<8> SpellingBuf;
Benjamin Kramer60be5632015-03-29 19:25:07 +00003692 SourceLocation SpellingLoc =
3693 PP.getSourceManager().getSpellingLoc(Tok.getLocation());
3694 StringRef Spelling = PP.getSpelling(SpellingLoc, SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003695 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003696 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003697 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003698 }
Craig Topper161e4db2014-05-21 06:02:52 +00003699 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003700 }
3701}
3702
Michael Han23214e52012-10-03 01:56:22 +00003703static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3704 IdentifierInfo *ScopeName) {
3705 switch (AttributeList::getKind(AttrName, ScopeName,
3706 AttributeList::AS_CXX11)) {
3707 case AttributeList::AT_CarriesDependency:
Aaron Ballman35f94212014-04-14 16:03:22 +00003708 case AttributeList::AT_Deprecated:
Michael Han23214e52012-10-03 01:56:22 +00003709 case AttributeList::AT_FallThrough:
Hans Wennborgdcfba332015-10-06 23:40:43 +00003710 case AttributeList::AT_CXX11NoReturn:
Michael Han23214e52012-10-03 01:56:22 +00003711 return true;
Aaron Ballmane7964782016-03-07 22:44:55 +00003712 case AttributeList::AT_WarnUnusedResult:
3713 return !ScopeName && AttrName->getName().equals("nodiscard");
Nico Weberac03bce2016-08-23 19:59:55 +00003714 case AttributeList::AT_Unused:
3715 return !ScopeName && AttrName->getName().equals("maybe_unused");
Michael Han23214e52012-10-03 01:56:22 +00003716 default:
3717 return false;
3718 }
3719}
3720
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003721/// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause.
3722///
3723/// [C++11] attribute-argument-clause:
3724/// '(' balanced-token-seq ')'
3725///
3726/// [C++11] balanced-token-seq:
3727/// balanced-token
3728/// balanced-token-seq balanced-token
3729///
3730/// [C++11] balanced-token:
3731/// '(' balanced-token-seq ')'
3732/// '[' balanced-token-seq ']'
3733/// '{' balanced-token-seq '}'
3734/// any token but '(', ')', '[', ']', '{', or '}'
3735bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName,
3736 SourceLocation AttrNameLoc,
3737 ParsedAttributes &Attrs,
3738 SourceLocation *EndLoc,
3739 IdentifierInfo *ScopeName,
3740 SourceLocation ScopeLoc) {
3741 assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
Aaron Ballman35f94212014-04-14 16:03:22 +00003742 SourceLocation LParenLoc = Tok.getLocation();
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003743
3744 // If the attribute isn't known, we will not attempt to parse any
3745 // arguments.
3746 if (!hasAttribute(AttrSyntax::CXX, ScopeName, AttrName,
Bob Wilson7c730832015-07-20 22:57:31 +00003747 getTargetInfo(), getLangOpts())) {
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003748 // Eat the left paren, then skip to the ending right paren.
3749 ConsumeParen();
3750 SkipUntil(tok::r_paren);
3751 return false;
3752 }
3753
3754 if (ScopeName && ScopeName->getName() == "gnu")
3755 // GNU-scoped attributes have some special cases to handle GNU-specific
3756 // behaviors.
3757 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
Craig Topper161e4db2014-05-21 06:02:52 +00003758 ScopeLoc, AttributeList::AS_CXX11, nullptr);
Aaron Ballman35f94212014-04-14 16:03:22 +00003759 else {
3760 unsigned NumArgs =
3761 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
3762 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3763
3764 const AttributeList *Attr = Attrs.getList();
3765 if (Attr && IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) {
3766 // If the attribute is a standard or built-in attribute and we are
3767 // parsing an argument list, we need to determine whether this attribute
3768 // was allowed to have an argument list (such as [[deprecated]]), and how
3769 // many arguments were parsed (so we can diagnose on [[deprecated()]]).
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003770 if (Attr->getMaxArgs() && !NumArgs) {
3771 // The attribute was allowed to have arguments, but none were provided
3772 // even though the attribute parsed successfully. This is an error.
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003773 Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
Aaron Ballmanbb5d8622016-03-08 21:31:32 +00003774 Attr->setInvalid(true);
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003775 } else if (!Attr->getMaxArgs()) {
3776 // The attribute parsed successfully, but was not allowed to have any
3777 // arguments. It doesn't matter whether any were provided -- the
Aaron Ballman35f94212014-04-14 16:03:22 +00003778 // presence of the argument list (even if empty) is diagnosed.
3779 Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
Aaron Ballman9b7cee62014-12-19 18:37:22 +00003780 << AttrName
3781 << FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc));
Aaron Ballmanbb5d8622016-03-08 21:31:32 +00003782 Attr->setInvalid(true);
Aaron Ballman35f94212014-04-14 16:03:22 +00003783 }
3784 }
3785 }
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003786 return true;
3787}
3788
3789/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003790///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003791/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003792/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003793/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003794///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003795/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003796/// attribute[opt]
3797/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003798/// attribute '...'
3799/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003800///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003801/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003802/// attribute-token attribute-argument-clause[opt]
3803///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003804/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003805/// identifier
3806/// attribute-scoped-token
3807///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003808/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003809/// attribute-namespace '::' identifier
3810///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003811/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003812/// identifier
Richard Smith3dff2512012-04-10 03:25:07 +00003813void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003814 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003815 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003816 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003817 ParseAlignmentSpecifier(attrs, endLoc);
3818 return;
3819 }
3820
Alexis Hunt96d5c762009-11-21 08:43:09 +00003821 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003822 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003823
Richard Smithf679b5b2011-10-14 20:48:27 +00003824 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3825
Alexis Hunt96d5c762009-11-21 08:43:09 +00003826 ConsumeBracket();
3827 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003828
Richard Smithb7d7a042016-06-24 12:15:12 +00003829 SourceLocation CommonScopeLoc;
3830 IdentifierInfo *CommonScopeName = nullptr;
3831 if (Tok.is(tok::kw_using)) {
3832 Diag(Tok.getLocation(), getLangOpts().CPlusPlus1z
3833 ? diag::warn_cxx14_compat_using_attribute_ns
3834 : diag::ext_using_attribute_ns);
3835 ConsumeToken();
3836
3837 CommonScopeName = TryParseCXX11AttributeIdentifier(CommonScopeLoc);
3838 if (!CommonScopeName) {
3839 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
3840 SkipUntil(tok::r_square, tok::colon, StopBeforeMatch);
3841 }
3842 if (!TryConsumeToken(tok::colon) && CommonScopeName)
3843 Diag(Tok.getLocation(), diag::err_expected) << tok::colon;
3844 }
3845
Richard Smith10876ef2013-01-17 01:30:42 +00003846 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3847
Richard Smith3dff2512012-04-10 03:25:07 +00003848 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003849 // attribute not present
Alp Toker97650562014-01-10 11:19:30 +00003850 if (TryConsumeToken(tok::comma))
Alexis Hunt96d5c762009-11-21 08:43:09 +00003851 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003852
Richard Smith3dff2512012-04-10 03:25:07 +00003853 SourceLocation ScopeLoc, AttrLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00003854 IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003855
3856 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3857 if (!AttrName)
3858 // Break out to the "expected ']'" diagnostic.
3859 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003860
Alexis Hunt96d5c762009-11-21 08:43:09 +00003861 // scoped attribute
Alp Toker97650562014-01-10 11:19:30 +00003862 if (TryConsumeToken(tok::coloncolon)) {
Richard Smith3dff2512012-04-10 03:25:07 +00003863 ScopeName = AttrName;
3864 ScopeLoc = AttrLoc;
3865
3866 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3867 if (!AttrName) {
Alp Tokerec543272013-12-24 09:48:30 +00003868 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003869 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003870 continue;
3871 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003872 }
3873
Richard Smithb7d7a042016-06-24 12:15:12 +00003874 if (CommonScopeName) {
3875 if (ScopeName) {
3876 Diag(ScopeLoc, diag::err_using_attribute_ns_conflict)
3877 << SourceRange(CommonScopeLoc);
3878 } else {
3879 ScopeName = CommonScopeName;
3880 ScopeLoc = CommonScopeLoc;
3881 }
3882 }
3883
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003884 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003885 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003886
Richard Smith10876ef2013-01-17 01:30:42 +00003887 if (StandardAttr &&
3888 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3889 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003890 << AttrName << SourceRange(SeenAttrs[AttrName]);
Richard Smith10876ef2013-01-17 01:30:42 +00003891
Michael Han23214e52012-10-03 01:56:22 +00003892 // Parse attribute arguments
Aaron Ballman35f94212014-04-14 16:03:22 +00003893 if (Tok.is(tok::l_paren))
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003894 AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3895 ScopeName, ScopeLoc);
Michael Han23214e52012-10-03 01:56:22 +00003896
3897 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003898 attrs.addNew(AttrName,
3899 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3900 AttrLoc),
Craig Topper161e4db2014-05-21 06:02:52 +00003901 ScopeName, ScopeLoc, nullptr, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003902
Alp Toker97650562014-01-10 11:19:30 +00003903 if (TryConsumeToken(tok::ellipsis))
Michael Han23214e52012-10-03 01:56:22 +00003904 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3905 << AttrName->getName();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003906 }
3907
Alp Toker383d2c42014-01-01 03:08:43 +00003908 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003909 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003910 if (endLoc)
3911 *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003912 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003913 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003914}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003915
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003916/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003917///
3918/// attribute-specifier-seq:
3919/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003920void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003921 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003922 assert(getLangOpts().CPlusPlus11);
3923
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003924 SourceLocation StartLoc = Tok.getLocation(), Loc;
3925 if (!endLoc)
3926 endLoc = &Loc;
3927
Douglas Gregor6f981002011-10-07 20:35:25 +00003928 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003929 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003930 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003931
3932 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003933}
3934
Richard Smithc2c8bb82013-10-15 01:34:54 +00003935void Parser::DiagnoseAndSkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00003936 // Start and end location of an attribute or an attribute list.
3937 SourceLocation StartLoc = Tok.getLocation();
Richard Smith955bf012014-06-19 11:42:00 +00003938 SourceLocation EndLoc = SkipCXX11Attributes();
3939
3940 if (EndLoc.isValid()) {
3941 SourceRange Range(StartLoc, EndLoc);
3942 Diag(StartLoc, diag::err_attributes_not_allowed)
3943 << Range;
3944 }
3945}
3946
3947SourceLocation Parser::SkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00003948 SourceLocation EndLoc;
3949
Richard Smith955bf012014-06-19 11:42:00 +00003950 if (!isCXX11AttributeSpecifier())
3951 return EndLoc;
3952
Richard Smithc2c8bb82013-10-15 01:34:54 +00003953 do {
3954 if (Tok.is(tok::l_square)) {
3955 BalancedDelimiterTracker T(*this, tok::l_square);
3956 T.consumeOpen();
3957 T.skipToEnd();
3958 EndLoc = T.getCloseLocation();
3959 } else {
3960 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3961 ConsumeToken();
3962 BalancedDelimiterTracker T(*this, tok::l_paren);
3963 if (!T.consumeOpen())
3964 T.skipToEnd();
3965 EndLoc = T.getCloseLocation();
3966 }
3967 } while (isCXX11AttributeSpecifier());
3968
Richard Smith955bf012014-06-19 11:42:00 +00003969 return EndLoc;
Richard Smithc2c8bb82013-10-15 01:34:54 +00003970}
3971
Nico Weber05e1dad2016-09-03 03:25:22 +00003972/// Parse uuid() attribute when it appears in a [] Microsoft attribute.
3973void Parser::ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs) {
3974 assert(Tok.is(tok::identifier) && "Not a Microsoft attribute list");
3975 IdentifierInfo *UuidIdent = Tok.getIdentifierInfo();
3976 assert(UuidIdent->getName() == "uuid" && "Not a Microsoft attribute list");
3977
3978 SourceLocation UuidLoc = Tok.getLocation();
3979 ConsumeToken();
3980
3981 // Ignore the left paren location for now.
3982 BalancedDelimiterTracker T(*this, tok::l_paren);
3983 if (T.consumeOpen()) {
3984 Diag(Tok, diag::err_expected) << tok::l_paren;
3985 return;
3986 }
3987
3988 ArgsVector ArgExprs;
3989 if (Tok.is(tok::string_literal)) {
3990 // Easy case: uuid("...") -- quoted string.
3991 ExprResult StringResult = ParseStringLiteralExpression();
3992 if (StringResult.isInvalid())
3993 return;
3994 ArgExprs.push_back(StringResult.get());
3995 } else {
3996 // something like uuid({000000A0-0000-0000-C000-000000000049}) -- no
3997 // quotes in the parens. Just append the spelling of all tokens encountered
3998 // until the closing paren.
3999
4000 SmallString<42> StrBuffer; // 2 "", 36 bytes UUID, 2 optional {}, 1 nul
4001 StrBuffer += "\"";
4002
4003 // Since none of C++'s keywords match [a-f]+, accepting just tok::l_brace,
4004 // tok::r_brace, tok::minus, tok::identifier (think C000) and
4005 // tok::numeric_constant (0000) should be enough. But the spelling of the
4006 // uuid argument is checked later anyways, so there's no harm in accepting
4007 // almost anything here.
4008 // cl is very strict about whitespace in this form and errors out if any
4009 // is present, so check the space flags on the tokens.
4010 SourceLocation StartLoc = Tok.getLocation();
4011 while (Tok.isNot(tok::r_paren)) {
4012 if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) {
4013 Diag(Tok, diag::err_attribute_uuid_malformed_guid);
4014 SkipUntil(tok::r_paren, StopAtSemi);
4015 return;
4016 }
4017 SmallString<16> SpellingBuffer;
4018 SpellingBuffer.resize(Tok.getLength() + 1);
4019 bool Invalid = false;
4020 StringRef TokSpelling = PP.getSpelling(Tok, SpellingBuffer, &Invalid);
4021 if (Invalid) {
4022 SkipUntil(tok::r_paren, StopAtSemi);
4023 return;
4024 }
4025 StrBuffer += TokSpelling;
4026 ConsumeAnyToken();
4027 }
4028 StrBuffer += "\"";
4029
4030 if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) {
4031 Diag(Tok, diag::err_attribute_uuid_malformed_guid);
4032 ConsumeParen();
4033 return;
4034 }
4035
4036 // Pretend the user wrote the appropriate string literal here.
4037 // ActOnStringLiteral() copies the string data into the literal, so it's
4038 // ok that the Token points to StrBuffer.
4039 Token Toks[1];
4040 Toks[0].startToken();
4041 Toks[0].setKind(tok::string_literal);
4042 Toks[0].setLocation(StartLoc);
4043 Toks[0].setLiteralData(StrBuffer.data());
4044 Toks[0].setLength(StrBuffer.size());
4045 StringLiteral *UuidString =
4046 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
4047 ArgExprs.push_back(UuidString);
4048 }
4049
4050 if (!T.consumeClose()) {
4051 // FIXME: Warn that this syntax is deprecated, with a Fix-It suggesting
4052 // using __declspec(uuid()) instead.
4053 Attrs.addNew(UuidIdent, SourceRange(UuidLoc, T.getCloseLocation()), nullptr,
4054 SourceLocation(), ArgExprs.data(), ArgExprs.size(),
4055 AttributeList::AS_Microsoft);
4056 }
4057}
4058
David Majnemere4752e752015-07-08 05:55:00 +00004059/// ParseMicrosoftAttributes - Parse Microsoft attributes [Attr]
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00004060///
4061/// [MS] ms-attribute:
4062/// '[' token-seq ']'
4063///
4064/// [MS] ms-attribute-seq:
4065/// ms-attribute[opt]
4066/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00004067void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
4068 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00004069 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
4070
Saleem Abdulrasool425efcf2015-06-15 20:57:04 +00004071 do {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004072 // FIXME: If this is actually a C++11 attribute, parse it as one.
Saleem Abdulrasool425efcf2015-06-15 20:57:04 +00004073 BalancedDelimiterTracker T(*this, tok::l_square);
4074 T.consumeOpen();
Nico Weber05e1dad2016-09-03 03:25:22 +00004075
4076 // Skip most ms attributes except for a whitelist.
4077 while (true) {
4078 SkipUntil(tok::r_square, tok::identifier, StopAtSemi | StopBeforeMatch);
4079 if (Tok.isNot(tok::identifier)) // ']', but also eof
4080 break;
4081 if (Tok.getIdentifierInfo()->getName() == "uuid")
4082 ParseMicrosoftUuidAttributeArgs(attrs);
4083 else
4084 ConsumeToken();
4085 }
4086
Saleem Abdulrasool425efcf2015-06-15 20:57:04 +00004087 T.consumeClose();
4088 if (endLoc)
4089 *endLoc = T.getCloseLocation();
4090 } while (Tok.is(tok::l_square));
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00004091}
Francois Pichet8f981d52011-05-25 10:19:49 +00004092
4093void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
4094 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00004095 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00004096 if (ParseMicrosoftIfExistsCondition(Result))
4097 return;
4098
Douglas Gregor43edb322011-10-24 22:31:10 +00004099 BalancedDelimiterTracker Braces(*this, tok::l_brace);
4100 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00004101 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet8f981d52011-05-25 10:19:49 +00004102 return;
4103 }
Francois Pichet8f981d52011-05-25 10:19:49 +00004104
Douglas Gregor43edb322011-10-24 22:31:10 +00004105 switch (Result.Behavior) {
4106 case IEB_Parse:
4107 // Parse the declarations below.
4108 break;
4109
4110 case IEB_Dependent:
4111 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
4112 << Result.IsIfExists;
4113 // Fall through to skip.
4114
4115 case IEB_Skip:
4116 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00004117 return;
4118 }
4119
Richard Smith34f30512013-11-23 04:06:09 +00004120 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00004121 // __if_exists, __if_not_exists can nest.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00004122 if (Tok.isOneOf(tok::kw___if_exists, tok::kw___if_not_exists)) {
Francois Pichet8f981d52011-05-25 10:19:49 +00004123 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
4124 continue;
4125 }
4126
4127 // Check for extraneous top-level semicolon.
4128 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00004129 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00004130 continue;
4131 }
4132
4133 AccessSpecifier AS = getAccessSpecifierIfPresent();
4134 if (AS != AS_none) {
4135 // Current token is a C++ access specifier.
4136 CurAS = AS;
4137 SourceLocation ASLoc = Tok.getLocation();
4138 ConsumeToken();
4139 if (Tok.is(tok::colon))
4140 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
4141 else
Alp Toker35d87032013-12-30 23:29:50 +00004142 Diag(Tok, diag::err_expected) << tok::colon;
Francois Pichet8f981d52011-05-25 10:19:49 +00004143 ConsumeToken();
4144 continue;
4145 }
4146
4147 // Parse all the comma separated declarators.
Craig Topper161e4db2014-05-21 06:02:52 +00004148 ParseCXXClassMemberDeclaration(CurAS, nullptr);
Francois Pichet8f981d52011-05-25 10:19:49 +00004149 }
Douglas Gregor43edb322011-10-24 22:31:10 +00004150
4151 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00004152}