blob: 241c959474ab804aefd32b2dbd7d070da45f564b [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'.
Richard Smith6f1daa42016-12-16 00:58:48 +0000424Parser::DeclGroupPtrTy
425Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000426 const ParsedTemplateInfo &TemplateInfo,
Richard Smith6f1daa42016-12-16 00:58:48 +0000427 SourceLocation &DeclEnd,
428 ParsedAttributesWithRange &attrs) {
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
Richard Smith6f1daa42016-12-16 00:58:48 +0000450 Decl *UsingDir = ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
451 return Actions.ConvertDeclToDeclGroup(UsingDir);
John McCall9b72f892010-11-10 02:40:36 +0000452 }
453
Richard Smithdda56e42011-04-15 14:24:37 +0000454 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000455
456 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000457 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000458
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000459 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Richard Smith6f1daa42016-12-16 00:58:48 +0000460 AS_none);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000461}
462
463/// ParseUsingDirective - Parse C++ using-directive, assumes
464/// that current token is 'namespace' and 'using' was already parsed.
465///
466/// using-directive: [C++ 7.3.p4: namespace.udir]
467/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
468/// namespace-name ;
469/// [GNU] using-directive:
470/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
471/// namespace-name attributes[opt] ;
472///
John McCall48871652010-08-21 09:40:31 +0000473Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000474 SourceLocation UsingLoc,
475 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000476 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000477 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
478
479 // Eat 'namespace'.
480 SourceLocation NamespcLoc = ConsumeToken();
481
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000482 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000483 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000484 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000485 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000486 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000487
Douglas Gregord7c4d982008-12-30 03:27:21 +0000488 CXXScopeSpec SS;
489 // Parse (optional) nested-name-specifier.
David Blaikieefdccaa2016-01-15 23:43:34 +0000490 ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000491
Craig Topper161e4db2014-05-21 06:02:52 +0000492 IdentifierInfo *NamespcName = nullptr;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000493 SourceLocation IdentLoc = SourceLocation();
494
495 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000496 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000497 Diag(Tok, diag::err_expected_namespace_name);
498 // If there was invalid namespace name, skip to end of decl, and eat ';'.
499 SkipUntil(tok::semi);
500 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Craig Topper161e4db2014-05-21 06:02:52 +0000501 return nullptr;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000502 }
Mike Stump11289f42009-09-09 15:08:12 +0000503
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000504 // Parse identifier.
505 NamespcName = Tok.getIdentifierInfo();
506 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000507
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000508 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000509 bool GNUAttr = false;
510 if (Tok.is(tok::kw___attribute)) {
511 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000512 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000513 }
Mike Stump11289f42009-09-09 15:08:12 +0000514
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000515 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000516 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000517 if (ExpectAndConsume(tok::semi,
518 GNUAttr ? diag::err_expected_semi_after_attribute_list
519 : diag::err_expected_semi_after_namespace_name))
520 SkipUntil(tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000521
Douglas Gregor0be31a22010-07-02 17:43:08 +0000522 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000523 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000524}
525
Richard Smith6f1daa42016-12-16 00:58:48 +0000526/// Parse a using-declarator (or the identifier in a C++11 alias-declaration).
Douglas Gregord7c4d982008-12-30 03:27:21 +0000527///
Richard Smith6f1daa42016-12-16 00:58:48 +0000528/// using-declarator:
529/// 'typename'[opt] nested-name-specifier unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000530///
Richard Smith6f1daa42016-12-16 00:58:48 +0000531bool Parser::ParseUsingDeclarator(unsigned Context, UsingDeclarator &D) {
532 D.clear();
Douglas Gregorfec52632009-06-20 00:51:54 +0000533
534 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000535 // FIXME: This is wrong; we should parse this as a typename-specifier.
Richard Smith6f1daa42016-12-16 00:58:48 +0000536 TryConsumeToken(tok::kw_typename, D.TypenameLoc);
Douglas Gregorfec52632009-06-20 00:51:54 +0000537
Nikola Smiljanic67860242014-09-26 00:28:20 +0000538 if (Tok.is(tok::kw___super)) {
539 Diag(Tok.getLocation(), diag::err_super_in_using_declaration);
Richard Smith6f1daa42016-12-16 00:58:48 +0000540 return true;
Nikola Smiljanic67860242014-09-26 00:28:20 +0000541 }
542
Douglas Gregorfec52632009-06-20 00:51:54 +0000543 // Parse nested-name-specifier.
Craig Topper161e4db2014-05-21 06:02:52 +0000544 IdentifierInfo *LastII = nullptr;
Richard Smith6f1daa42016-12-16 00:58:48 +0000545 ParseOptionalCXXScopeSpecifier(D.SS, nullptr, /*EnteringContext=*/false,
Craig Topper161e4db2014-05-21 06:02:52 +0000546 /*MayBePseudoDtor=*/nullptr,
547 /*IsTypename=*/false,
Richard Smith7447af42013-03-26 01:15:19 +0000548 /*LastII=*/&LastII);
Richard Smith6f1daa42016-12-16 00:58:48 +0000549 if (D.SS.isInvalid())
550 return true;
Richard Smith7447af42013-03-26 01:15:19 +0000551
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000552 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000553 // destructor names and allow the action module to diagnose any semantic
554 // errors.
Richard Smith7447af42013-03-26 01:15:19 +0000555 //
556 // C++11 [class.qual]p2:
557 // [...] in a using-declaration that is a member-declaration, if the name
558 // specified after the nested-name-specifier is the same as the identifier
559 // or the simple-template-id's template-name in the last component of the
560 // nested-name-specifier, the name is [...] considered to name the
561 // constructor.
562 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
Richard Smith151c4562016-12-20 21:35:28 +0000563 Tok.is(tok::identifier) &&
564 (NextToken().is(tok::semi) || NextToken().is(tok::comma) ||
565 NextToken().is(tok::ellipsis)) &&
Richard Smith6f1daa42016-12-16 00:58:48 +0000566 D.SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
567 !D.SS.getScopeRep()->getAsNamespace() &&
568 !D.SS.getScopeRep()->getAsNamespaceAlias()) {
Richard Smith7447af42013-03-26 01:15:19 +0000569 SourceLocation IdLoc = ConsumeToken();
Richard Smith6f1daa42016-12-16 00:58:48 +0000570 ParsedType Type =
571 Actions.getInheritingConstructorName(D.SS, IdLoc, *LastII);
572 D.Name.setConstructorName(Type, IdLoc, IdLoc);
573 } else {
574 if (ParseUnqualifiedId(
575 D.SS, /*EnteringContext=*/false,
576 /*AllowDestructorName=*/true,
577 /*AllowConstructorName=*/!(Tok.is(tok::identifier) &&
578 NextToken().is(tok::equal)),
Richard Smith35845152017-02-07 01:37:30 +0000579 /*AllowDeductionGuide=*/false,
Richard Smith6f1daa42016-12-16 00:58:48 +0000580 nullptr, D.TemplateKWLoc, D.Name))
581 return true;
Douglas Gregorfec52632009-06-20 00:51:54 +0000582 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000583
Richard Smith151c4562016-12-20 21:35:28 +0000584 if (TryConsumeToken(tok::ellipsis, D.EllipsisLoc))
585 Diag(Tok.getLocation(), getLangOpts().CPlusPlus1z ?
586 diag::warn_cxx1z_compat_using_declaration_pack :
587 diag::ext_using_declaration_pack);
Richard Smith6f1daa42016-12-16 00:58:48 +0000588
589 return false;
590}
591
592/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
593/// Assumes that 'using' was already seen.
594///
595/// using-declaration: [C++ 7.3.p3: namespace.udecl]
596/// 'using' using-declarator-list[opt] ;
597///
598/// using-declarator-list: [C++1z]
599/// using-declarator '...'[opt]
600/// using-declarator-list ',' using-declarator '...'[opt]
601///
602/// using-declarator-list: [C++98-14]
603/// using-declarator
604///
605/// alias-declaration: C++11 [dcl.dcl]p1
606/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
607///
608Parser::DeclGroupPtrTy
609Parser::ParseUsingDeclaration(unsigned Context,
610 const ParsedTemplateInfo &TemplateInfo,
611 SourceLocation UsingLoc, SourceLocation &DeclEnd,
612 AccessSpecifier AS) {
613 // Check for misplaced attributes before the identifier in an
614 // alias-declaration.
615 ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
616 MaybeParseCXX11Attributes(MisplacedAttrs);
617
618 UsingDeclarator D;
619 bool InvalidDeclarator = ParseUsingDeclarator(Context, D);
620
Richard Smithc2c8bb82013-10-15 01:34:54 +0000621 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith37a45dd2013-10-24 01:21:09 +0000622 MaybeParseGNUAttributes(Attrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000623 MaybeParseCXX11Attributes(Attrs);
Richard Smithdda56e42011-04-15 14:24:37 +0000624
625 // Maybe this is an alias-declaration.
Richard Smith6f1daa42016-12-16 00:58:48 +0000626 if (Tok.is(tok::equal)) {
627 if (InvalidDeclarator) {
628 SkipUntil(tok::semi);
629 return nullptr;
630 }
631
Richard Smithc2c8bb82013-10-15 01:34:54 +0000632 // If we had any misplaced attributes from earlier, this is where they
633 // should have been written.
634 if (MisplacedAttrs.Range.isValid()) {
635 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
636 << FixItHint::CreateInsertionFromRange(
637 Tok.getLocation(),
638 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
639 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
640 Attrs.takeAllFrom(MisplacedAttrs);
641 }
642
Richard Smith6f1daa42016-12-16 00:58:48 +0000643 Decl *DeclFromDeclSpec = nullptr;
644 Decl *AD = ParseAliasDeclarationAfterDeclarator(
645 TemplateInfo, UsingLoc, D, DeclEnd, AS, Attrs, &DeclFromDeclSpec);
646 return Actions.ConvertDeclToDeclGroup(AD, DeclFromDeclSpec);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000647 }
Mike Stump11289f42009-09-09 15:08:12 +0000648
Richard Smith6f1daa42016-12-16 00:58:48 +0000649 // C++11 attributes are not allowed on a using-declaration, but GNU ones
650 // are.
651 ProhibitAttributes(MisplacedAttrs);
652 ProhibitAttributes(Attrs);
Douglas Gregorfec52632009-06-20 00:51:54 +0000653
John McCall9b72f892010-11-10 02:40:36 +0000654 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000655 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000656 // template <...> using id = type;
Richard Smith6f1daa42016-12-16 00:58:48 +0000657 if (TemplateInfo.Kind) {
John McCall9b72f892010-11-10 02:40:36 +0000658 SourceRange R = TemplateInfo.getSourceRange();
Craig Topper54a6a682015-11-14 18:16:08 +0000659 Diag(UsingLoc, diag::err_templated_using_directive_declaration)
660 << 1 /* declaration */ << R << FixItHint::CreateRemoval(R);
John McCall9b72f892010-11-10 02:40:36 +0000661
662 // Unfortunately, we have to bail out instead of recovering by
663 // ignoring the parameters, just in case the nested name specifier
664 // depends on the parameters.
Craig Topper161e4db2014-05-21 06:02:52 +0000665 return nullptr;
John McCall9b72f892010-11-10 02:40:36 +0000666 }
667
Richard Smith6f1daa42016-12-16 00:58:48 +0000668 SmallVector<Decl *, 8> DeclsInGroup;
669 while (true) {
670 // Parse (optional) attributes (most likely GNU strong-using extension).
671 MaybeParseGNUAttributes(Attrs);
672
673 if (InvalidDeclarator)
674 SkipUntil(tok::comma, tok::semi, StopBeforeMatch);
675 else {
676 // "typename" keyword is allowed for identifiers only,
677 // because it may be a type definition.
678 if (D.TypenameLoc.isValid() &&
679 D.Name.getKind() != UnqualifiedId::IK_Identifier) {
680 Diag(D.Name.getSourceRange().getBegin(),
681 diag::err_typename_identifiers_only)
682 << FixItHint::CreateRemoval(SourceRange(D.TypenameLoc));
683 // Proceed parsing, but discard the typename keyword.
684 D.TypenameLoc = SourceLocation();
685 }
686
Richard Smith151c4562016-12-20 21:35:28 +0000687 Decl *UD = Actions.ActOnUsingDeclaration(getCurScope(), AS, UsingLoc,
688 D.TypenameLoc, D.SS, D.Name,
689 D.EllipsisLoc, Attrs.getList());
Richard Smith6f1daa42016-12-16 00:58:48 +0000690 if (UD)
691 DeclsInGroup.push_back(UD);
692 }
693
694 if (!TryConsumeToken(tok::comma))
695 break;
696
697 // Parse another using-declarator.
698 Attrs.clear();
699 InvalidDeclarator = ParseUsingDeclarator(Context, D);
Douglas Gregor882a61a2011-09-26 14:30:28 +0000700 }
701
Richard Smith6f1daa42016-12-16 00:58:48 +0000702 if (DeclsInGroup.size() > 1)
703 Diag(Tok.getLocation(), getLangOpts().CPlusPlus1z ?
704 diag::warn_cxx1z_compat_multi_using_declaration :
705 diag::ext_multi_using_declaration);
706
707 // Eat ';'.
708 DeclEnd = Tok.getLocation();
709 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
710 !Attrs.empty() ? "attributes list"
711 : "using declaration"))
712 SkipUntil(tok::semi);
713
Richard Smith3beb7c62017-01-12 02:27:38 +0000714 return Actions.BuildDeclaratorGroup(DeclsInGroup);
Richard Smith6f1daa42016-12-16 00:58:48 +0000715}
716
Richard Smith6f1daa42016-12-16 00:58:48 +0000717Decl *Parser::ParseAliasDeclarationAfterDeclarator(
718 const ParsedTemplateInfo &TemplateInfo, SourceLocation UsingLoc,
719 UsingDeclarator &D, SourceLocation &DeclEnd, AccessSpecifier AS,
720 ParsedAttributes &Attrs, Decl **OwnedType) {
721 if (ExpectAndConsume(tok::equal)) {
722 SkipUntil(tok::semi);
723 return nullptr;
724 }
725
726 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
727 diag::warn_cxx98_compat_alias_declaration :
728 diag::ext_alias_declaration);
729
730 // Type alias templates cannot be specialized.
731 int SpecKind = -1;
732 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
733 D.Name.getKind() == UnqualifiedId::IK_TemplateId)
734 SpecKind = 0;
735 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
736 SpecKind = 1;
737 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
738 SpecKind = 2;
739 if (SpecKind != -1) {
740 SourceRange Range;
741 if (SpecKind == 0)
742 Range = SourceRange(D.Name.TemplateId->LAngleLoc,
743 D.Name.TemplateId->RAngleLoc);
744 else
745 Range = TemplateInfo.getSourceRange();
746 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
747 << SpecKind << Range;
748 SkipUntil(tok::semi);
749 return nullptr;
750 }
751
752 // Name must be an identifier.
753 if (D.Name.getKind() != UnqualifiedId::IK_Identifier) {
754 Diag(D.Name.StartLocation, diag::err_alias_declaration_not_identifier);
755 // No removal fixit: can't recover from this.
756 SkipUntil(tok::semi);
757 return nullptr;
758 } else if (D.TypenameLoc.isValid())
759 Diag(D.TypenameLoc, diag::err_alias_declaration_not_identifier)
760 << FixItHint::CreateRemoval(SourceRange(
761 D.TypenameLoc,
762 D.SS.isNotEmpty() ? D.SS.getEndLoc() : D.TypenameLoc));
763 else if (D.SS.isNotEmpty())
764 Diag(D.SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
765 << FixItHint::CreateRemoval(D.SS.getRange());
Richard Smith151c4562016-12-20 21:35:28 +0000766 if (D.EllipsisLoc.isValid())
767 Diag(D.EllipsisLoc, diag::err_alias_declaration_pack_expansion)
768 << FixItHint::CreateRemoval(SourceRange(D.EllipsisLoc));
Richard Smith6f1daa42016-12-16 00:58:48 +0000769
770 Decl *DeclFromDeclSpec = nullptr;
771 TypeResult TypeAlias =
772 ParseTypeName(nullptr,
773 TemplateInfo.Kind ? Declarator::AliasTemplateContext
774 : Declarator::AliasDeclContext,
775 AS, &DeclFromDeclSpec, &Attrs);
776 if (OwnedType)
777 *OwnedType = DeclFromDeclSpec;
778
779 // Eat ';'.
780 DeclEnd = Tok.getLocation();
781 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
782 !Attrs.empty() ? "attributes list"
783 : "alias declaration"))
784 SkipUntil(tok::semi);
785
786 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
787 MultiTemplateParamsArg TemplateParamsArg(
788 TemplateParams ? TemplateParams->data() : nullptr,
789 TemplateParams ? TemplateParams->size() : 0);
790 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
791 UsingLoc, D.Name, Attrs.getList(),
792 TypeAlias, DeclFromDeclSpec);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000793}
794
Benjamin Kramere56f3932011-12-23 17:00:35 +0000795/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000796///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000797/// [C++0x] static_assert-declaration:
798/// static_assert ( constant-expression , string-literal ) ;
799///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000800/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000801/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000802///
John McCall48871652010-08-21 09:40:31 +0000803Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000804 assert(Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert) &&
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000805 "Not a static_assert declaration");
806
David Blaikiebbafb8a2012-03-11 07:00:24 +0000807 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000808 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000809 if (Tok.is(tok::kw_static_assert))
810 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000811
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000812 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000813
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000814 BalancedDelimiterTracker T(*this, tok::l_paren);
815 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000816 Diag(Tok, diag::err_expected) << tok::l_paren;
Richard Smith76965712012-09-13 19:12:50 +0000817 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000818 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000819 }
Mike Stump11289f42009-09-09 15:08:12 +0000820
John McCalldadc5752010-08-24 06:29:42 +0000821 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000822 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000823 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000824 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000825 }
Mike Stump11289f42009-09-09 15:08:12 +0000826
Richard Smith085a64f2014-06-20 19:57:12 +0000827 ExprResult AssertMessage;
828 if (Tok.is(tok::r_paren)) {
829 Diag(Tok, getLangOpts().CPlusPlus1z
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000830 ? diag::warn_cxx14_compat_static_assert_no_message
Richard Smith085a64f2014-06-20 19:57:12 +0000831 : diag::ext_static_assert_no_message)
832 << (getLangOpts().CPlusPlus1z
833 ? FixItHint()
834 : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\""));
835 } else {
836 if (ExpectAndConsume(tok::comma)) {
837 SkipUntil(tok::semi);
838 return nullptr;
839 }
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000840
Richard Smith085a64f2014-06-20 19:57:12 +0000841 if (!isTokenStringLiteral()) {
842 Diag(Tok, diag::err_expected_string_literal)
843 << /*Source='static_assert'*/1;
844 SkipMalformedDecl();
845 return nullptr;
846 }
Mike Stump11289f42009-09-09 15:08:12 +0000847
Richard Smith085a64f2014-06-20 19:57:12 +0000848 AssertMessage = ParseStringLiteralExpression();
849 if (AssertMessage.isInvalid()) {
850 SkipMalformedDecl();
851 return nullptr;
852 }
Richard Smithd67aea22012-03-06 03:21:47 +0000853 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000854
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000855 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000856
Chris Lattner49836b42009-04-02 04:16:50 +0000857 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000858 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000859
John McCallb268a282010-08-23 23:25:46 +0000860 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000861 AssertExpr.get(),
862 AssertMessage.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000863 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000864}
865
Richard Smith74aeef52013-04-26 16:15:35 +0000866/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000867///
868/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000869/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000870///
David Blaikie15a430a2011-12-04 05:04:18 +0000871SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000872 assert(Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)
David Blaikie15a430a2011-12-04 05:04:18 +0000873 && "Not a decltype specifier");
874
David Blaikie15a430a2011-12-04 05:04:18 +0000875 ExprResult Result;
876 SourceLocation StartLoc = Tok.getLocation();
877 SourceLocation EndLoc;
878
879 if (Tok.is(tok::annot_decltype)) {
880 Result = getExprAnnotation(Tok);
881 EndLoc = Tok.getAnnotationEndLoc();
882 ConsumeToken();
883 if (Result.isInvalid()) {
884 DS.SetTypeSpecError();
885 return EndLoc;
886 }
887 } else {
Richard Smith324df552012-02-24 22:30:04 +0000888 if (Tok.getIdentifierInfo()->isStr("decltype"))
889 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000890
David Blaikie15a430a2011-12-04 05:04:18 +0000891 ConsumeToken();
892
893 BalancedDelimiterTracker T(*this, tok::l_paren);
894 if (T.expectAndConsume(diag::err_expected_lparen_after,
895 "decltype", tok::r_paren)) {
896 DS.SetTypeSpecError();
897 return T.getOpenLocation() == Tok.getLocation() ?
898 StartLoc : T.getOpenLocation();
899 }
900
Richard Smith74aeef52013-04-26 16:15:35 +0000901 // Check for C++1y 'decltype(auto)'.
902 if (Tok.is(tok::kw_auto)) {
903 // No need to disambiguate here: an expression can't start with 'auto',
904 // because the typename-specifier in a function-style cast operation can't
905 // be 'auto'.
906 Diag(Tok.getLocation(),
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000907 getLangOpts().CPlusPlus14
Richard Smith74aeef52013-04-26 16:15:35 +0000908 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
909 : diag::ext_decltype_auto_type_specifier);
910 ConsumeToken();
911 } else {
912 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000913
Richard Smith74aeef52013-04-26 16:15:35 +0000914 // C++11 [dcl.type.simple]p4:
915 // The operand of the decltype specifier is an unevaluated operand.
916 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
Craig Topper161e4db2014-05-21 06:02:52 +0000917 nullptr,/*IsDecltype=*/true);
Kaelyn Takata5cc85352015-04-10 19:16:46 +0000918 Result =
919 Actions.CorrectDelayedTyposInExpr(ParseExpression(), [](Expr *E) {
920 return E->hasPlaceholderType() ? ExprError() : E;
921 });
Richard Smith74aeef52013-04-26 16:15:35 +0000922 if (Result.isInvalid()) {
923 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000924 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smith74aeef52013-04-26 16:15:35 +0000925 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000926 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000927 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
928 // Backtrack to get the location of the last token before the semi.
929 PP.RevertCachedTokens(2);
930 ConsumeToken(); // the semi.
931 EndLoc = ConsumeAnyToken();
932 assert(Tok.is(tok::semi));
933 } else {
934 EndLoc = Tok.getLocation();
935 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000936 }
Richard Smith74aeef52013-04-26 16:15:35 +0000937 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000938 }
Richard Smith74aeef52013-04-26 16:15:35 +0000939
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000940 Result = Actions.ActOnDecltypeExpression(Result.get());
David Blaikie15a430a2011-12-04 05:04:18 +0000941 }
942
943 // Match the ')'
944 T.consumeClose();
945 if (T.getCloseLocation().isInvalid()) {
946 DS.SetTypeSpecError();
947 // FIXME: this should return the location of the last token
948 // that was consumed (by "consumeClose()")
949 return T.getCloseLocation();
950 }
951
Richard Smithfd555f62012-02-22 02:04:18 +0000952 if (Result.isInvalid()) {
953 DS.SetTypeSpecError();
954 return T.getCloseLocation();
955 }
956
David Blaikie15a430a2011-12-04 05:04:18 +0000957 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000958 }
Richard Smith74aeef52013-04-26 16:15:35 +0000959 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000960
Craig Topper161e4db2014-05-21 06:02:52 +0000961 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +0000962 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000963 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Anders Carlsson74948d02009-06-24 17:47:40 +0000964 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000965 if (Result.get()
966 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000967 DiagID, Result.get(), Policy)
Richard Smith74aeef52013-04-26 16:15:35 +0000968 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000969 DiagID, Policy)) {
John McCall49bfce42009-08-03 20:12:06 +0000970 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000971 DS.SetTypeSpecError();
972 }
973 return EndLoc;
974}
975
976void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
977 SourceLocation StartLoc,
978 SourceLocation EndLoc) {
979 // make sure we have a token we can turn into an annotation token
980 if (PP.isBacktrackEnabled())
981 PP.RevertCachedTokens(1);
982 else
983 PP.EnterToken(Tok);
984
985 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000986 setExprAnnotation(Tok,
987 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
988 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
989 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000990 Tok.setAnnotationEndLoc(EndLoc);
991 Tok.setLocation(StartLoc);
992 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000993}
994
Alexis Hunt4a257072011-05-19 05:37:45 +0000995void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
996 assert(Tok.is(tok::kw___underlying_type) &&
997 "Not an underlying type specifier");
998
999 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001000 BalancedDelimiterTracker T(*this, tok::l_paren);
1001 if (T.expectAndConsume(diag::err_expected_lparen_after,
1002 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +00001003 return;
1004 }
1005
1006 TypeResult Result = ParseTypeName();
1007 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001008 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt4a257072011-05-19 05:37:45 +00001009 return;
1010 }
1011
1012 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001013 T.consumeClose();
1014 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +00001015 return;
1016
Craig Topper161e4db2014-05-21 06:02:52 +00001017 const char *PrevSpec = nullptr;
Alexis Hunt4a257072011-05-19 05:37:45 +00001018 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +00001019 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001020 DiagID, Result.get(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001021 Actions.getASTContext().getPrintingPolicy()))
Alexis Hunt4a257072011-05-19 05:37:45 +00001022 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanellaa90af722013-07-06 18:54:58 +00001023 DS.setTypeofParensRange(T.getRange());
Alexis Hunt4a257072011-05-19 05:37:45 +00001024}
1025
David Blaikie00ee7a082011-10-25 15:01:20 +00001026/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
1027/// class name or decltype-specifier. Note that we only check that the result
1028/// names a type; semantic analysis will need to verify that the type names a
1029/// class. The result is either a type or null, depending on whether a type
1030/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +00001031///
Richard Smith4c96e992013-02-19 23:47:15 +00001032/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +00001033/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +00001034/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +00001035/// nested-name-specifier[opt] class-name
1036/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +00001037/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +00001038/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +00001039/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +00001040///
Richard Smith4c96e992013-02-19 23:47:15 +00001041/// In C++98, instead of base-type-specifier, we have:
1042///
1043/// ::[opt] nested-name-specifier[opt] class-name
Craig Topper9ad7e262014-10-31 06:57:07 +00001044TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
1045 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +00001046 // Ignore attempts to use typename
1047 if (Tok.is(tok::kw_typename)) {
1048 Diag(Tok, diag::err_expected_class_name_not_template)
1049 << FixItHint::CreateRemoval(Tok.getLocation());
1050 ConsumeToken();
1051 }
1052
David Blaikieafa155f2011-10-25 18:17:58 +00001053 // Parse optional nested-name-specifier
1054 CXXScopeSpec SS;
David Blaikieefdccaa2016-01-15 23:43:34 +00001055 ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false);
David Blaikieafa155f2011-10-25 18:17:58 +00001056
1057 BaseLoc = Tok.getLocation();
1058
David Blaikie1cd50022011-10-25 17:10:12 +00001059 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +00001060 // tok == kw_decltype is just error recovery, it can only happen when SS
1061 // isn't empty
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001062 if (Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +00001063 if (SS.isNotEmpty())
1064 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
1065 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +00001066 // Fake up a Declarator to use with ActOnTypeName.
1067 DeclSpec DS(AttrFactory);
1068
David Blaikie7491e732011-12-08 04:53:15 +00001069 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +00001070
1071 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1072 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1073 }
1074
Douglas Gregord54dfb82009-02-25 23:52:28 +00001075 // Check whether we have a template-id that names a type.
1076 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001077 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00001078 if (TemplateId->Kind == TNK_Type_template ||
1079 TemplateId->Kind == TNK_Dependent_template_name) {
Richard Smith62559bd2017-02-01 21:36:38 +00001080 AnnotateTemplateIdTokenAsType(/*IsClassName*/true);
Douglas Gregord54dfb82009-02-25 23:52:28 +00001081
1082 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00001083 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +00001084 EndLocation = Tok.getAnnotationEndLoc();
1085 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001086
1087 if (Type)
1088 return Type;
1089 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +00001090 }
1091
1092 // Fall through to produce an error below.
1093 }
1094
Douglas Gregor831c93f2008-11-05 20:51:48 +00001095 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001096 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001097 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +00001098 }
1099
Douglas Gregor18473f32010-01-12 21:28:44 +00001100 IdentifierInfo *Id = Tok.getIdentifierInfo();
1101 SourceLocation IdLoc = ConsumeToken();
1102
1103 if (Tok.is(tok::less)) {
1104 // It looks the user intended to write a template-id here, but the
1105 // template-name was wrong. Try to fix that.
1106 TemplateNameKind TNK = TNK_Type_template;
1107 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001108 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +00001109 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +00001110 Diag(IdLoc, diag::err_unknown_template_name)
1111 << Id;
1112 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001113
Serge Pavlovb716b3c2013-08-10 05:54:47 +00001114 if (!Template) {
1115 TemplateArgList TemplateArgs;
1116 SourceLocation LAngleLoc, RAngleLoc;
David Blaikiee20506d2016-01-15 23:43:28 +00001117 ParseTemplateIdAfterTemplateName(nullptr, IdLoc, SS, true, LAngleLoc,
1118 TemplateArgs, RAngleLoc);
Douglas Gregor18473f32010-01-12 21:28:44 +00001119 return true;
Serge Pavlovb716b3c2013-08-10 05:54:47 +00001120 }
Douglas Gregor18473f32010-01-12 21:28:44 +00001121
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001122 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +00001123 UnqualifiedId TemplateName;
1124 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001125
Douglas Gregor18473f32010-01-12 21:28:44 +00001126 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001127 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
Richard Smith62559bd2017-02-01 21:36:38 +00001128 TemplateName))
Douglas Gregor18473f32010-01-12 21:28:44 +00001129 return true;
Richard Smith62559bd2017-02-01 21:36:38 +00001130 if (TNK == TNK_Type_template || TNK == TNK_Dependent_template_name)
1131 AnnotateTemplateIdTokenAsType(/*IsClassName*/true);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001132
Douglas Gregor18473f32010-01-12 21:28:44 +00001133 // If we didn't end up with a typename token, there's nothing more we
1134 // can do.
1135 if (Tok.isNot(tok::annot_typename))
1136 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001137
Douglas Gregor18473f32010-01-12 21:28:44 +00001138 // Retrieve the type from the annotation token, consume that token, and
1139 // return.
1140 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +00001141 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +00001142 ConsumeToken();
1143 return Type;
1144 }
1145
Douglas Gregor831c93f2008-11-05 20:51:48 +00001146 // We have an identifier; check whether it is actually a type.
Craig Topper161e4db2014-05-21 06:02:52 +00001147 IdentifierInfo *CorrectedII = nullptr;
Richard Smith600b5262017-01-26 20:40:47 +00001148 ParsedType Type = Actions.getTypeName(
Richard Smith62559bd2017-02-01 21:36:38 +00001149 *Id, IdLoc, getCurScope(), &SS, /*IsClassName=*/true, false, nullptr,
Richard Smith600b5262017-01-26 20:40:47 +00001150 /*IsCtorOrDtorName=*/false,
1151 /*NonTrivialTypeSourceInfo=*/true,
1152 /*IsClassTemplateDeductionContext*/ false, &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001153 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001154 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001155 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +00001156 }
1157
1158 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +00001159 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001160
1161 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +00001162 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +00001163 DS.SetRangeStart(IdLoc);
1164 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +00001165 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001166
Craig Topper161e4db2014-05-21 06:02:52 +00001167 const char *PrevSpec = nullptr;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001168 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001169 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
1170 Actions.getASTContext().getPrintingPolicy());
Nick Lewycky19b9f952010-07-26 16:56:01 +00001171
1172 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1173 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001174}
1175
John McCall8d32c052012-05-22 21:28:12 +00001176void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001177 while (Tok.isOneOf(tok::kw___single_inheritance,
1178 tok::kw___multiple_inheritance,
1179 tok::kw___virtual_inheritance)) {
John McCall8d32c052012-05-22 21:28:12 +00001180 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1181 SourceLocation AttrNameLoc = ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001182 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Aaron Ballman8edb5c22013-12-18 23:44:18 +00001183 AttributeList::AS_Keyword);
John McCall8d32c052012-05-22 21:28:12 +00001184 }
1185}
1186
Richard Smith369b9f92012-06-25 21:37:02 +00001187/// Determine whether the following tokens are valid after a type-specifier
1188/// which could be a standalone declaration. This will conservatively return
1189/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +00001190bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +00001191 // This switch enumerates the valid "follow" set for type-specifiers.
1192 switch (Tok.getKind()) {
1193 default: break;
1194 case tok::semi: // struct foo {...} ;
1195 case tok::star: // struct foo {...} * P;
1196 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +00001197 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +00001198 case tok::identifier: // struct foo {...} V ;
1199 case tok::r_paren: //(struct foo {...} ) {4}
1200 case tok::annot_cxxscope: // struct foo {...} a:: b;
1201 case tok::annot_typename: // struct foo {...} a ::b;
1202 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1203 case tok::l_paren: // struct foo {...} ( x);
1204 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001205 case tok::kw_operator: // struct foo operator ++() {...}
Alp Tokerd3f79c52013-11-24 20:24:54 +00001206 case tok::kw___declspec: // struct foo {...} __declspec(...)
Richard Smith843f18f2014-08-13 02:13:15 +00001207 case tok::l_square: // void f(struct f [ 3])
1208 case tok::ellipsis: // void f(struct f ... [Ns])
Abramo Bagnara152eb392014-08-16 08:29:27 +00001209 // FIXME: we should emit semantic diagnostic when declaration
1210 // attribute is in type attribute position.
1211 case tok::kw___attribute: // struct foo __attribute__((used)) x;
David Majnemer15b311c2016-06-14 03:20:28 +00001212 case tok::annot_pragma_pack: // struct foo {...} _Pragma(pack(pop));
1213 // struct foo {...} _Pragma(section(...));
1214 case tok::annot_pragma_ms_pragma:
1215 // struct foo {...} _Pragma(vtordisp(pop));
1216 case tok::annot_pragma_ms_vtordisp:
1217 // struct foo {...} _Pragma(pointers_to_members(...));
1218 case tok::annot_pragma_ms_pointers_to_members:
Richard Smith369b9f92012-06-25 21:37:02 +00001219 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001220 case tok::colon:
1221 return CouldBeBitfield; // enum E { ... } : 2;
Reid Klecknercfa91552016-03-21 16:08:49 +00001222 // Microsoft compatibility
1223 case tok::kw___cdecl: // struct foo {...} __cdecl x;
1224 case tok::kw___fastcall: // struct foo {...} __fastcall x;
1225 case tok::kw___stdcall: // struct foo {...} __stdcall x;
1226 case tok::kw___thiscall: // struct foo {...} __thiscall x;
1227 case tok::kw___vectorcall: // struct foo {...} __vectorcall x;
1228 // We will diagnose these calling-convention specifiers on non-function
1229 // declarations later, so claim they are valid after a type specifier.
1230 return getLangOpts().MicrosoftExt;
Richard Smith369b9f92012-06-25 21:37:02 +00001231 // Type qualifiers
1232 case tok::kw_const: // struct foo {...} const x;
1233 case tok::kw_volatile: // struct foo {...} volatile x;
1234 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith843f18f2014-08-13 02:13:15 +00001235 case tok::kw__Atomic: // struct foo {...} _Atomic x;
Nico Rieck3e1ee832014-12-04 23:30:25 +00001236 case tok::kw___unaligned: // struct foo {...} __unaligned *x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001237 // Function specifiers
1238 // Note, no 'explicit'. An explicit function must be either a conversion
1239 // operator or a constructor. Either way, it can't have a return type.
1240 case tok::kw_inline: // struct foo inline f();
1241 case tok::kw_virtual: // struct foo virtual f();
1242 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001243 // Storage-class specifiers
1244 case tok::kw_static: // struct foo {...} static x;
1245 case tok::kw_extern: // struct foo {...} extern x;
1246 case tok::kw_typedef: // struct foo {...} typedef x;
1247 case tok::kw_register: // struct foo {...} register x;
1248 case tok::kw_auto: // struct foo {...} auto x;
1249 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001250 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001251 case tok::kw_constexpr: // struct foo {...} constexpr x;
1252 // As shown above, type qualifiers and storage class specifiers absolutely
1253 // can occur after class specifiers according to the grammar. However,
1254 // almost no one actually writes code like this. If we see one of these,
1255 // it is much more likely that someone missed a semi colon and the
1256 // type/storage class specifier we're seeing is part of the *next*
1257 // intended declaration, as in:
1258 //
1259 // struct foo { ... }
1260 // typedef int X;
1261 //
1262 // We'd really like to emit a missing semicolon error instead of emitting
1263 // an error on the 'int' saying that you can't have two type specifiers in
1264 // the same declaration of X. Because of this, we look ahead past this
1265 // token to see if it's a type specifier. If so, we know the code is
1266 // otherwise invalid, so we can produce the expected semi error.
1267 if (!isKnownToBeTypeSpecifier(NextToken()))
1268 return true;
1269 break;
1270 case tok::r_brace: // struct bar { struct foo {...} }
1271 // Missing ';' at end of struct is accepted as an extension in C mode.
1272 if (!getLangOpts().CPlusPlus)
1273 return true;
1274 break;
Richard Smith52c5b872013-01-29 04:13:32 +00001275 case tok::greater:
1276 // template<class T = class X>
1277 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001278 }
1279 return false;
1280}
1281
Douglas Gregor556877c2008-04-13 21:30:24 +00001282/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1283/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1284/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001285/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001286///
1287/// class-specifier: [C++ class]
1288/// class-head '{' member-specification[opt] '}'
1289/// class-head '{' member-specification[opt] '}' attributes[opt]
1290/// class-head:
1291/// class-key identifier[opt] base-clause[opt]
1292/// class-key nested-name-specifier identifier base-clause[opt]
1293/// class-key nested-name-specifier[opt] simple-template-id
1294/// base-clause[opt]
1295/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001296/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001297/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001298/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001299/// simple-template-id base-clause[opt]
1300/// class-key:
1301/// 'class'
1302/// 'struct'
1303/// 'union'
1304///
1305/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001306/// class-key ::[opt] nested-name-specifier[opt] identifier
1307/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1308/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001309///
1310/// Note that the C++ class-specifier and elaborated-type-specifier,
1311/// together, subsume the C99 struct-or-union-specifier:
1312///
1313/// struct-or-union-specifier: [C99 6.7.2.1]
1314/// struct-or-union identifier[opt] '{' struct-contents '}'
1315/// struct-or-union identifier
1316/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1317/// '}' attributes[opt]
1318/// [GNU] struct-or-union attributes[opt] identifier
1319/// struct-or-union:
1320/// 'struct'
1321/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001322void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1323 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001324 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001325 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001326 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001327 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001328 DeclSpec::TST TagType;
1329 if (TagTokKind == tok::kw_struct)
1330 TagType = DeclSpec::TST_struct;
1331 else if (TagTokKind == tok::kw___interface)
1332 TagType = DeclSpec::TST_interface;
1333 else if (TagTokKind == tok::kw_class)
1334 TagType = DeclSpec::TST_class;
1335 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001336 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1337 TagType = DeclSpec::TST_union;
1338 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001339
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001340 if (Tok.is(tok::code_completion)) {
1341 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001342 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001343 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001344 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001345
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001346 // C++03 [temp.explicit] 14.7.2/8:
1347 // The usual access checking rules do not apply to names used to specify
1348 // explicit instantiations.
1349 //
1350 // As an extension we do not perform access checking on the names used to
1351 // specify explicit specializations either. This is important to allow
1352 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001353 //
1354 // Note that we don't suppress if this turns out to be an elaborated
1355 // type specifier.
1356 bool shouldDelayDiagsInTag =
1357 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1358 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1359 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001360
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001361 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001362 // If attributes exist after tag, parse them.
Richard Smith37a45dd2013-10-24 01:21:09 +00001363 MaybeParseGNUAttributes(attrs);
Aaron Ballman068aa512015-05-20 20:58:33 +00001364 MaybeParseMicrosoftDeclSpecs(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001365
John McCall8d32c052012-05-22 21:28:12 +00001366 // Parse inheritance specifiers.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001367 if (Tok.isOneOf(tok::kw___single_inheritance,
1368 tok::kw___multiple_inheritance,
1369 tok::kw___virtual_inheritance))
Richard Smith37a45dd2013-10-24 01:21:09 +00001370 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCall8d32c052012-05-22 21:28:12 +00001371
Alexis Hunt96d5c762009-11-21 08:43:09 +00001372 // If C++0x attributes exist here, parse them.
1373 // FIXME: Are we consistent with the ordering of parsing of different
1374 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001375 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001376
Michael Han309af292013-01-07 16:57:11 +00001377 // Source location used by FIXIT to insert misplaced
1378 // C++11 attributes
1379 SourceLocation AttrFixitLoc = Tok.getLocation();
1380
Nico Weber7c3c5be2014-09-23 04:09:56 +00001381 if (TagType == DeclSpec::TST_struct &&
David Majnemer86330af2014-12-29 02:14:26 +00001382 Tok.isNot(tok::identifier) &&
1383 !Tok.isAnnotation() &&
Nico Weber7c3c5be2014-09-23 04:09:56 +00001384 Tok.getIdentifierInfo() &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001385 Tok.isOneOf(tok::kw___is_abstract,
1386 tok::kw___is_arithmetic,
1387 tok::kw___is_array,
David Majnemerb3d96882016-05-23 17:21:55 +00001388 tok::kw___is_assignable,
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001389 tok::kw___is_base_of,
1390 tok::kw___is_class,
1391 tok::kw___is_complete_type,
1392 tok::kw___is_compound,
1393 tok::kw___is_const,
1394 tok::kw___is_constructible,
1395 tok::kw___is_convertible,
1396 tok::kw___is_convertible_to,
1397 tok::kw___is_destructible,
1398 tok::kw___is_empty,
1399 tok::kw___is_enum,
1400 tok::kw___is_floating_point,
1401 tok::kw___is_final,
1402 tok::kw___is_function,
1403 tok::kw___is_fundamental,
1404 tok::kw___is_integral,
1405 tok::kw___is_interface_class,
1406 tok::kw___is_literal,
1407 tok::kw___is_lvalue_expr,
1408 tok::kw___is_lvalue_reference,
1409 tok::kw___is_member_function_pointer,
1410 tok::kw___is_member_object_pointer,
1411 tok::kw___is_member_pointer,
1412 tok::kw___is_nothrow_assignable,
1413 tok::kw___is_nothrow_constructible,
1414 tok::kw___is_nothrow_destructible,
1415 tok::kw___is_object,
1416 tok::kw___is_pod,
1417 tok::kw___is_pointer,
1418 tok::kw___is_polymorphic,
1419 tok::kw___is_reference,
1420 tok::kw___is_rvalue_expr,
1421 tok::kw___is_rvalue_reference,
1422 tok::kw___is_same,
1423 tok::kw___is_scalar,
1424 tok::kw___is_sealed,
1425 tok::kw___is_signed,
1426 tok::kw___is_standard_layout,
1427 tok::kw___is_trivial,
1428 tok::kw___is_trivially_assignable,
1429 tok::kw___is_trivially_constructible,
1430 tok::kw___is_trivially_copyable,
1431 tok::kw___is_union,
1432 tok::kw___is_unsigned,
1433 tok::kw___is_void,
1434 tok::kw___is_volatile))
Nico Weber7c3c5be2014-09-23 04:09:56 +00001435 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
1436 // name of struct templates, but some are keywords in GCC >= 4.3
1437 // and Clang. Therefore, when we see the token sequence "struct
1438 // X", make X into a normal identifier rather than a keyword, to
1439 // allow libstdc++ 4.2 and libc++ to work properly.
1440 TryKeywordIdentFallback(true);
Mike Stump11289f42009-09-09 15:08:12 +00001441
David Majnemer51fd8a02015-07-22 23:46:18 +00001442 struct PreserveAtomicIdentifierInfoRAII {
1443 PreserveAtomicIdentifierInfoRAII(Token &Tok, bool Enabled)
1444 : AtomicII(nullptr) {
1445 if (!Enabled)
1446 return;
1447 assert(Tok.is(tok::kw__Atomic));
1448 AtomicII = Tok.getIdentifierInfo();
1449 AtomicII->revertTokenIDToIdentifier();
1450 Tok.setKind(tok::identifier);
1451 }
1452 ~PreserveAtomicIdentifierInfoRAII() {
1453 if (!AtomicII)
1454 return;
1455 AtomicII->revertIdentifierToTokenID(tok::kw__Atomic);
1456 }
1457 IdentifierInfo *AtomicII;
1458 };
1459
1460 // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
1461 // implementation for VS2013 uses _Atomic as an identifier for one of the
1462 // classes in <atomic>. When we are parsing 'struct _Atomic', don't consider
1463 // '_Atomic' to be a keyword. We are careful to undo this so that clang can
1464 // use '_Atomic' in its own header files.
1465 bool ShouldChangeAtomicToIdentifier = getLangOpts().MSVCCompat &&
1466 Tok.is(tok::kw__Atomic) &&
1467 TagType == DeclSpec::TST_struct;
1468 PreserveAtomicIdentifierInfoRAII AtomicTokenGuard(
1469 Tok, ShouldChangeAtomicToIdentifier);
1470
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001471 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001472 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001473 if (getLangOpts().CPlusPlus) {
Serge Pavlov458ea762014-07-16 05:16:52 +00001474 // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it
1475 // is a base-specifier-list.
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001476 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001477
Nico Webercfaa4cd2015-02-15 07:26:13 +00001478 CXXScopeSpec Spec;
1479 bool HasValidSpec = true;
David Blaikieefdccaa2016-01-15 23:43:34 +00001480 if (ParseOptionalCXXScopeSpecifier(Spec, nullptr, EnteringContext)) {
John McCall413021a2010-07-30 06:26:29 +00001481 DS.SetTypeSpecError();
Nico Webercfaa4cd2015-02-15 07:26:13 +00001482 HasValidSpec = false;
1483 }
1484 if (Spec.isSet())
1485 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) {
Alp Tokerec543272013-12-24 09:48:30 +00001486 Diag(Tok, diag::err_expected) << tok::identifier;
Nico Webercfaa4cd2015-02-15 07:26:13 +00001487 HasValidSpec = false;
1488 }
1489 if (HasValidSpec)
1490 SS = Spec;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001491 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001492
Douglas Gregor916462b2009-10-30 21:46:58 +00001493 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1494
Douglas Gregor67a65642009-02-17 23:15:12 +00001495 // Parse the (optional) class name or simple-template-id.
Craig Topper161e4db2014-05-21 06:02:52 +00001496 IdentifierInfo *Name = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001497 SourceLocation NameLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001498 TemplateIdAnnotation *TemplateId = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001499 if (Tok.is(tok::identifier)) {
1500 Name = Tok.getIdentifierInfo();
1501 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001502
David Blaikiebbafb8a2012-03-11 07:00:24 +00001503 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001504 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001505 // Eat the template argument list and try to continue parsing this as
1506 // a class (or template thereof).
1507 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001508 SourceLocation LAngleLoc, RAngleLoc;
David Blaikiee20506d2016-01-15 23:43:28 +00001509 if (ParseTemplateIdAfterTemplateName(
1510 nullptr, NameLoc, SS, true, LAngleLoc, TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001511 // We couldn't parse the template argument list at all, so don't
1512 // try to give any location information for the list.
1513 LAngleLoc = RAngleLoc = SourceLocation();
1514 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001515
Douglas Gregor916462b2009-10-30 21:46:58 +00001516 Diag(NameLoc, diag::err_explicit_spec_non_template)
Alp Toker01d65e12014-01-06 12:54:41 +00001517 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1518 << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
Joao Matose9a3ed42012-08-31 22:18:20 +00001519
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001520 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001521 // we've removed its template argument list.
1522 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
Hubert Tong97b06632016-04-13 18:41:03 +00001523 if (TemplateParams->size() > 1) {
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001524 TemplateParams->pop_back();
1525 } else {
Craig Topper161e4db2014-05-21 06:02:52 +00001526 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001527 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001528 = ParsedTemplateInfo::NonTemplate;
1529 }
1530 } else if (TemplateInfo.Kind
1531 == ParsedTemplateInfo::ExplicitInstantiation) {
1532 // Pretend this is just a forward declaration.
Craig Topper161e4db2014-05-21 06:02:52 +00001533 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001534 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001535 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001536 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001537 = SourceLocation();
1538 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1539 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001540 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001541 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001542 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001543 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001544 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001545
Douglas Gregore7c20652011-03-02 00:47:37 +00001546 if (TemplateId->Kind != TNK_Type_template &&
1547 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001548 // The template-name in the simple-template-id refers to
1549 // something other than a class template. Give an appropriate
1550 // error message and skip to the ';'.
1551 SourceRange Range(NameLoc);
1552 if (SS.isNotEmpty())
1553 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001554
Richard Smith72bfbd82013-12-04 00:28:23 +00001555 // FIXME: Name may be null here.
Douglas Gregor7f741122009-02-25 19:37:18 +00001556 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001557 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001558
Douglas Gregor7f741122009-02-25 19:37:18 +00001559 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001560 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor7f741122009-02-25 19:37:18 +00001561 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001562 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001563 }
1564
Richard Smithbfdb1082012-03-12 08:56:40 +00001565 // There are four options here.
1566 // - If we are in a trailing return type, this is always just a reference,
1567 // and we must not try to parse a definition. For instance,
1568 // [] () -> struct S { };
1569 // does not define a type.
1570 // - If we have 'struct foo {...', 'struct foo :...',
1571 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1572 // - If we have 'struct foo;', then this is either a forward declaration
1573 // or a friend declaration, which have to be treated differently.
1574 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001575 //
1576 // We also detect these erroneous cases to provide better diagnostic for
1577 // C++11 attributes parsing.
1578 // - attributes follow class name:
1579 // struct foo [[]] {};
1580 // - attributes appear before or after 'final':
1581 // struct foo [[]] final [[]] {};
1582 //
Richard Smithc5b05522012-03-12 07:56:15 +00001583 // However, in type-specifier-seq's, things look like declarations but are
1584 // just references, e.g.
1585 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001586 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001587 // &T::operator struct s;
Richard Smith649c7b062014-01-08 00:56:48 +00001588 // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
Michael Han9407e502012-11-26 22:54:45 +00001589
1590 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001591 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001592
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001593 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
John McCallfaf5fb42010-08-26 23:41:50 +00001594 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001595 if (DSC == DSC_trailing)
1596 TUK = Sema::TUK_Reference;
1597 else if (Tok.is(tok::l_brace) ||
1598 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001599 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001600 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001601 if (DS.isFriendSpecified()) {
1602 // C++ [class.friend]p2:
1603 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001604 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001605 << SourceRange(DS.getFriendSpecLoc());
1606
1607 // Skip everything up to the semicolon, so that this looks like a proper
1608 // friend class (or template thereof) declaration.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001609 SkipUntil(tok::semi, StopBeforeMatch);
John McCallfaf5fb42010-08-26 23:41:50 +00001610 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001611 } else {
1612 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001613 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001614 }
Richard Smith434516c2013-02-22 06:46:23 +00001615 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1616 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001617 // We can't tell if this is a definition or reference
1618 // until we skipped the 'final' and C++11 attribute specifiers.
1619 TentativeParsingAction PA(*this);
1620
1621 // Skip the 'final' keyword.
1622 ConsumeToken();
1623
1624 // Skip C++11 attribute specifiers.
1625 while (true) {
1626 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1627 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001628 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001629 break;
Richard Smith434516c2013-02-22 06:46:23 +00001630 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001631 ConsumeToken();
1632 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001633 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001634 break;
1635 } else {
1636 break;
1637 }
1638 }
1639
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001640 if (Tok.isOneOf(tok::l_brace, tok::colon))
Michael Han9407e502012-11-26 22:54:45 +00001641 TUK = Sema::TUK_Definition;
1642 else
1643 TUK = Sema::TUK_Reference;
1644
1645 PA.Revert();
Richard Smith649c7b062014-01-08 00:56:48 +00001646 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00001647 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001648 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001649 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001650 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001651 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Joao Matose9a3ed42012-08-31 22:18:20 +00001652 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00001653 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001654 DeclSpec::getSpecifierName(TagType, PPol));
Joao Matose9a3ed42012-08-31 22:18:20 +00001655 PP.EnterToken(Tok);
1656 Tok.setKind(tok::semi);
1657 }
Richard Smith369b9f92012-06-25 21:37:02 +00001658 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001659 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001660
Michael Han9407e502012-11-26 22:54:45 +00001661 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1662 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001663 if (TUK != Sema::TUK_Reference) {
1664 // If this is not a reference, then the only possible
1665 // valid place for C++11 attributes to appear here
1666 // is between class-key and class-name. If there are
1667 // any attributes after class-name, we try a fixit to move
1668 // them to the right place.
1669 SourceRange AttrRange = Attributes.Range;
1670 if (AttrRange.isValid()) {
1671 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1672 << AttrRange
1673 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1674 CharSourceRange(AttrRange, true))
1675 << FixItHint::CreateRemoval(AttrRange);
1676
1677 // Recover by adding misplaced attributes to the attribute list
1678 // of the class so they can be applied on the class later.
1679 attrs.takeAllFrom(Attributes);
1680 }
1681 }
Michael Han9407e502012-11-26 22:54:45 +00001682
John McCall6347b682012-05-07 06:16:58 +00001683 // If this is an elaborated type specifier, and we delayed
1684 // diagnostics before, just merge them into the current pool.
1685 if (shouldDelayDiagsInTag) {
1686 diagsFromTag.done();
1687 if (TUK == Sema::TUK_Reference)
1688 diagsFromTag.redelay();
1689 }
1690
John McCall413021a2010-07-30 06:26:29 +00001691 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001692 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001693 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1694 // We have a declaration or reference to an anonymous class.
1695 Diag(StartLoc, diag::err_anon_type_definition)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001696 << DeclSpec::getSpecifierName(TagType, Policy);
John McCall413021a2010-07-30 06:26:29 +00001697 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001698
David Majnemer3252fd02013-12-05 01:36:53 +00001699 // If we are parsing a definition and stop at a base-clause, continue on
1700 // until the semicolon. Continuing from the comma will just trick us into
1701 // thinking we are seeing a variable declaration.
1702 if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1703 SkipUntil(tok::semi, StopBeforeMatch);
1704 else
1705 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor556877c2008-04-13 21:30:24 +00001706 return;
1707 }
1708
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001709 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001710 DeclResult TagOrTempResult = true; // invalid
1711 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001712
Douglas Gregord6ab8742009-05-28 23:31:59 +00001713 bool Owned = false;
Richard Smithd9ba2242015-05-07 03:54:19 +00001714 Sema::SkipBodyInfo SkipBody;
John McCall06f6fe8d2009-09-04 01:14:41 +00001715 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001716 // Explicit specialization, class template partial specialization,
1717 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001718 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001719 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001720 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001721 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001722 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001723 ProhibitAttributes(attrs);
1724
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001725 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001726 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001727 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001728 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001729 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001730 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001731 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001732 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001733 TemplateId->TemplateNameLoc,
1734 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001735 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001736 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001737 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001738
1739 // Friend template-ids are treated as references unless
1740 // they have template headers, in which case they're ill-formed
1741 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1742 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001743 } else if (TUK == Sema::TUK_Reference ||
1744 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001745 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001746 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001747 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001748 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001749 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001750 TemplateId->Template,
1751 TemplateId->TemplateNameLoc,
1752 TemplateId->LAngleLoc,
1753 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001754 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001755 } else {
1756 // This is an explicit specialization or a class template
1757 // partial specialization.
1758 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001759 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1760 // This looks like an explicit instantiation, because we have
1761 // something like
1762 //
1763 // template class Foo<X>
1764 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001765 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001766 // meant to be an explicit specialization, but the user forgot
1767 // the '<>' after 'template'.
Richard Smith003c5e12013-11-08 19:03:29 +00001768 // It this is friend declaration however, since it cannot have a
1769 // template header, it is most likely that the user meant to
1770 // remove the 'template' keyword.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001771 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith003c5e12013-11-08 19:03:29 +00001772 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001773
Richard Smith003c5e12013-11-08 19:03:29 +00001774 if (TUK == Sema::TUK_Friend) {
1775 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
Craig Topper161e4db2014-05-21 06:02:52 +00001776 TemplateParams = nullptr;
Richard Smith003c5e12013-11-08 19:03:29 +00001777 } else {
1778 SourceLocation LAngleLoc =
1779 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1780 Diag(TemplateId->TemplateNameLoc,
1781 diag::err_explicit_instantiation_with_definition)
1782 << SourceRange(TemplateInfo.TemplateLoc)
1783 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1784
1785 // Create a fake template parameter list that contains only
1786 // "template<>", so that we treat this construct as a class
1787 // template specialization.
1788 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
Craig Topper96225a52015-12-24 23:58:25 +00001789 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None,
Hubert Tongf608c052016-04-29 18:05:37 +00001790 LAngleLoc, nullptr));
Richard Smith003c5e12013-11-08 19:03:29 +00001791 TemplateParams = &FakedParamLists;
1792 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001793 }
1794
1795 // Build the class template specialization.
Richard Smith4b55a9c2014-04-17 03:29:33 +00001796 TagOrTempResult = Actions.ActOnClassTemplateSpecialization(
1797 getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(),
1798 *TemplateId, attrs.getList(),
Craig Topper161e4db2014-05-21 06:02:52 +00001799 MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0]
1800 : nullptr,
Richard Smithc7e6ff02015-05-18 20:36:47 +00001801 TemplateParams ? TemplateParams->size() : 0),
1802 &SkipBody);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001803 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001804 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001805 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001806 // Explicit instantiation of a member of a class template
1807 // specialization, e.g.,
1808 //
1809 // template struct Outer<int>::Inner;
1810 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001811 ProhibitAttributes(attrs);
1812
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001813 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001814 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001815 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001816 TemplateInfo.TemplateLoc,
1817 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001818 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001819 } else if (TUK == Sema::TUK_Friend &&
1820 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001821 ProhibitAttributes(attrs);
1822
John McCallace48cd2010-10-19 01:40:49 +00001823 TagOrTempResult =
1824 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1825 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001826 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001827 MultiTemplateParamsArg(
Craig Topper161e4db2014-05-21 06:02:52 +00001828 TemplateParams? &(*TemplateParams)[0]
1829 : nullptr,
John McCallace48cd2010-10-19 01:40:49 +00001830 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001831 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001832 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1833 ProhibitAttributes(attrs);
Richard Smith003c5e12013-11-08 19:03:29 +00001834
Larisse Voufo725de3e2013-06-21 00:08:46 +00001835 if (TUK == Sema::TUK_Definition &&
1836 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1837 // If the declarator-id is not a template-id, issue a diagnostic and
1838 // recover by ignoring the 'template' keyword.
1839 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1840 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Craig Topper161e4db2014-05-21 06:02:52 +00001841 TemplateParams = nullptr;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001842 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001843
John McCall7f41d982009-09-11 04:59:25 +00001844 bool IsDependent = false;
1845
John McCall32723e92010-10-19 18:40:57 +00001846 // Don't pass down template parameter lists if this is just a tag
1847 // reference. For example, we don't need the template parameters here:
1848 // template <class T> class A *makeA(T t);
1849 MultiTemplateParamsArg TParams;
1850 if (TUK != Sema::TUK_Reference && TemplateParams)
1851 TParams =
1852 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1853
Nico Weber32a0fc72016-09-03 03:01:32 +00001854 stripTypeAttributesOffDeclSpec(attrs, DS, TUK);
David Majnemer936b4112015-04-19 07:53:29 +00001855
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001856 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001857 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001858 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001859 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001860 TParams, Owned, IsDependent,
1861 SourceLocation(), false,
Richard Smith649c7b062014-01-08 00:56:48 +00001862 clang::TypeResult(),
Richard Smith65ebb4a2015-03-26 04:09:53 +00001863 DSC == DSC_type_specifier,
1864 &SkipBody);
John McCall7f41d982009-09-11 04:59:25 +00001865
1866 // If ActOnTag said the type was dependent, try again with the
1867 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001868 if (IsDependent) {
1869 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001870 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001871 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001872 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001873 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001874
Douglas Gregor556877c2008-04-13 21:30:24 +00001875 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001876 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001877 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001878 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001879 isCXX11FinalKeyword());
Richard Smithd9ba2242015-05-07 03:54:19 +00001880 if (SkipBody.ShouldSkip)
Richard Smith65ebb4a2015-03-26 04:09:53 +00001881 SkipCXXMemberSpecification(StartLoc, AttrFixitLoc, TagType,
1882 TagOrTempResult.get());
1883 else if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001884 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1885 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001886 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001887 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001888 }
1889
Erich Keane2fe684b2017-02-28 20:44:39 +00001890 if (!TagOrTempResult.isInvalid())
1891 // Delayed proccessing of attributes.
1892 Actions.ProcessDeclAttributeDelayed(TagOrTempResult.get(), attrs.getList());
1893
Craig Topper161e4db2014-05-21 06:02:52 +00001894 const char *PrevSpec = nullptr;
John McCallba7bf592010-08-24 05:47:05 +00001895 unsigned DiagID;
1896 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001897 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001898 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1899 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001900 PrevSpec, DiagID, TypeResult.get(), Policy);
John McCall7f41d982009-09-11 04:59:25 +00001901 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001902 Result = DS.SetTypeSpecType(TagType, StartLoc,
1903 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001904 PrevSpec, DiagID, TagOrTempResult.get(), Owned,
1905 Policy);
John McCall7f41d982009-09-11 04:59:25 +00001906 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001907 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001908 return;
1909 }
Mike Stump11289f42009-09-09 15:08:12 +00001910
John McCallba7bf592010-08-24 05:47:05 +00001911 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001912 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001913
Chris Lattnercf251412010-02-02 01:23:29 +00001914 // At this point, we've successfully parsed a class-specifier in 'definition'
1915 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1916 // going to look at what comes after it to improve error recovery. If an
1917 // impossible token occurs next, we assume that the programmer forgot a ; at
1918 // the end of the declaration and recover that way.
1919 //
Richard Smith369b9f92012-06-25 21:37:02 +00001920 // Also enforce C++ [temp]p3:
1921 // In a template-declaration which defines a class, no declarator
1922 // is permitted.
Richard Smith843f18f2014-08-13 02:13:15 +00001923 //
1924 // After a type-specifier, we don't expect a semicolon. This only happens in
1925 // C, since definitions are not permitted in this context in C++.
Joao Matose9a3ed42012-08-31 22:18:20 +00001926 if (TUK == Sema::TUK_Definition &&
Richard Smith843f18f2014-08-13 02:13:15 +00001927 (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) &&
Joao Matose9a3ed42012-08-31 22:18:20 +00001928 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001929 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001930 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Alp Toker383d2c42014-01-01 03:08:43 +00001931 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001932 DeclSpec::getSpecifierName(TagType, PPol));
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001933 // Push this token back into the preprocessor and change our current token
1934 // to ';' so that the rest of the code recovers as though there were an
1935 // ';' after the definition.
1936 PP.EnterToken(Tok);
1937 Tok.setKind(tok::semi);
1938 }
Chris Lattnercf251412010-02-02 01:23:29 +00001939 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001940}
1941
Mike Stump11289f42009-09-09 15:08:12 +00001942/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001943///
1944/// base-clause : [C++ class.derived]
1945/// ':' base-specifier-list
1946/// base-specifier-list:
1947/// base-specifier '...'[opt]
1948/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001949void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001950 assert(Tok.is(tok::colon) && "Not a base clause");
1951 ConsumeToken();
1952
Douglas Gregor29a92472008-10-22 17:49:05 +00001953 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001954 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001955
Douglas Gregor556877c2008-04-13 21:30:24 +00001956 while (true) {
1957 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001958 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001959 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001960 // Skip the rest of this base specifier, up until the comma or
1961 // opening brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001962 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor29a92472008-10-22 17:49:05 +00001963 } else {
1964 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001965 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001966 }
1967
1968 // If the next token is a comma, consume it and keep reading
1969 // base-specifiers.
Alp Toker97650562014-01-10 11:19:30 +00001970 if (!TryConsumeToken(tok::comma))
1971 break;
Douglas Gregor556877c2008-04-13 21:30:24 +00001972 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001973
1974 // Attach the base specifiers
Craig Topperaa700cb2015-12-27 21:55:19 +00001975 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo);
Douglas Gregor556877c2008-04-13 21:30:24 +00001976}
1977
1978/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1979/// one entry in the base class list of a class specifier, for example:
1980/// class foo : public bar, virtual private baz {
1981/// 'public bar' and 'virtual private baz' are each base-specifiers.
1982///
1983/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001984/// attribute-specifier-seq[opt] base-type-specifier
1985/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1986/// base-type-specifier
1987/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1988/// base-type-specifier
Craig Topper9ad7e262014-10-31 06:57:07 +00001989BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001990 bool IsVirtual = false;
1991 SourceLocation StartLoc = Tok.getLocation();
1992
Richard Smith4c96e992013-02-19 23:47:15 +00001993 ParsedAttributesWithRange Attributes(AttrFactory);
1994 MaybeParseCXX11Attributes(Attributes);
1995
Douglas Gregor556877c2008-04-13 21:30:24 +00001996 // Parse the 'virtual' keyword.
Alp Toker97650562014-01-10 11:19:30 +00001997 if (TryConsumeToken(tok::kw_virtual))
Douglas Gregor556877c2008-04-13 21:30:24 +00001998 IsVirtual = true;
Douglas Gregor556877c2008-04-13 21:30:24 +00001999
Richard Smith4c96e992013-02-19 23:47:15 +00002000 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
2001
Douglas Gregor556877c2008-04-13 21:30:24 +00002002 // Parse an (optional) access specifier.
2003 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00002004 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00002005 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002006
Richard Smith4c96e992013-02-19 23:47:15 +00002007 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
2008
Douglas Gregor556877c2008-04-13 21:30:24 +00002009 // Parse the 'virtual' keyword (again!), in case it came after the
2010 // access specifier.
2011 if (Tok.is(tok::kw_virtual)) {
2012 SourceLocation VirtualLoc = ConsumeToken();
2013 if (IsVirtual) {
2014 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00002015 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00002016 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00002017 }
2018
2019 IsVirtual = true;
2020 }
2021
Richard Smith4c96e992013-02-19 23:47:15 +00002022 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
2023
Douglas Gregor831c93f2008-11-05 20:51:48 +00002024 // Parse the class-name.
David Majnemer51fd8a02015-07-22 23:46:18 +00002025
2026 // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
2027 // implementation for VS2013 uses _Atomic as an identifier for one of the
2028 // classes in <atomic>. Treat '_Atomic' to be an identifier when we are
2029 // parsing the class-name for a base specifier.
2030 if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) &&
2031 NextToken().is(tok::less))
2032 Tok.setKind(tok::identifier);
2033
Douglas Gregord54dfb82009-02-25 23:52:28 +00002034 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00002035 SourceLocation BaseLoc;
2036 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002037 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00002038 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002039
Douglas Gregor752a5952011-01-03 22:36:02 +00002040 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
2041 // actually part of the base-specifier-list grammar productions, but we
2042 // parse it here for convenience.
2043 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00002044 TryConsumeToken(tok::ellipsis, EllipsisLoc);
2045
Mike Stump11289f42009-09-09 15:08:12 +00002046 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00002047 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00002048
Douglas Gregor556877c2008-04-13 21:30:24 +00002049 // Notify semantic analysis that we have parsed a complete
2050 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00002051 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
2052 Access, BaseType.get(), BaseLoc,
2053 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00002054}
2055
2056/// getAccessSpecifierIfPresent - Determine whether the next token is
2057/// a C++ access-specifier.
2058///
2059/// access-specifier: [C++ class.derived]
2060/// 'private'
2061/// 'protected'
2062/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00002063AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00002064 switch (Tok.getKind()) {
2065 default: return AS_none;
2066 case tok::kw_private: return AS_private;
2067 case tok::kw_protected: return AS_protected;
2068 case tok::kw_public: return AS_public;
2069 }
2070}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002071
Douglas Gregor433e0532012-04-16 18:27:27 +00002072/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith0b3a4622014-11-13 20:01:57 +00002073/// delayed, e.g., default arguments or an exception-specification, create a
2074/// late-parsed method declaration record to handle the parsing at the end of
2075/// the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00002076void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
2077 Decl *ThisDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002078 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00002079 = DeclaratorInfo.getFunctionTypeInfo();
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002080 // If there was a late-parsed exception-specification, we'll need a
2081 // late parse
2082 bool NeedLateParse = FTI.getExceptionSpecType() == EST_Unparsed;
Douglas Gregor433e0532012-04-16 18:27:27 +00002083
Nathan Sidwell5bb231c2015-02-19 14:03:22 +00002084 if (!NeedLateParse) {
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002085 // Look ahead to see if there are any default args
Nathan Sidwell5bb231c2015-02-19 14:03:22 +00002086 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
2087 auto Param = cast<ParmVarDecl>(FTI.Params[ParamIdx].Param);
2088 if (Param->hasUnparsedDefaultArg()) {
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002089 NeedLateParse = true;
2090 break;
2091 }
Nathan Sidwell5bb231c2015-02-19 14:03:22 +00002092 }
2093 }
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002094
2095 if (NeedLateParse) {
Richard Smith0b3a4622014-11-13 20:01:57 +00002096 // Push this method onto the stack of late-parsed method
2097 // declarations.
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002098 auto LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
Richard Smith0b3a4622014-11-13 20:01:57 +00002099 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
2100 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
2101
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002102 // Stash the exception-specification tokens in the late-pased method.
Richard Smith0b3a4622014-11-13 20:01:57 +00002103 LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens;
Hans Wennborgdcfba332015-10-06 23:40:43 +00002104 FTI.ExceptionSpecTokens = nullptr;
Richard Smith0b3a4622014-11-13 20:01:57 +00002105
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002106 // Push tokens for each parameter. Those that do not have
2107 // defaults will be NULL.
Richard Smith0b3a4622014-11-13 20:01:57 +00002108 LateMethod->DefaultArgs.reserve(FTI.NumParams);
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00002109 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx)
Alp Tokerc5350722014-02-26 22:27:52 +00002110 LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
Malcolm Parsonsca9d8342016-11-17 21:00:09 +00002111 FTI.Params[ParamIdx].Param,
2112 std::move(FTI.Params[ParamIdx].DefaultArgTokens)));
Eli Friedman3af2a772009-07-22 21:45:50 +00002113 }
2114}
2115
Richard Smith89645bc2013-01-02 12:01:23 +00002116/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002117/// virt-specifier.
2118///
2119/// virt-specifier:
2120/// override
2121/// final
Andrey Bokhanko276055b2016-07-29 10:42:48 +00002122/// __final
Richard Smith89645bc2013-01-02 12:01:23 +00002123VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002124 if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002125 return VirtSpecifiers::VS_None;
2126
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002127 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002128
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002129 // Initialize the contextual keywords.
2130 if (!Ident_final) {
2131 Ident_final = &PP.getIdentifierTable().get("final");
Andrey Bokhanko276055b2016-07-29 10:42:48 +00002132 if (getLangOpts().GNUKeywords)
2133 Ident_GNU_final = &PP.getIdentifierTable().get("__final");
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002134 if (getLangOpts().MicrosoftExt)
2135 Ident_sealed = &PP.getIdentifierTable().get("sealed");
2136 Ident_override = &PP.getIdentifierTable().get("override");
Anders Carlsson56104902011-01-17 03:05:47 +00002137 }
2138
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002139 if (II == Ident_override)
2140 return VirtSpecifiers::VS_Override;
2141
2142 if (II == Ident_sealed)
2143 return VirtSpecifiers::VS_Sealed;
2144
2145 if (II == Ident_final)
2146 return VirtSpecifiers::VS_Final;
2147
Andrey Bokhanko276055b2016-07-29 10:42:48 +00002148 if (II == Ident_GNU_final)
2149 return VirtSpecifiers::VS_GNU_Final;
2150
Anders Carlsson56104902011-01-17 03:05:47 +00002151 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002152}
2153
Richard Smith89645bc2013-01-02 12:01:23 +00002154/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002155///
2156/// virt-specifier-seq:
2157/// virt-specifier
2158/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00002159void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
Richard Smith3d1a94c2014-08-12 00:22:39 +00002160 bool IsInterface,
2161 SourceLocation FriendLoc) {
Anders Carlsson56104902011-01-17 03:05:47 +00002162 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00002163 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00002164 if (Specifier == VirtSpecifiers::VS_None)
2165 return;
2166
Richard Smith3d1a94c2014-08-12 00:22:39 +00002167 if (FriendLoc.isValid()) {
2168 Diag(Tok.getLocation(), diag::err_friend_decl_spec)
2169 << VirtSpecifiers::getSpecifierName(Specifier)
2170 << FixItHint::CreateRemoval(Tok.getLocation())
2171 << SourceRange(FriendLoc, FriendLoc);
2172 ConsumeToken();
2173 continue;
2174 }
2175
Anders Carlsson56104902011-01-17 03:05:47 +00002176 // C++ [class.mem]p8:
2177 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Craig Topper161e4db2014-05-21 06:02:52 +00002178 const char *PrevSpec = nullptr;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00002179 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00002180 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
2181 << PrevSpec
2182 << FixItHint::CreateRemoval(Tok.getLocation());
2183
David Majnemera5433082013-10-18 00:33:31 +00002184 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
2185 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalldb632ac2012-09-25 07:32:39 +00002186 Diag(Tok.getLocation(), diag::err_override_control_interface)
2187 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemera5433082013-10-18 00:33:31 +00002188 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
2189 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
Andrey Bokhanko276055b2016-07-29 10:42:48 +00002190 } else if (Specifier == VirtSpecifiers::VS_GNU_Final) {
2191 Diag(Tok.getLocation(), diag::ext_warn_gnu_final);
John McCalldb632ac2012-09-25 07:32:39 +00002192 } else {
David Majnemera5433082013-10-18 00:33:31 +00002193 Diag(Tok.getLocation(),
2194 getLangOpts().CPlusPlus11
2195 ? diag::warn_cxx98_compat_override_control_keyword
2196 : diag::ext_override_control_keyword)
2197 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalldb632ac2012-09-25 07:32:39 +00002198 }
Anders Carlsson56104902011-01-17 03:05:47 +00002199 ConsumeToken();
2200 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002201}
2202
Richard Smith89645bc2013-01-02 12:01:23 +00002203/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002204/// 'final' or Microsoft 'sealed' contextual keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00002205bool Parser::isCXX11FinalKeyword() const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002206 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
2207 return Specifier == VirtSpecifiers::VS_Final ||
Andrey Bokhanko276055b2016-07-29 10:42:48 +00002208 Specifier == VirtSpecifiers::VS_GNU_Final ||
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002209 Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002210}
2211
Richard Smith72553fc2014-01-23 23:53:27 +00002212/// \brief Parse a C++ member-declarator up to, but not including, the optional
2213/// brace-or-equal-initializer or pure-specifier.
Nico Weberd89e6f72015-01-16 19:34:13 +00002214bool Parser::ParseCXXMemberDeclaratorBeforeInitializer(
Richard Smith72553fc2014-01-23 23:53:27 +00002215 Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
2216 LateParsedAttrList &LateParsedAttrs) {
2217 // member-declarator:
2218 // declarator pure-specifier[opt]
2219 // declarator brace-or-equal-initializer[opt]
2220 // identifier[opt] ':' constant-expression
Serge Pavlov458ea762014-07-16 05:16:52 +00002221 if (Tok.isNot(tok::colon))
Richard Smith72553fc2014-01-23 23:53:27 +00002222 ParseDeclarator(DeclaratorInfo);
Richard Smith3d1a94c2014-08-12 00:22:39 +00002223 else
2224 DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation());
Richard Smith72553fc2014-01-23 23:53:27 +00002225
2226 if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002227 assert(DeclaratorInfo.isPastIdentifier() &&
2228 "don't know where identifier would go yet?");
Richard Smith72553fc2014-01-23 23:53:27 +00002229 BitfieldSize = ParseConstantExpression();
2230 if (BitfieldSize.isInvalid())
2231 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002232 } else {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002233 ParseOptionalCXX11VirtSpecifierSeq(
2234 VS, getCurrentClass().IsInterface,
2235 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002236 if (!VS.isUnset())
2237 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
2238 }
Richard Smith72553fc2014-01-23 23:53:27 +00002239
2240 // If a simple-asm-expr is present, parse it.
2241 if (Tok.is(tok::kw_asm)) {
2242 SourceLocation Loc;
2243 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
2244 if (AsmLabel.isInvalid())
2245 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2246
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002247 DeclaratorInfo.setAsmLabel(AsmLabel.get());
Richard Smith72553fc2014-01-23 23:53:27 +00002248 DeclaratorInfo.SetRangeEnd(Loc);
2249 }
2250
2251 // If attributes exist after the declarator, but before an '{', parse them.
2252 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Richard Smith4b5a9492014-01-24 22:34:35 +00002253
2254 // For compatibility with code written to older Clang, also accept a
2255 // virt-specifier *after* the GNU attributes.
Aaron Ballman5d153e32014-08-04 17:03:51 +00002256 if (BitfieldSize.isUnset() && VS.isUnset()) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002257 ParseOptionalCXX11VirtSpecifierSeq(
2258 VS, getCurrentClass().IsInterface,
2259 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Aaron Ballman5d153e32014-08-04 17:03:51 +00002260 if (!VS.isUnset()) {
2261 // If we saw any GNU-style attributes that are known to GCC followed by a
2262 // virt-specifier, issue a GCC-compat warning.
2263 const AttributeList *Attr = DeclaratorInfo.getAttributes();
2264 while (Attr) {
2265 if (Attr->isKnownToGCC() && !Attr->isCXX11Attribute())
2266 Diag(Attr->getLoc(), diag::warn_gcc_attribute_location);
2267 Attr = Attr->getNext();
2268 }
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002269 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
Aaron Ballman5d153e32014-08-04 17:03:51 +00002270 }
2271 }
Nico Weberd89e6f72015-01-16 19:34:13 +00002272
2273 // If this has neither a name nor a bit width, something has gone seriously
2274 // wrong. Skip until the semi-colon or }.
2275 if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
2276 // If so, skip until the semi-colon or a }.
2277 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2278 return true;
2279 }
2280 return false;
Richard Smith72553fc2014-01-23 23:53:27 +00002281}
2282
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002283/// \brief Look for declaration specifiers possibly occurring after C++11
2284/// virt-specifier-seq and diagnose them.
2285void Parser::MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(
2286 Declarator &D,
2287 VirtSpecifiers &VS) {
2288 DeclSpec DS(AttrFactory);
2289
2290 // GNU-style and C++11 attributes are not allowed here, but they will be
2291 // handled by the caller. Diagnose everything else.
Alex Lorenz8f4d3992017-02-13 23:19:40 +00002292 ParseTypeQualifierListOpt(
2293 DS, AR_NoAttributesParsed, false,
2294 /*IdentifierRequired=*/false, llvm::function_ref<void()>([&]() {
2295 Actions.CodeCompleteFunctionQualifiers(DS, D, &VS);
2296 }));
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002297 D.ExtendWithDeclSpec(DS);
2298
2299 if (D.isFunctionDeclarator()) {
Ehsan Akhgaric07d1e22015-03-25 00:53:33 +00002300 auto &Function = D.getFunctionTypeInfo();
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002301 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2302 auto DeclSpecCheck = [&] (DeclSpec::TQ TypeQual,
2303 const char *FixItName,
2304 SourceLocation SpecLoc,
2305 unsigned* QualifierLoc) {
2306 FixItHint Insertion;
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002307 if (DS.getTypeQualifiers() & TypeQual) {
2308 if (!(Function.TypeQuals & TypeQual)) {
2309 std::string Name(FixItName);
2310 Name += " ";
Malcolm Parsonsf76f6502016-11-02 10:39:27 +00002311 Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name);
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002312 Function.TypeQuals |= TypeQual;
2313 *QualifierLoc = SpecLoc.getRawEncoding();
2314 }
2315 Diag(SpecLoc, diag::err_declspec_after_virtspec)
2316 << FixItName
2317 << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
2318 << FixItHint::CreateRemoval(SpecLoc)
2319 << Insertion;
2320 }
2321 };
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002322 DeclSpecCheck(DeclSpec::TQ_const, "const", DS.getConstSpecLoc(),
2323 &Function.ConstQualifierLoc);
2324 DeclSpecCheck(DeclSpec::TQ_volatile, "volatile", DS.getVolatileSpecLoc(),
2325 &Function.VolatileQualifierLoc);
2326 DeclSpecCheck(DeclSpec::TQ_restrict, "restrict", DS.getRestrictSpecLoc(),
2327 &Function.RestrictQualifierLoc);
2328 }
Ehsan Akhgaric07d1e22015-03-25 00:53:33 +00002329
2330 // Parse ref-qualifiers.
2331 bool RefQualifierIsLValueRef = true;
2332 SourceLocation RefQualifierLoc;
2333 if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) {
2334 const char *Name = (RefQualifierIsLValueRef ? "& " : "&& ");
2335 FixItHint Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name);
2336 Function.RefQualifierIsLValueRef = RefQualifierIsLValueRef;
2337 Function.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
2338
2339 Diag(RefQualifierLoc, diag::err_declspec_after_virtspec)
2340 << (RefQualifierIsLValueRef ? "&" : "&&")
2341 << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
2342 << FixItHint::CreateRemoval(RefQualifierLoc)
2343 << Insertion;
2344 D.SetRangeEnd(RefQualifierLoc);
2345 }
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002346 }
2347}
2348
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002349/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
2350///
2351/// member-declaration:
2352/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
2353/// function-definition ';'[opt]
2354/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
2355/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00002356/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002357/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002358/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002359///
2360/// member-declarator-list:
2361/// member-declarator
2362/// member-declarator-list ',' member-declarator
2363///
2364/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002365/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002366/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00002367/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002368/// identifier[opt] ':' constant-expression
2369///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002370/// virt-specifier-seq:
2371/// virt-specifier
2372/// virt-specifier-seq virt-specifier
2373///
2374/// virt-specifier:
2375/// override
2376/// final
David Majnemera5433082013-10-18 00:33:31 +00002377/// [MS] sealed
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002378///
Sebastian Redl42e92c42009-04-12 17:16:29 +00002379/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002380/// '= 0'
2381///
2382/// constant-initializer:
2383/// '=' constant-expression
2384///
Alexey Bataev05c25d62015-07-31 08:42:25 +00002385Parser::DeclGroupPtrTy
2386Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
2387 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00002388 const ParsedTemplateInfo &TemplateInfo,
2389 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00002390 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002391 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00002392 Diag(Tok, diag::err_at_defs_cxx);
2393 else
2394 Diag(Tok, diag::err_at_in_class);
Richard Smithda35e962013-11-09 04:52:51 +00002395
Douglas Gregor23c84762011-04-14 17:21:19 +00002396 ConsumeToken();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002397 SkipUntil(tok::r_brace, StopAtSemi);
David Blaikie0403cb12016-01-15 23:43:25 +00002398 return nullptr;
Douglas Gregor23c84762011-04-14 17:21:19 +00002399 }
Richard Smithda35e962013-11-09 04:52:51 +00002400
Serge Pavlov458ea762014-07-16 05:16:52 +00002401 // Turn on colon protection early, while parsing declspec, although there is
2402 // nothing to protect there. It prevents from false errors if error recovery
2403 // incorrectly determines where the declspec ends, as in the example:
2404 // struct A { enum class B { C }; };
2405 // const int C = 4;
2406 // struct D { A::B : C; };
2407 ColonProtectionRAIIObject X(*this);
2408
John McCalla0097262009-12-11 02:10:03 +00002409 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00002410 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00002411 if (!TemplateInfo.Kind &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002412 Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw___super)) {
Richard Smith45855df2012-05-09 08:23:23 +00002413 if (TryAnnotateCXXScopeToken())
2414 MalformedTypeSpec = true;
2415
2416 bool isAccessDecl;
2417 if (Tok.isNot(tok::annot_cxxscope))
2418 isAccessDecl = false;
2419 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00002420 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
2421 else
2422 isAccessDecl = NextToken().is(tok::kw_operator);
2423
2424 if (isAccessDecl) {
2425 // Collect the scope specifier token we annotated earlier.
2426 CXXScopeSpec SS;
David Blaikieefdccaa2016-01-15 23:43:34 +00002427 ParseOptionalCXXScopeSpecifier(SS, nullptr,
Douglas Gregordf593fb2011-11-07 17:33:42 +00002428 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00002429
Nico Weberef03e702014-09-10 00:59:37 +00002430 if (SS.isInvalid()) {
2431 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002432 return nullptr;
Nico Weberef03e702014-09-10 00:59:37 +00002433 }
2434
John McCalla0097262009-12-11 02:10:03 +00002435 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00002436 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00002437 UnqualifiedId Name;
Richard Smith35845152017-02-07 01:37:30 +00002438 if (ParseUnqualifiedId(SS, false, true, true, false, nullptr,
2439 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00002440 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002441 return nullptr;
John McCalla0097262009-12-11 02:10:03 +00002442 }
2443
2444 // TODO: recover from mistakenly-qualified operator declarations.
Alp Toker383d2c42014-01-01 03:08:43 +00002445 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2446 "access declaration")) {
2447 SkipUntil(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002448 return nullptr;
Alp Toker383d2c42014-01-01 03:08:43 +00002449 }
John McCalla0097262009-12-11 02:10:03 +00002450
Alexey Bataev05c25d62015-07-31 08:42:25 +00002451 return DeclGroupPtrTy::make(DeclGroupRef(Actions.ActOnUsingDeclaration(
Richard Smith151c4562016-12-20 21:35:28 +00002452 getCurScope(), AS, /*UsingLoc*/ SourceLocation(),
2453 /*TypenameLoc*/ SourceLocation(), SS, Name,
2454 /*EllipsisLoc*/ SourceLocation(), /*AttrList*/ nullptr)));
John McCalla0097262009-12-11 02:10:03 +00002455 }
2456 }
2457
Aaron Ballmane7c544d2014-08-04 20:28:35 +00002458 // static_assert-declaration. A templated static_assert declaration is
2459 // diagnosed in Parser::ParseSingleDeclarationAfterTemplate.
2460 if (!TemplateInfo.Kind &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002461 Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert)) {
Chris Lattner49836b42009-04-02 04:16:50 +00002462 SourceLocation DeclEnd;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002463 return DeclGroupPtrTy::make(
2464 DeclGroupRef(ParseStaticAssertDeclaration(DeclEnd)));
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002465 }
Mike Stump11289f42009-09-09 15:08:12 +00002466
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002467 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00002468 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00002469 "Nested template improperly parsed?");
Richard Smith3af70092017-02-09 22:14:25 +00002470 ObjCDeclContextSwitch ObjCDC(*this);
Chris Lattner49836b42009-04-02 04:16:50 +00002471 SourceLocation DeclEnd;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002472 return DeclGroupPtrTy::make(
Richard Smith3af70092017-02-09 22:14:25 +00002473 DeclGroupRef(ParseTemplateDeclarationOrSpecialization(
Alexey Bataev05c25d62015-07-31 08:42:25 +00002474 Declarator::MemberContext, DeclEnd, AS, AccessAttrs)));
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002475 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002476
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002477 // Handle: member-declaration ::= '__extension__' member-declaration
2478 if (Tok.is(tok::kw___extension__)) {
2479 // __extension__ silences extension warnings in the subexpression.
2480 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2481 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002482 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2483 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002484 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002485
John McCall084e83d2011-03-24 11:26:52 +00002486 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002487 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002488 // Optional C++11 attribute-specifier
2489 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002490 // We need to keep these attributes for future diagnostic
2491 // before they are taken over by declaration specifier.
2492 FnAttrs.addAll(attrs.getList());
2493 FnAttrs.Range = attrs.Range;
2494
John McCall53fa7142010-12-24 02:08:15 +00002495 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002496
Douglas Gregorfec52632009-06-20 00:51:54 +00002497 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002498 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002499
Douglas Gregorfec52632009-06-20 00:51:54 +00002500 // Eat 'using'.
2501 SourceLocation UsingLoc = ConsumeToken();
2502
2503 if (Tok.is(tok::kw_namespace)) {
2504 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002505 SkipUntil(tok::semi, StopBeforeMatch);
David Blaikie0403cb12016-01-15 23:43:25 +00002506 return nullptr;
Douglas Gregorfec52632009-06-20 00:51:54 +00002507 }
Alexey Bataev05c25d62015-07-31 08:42:25 +00002508 SourceLocation DeclEnd;
2509 // Otherwise, it must be a using-declaration or an alias-declaration.
Richard Smith6f1daa42016-12-16 00:58:48 +00002510 return ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2511 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00002512 }
2513
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002514 // Hold late-parsed attributes so we can attach a Decl to them later.
2515 LateParsedAttrList CommonLateParsedAttrs;
2516
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002517 // decl-specifier-seq:
2518 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002519 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002520 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002521 if (MalformedTypeSpec)
2522 DS.SetTypeSpecError();
Richard Smith72553fc2014-01-23 23:53:27 +00002523
Serge Pavlov458ea762014-07-16 05:16:52 +00002524 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2525 &CommonLateParsedAttrs);
2526
2527 // Turn off colon protection that was set for declspec.
2528 X.restore();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002529
Richard Smith404dfb42013-11-19 22:47:36 +00002530 // If we had a free-standing type definition with a missing semicolon, we
2531 // may get this far before the problem becomes obvious.
2532 if (DS.hasTagDefinition() &&
2533 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2534 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2535 &CommonLateParsedAttrs))
David Blaikie0403cb12016-01-15 23:43:25 +00002536 return nullptr;
Richard Smith404dfb42013-11-19 22:47:36 +00002537
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002538 MultiTemplateParamsArg TemplateParams(
Craig Topper161e4db2014-05-21 06:02:52 +00002539 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data()
2540 : nullptr,
John McCall11083da2009-09-16 22:47:08 +00002541 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2542
Alp Toker35d87032013-12-30 23:29:50 +00002543 if (TryConsumeToken(tok::semi)) {
Michael Handdc016d2012-11-28 23:17:40 +00002544 if (DS.isFriendSpecified())
2545 ProhibitAttributes(FnAttrs);
2546
Nico Weber7b837f52016-01-28 19:25:00 +00002547 RecordDecl *AnonRecord = nullptr;
2548 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(
2549 getCurScope(), AS, DS, TemplateParams, false, AnonRecord);
John McCall796c2a52010-07-16 08:13:16 +00002550 DS.complete(TheDecl);
Nico Weber7b837f52016-01-28 19:25:00 +00002551 if (AnonRecord) {
2552 Decl* decls[] = {AnonRecord, TheDecl};
Richard Smith3beb7c62017-01-12 02:27:38 +00002553 return Actions.BuildDeclaratorGroup(decls);
Nico Weber7b837f52016-01-28 19:25:00 +00002554 }
2555 return Actions.ConvertDeclToDeclGroup(TheDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002556 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002557
John McCall28a6aea2009-11-04 02:18:39 +00002558 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002559 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002560
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002561 // Hold late-parsed attributes so we can attach a Decl to them later.
2562 LateParsedAttrList LateParsedAttrs;
2563
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002564 SourceLocation EqualLoc;
Richard Smith9ba0fec2015-06-30 01:28:56 +00002565 SourceLocation PureSpecLoc;
2566
Yaron Keren180c1672015-06-30 07:35:19 +00002567 auto TryConsumePureSpecifier = [&] (bool AllowDefinition) {
Richard Smith9ba0fec2015-06-30 01:28:56 +00002568 if (Tok.isNot(tok::equal))
2569 return false;
2570
2571 auto &Zero = NextToken();
2572 SmallString<8> Buffer;
2573 if (Zero.isNot(tok::numeric_constant) || Zero.getLength() != 1 ||
2574 PP.getSpelling(Zero, Buffer) != "0")
2575 return false;
2576
2577 auto &After = GetLookAheadToken(2);
2578 if (!After.isOneOf(tok::semi, tok::comma) &&
2579 !(AllowDefinition &&
2580 After.isOneOf(tok::l_brace, tok::colon, tok::kw_try)))
2581 return false;
2582
2583 EqualLoc = ConsumeToken();
2584 PureSpecLoc = ConsumeToken();
2585 return true;
2586 };
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002587
Richard Smith72553fc2014-01-23 23:53:27 +00002588 SmallVector<Decl *, 8> DeclsInGroup;
2589 ExprResult BitfieldSize;
2590 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002591
Richard Smith72553fc2014-01-23 23:53:27 +00002592 // Parse the first declarator.
Nico Weberd89e6f72015-01-16 19:34:13 +00002593 if (ParseCXXMemberDeclaratorBeforeInitializer(
2594 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) {
Richard Smith72553fc2014-01-23 23:53:27 +00002595 TryConsumeToken(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002596 return nullptr;
Richard Smith72553fc2014-01-23 23:53:27 +00002597 }
John Thompson5bc5cbe2009-11-25 22:58:06 +00002598
Richard Smith72553fc2014-01-23 23:53:27 +00002599 // Check for a member function definition.
Richard Smith4b5a9492014-01-24 22:34:35 +00002600 if (BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002601 // MSVC permits pure specifier on inline functions defined at class scope.
Francois Pichet3abc9b82011-05-11 02:14:46 +00002602 // Hence check for =0 before checking for function definition.
Richard Smith9ba0fec2015-06-30 01:28:56 +00002603 if (getLangOpts().MicrosoftExt && DeclaratorInfo.isDeclarationOfFunction())
2604 TryConsumePureSpecifier(/*AllowDefinition*/ true);
Francois Pichet3abc9b82011-05-11 02:14:46 +00002605
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002606 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002607 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002608 //
2609 // In C++11, a non-function declarator followed by an open brace is a
2610 // braced-init-list for an in-class member initialization, not an
2611 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002612 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002613 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002614 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002615 if (Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002616 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002617 } else if (Tok.is(tok::equal)) {
2618 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002619 if (KW.is(tok::kw_default))
2620 DefinitionKind = FDK_Defaulted;
2621 else if (KW.is(tok::kw_delete))
2622 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002623 }
2624 }
Eli Bendersky41842222015-03-23 23:49:41 +00002625 DeclaratorInfo.setFunctionDefinitionKind(DefinitionKind);
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002626
Michael Handdc016d2012-11-28 23:17:40 +00002627 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2628 // to a friend declaration, that declaration shall be a definition.
2629 if (DeclaratorInfo.isFunctionDeclarator() &&
2630 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2631 // Diagnose attributes that appear before decl specifier:
2632 // [[]] friend int foo();
2633 ProhibitAttributes(FnAttrs);
2634 }
2635
Nico Webera7f137d2015-01-16 19:35:01 +00002636 if (DefinitionKind != FDK_Declaration) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002637 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002638 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002639 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002640 SkipUntil(tok::r_brace);
Michael Handdc016d2012-11-28 23:17:40 +00002641
Douglas Gregor8a4db832011-01-19 16:41:58 +00002642 // Consume the optional ';'
Alp Toker35d87032013-12-30 23:29:50 +00002643 TryConsumeToken(tok::semi);
2644
David Blaikie0403cb12016-01-15 23:43:25 +00002645 return nullptr;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002646 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002647
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002648 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002649 Diag(DeclaratorInfo.getIdentifierLoc(),
2650 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002651
Richard Smith2603b092012-11-15 22:54:20 +00002652 // Recover by treating the 'typedef' as spurious.
2653 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002654 }
2655
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002656 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002657 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Richard Smith9ba0fec2015-06-30 01:28:56 +00002658 VS, PureSpecLoc);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002659
David Majnemer23252a32013-08-01 04:22:55 +00002660 if (FunDecl) {
2661 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2662 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2663 }
2664 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2665 LateParsedAttrs[i]->addDecl(FunDecl);
2666 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002667 }
2668 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002669
2670 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002671 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002672 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002673
Alexey Bataev05c25d62015-07-31 08:42:25 +00002674 return DeclGroupPtrTy::make(DeclGroupRef(FunDecl));
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002675 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002676 }
2677
2678 // member-declarator-list:
2679 // member-declarator
2680 // member-declarator-list ',' member-declarator
2681
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002682 while (1) {
Richard Smith2b013182012-06-10 03:12:00 +00002683 InClassInitStyle HasInClassInit = ICIS_NoInit;
Richard Smith9ba0fec2015-06-30 01:28:56 +00002684 bool HasStaticInitializer = false;
2685 if (Tok.isOneOf(tok::equal, tok::l_brace) && PureSpecLoc.isInvalid()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002686 if (BitfieldSize.get()) {
2687 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002688 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith9ba0fec2015-06-30 01:28:56 +00002689 } else if (DeclaratorInfo.isDeclarationOfFunction()) {
2690 // It's a pure-specifier.
2691 if (!TryConsumePureSpecifier(/*AllowFunctionDefinition*/ false))
2692 // Parse it as an expression so that Sema can diagnose it.
2693 HasStaticInitializer = true;
2694 } else if (DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2695 DeclSpec::SCS_static &&
2696 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2697 DeclSpec::SCS_typedef &&
2698 !DS.isFriendSpecified()) {
2699 // It's a default member initializer.
2700 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002701 } else {
Richard Smith9ba0fec2015-06-30 01:28:56 +00002702 HasStaticInitializer = true;
Richard Smith938f40b2011-06-11 17:19:42 +00002703 }
2704 }
2705
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002706 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002707 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002708 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002709
Craig Topper161e4db2014-05-21 06:02:52 +00002710 NamedDecl *ThisDecl = nullptr;
John McCall07e91c02009-08-06 02:15:43 +00002711 if (DS.isFriendSpecified()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002712 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
Michael Handdc016d2012-11-28 23:17:40 +00002713 // to a friend declaration, that declaration shall be a definition.
2714 //
Richard Smith72553fc2014-01-23 23:53:27 +00002715 // Diagnose attributes that appear in a friend member function declarator:
2716 // friend int foo [[]] ();
Michael Handdc016d2012-11-28 23:17:40 +00002717 SmallVector<SourceRange, 4> Ranges;
2718 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
Richard Smith72553fc2014-01-23 23:53:27 +00002719 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
2720 E = Ranges.end(); I != E; ++I)
2721 Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
Michael Handdc016d2012-11-28 23:17:40 +00002722
Douglas Gregor0be31a22010-07-02 17:43:08 +00002723 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002724 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002725 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002726 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002727 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002728 TemplateParams,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002729 BitfieldSize.get(),
Richard Smith2b013182012-06-10 03:12:00 +00002730 VS, HasInClassInit);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002731
2732 if (VarTemplateDecl *VT =
Craig Topper161e4db2014-05-21 06:02:52 +00002733 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr)
Larisse Voufo39a1e502013-08-06 01:03:05 +00002734 // Re-direct this decl to refer to the templated decl so that we can
2735 // initialize it.
2736 ThisDecl = VT->getTemplatedDecl();
2737
David Majnemer23252a32013-08-01 04:22:55 +00002738 if (ThisDecl && AccessAttrs)
Richard Smithf8a75c32013-08-29 00:47:48 +00002739 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00002740 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002741
Richard Smith9ba0fec2015-06-30 01:28:56 +00002742 // Error recovery might have converted a non-static member into a static
2743 // member.
David Blaikie35506f82013-01-30 01:22:18 +00002744 if (HasInClassInit != ICIS_NoInit &&
Richard Smith9ba0fec2015-06-30 01:28:56 +00002745 DeclaratorInfo.getDeclSpec().getStorageClassSpec() ==
2746 DeclSpec::SCS_static) {
2747 HasInClassInit = ICIS_NoInit;
2748 HasStaticInitializer = true;
2749 }
2750
2751 if (ThisDecl && PureSpecLoc.isValid())
2752 Actions.ActOnPureSpecifier(ThisDecl, PureSpecLoc);
2753
2754 // Handle the initializer.
2755 if (HasInClassInit != ICIS_NoInit) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002756 // The initializer was deferred; parse it and cache the tokens.
David Majnemer23252a32013-08-01 04:22:55 +00002757 Diag(Tok, getLangOpts().CPlusPlus11
2758 ? diag::warn_cxx98_compat_nonstatic_member_init
2759 : diag::ext_nonstatic_member_init);
Richard Smith5d164bc2011-10-15 05:09:34 +00002760
Richard Smith938f40b2011-06-11 17:19:42 +00002761 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002762 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2763 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002764 //
2765 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002766 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002767 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002768 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemer23252a32013-08-01 04:22:55 +00002769
2770 // Avoid later warnings about a class member of incomplete type.
David Blaikiecdd91db2012-02-14 09:00:46 +00002771 if (ThisDecl)
David Blaikiecdd91db2012-02-14 09:00:46 +00002772 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002773 } else
2774 ParseCXXNonStaticMemberInitializer(ThisDecl);
Richard Smith9ba0fec2015-06-30 01:28:56 +00002775 } else if (HasStaticInitializer) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002776 // Normal initializer.
Richard Smith9ba0fec2015-06-30 01:28:56 +00002777 ExprResult Init = ParseCXXMemberInitializer(
2778 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
David Majnemer23252a32013-08-01 04:22:55 +00002779
Douglas Gregor728d00b2011-10-10 14:49:18 +00002780 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002781 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002782 else if (ThisDecl)
Richard Smith3beb7c62017-01-12 02:27:38 +00002783 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid());
David Majnemer23252a32013-08-01 04:22:55 +00002784 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor728d00b2011-10-10 14:49:18 +00002785 // No initializer.
Richard Smith3beb7c62017-01-12 02:27:38 +00002786 Actions.ActOnUninitializedDecl(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002787
Douglas Gregor728d00b2011-10-10 14:49:18 +00002788 if (ThisDecl) {
David Majnemer23252a32013-08-01 04:22:55 +00002789 if (!ThisDecl->isInvalidDecl()) {
2790 // Set the Decl for any late parsed attributes
2791 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2792 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2793
2794 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2795 LateParsedAttrs[i]->addDecl(ThisDecl);
2796 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002797 Actions.FinalizeDeclaration(ThisDecl);
2798 DeclsInGroup.push_back(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002799
2800 if (DeclaratorInfo.isFunctionDeclarator() &&
2801 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2802 DeclSpec::SCS_typedef)
2803 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002804 }
David Majnemer23252a32013-08-01 04:22:55 +00002805 LateParsedAttrs.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002806
2807 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002808
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002809 // If we don't have a comma, it is either the end of the list (a ';')
2810 // or an error, bail out.
Alp Toker094e5212014-01-05 03:27:11 +00002811 SourceLocation CommaLoc;
2812 if (!TryConsumeToken(tok::comma, CommaLoc))
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002813 break;
Mike Stump11289f42009-09-09 15:08:12 +00002814
Richard Smithc8a79032012-01-09 22:31:44 +00002815 if (Tok.isAtStartOfLine() &&
2816 !MightBeDeclarator(Declarator::MemberContext)) {
2817 // This comma was followed by a line-break and something which can't be
2818 // the start of a declarator. The comma was probably a typo for a
2819 // semicolon.
2820 Diag(CommaLoc, diag::err_expected_semi_declaration)
2821 << FixItHint::CreateReplacement(CommaLoc, ";");
2822 ExpectSemi = false;
2823 break;
2824 }
Mike Stump11289f42009-09-09 15:08:12 +00002825
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002826 // Parse the next declarator.
2827 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002828 VS.clear();
Nico Weberf56c85b2015-01-17 02:26:40 +00002829 BitfieldSize = ExprResult(/*Invalid=*/false);
Richard Smith9ba0fec2015-06-30 01:28:56 +00002830 EqualLoc = PureSpecLoc = SourceLocation();
Richard Smith8d06f422012-01-12 23:53:29 +00002831 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002832
Richard Smith72553fc2014-01-23 23:53:27 +00002833 // GNU attributes are allowed before the second and subsequent declarator.
John McCall53fa7142010-12-24 02:08:15 +00002834 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002835
Nico Weberd89e6f72015-01-16 19:34:13 +00002836 if (ParseCXXMemberDeclaratorBeforeInitializer(
2837 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs))
2838 break;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002839 }
2840
Richard Smithc8a79032012-01-09 22:31:44 +00002841 if (ExpectSemi &&
2842 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002843 // Skip to end of block or statement.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002844 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002845 // If we stopped at a ';', eat it.
Alp Toker35d87032013-12-30 23:29:50 +00002846 TryConsumeToken(tok::semi);
David Blaikie0403cb12016-01-15 23:43:25 +00002847 return nullptr;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002848 }
2849
Alexey Bataev05c25d62015-07-31 08:42:25 +00002850 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002851}
2852
Richard Smith9ba0fec2015-06-30 01:28:56 +00002853/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer.
2854/// Also detect and reject any attempted defaulted/deleted function definition.
2855/// The location of the '=', if any, will be placed in EqualLoc.
Richard Smith938f40b2011-06-11 17:19:42 +00002856///
Richard Smith9ba0fec2015-06-30 01:28:56 +00002857/// This does not check for a pure-specifier; that's handled elsewhere.
Sebastian Redleef474c2012-02-22 10:50:08 +00002858///
Richard Smith938f40b2011-06-11 17:19:42 +00002859/// brace-or-equal-initializer:
2860/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002861/// braced-init-list
2862///
Richard Smith938f40b2011-06-11 17:19:42 +00002863/// initializer-clause:
2864/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002865/// braced-init-list
2866///
Richard Smithda35e962013-11-09 04:52:51 +00002867/// defaulted/deleted function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002868/// '=' 'default'
2869/// '=' 'delete'
2870///
2871/// Prior to C++0x, the assignment-expression in an initializer-clause must
2872/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002873ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002874 SourceLocation &EqualLoc) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002875 assert(Tok.isOneOf(tok::equal, tok::l_brace)
Richard Smith938f40b2011-06-11 17:19:42 +00002876 && "Data member initializer not starting with '=' or '{'");
2877
Douglas Gregor926410d2012-02-21 02:22:07 +00002878 EnterExpressionEvaluationContext Context(Actions,
2879 Sema::PotentiallyEvaluated,
2880 D);
Alp Toker094e5212014-01-05 03:27:11 +00002881 if (TryConsumeToken(tok::equal, EqualLoc)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002882 if (Tok.is(tok::kw_delete)) {
2883 // In principle, an initializer of '= delete p;' is legal, but it will
2884 // never type-check. It's better to diagnose it as an ill-formed expression
2885 // than as an ill-formed deleted non-function member.
2886 // An initializer of '= delete p, foo' will never be parsed, because
2887 // a top-level comma always ends the initializer expression.
2888 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002889 if (IsFunction || Next.isOneOf(tok::semi, tok::comma, tok::eof)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002890 if (IsFunction)
2891 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2892 << 1 /* delete */;
2893 else
2894 Diag(ConsumeToken(), diag::err_deleted_non_function);
Richard Smithedcb26e2014-06-11 00:49:52 +00002895 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002896 }
2897 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002898 if (IsFunction)
2899 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2900 << 0 /* default */;
2901 else
2902 Diag(ConsumeToken(), diag::err_default_special_members);
Richard Smithedcb26e2014-06-11 00:49:52 +00002903 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002904 }
David Majnemer87ff66c2014-12-13 11:34:16 +00002905 }
2906 if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) {
2907 Diag(Tok, diag::err_ms_property_initializer) << PD;
2908 return ExprError();
Sebastian Redleef474c2012-02-22 10:50:08 +00002909 }
2910 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002911}
2912
Richard Smith65ebb4a2015-03-26 04:09:53 +00002913void Parser::SkipCXXMemberSpecification(SourceLocation RecordLoc,
2914 SourceLocation AttrFixitLoc,
2915 unsigned TagType, Decl *TagDecl) {
2916 // Skip the optional 'final' keyword.
2917 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
2918 assert(isCXX11FinalKeyword() && "not a class definition");
2919 ConsumeToken();
2920
2921 // Diagnose any C++11 attributes after 'final' keyword.
2922 // We deliberately discard these attributes.
2923 ParsedAttributesWithRange Attrs(AttrFactory);
2924 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
2925
2926 // This can only happen if we had malformed misplaced attributes;
2927 // we only get called if there is a colon or left-brace after the
2928 // attributes.
2929 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_brace))
2930 return;
2931 }
2932
2933 // Skip the base clauses. This requires actually parsing them, because
2934 // otherwise we can't be sure where they end (a left brace may appear
2935 // within a template argument).
2936 if (Tok.is(tok::colon)) {
2937 // Enter the scope of the class so that we can correctly parse its bases.
2938 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
2939 ParsingClassDefinition ParsingDef(*this, TagDecl, /*NonNestedClass*/ true,
2940 TagType == DeclSpec::TST_interface);
Richard Smith0f192e82015-06-11 22:48:25 +00002941 auto OldContext =
2942 Actions.ActOnTagStartSkippedDefinition(getCurScope(), TagDecl);
Richard Smith65ebb4a2015-03-26 04:09:53 +00002943
2944 // Parse the bases but don't attach them to the class.
2945 ParseBaseClause(nullptr);
2946
Richard Smith0f192e82015-06-11 22:48:25 +00002947 Actions.ActOnTagFinishSkippedDefinition(OldContext);
Richard Smith65ebb4a2015-03-26 04:09:53 +00002948
2949 if (!Tok.is(tok::l_brace)) {
2950 Diag(PP.getLocForEndOfToken(PrevTokLocation),
2951 diag::err_expected_lbrace_after_base_specifiers);
2952 return;
2953 }
2954 }
2955
2956 // Skip the body.
2957 assert(Tok.is(tok::l_brace));
2958 BalancedDelimiterTracker T(*this, tok::l_brace);
2959 T.consumeOpen();
2960 T.skipToEnd();
Richard Smith04c6c1f2015-07-01 18:56:50 +00002961
2962 // Parse and discard any trailing attributes.
2963 ParsedAttributes Attrs(AttrFactory);
2964 if (Tok.is(tok::kw___attribute))
2965 MaybeParseGNUAttributes(Attrs);
Richard Smith65ebb4a2015-03-26 04:09:53 +00002966}
2967
Alexey Bataev05c25d62015-07-31 08:42:25 +00002968Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclarationWithPragmas(
2969 AccessSpecifier &AS, ParsedAttributesWithRange &AccessAttrs,
2970 DeclSpec::TST TagType, Decl *TagDecl) {
Richard Smithb55f7582017-01-28 01:12:10 +00002971 switch (Tok.getKind()) {
2972 case tok::kw___if_exists:
2973 case tok::kw___if_not_exists:
Alexey Bataev05c25d62015-07-31 08:42:25 +00002974 ParseMicrosoftIfExistsClassDeclaration(TagType, AS);
David Blaikie0403cb12016-01-15 23:43:25 +00002975 return nullptr;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002976
Richard Smithb55f7582017-01-28 01:12:10 +00002977 case tok::semi:
2978 // Check for extraneous top-level semicolon.
Alexey Bataev05c25d62015-07-31 08:42:25 +00002979 ConsumeExtraSemi(InsideStruct, TagType);
David Blaikie0403cb12016-01-15 23:43:25 +00002980 return nullptr;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002981
Richard Smithb55f7582017-01-28 01:12:10 +00002982 // Handle pragmas that can appear as member declarations.
2983 case tok::annot_pragma_vis:
Alexey Bataev05c25d62015-07-31 08:42:25 +00002984 HandlePragmaVisibility();
David Blaikie0403cb12016-01-15 23:43:25 +00002985 return nullptr;
Richard Smithb55f7582017-01-28 01:12:10 +00002986 case tok::annot_pragma_pack:
Alexey Bataev05c25d62015-07-31 08:42:25 +00002987 HandlePragmaPack();
David Blaikie0403cb12016-01-15 23:43:25 +00002988 return nullptr;
Richard Smithb55f7582017-01-28 01:12:10 +00002989 case tok::annot_pragma_align:
Alexey Bataev05c25d62015-07-31 08:42:25 +00002990 HandlePragmaAlign();
David Blaikie0403cb12016-01-15 23:43:25 +00002991 return nullptr;
Richard Smithb55f7582017-01-28 01:12:10 +00002992 case tok::annot_pragma_ms_pointers_to_members:
Alexey Bataev05c25d62015-07-31 08:42:25 +00002993 HandlePragmaMSPointersToMembers();
David Blaikie0403cb12016-01-15 23:43:25 +00002994 return nullptr;
Richard Smithb55f7582017-01-28 01:12:10 +00002995 case tok::annot_pragma_ms_pragma:
Alexey Bataev05c25d62015-07-31 08:42:25 +00002996 HandlePragmaMSPragma();
David Blaikie0403cb12016-01-15 23:43:25 +00002997 return nullptr;
Richard Smithb55f7582017-01-28 01:12:10 +00002998 case tok::annot_pragma_ms_vtordisp:
Alexey Bataev3d42f342015-11-20 07:02:57 +00002999 HandlePragmaMSVtorDisp();
David Blaikie0403cb12016-01-15 23:43:25 +00003000 return nullptr;
Richard Smithb256d302017-01-28 01:20:57 +00003001 case tok::annot_pragma_dump:
3002 HandlePragmaDump();
3003 return nullptr;
Alexey Bataev3d42f342015-11-20 07:02:57 +00003004
Richard Smithb55f7582017-01-28 01:12:10 +00003005 case tok::kw_namespace:
3006 // If we see a namespace here, a close brace was missing somewhere.
Alexey Bataev05c25d62015-07-31 08:42:25 +00003007 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
David Blaikie0403cb12016-01-15 23:43:25 +00003008 return nullptr;
Alexey Bataev05c25d62015-07-31 08:42:25 +00003009
Richard Smithb55f7582017-01-28 01:12:10 +00003010 case tok::kw_public:
3011 case tok::kw_protected:
3012 case tok::kw_private: {
3013 AccessSpecifier NewAS = getAccessSpecifierIfPresent();
3014 assert(NewAS != AS_none);
Alexey Bataev05c25d62015-07-31 08:42:25 +00003015 // Current token is a C++ access specifier.
3016 AS = NewAS;
3017 SourceLocation ASLoc = Tok.getLocation();
3018 unsigned TokLength = Tok.getLength();
3019 ConsumeToken();
3020 AccessAttrs.clear();
3021 MaybeParseGNUAttributes(AccessAttrs);
3022
3023 SourceLocation EndLoc;
3024 if (TryConsumeToken(tok::colon, EndLoc)) {
3025 } else if (TryConsumeToken(tok::semi, EndLoc)) {
3026 Diag(EndLoc, diag::err_expected)
3027 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
3028 } else {
3029 EndLoc = ASLoc.getLocWithOffset(TokLength);
3030 Diag(EndLoc, diag::err_expected)
3031 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
3032 }
3033
3034 // The Microsoft extension __interface does not permit non-public
3035 // access specifiers.
3036 if (TagType == DeclSpec::TST_interface && AS != AS_public) {
3037 Diag(ASLoc, diag::err_access_specifier_interface) << (AS == AS_protected);
3038 }
3039
3040 if (Actions.ActOnAccessSpecifier(NewAS, ASLoc, EndLoc,
3041 AccessAttrs.getList())) {
3042 // found another attribute than only annotations
3043 AccessAttrs.clear();
3044 }
3045
David Blaikie0403cb12016-01-15 23:43:25 +00003046 return nullptr;
Alexey Bataev05c25d62015-07-31 08:42:25 +00003047 }
3048
Richard Smithb55f7582017-01-28 01:12:10 +00003049 case tok::annot_pragma_openmp:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003050 return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, AccessAttrs, TagType,
3051 TagDecl);
Alexey Bataev05c25d62015-07-31 08:42:25 +00003052
Richard Smithb55f7582017-01-28 01:12:10 +00003053 default:
3054 return ParseCXXClassMemberDeclaration(AS, AccessAttrs.getList());
3055 }
Alexey Bataev05c25d62015-07-31 08:42:25 +00003056}
3057
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003058/// ParseCXXMemberSpecification - Parse the class definition.
3059///
3060/// member-specification:
3061/// member-declaration member-specification[opt]
3062/// access-specifier ':' member-specification[opt]
3063///
Joao Matose9a3ed42012-08-31 22:18:20 +00003064void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00003065 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00003066 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00003067 unsigned TagType, Decl *TagDecl) {
3068 assert((TagType == DeclSpec::TST_struct ||
3069 TagType == DeclSpec::TST_interface ||
3070 TagType == DeclSpec::TST_union ||
3071 TagType == DeclSpec::TST_class) && "Invalid TagType!");
3072
John McCallfaf5fb42010-08-26 23:41:50 +00003073 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3074 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00003075
Douglas Gregoredf8f392010-01-16 20:52:59 +00003076 // Determine whether this is a non-nested class. Note that local
3077 // classes are *not* considered to be nested classes.
3078 bool NonNestedClass = true;
3079 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003080 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003081 if (S->isClassScope()) {
3082 // We're inside a class scope, so this is a nested class.
3083 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00003084
3085 // The Microsoft extension __interface does not permit nested classes.
3086 if (getCurrentClass().IsInterface) {
3087 Diag(RecordLoc, diag::err_invalid_member_in_interface)
3088 << /*ErrorType=*/6
3089 << (isa<NamedDecl>(TagDecl)
3090 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
David Blaikieabe1a392014-04-02 05:58:29 +00003091 : "(anonymous)");
John McCalldb632ac2012-09-25 07:32:39 +00003092 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00003093 break;
3094 }
3095
Serge Pavlovd9c0bcf2015-07-14 10:02:10 +00003096 if ((S->getFlags() & Scope::FnScope))
3097 // If we're in a function or function template then this is a local
3098 // class rather than a nested class.
3099 break;
Douglas Gregoredf8f392010-01-16 20:52:59 +00003100 }
3101 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003102
3103 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00003104 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003105
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003106 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00003107 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
3108 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003109
Douglas Gregorcd72ba92009-02-06 22:42:48 +00003110 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003111 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00003112
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00003113 SourceLocation FinalLoc;
David Majnemera5433082013-10-18 00:33:31 +00003114 bool IsFinalSpelledSealed = false;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00003115
3116 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003117 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemera5433082013-10-18 00:33:31 +00003118 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
3119 assert((Specifier == VirtSpecifiers::VS_Final ||
Andrey Bokhanko276055b2016-07-29 10:42:48 +00003120 Specifier == VirtSpecifiers::VS_GNU_Final ||
David Majnemera5433082013-10-18 00:33:31 +00003121 Specifier == VirtSpecifiers::VS_Sealed) &&
3122 "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00003123 FinalLoc = ConsumeToken();
David Majnemera5433082013-10-18 00:33:31 +00003124 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00003125
David Majnemera5433082013-10-18 00:33:31 +00003126 if (TagType == DeclSpec::TST_interface)
John McCalldb632ac2012-09-25 07:32:39 +00003127 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemera5433082013-10-18 00:33:31 +00003128 << VirtSpecifiers::getSpecifierName(Specifier);
3129 else if (Specifier == VirtSpecifiers::VS_Final)
3130 Diag(FinalLoc, getLangOpts().CPlusPlus11
3131 ? diag::warn_cxx98_compat_override_control_keyword
3132 : diag::ext_override_control_keyword)
3133 << VirtSpecifiers::getSpecifierName(Specifier);
3134 else if (Specifier == VirtSpecifiers::VS_Sealed)
3135 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Andrey Bokhanko276055b2016-07-29 10:42:48 +00003136 else if (Specifier == VirtSpecifiers::VS_GNU_Final)
3137 Diag(FinalLoc, diag::ext_warn_gnu_final);
Michael Han9407e502012-11-26 22:54:45 +00003138
Michael Han309af292013-01-07 16:57:11 +00003139 // Parse any C++11 attributes after 'final' keyword.
3140 // These attributes are not allowed to appear here,
3141 // and the only possible place for them to appertain
3142 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00003143 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Nico Weber4b4be842014-12-29 06:56:50 +00003144
3145 // ParseClassSpecifier() does only a superficial check for attributes before
3146 // deciding to call this method. For example, for
3147 // `class C final alignas ([l) {` it will decide that this looks like a
3148 // misplaced attribute since it sees `alignas '(' ')'`. But the actual
3149 // attribute parsing code will try to parse the '[' as a constexpr lambda
3150 // and consume enough tokens that the alignas parsing code will eat the
3151 // opening '{'. So bail out if the next token isn't one we expect.
Nico Weber36de3a22014-12-29 21:56:22 +00003152 if (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)) {
3153 if (TagDecl)
3154 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
Nico Weber4b4be842014-12-29 06:56:50 +00003155 return;
Nico Weber36de3a22014-12-29 21:56:22 +00003156 }
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00003157 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00003158
John McCall2d814c32009-12-19 21:48:58 +00003159 if (Tok.is(tok::colon)) {
3160 ParseBaseClause(TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00003161 if (!Tok.is(tok::l_brace)) {
Ismail Pazarbasi129c44c2014-09-25 21:13:02 +00003162 bool SuggestFixIt = false;
3163 SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation);
3164 if (Tok.isAtStartOfLine()) {
3165 switch (Tok.getKind()) {
3166 case tok::kw_private:
3167 case tok::kw_protected:
3168 case tok::kw_public:
3169 SuggestFixIt = NextToken().getKind() == tok::colon;
3170 break;
3171 case tok::kw_static_assert:
3172 case tok::r_brace:
3173 case tok::kw_using:
3174 // base-clause can have simple-template-id; 'template' can't be there
3175 case tok::kw_template:
3176 SuggestFixIt = true;
3177 break;
3178 case tok::identifier:
3179 SuggestFixIt = isConstructorDeclarator(true);
3180 break;
3181 default:
3182 SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
3183 break;
3184 }
3185 }
3186 DiagnosticBuilder LBraceDiag =
3187 Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers);
3188 if (SuggestFixIt) {
3189 LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {");
3190 // Try recovering from missing { after base-clause.
3191 PP.EnterToken(Tok);
3192 Tok.setKind(tok::l_brace);
3193 } else {
3194 if (TagDecl)
3195 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
3196 return;
3197 }
John McCall2d814c32009-12-19 21:48:58 +00003198 }
3199 }
3200
3201 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003202 BalancedDelimiterTracker T(*this, tok::l_brace);
3203 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00003204
John McCall08bede42010-05-28 08:11:17 +00003205 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00003206 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemera5433082013-10-18 00:33:31 +00003207 IsFinalSpelledSealed,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003208 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00003209
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003210 // C++ 11p3: Members of a class defined with the keyword class are private
3211 // by default. Members of a class defined with the keywords struct or union
3212 // are public by default.
3213 AccessSpecifier CurAS;
3214 if (TagType == DeclSpec::TST_class)
3215 CurAS = AS_private;
3216 else
3217 CurAS = AS_public;
Alexey Bataev05c25d62015-07-31 08:42:25 +00003218 ParsedAttributesWithRange AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003219
Douglas Gregor9377c822010-06-21 22:31:09 +00003220 if (TagDecl) {
3221 // While we still have something to read, read the member-declarations.
Richard Smith752ada82015-11-17 23:32:01 +00003222 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
3223 Tok.isNot(tok::eof)) {
Douglas Gregor9377c822010-06-21 22:31:09 +00003224 // Each iteration of this loop reads one member-declaration.
Alexey Bataev05c25d62015-07-31 08:42:25 +00003225 ParseCXXClassMemberDeclarationWithPragmas(
3226 CurAS, AccessAttrs, static_cast<DeclSpec::TST>(TagType), TagDecl);
Serge Pavlovc4e04a22015-09-19 05:32:57 +00003227 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003228 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00003229 } else {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003230 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003231 }
Mike Stump11289f42009-09-09 15:08:12 +00003232
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003233 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003234 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003235 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003236
John McCall08bede42010-05-28 08:11:17 +00003237 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003238 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003239 T.getOpenLocation(),
3240 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003241 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003242
Douglas Gregor433e0532012-04-16 18:27:27 +00003243 // C++11 [class.mem]p2:
3244 // Within the class member-specification, the class is regarded as complete
Richard Smith0b3a4622014-11-13 20:01:57 +00003245 // within function bodies, default arguments, exception-specifications, and
Douglas Gregor433e0532012-04-16 18:27:27 +00003246 // brace-or-equal-initializers for non-static data members (including such
3247 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00003248 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003249 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00003250 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00003251 // declarations and the lexed inline method definitions, along with any
3252 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00003253 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00003254 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003255 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00003256
3257 // We've finished with all pending member declarations.
3258 Actions.ActOnFinishCXXMemberDecls();
3259
Richard Smith938f40b2011-06-11 17:19:42 +00003260 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003261 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00003262 PrevTokLocation = SavedPrevTokLocation;
Reid Klecknerbba3cb92015-03-17 19:00:50 +00003263
3264 // We've finished parsing everything, including default argument
3265 // initializers.
Hans Wennborg99000c22015-08-15 01:18:16 +00003266 Actions.ActOnFinishCXXNonNestedClass(TagDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003267 }
3268
John McCall08bede42010-05-28 08:11:17 +00003269 if (TagDecl)
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00003270 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange());
John McCall2ff380a2010-03-17 00:38:33 +00003271
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003272 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003273 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00003274 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003275}
Douglas Gregore8381c02008-11-05 04:29:56 +00003276
Richard Smith2ac43ad2013-11-15 23:00:02 +00003277void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithda35e962013-11-09 04:52:51 +00003278 assert(Tok.is(tok::kw_namespace));
3279
3280 // FIXME: Suggest where the close brace should have gone by looking
3281 // at indentation changes within the definition body.
Richard Smith2ac43ad2013-11-15 23:00:02 +00003282 Diag(D->getLocation(),
3283 diag::err_missing_end_of_definition) << D;
Richard Smithda35e962013-11-09 04:52:51 +00003284 Diag(Tok.getLocation(),
Richard Smith2ac43ad2013-11-15 23:00:02 +00003285 diag::note_missing_end_of_definition_before) << D;
Richard Smithda35e962013-11-09 04:52:51 +00003286
3287 // Push '};' onto the token stream to recover.
3288 PP.EnterToken(Tok);
3289
3290 Tok.startToken();
3291 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
3292 Tok.setKind(tok::semi);
3293 PP.EnterToken(Tok);
3294
3295 Tok.setKind(tok::r_brace);
3296}
3297
Douglas Gregore8381c02008-11-05 04:29:56 +00003298/// ParseConstructorInitializer - Parse a C++ constructor initializer,
3299/// which explicitly initializes the members or base classes of a
3300/// class (C++ [class.base.init]). For example, the three initializers
3301/// after the ':' in the Derived constructor below:
3302///
3303/// @code
3304/// class Base { };
3305/// class Derived : Base {
3306/// int x;
3307/// float f;
3308/// public:
3309/// Derived(float f) : Base(), x(17), f(f) { }
3310/// };
3311/// @endcode
3312///
Mike Stump11289f42009-09-09 15:08:12 +00003313/// [C++] ctor-initializer:
3314/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00003315///
Mike Stump11289f42009-09-09 15:08:12 +00003316/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00003317/// mem-initializer ...[opt]
3318/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00003319void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Nico Weber3b00fdc2015-03-07 19:52:39 +00003320 assert(Tok.is(tok::colon) &&
3321 "Constructor initializer always starts with ':'");
Douglas Gregore8381c02008-11-05 04:29:56 +00003322
Nico Weber3b00fdc2015-03-07 19:52:39 +00003323 // Poison the SEH identifiers so they are flagged as illegal in constructor
3324 // initializers.
John Wiegley1c0675e2011-04-28 01:08:34 +00003325 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00003326 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003327
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003328 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00003329 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003330
Douglas Gregore8381c02008-11-05 04:29:56 +00003331 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00003332 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00003333 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
3334 MemInitializers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003335 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00003336 }
Alexey Bataev79de17d2016-01-20 05:25:51 +00003337
3338 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
3339 if (!MemInit.isInvalid())
3340 MemInitializers.push_back(MemInit.get());
3341 else
3342 AnyErrors = true;
3343
Douglas Gregore8381c02008-11-05 04:29:56 +00003344 if (Tok.is(tok::comma))
3345 ConsumeToken();
3346 else if (Tok.is(tok::l_brace))
3347 break;
Alexey Bataev79de17d2016-01-20 05:25:51 +00003348 // If the previous initializer was valid and the next token looks like a
3349 // base or member initializer, assume that we're just missing a comma.
3350 else if (!MemInit.isInvalid() &&
3351 Tok.isOneOf(tok::identifier, tok::coloncolon)) {
Douglas Gregorce66d022010-09-07 14:51:08 +00003352 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3353 Diag(Loc, diag::err_ctor_init_missing_comma)
3354 << FixItHint::CreateInsertion(Loc, ", ");
3355 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00003356 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alexey Bataev79de17d2016-01-20 05:25:51 +00003357 if (!MemInit.isInvalid())
3358 Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
3359 << tok::comma;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003360 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregore8381c02008-11-05 04:29:56 +00003361 break;
3362 }
3363 } while (true);
3364
David Blaikie3fc2f912013-01-17 05:26:25 +00003365 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00003366 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00003367}
3368
3369/// ParseMemInitializer - Parse a C++ member initializer, which is
3370/// part of a constructor initializer that explicitly initializes one
3371/// member or base class (C++ [class.base.init]). See
3372/// ParseConstructorInitializer for an example.
3373///
3374/// [C++] mem-initializer:
3375/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00003376/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00003377///
Douglas Gregore8381c02008-11-05 04:29:56 +00003378/// [C++] mem-initializer-id:
3379/// '::'[opt] nested-name-specifier[opt] class-name
3380/// identifier
Craig Topper9ad7e262014-10-31 06:57:07 +00003381MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00003382 // parse '::'[opt] nested-name-specifier[opt]
3383 CXXScopeSpec SS;
David Blaikieefdccaa2016-01-15 23:43:34 +00003384 ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00003385 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003386 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00003387 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00003388 if (TemplateId->Kind == TNK_Type_template ||
3389 TemplateId->Kind == TNK_Dependent_template_name) {
Richard Smith62559bd2017-02-01 21:36:38 +00003390 AnnotateTemplateIdTokenAsType(/*IsClassName*/true);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003391 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00003392 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003393 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003394 }
David Blaikie186a8892012-01-24 06:03:59 +00003395 // Uses of decltype will already have been converted to annot_decltype by
3396 // ParseOptionalCXXScopeSpecifier at this point.
3397 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
3398 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00003399 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00003400 return true;
3401 }
Mike Stump11289f42009-09-09 15:08:12 +00003402
Craig Topper161e4db2014-05-21 06:02:52 +00003403 IdentifierInfo *II = nullptr;
David Blaikie186a8892012-01-24 06:03:59 +00003404 DeclSpec DS(AttrFactory);
3405 SourceLocation IdLoc = Tok.getLocation();
3406 if (Tok.is(tok::annot_decltype)) {
3407 // Get the decltype expression, if there is one.
3408 ParseDecltypeSpecifier(DS);
3409 } else {
3410 if (Tok.is(tok::identifier))
3411 // Get the identifier. This may be a member name or a class name,
3412 // but we'll let the semantic analysis determine which it is.
3413 II = Tok.getIdentifierInfo();
3414 ConsumeToken();
3415 }
3416
Douglas Gregore8381c02008-11-05 04:29:56 +00003417
3418 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003419 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003420 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3421
Sebastian Redla74948d2011-09-24 17:48:25 +00003422 ExprResult InitList = ParseBraceInitializer();
3423 if (InitList.isInvalid())
3424 return true;
3425
3426 SourceLocation EllipsisLoc;
Alp Toker094e5212014-01-05 03:27:11 +00003427 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redla74948d2011-09-24 17:48:25 +00003428
3429 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00003430 TemplateTypeTy, DS, IdLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003431 InitList.get(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00003432 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003433 BalancedDelimiterTracker T(*this, tok::l_paren);
3434 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00003435
Sebastian Redl3da34892011-06-05 12:23:16 +00003436 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00003437 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00003438 CommaLocsTy CommaLocs;
3439 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003440 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00003441 return true;
3442 }
3443
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003444 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00003445
3446 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00003447 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00003448
3449 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00003450 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00003451 T.getOpenLocation(), ArgExprs,
3452 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00003453 }
3454
Alp Tokerec543272013-12-24 09:48:30 +00003455 if (getLangOpts().CPlusPlus11)
3456 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
3457 else
3458 return Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregore8381c02008-11-05 04:29:56 +00003459}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003460
Sebastian Redl965b0e32011-03-05 14:45:16 +00003461/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003462///
Douglas Gregor356513d2008-12-01 18:00:20 +00003463/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00003464/// dynamic-exception-specification
3465/// noexcept-specification
3466///
3467/// noexcept-specification:
3468/// 'noexcept'
3469/// 'noexcept' '(' constant-expression ')'
3470ExceptionSpecificationType
Richard Smith0b3a4622014-11-13 20:01:57 +00003471Parser::tryParseExceptionSpecification(bool Delayed,
Douglas Gregor433e0532012-04-16 18:27:27 +00003472 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003473 SmallVectorImpl<ParsedType> &DynamicExceptions,
3474 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith0b3a4622014-11-13 20:01:57 +00003475 ExprResult &NoexceptExpr,
3476 CachedTokens *&ExceptionSpecTokens) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003477 ExceptionSpecificationType Result = EST_None;
Hans Wennborgdcfba332015-10-06 23:40:43 +00003478 ExceptionSpecTokens = nullptr;
Richard Smith0b3a4622014-11-13 20:01:57 +00003479
3480 // Handle delayed parsing of exception-specifications.
3481 if (Delayed) {
3482 if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept))
3483 return EST_None;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003484
Richard Smith0b3a4622014-11-13 20:01:57 +00003485 // Consume and cache the starting token.
3486 bool IsNoexcept = Tok.is(tok::kw_noexcept);
3487 Token StartTok = Tok;
3488 SpecificationRange = SourceRange(ConsumeToken());
3489
3490 // Check for a '('.
3491 if (!Tok.is(tok::l_paren)) {
3492 // If this is a bare 'noexcept', we're done.
3493 if (IsNoexcept) {
3494 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
Hans Wennborgdcfba332015-10-06 23:40:43 +00003495 NoexceptExpr = nullptr;
Richard Smith0b3a4622014-11-13 20:01:57 +00003496 return EST_BasicNoexcept;
3497 }
3498
3499 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3500 return EST_DynamicNone;
3501 }
3502
3503 // Cache the tokens for the exception-specification.
3504 ExceptionSpecTokens = new CachedTokens;
3505 ExceptionSpecTokens->push_back(StartTok); // 'throw' or 'noexcept'
3506 ExceptionSpecTokens->push_back(Tok); // '('
3507 SpecificationRange.setEnd(ConsumeParen()); // '('
Richard Smithb1c217e2015-01-13 02:24:58 +00003508
3509 ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens,
3510 /*StopAtSemi=*/true,
3511 /*ConsumeFinalToken=*/true);
Aaron Ballman580ccaf2016-01-12 21:04:22 +00003512 SpecificationRange.setEnd(ExceptionSpecTokens->back().getLocation());
3513
Richard Smith0b3a4622014-11-13 20:01:57 +00003514 return EST_Unparsed;
3515 }
3516
Sebastian Redl965b0e32011-03-05 14:45:16 +00003517 // See if there's a dynamic specification.
3518 if (Tok.is(tok::kw_throw)) {
3519 Result = ParseDynamicExceptionSpecification(SpecificationRange,
3520 DynamicExceptions,
3521 DynamicExceptionRanges);
3522 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
3523 "Produced different number of exception types and ranges.");
3524 }
3525
3526 // If there's no noexcept specification, we're done.
3527 if (Tok.isNot(tok::kw_noexcept))
3528 return Result;
3529
Richard Smithb15c11c2011-10-17 23:06:20 +00003530 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3531
Sebastian Redl965b0e32011-03-05 14:45:16 +00003532 // If we already had a dynamic specification, parse the noexcept for,
3533 // recovery, but emit a diagnostic and don't store the results.
3534 SourceRange NoexceptRange;
3535 ExceptionSpecificationType NoexceptType = EST_None;
3536
3537 SourceLocation KeywordLoc = ConsumeToken();
3538 if (Tok.is(tok::l_paren)) {
3539 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003540 BalancedDelimiterTracker T(*this, tok::l_paren);
3541 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003542 NoexceptType = EST_ComputedNoexcept;
3543 NoexceptExpr = ParseConstantExpression();
Serge Pavlov3739f5e72015-06-29 17:50:19 +00003544 T.consumeClose();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003545 // The argument must be contextually convertible to bool. We use
Richard Smith03a4aa32016-06-23 19:02:52 +00003546 // CheckBooleanCondition for this purpose.
3547 // FIXME: Add a proper Sema entry point for this.
Serge Pavlov3739f5e72015-06-29 17:50:19 +00003548 if (!NoexceptExpr.isInvalid()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00003549 NoexceptExpr =
3550 Actions.CheckBooleanCondition(KeywordLoc, NoexceptExpr.get());
Serge Pavlov3739f5e72015-06-29 17:50:19 +00003551 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
3552 } else {
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +00003553 NoexceptType = EST_BasicNoexcept;
Serge Pavlov3739f5e72015-06-29 17:50:19 +00003554 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003555 } else {
3556 // There is no argument.
3557 NoexceptType = EST_BasicNoexcept;
3558 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
3559 }
3560
3561 if (Result == EST_None) {
3562 SpecificationRange = NoexceptRange;
3563 Result = NoexceptType;
3564
3565 // If there's a dynamic specification after a noexcept specification,
3566 // parse that and ignore the results.
3567 if (Tok.is(tok::kw_throw)) {
3568 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3569 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
3570 DynamicExceptionRanges);
3571 }
3572 } else {
3573 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3574 }
3575
3576 return Result;
3577}
3578
Richard Smith8ca78a12013-06-13 02:02:51 +00003579static void diagnoseDynamicExceptionSpecification(
Craig Toppere335f252015-10-04 04:53:55 +00003580 Parser &P, SourceRange Range, bool IsNoexcept) {
Richard Smith8ca78a12013-06-13 02:02:51 +00003581 if (P.getLangOpts().CPlusPlus11) {
3582 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
Richard Smith82da19d2016-12-08 02:49:07 +00003583 P.Diag(Range.getBegin(),
3584 P.getLangOpts().CPlusPlus1z && !IsNoexcept
3585 ? diag::ext_dynamic_exception_spec
3586 : diag::warn_exception_spec_deprecated)
3587 << Range;
Richard Smith8ca78a12013-06-13 02:02:51 +00003588 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
3589 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
3590 }
3591}
3592
Sebastian Redl965b0e32011-03-05 14:45:16 +00003593/// ParseDynamicExceptionSpecification - Parse a C++
3594/// dynamic-exception-specification (C++ [except.spec]).
3595///
3596/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00003597/// 'throw' '(' type-id-list [opt] ')'
3598/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00003599///
Douglas Gregor356513d2008-12-01 18:00:20 +00003600/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00003601/// type-id ... [opt]
3602/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003603///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003604ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
3605 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003606 SmallVectorImpl<ParsedType> &Exceptions,
3607 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003608 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00003609
Sebastian Redl965b0e32011-03-05 14:45:16 +00003610 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003611 BalancedDelimiterTracker T(*this, tok::l_paren);
3612 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003613 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3614 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003615 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003616 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003617
Douglas Gregor356513d2008-12-01 18:00:20 +00003618 // Parse throw(...), a Microsoft extension that means "this function
3619 // can throw anything".
3620 if (Tok.is(tok::ellipsis)) {
3621 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003622 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00003623 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003624 T.consumeClose();
3625 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003626 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003627 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00003628 }
3629
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003630 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00003631 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003632 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00003633 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00003634
Douglas Gregor830837d2010-12-20 23:57:46 +00003635 if (Tok.is(tok::ellipsis)) {
3636 // C++0x [temp.variadic]p5:
3637 // - In a dynamic-exception-specification (15.4); the pattern is a
3638 // type-id.
3639 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003640 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00003641 if (!Res.isInvalid())
3642 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3643 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003644
Sebastian Redld6434562009-05-29 18:02:33 +00003645 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003646 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00003647 Ranges.push_back(Range);
3648 }
Alp Toker97650562014-01-10 11:19:30 +00003649
3650 if (!TryConsumeToken(tok::comma))
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003651 break;
3652 }
3653
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003654 T.consumeClose();
3655 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003656 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3657 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003658 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003659}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003660
Douglas Gregor7fb25412010-10-01 18:44:50 +00003661/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3662/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003663TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00003664 assert(Tok.is(tok::arrow) && "expected arrow");
3665
3666 ConsumeToken();
3667
Richard Smithbfdb1082012-03-12 08:56:40 +00003668 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003669}
3670
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003671/// \brief We have just started parsing the definition of a new class,
3672/// so push that class onto our stack of classes that is currently
3673/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003674Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003675Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3676 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003677 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003678 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003679 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003680 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003681}
3682
3683/// \brief Deallocate the given parsed class and all of its nested
3684/// classes.
3685void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003686 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3687 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003688 delete Class;
3689}
3690
3691/// \brief Pop the top class of the stack of classes that are
3692/// currently being parsed.
3693///
3694/// This routine should be called when we have finished parsing the
3695/// definition of a class, but have not yet popped the Scope
3696/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003697void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003698 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003699
John McCallc1465822011-02-14 07:13:47 +00003700 Actions.PopParsingClass(state);
3701
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003702 ParsingClass *Victim = ClassStack.top();
3703 ClassStack.pop();
3704 if (Victim->TopLevelClass) {
3705 // Deallocate all of the nested classes of this class,
3706 // recursively: we don't need to keep any of this information.
3707 DeallocateParsedClasses(Victim);
3708 return;
Mike Stump11289f42009-09-09 15:08:12 +00003709 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003710 assert(!ClassStack.empty() && "Missing top-level class?");
3711
Douglas Gregorefc46952010-10-12 16:25:54 +00003712 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003713 // The victim is a nested class, but we will not need to perform
3714 // any processing after the definition of this class since it has
3715 // no members whose handling was delayed. Therefore, we can just
3716 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003717 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003718 return;
3719 }
3720
3721 // This nested class has some members that will need to be processed
3722 // after the top-level class is completely defined. Therefore, add
3723 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003724 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003725 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003726 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003727}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003728
Richard Smith3dff2512012-04-10 03:25:07 +00003729/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3730///
3731/// \return the parsed identifier on success, and 0 if the next token is not an
3732/// attribute-token.
3733///
3734/// C++11 [dcl.attr.grammar]p3:
3735/// If a keyword or an alternative token that satisfies the syntactic
3736/// requirements of an identifier is contained in an attribute-token,
3737/// it is considered an identifier.
3738IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3739 switch (Tok.getKind()) {
3740 default:
3741 // Identifiers and keywords have identifier info attached.
David Majnemerd5271992015-01-09 18:09:39 +00003742 if (!Tok.isAnnotation()) {
3743 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3744 Loc = ConsumeToken();
3745 return II;
3746 }
Richard Smith3dff2512012-04-10 03:25:07 +00003747 }
Craig Topper161e4db2014-05-21 06:02:52 +00003748 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003749
3750 case tok::ampamp: // 'and'
3751 case tok::pipe: // 'bitor'
3752 case tok::pipepipe: // 'or'
3753 case tok::caret: // 'xor'
3754 case tok::tilde: // 'compl'
3755 case tok::amp: // 'bitand'
3756 case tok::ampequal: // 'and_eq'
3757 case tok::pipeequal: // 'or_eq'
3758 case tok::caretequal: // 'xor_eq'
3759 case tok::exclaim: // 'not'
3760 case tok::exclaimequal: // 'not_eq'
3761 // Alternative tokens do not have identifier info, but their spelling
3762 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003763 SmallString<8> SpellingBuf;
Benjamin Kramer60be5632015-03-29 19:25:07 +00003764 SourceLocation SpellingLoc =
3765 PP.getSourceManager().getSpellingLoc(Tok.getLocation());
3766 StringRef Spelling = PP.getSpelling(SpellingLoc, SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003767 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003768 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003769 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003770 }
Craig Topper161e4db2014-05-21 06:02:52 +00003771 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003772 }
3773}
3774
Michael Han23214e52012-10-03 01:56:22 +00003775static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3776 IdentifierInfo *ScopeName) {
3777 switch (AttributeList::getKind(AttrName, ScopeName,
3778 AttributeList::AS_CXX11)) {
3779 case AttributeList::AT_CarriesDependency:
Aaron Ballman35f94212014-04-14 16:03:22 +00003780 case AttributeList::AT_Deprecated:
Michael Han23214e52012-10-03 01:56:22 +00003781 case AttributeList::AT_FallThrough:
Hans Wennborgdcfba332015-10-06 23:40:43 +00003782 case AttributeList::AT_CXX11NoReturn:
Michael Han23214e52012-10-03 01:56:22 +00003783 return true;
Aaron Ballmane7964782016-03-07 22:44:55 +00003784 case AttributeList::AT_WarnUnusedResult:
3785 return !ScopeName && AttrName->getName().equals("nodiscard");
Nico Weberac03bce2016-08-23 19:59:55 +00003786 case AttributeList::AT_Unused:
3787 return !ScopeName && AttrName->getName().equals("maybe_unused");
Michael Han23214e52012-10-03 01:56:22 +00003788 default:
3789 return false;
3790 }
3791}
3792
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003793/// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause.
3794///
3795/// [C++11] attribute-argument-clause:
3796/// '(' balanced-token-seq ')'
3797///
3798/// [C++11] balanced-token-seq:
3799/// balanced-token
3800/// balanced-token-seq balanced-token
3801///
3802/// [C++11] balanced-token:
3803/// '(' balanced-token-seq ')'
3804/// '[' balanced-token-seq ']'
3805/// '{' balanced-token-seq '}'
3806/// any token but '(', ')', '[', ']', '{', or '}'
3807bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName,
3808 SourceLocation AttrNameLoc,
3809 ParsedAttributes &Attrs,
3810 SourceLocation *EndLoc,
3811 IdentifierInfo *ScopeName,
3812 SourceLocation ScopeLoc) {
3813 assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
Aaron Ballman35f94212014-04-14 16:03:22 +00003814 SourceLocation LParenLoc = Tok.getLocation();
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003815
3816 // If the attribute isn't known, we will not attempt to parse any
3817 // arguments.
3818 if (!hasAttribute(AttrSyntax::CXX, ScopeName, AttrName,
Bob Wilson7c730832015-07-20 22:57:31 +00003819 getTargetInfo(), getLangOpts())) {
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003820 // Eat the left paren, then skip to the ending right paren.
3821 ConsumeParen();
3822 SkipUntil(tok::r_paren);
3823 return false;
3824 }
3825
3826 if (ScopeName && ScopeName->getName() == "gnu")
3827 // GNU-scoped attributes have some special cases to handle GNU-specific
3828 // behaviors.
3829 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
Craig Topper161e4db2014-05-21 06:02:52 +00003830 ScopeLoc, AttributeList::AS_CXX11, nullptr);
Aaron Ballman35f94212014-04-14 16:03:22 +00003831 else {
3832 unsigned NumArgs =
3833 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
3834 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3835
3836 const AttributeList *Attr = Attrs.getList();
3837 if (Attr && IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) {
3838 // If the attribute is a standard or built-in attribute and we are
3839 // parsing an argument list, we need to determine whether this attribute
3840 // was allowed to have an argument list (such as [[deprecated]]), and how
3841 // many arguments were parsed (so we can diagnose on [[deprecated()]]).
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003842 if (Attr->getMaxArgs() && !NumArgs) {
3843 // The attribute was allowed to have arguments, but none were provided
3844 // even though the attribute parsed successfully. This is an error.
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003845 Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
Aaron Ballmanbb5d8622016-03-08 21:31:32 +00003846 Attr->setInvalid(true);
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003847 } else if (!Attr->getMaxArgs()) {
3848 // The attribute parsed successfully, but was not allowed to have any
3849 // arguments. It doesn't matter whether any were provided -- the
Aaron Ballman35f94212014-04-14 16:03:22 +00003850 // presence of the argument list (even if empty) is diagnosed.
3851 Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
Aaron Ballman9b7cee62014-12-19 18:37:22 +00003852 << AttrName
3853 << FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc));
Aaron Ballmanbb5d8622016-03-08 21:31:32 +00003854 Attr->setInvalid(true);
Aaron Ballman35f94212014-04-14 16:03:22 +00003855 }
3856 }
3857 }
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003858 return true;
3859}
3860
3861/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003862///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003863/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003864/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003865/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003866///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003867/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003868/// attribute[opt]
3869/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003870/// attribute '...'
3871/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003872///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003873/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003874/// attribute-token attribute-argument-clause[opt]
3875///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003876/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003877/// identifier
3878/// attribute-scoped-token
3879///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003880/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003881/// attribute-namespace '::' identifier
3882///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003883/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003884/// identifier
Richard Smith3dff2512012-04-10 03:25:07 +00003885void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003886 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003887 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003888 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003889 ParseAlignmentSpecifier(attrs, endLoc);
3890 return;
3891 }
3892
Alexis Hunt96d5c762009-11-21 08:43:09 +00003893 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003894 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003895
Richard Smithf679b5b2011-10-14 20:48:27 +00003896 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3897
Alexis Hunt96d5c762009-11-21 08:43:09 +00003898 ConsumeBracket();
3899 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003900
Richard Smithb7d7a042016-06-24 12:15:12 +00003901 SourceLocation CommonScopeLoc;
3902 IdentifierInfo *CommonScopeName = nullptr;
3903 if (Tok.is(tok::kw_using)) {
3904 Diag(Tok.getLocation(), getLangOpts().CPlusPlus1z
3905 ? diag::warn_cxx14_compat_using_attribute_ns
3906 : diag::ext_using_attribute_ns);
3907 ConsumeToken();
3908
3909 CommonScopeName = TryParseCXX11AttributeIdentifier(CommonScopeLoc);
3910 if (!CommonScopeName) {
3911 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
3912 SkipUntil(tok::r_square, tok::colon, StopBeforeMatch);
3913 }
3914 if (!TryConsumeToken(tok::colon) && CommonScopeName)
3915 Diag(Tok.getLocation(), diag::err_expected) << tok::colon;
3916 }
3917
Richard Smith10876ef2013-01-17 01:30:42 +00003918 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3919
Richard Smith3dff2512012-04-10 03:25:07 +00003920 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003921 // attribute not present
Alp Toker97650562014-01-10 11:19:30 +00003922 if (TryConsumeToken(tok::comma))
Alexis Hunt96d5c762009-11-21 08:43:09 +00003923 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003924
Richard Smith3dff2512012-04-10 03:25:07 +00003925 SourceLocation ScopeLoc, AttrLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00003926 IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003927
3928 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3929 if (!AttrName)
3930 // Break out to the "expected ']'" diagnostic.
3931 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003932
Alexis Hunt96d5c762009-11-21 08:43:09 +00003933 // scoped attribute
Alp Toker97650562014-01-10 11:19:30 +00003934 if (TryConsumeToken(tok::coloncolon)) {
Richard Smith3dff2512012-04-10 03:25:07 +00003935 ScopeName = AttrName;
3936 ScopeLoc = AttrLoc;
3937
3938 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3939 if (!AttrName) {
Alp Tokerec543272013-12-24 09:48:30 +00003940 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003941 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003942 continue;
3943 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003944 }
3945
Richard Smithb7d7a042016-06-24 12:15:12 +00003946 if (CommonScopeName) {
3947 if (ScopeName) {
3948 Diag(ScopeLoc, diag::err_using_attribute_ns_conflict)
3949 << SourceRange(CommonScopeLoc);
3950 } else {
3951 ScopeName = CommonScopeName;
3952 ScopeLoc = CommonScopeLoc;
3953 }
3954 }
3955
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003956 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003957 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003958
Richard Smith10876ef2013-01-17 01:30:42 +00003959 if (StandardAttr &&
3960 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3961 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003962 << AttrName << SourceRange(SeenAttrs[AttrName]);
Richard Smith10876ef2013-01-17 01:30:42 +00003963
Michael Han23214e52012-10-03 01:56:22 +00003964 // Parse attribute arguments
Aaron Ballman35f94212014-04-14 16:03:22 +00003965 if (Tok.is(tok::l_paren))
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003966 AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3967 ScopeName, ScopeLoc);
Michael Han23214e52012-10-03 01:56:22 +00003968
3969 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003970 attrs.addNew(AttrName,
3971 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3972 AttrLoc),
Craig Topper161e4db2014-05-21 06:02:52 +00003973 ScopeName, ScopeLoc, nullptr, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003974
Alp Toker97650562014-01-10 11:19:30 +00003975 if (TryConsumeToken(tok::ellipsis))
Michael Han23214e52012-10-03 01:56:22 +00003976 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3977 << AttrName->getName();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003978 }
3979
Alp Toker383d2c42014-01-01 03:08:43 +00003980 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003981 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003982 if (endLoc)
3983 *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003984 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003985 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003986}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003987
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003988/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003989///
3990/// attribute-specifier-seq:
3991/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003992void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003993 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003994 assert(getLangOpts().CPlusPlus11);
3995
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003996 SourceLocation StartLoc = Tok.getLocation(), Loc;
3997 if (!endLoc)
3998 endLoc = &Loc;
3999
Douglas Gregor6f981002011-10-07 20:35:25 +00004000 do {
Richard Smith3dff2512012-04-10 03:25:07 +00004001 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004002 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00004003
4004 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004005}
4006
Richard Smithc2c8bb82013-10-15 01:34:54 +00004007void Parser::DiagnoseAndSkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00004008 // Start and end location of an attribute or an attribute list.
4009 SourceLocation StartLoc = Tok.getLocation();
Richard Smith955bf012014-06-19 11:42:00 +00004010 SourceLocation EndLoc = SkipCXX11Attributes();
4011
4012 if (EndLoc.isValid()) {
4013 SourceRange Range(StartLoc, EndLoc);
4014 Diag(StartLoc, diag::err_attributes_not_allowed)
4015 << Range;
4016 }
4017}
4018
4019SourceLocation Parser::SkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00004020 SourceLocation EndLoc;
4021
Richard Smith955bf012014-06-19 11:42:00 +00004022 if (!isCXX11AttributeSpecifier())
4023 return EndLoc;
4024
Richard Smithc2c8bb82013-10-15 01:34:54 +00004025 do {
4026 if (Tok.is(tok::l_square)) {
4027 BalancedDelimiterTracker T(*this, tok::l_square);
4028 T.consumeOpen();
4029 T.skipToEnd();
4030 EndLoc = T.getCloseLocation();
4031 } else {
4032 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
4033 ConsumeToken();
4034 BalancedDelimiterTracker T(*this, tok::l_paren);
4035 if (!T.consumeOpen())
4036 T.skipToEnd();
4037 EndLoc = T.getCloseLocation();
4038 }
4039 } while (isCXX11AttributeSpecifier());
4040
Richard Smith955bf012014-06-19 11:42:00 +00004041 return EndLoc;
Richard Smithc2c8bb82013-10-15 01:34:54 +00004042}
4043
Nico Weber05e1dad2016-09-03 03:25:22 +00004044/// Parse uuid() attribute when it appears in a [] Microsoft attribute.
4045void Parser::ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs) {
4046 assert(Tok.is(tok::identifier) && "Not a Microsoft attribute list");
4047 IdentifierInfo *UuidIdent = Tok.getIdentifierInfo();
4048 assert(UuidIdent->getName() == "uuid" && "Not a Microsoft attribute list");
4049
4050 SourceLocation UuidLoc = Tok.getLocation();
4051 ConsumeToken();
4052
4053 // Ignore the left paren location for now.
4054 BalancedDelimiterTracker T(*this, tok::l_paren);
4055 if (T.consumeOpen()) {
4056 Diag(Tok, diag::err_expected) << tok::l_paren;
4057 return;
4058 }
4059
4060 ArgsVector ArgExprs;
4061 if (Tok.is(tok::string_literal)) {
4062 // Easy case: uuid("...") -- quoted string.
4063 ExprResult StringResult = ParseStringLiteralExpression();
4064 if (StringResult.isInvalid())
4065 return;
4066 ArgExprs.push_back(StringResult.get());
4067 } else {
4068 // something like uuid({000000A0-0000-0000-C000-000000000049}) -- no
4069 // quotes in the parens. Just append the spelling of all tokens encountered
4070 // until the closing paren.
4071
4072 SmallString<42> StrBuffer; // 2 "", 36 bytes UUID, 2 optional {}, 1 nul
4073 StrBuffer += "\"";
4074
4075 // Since none of C++'s keywords match [a-f]+, accepting just tok::l_brace,
4076 // tok::r_brace, tok::minus, tok::identifier (think C000) and
4077 // tok::numeric_constant (0000) should be enough. But the spelling of the
4078 // uuid argument is checked later anyways, so there's no harm in accepting
4079 // almost anything here.
4080 // cl is very strict about whitespace in this form and errors out if any
4081 // is present, so check the space flags on the tokens.
4082 SourceLocation StartLoc = Tok.getLocation();
4083 while (Tok.isNot(tok::r_paren)) {
4084 if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) {
4085 Diag(Tok, diag::err_attribute_uuid_malformed_guid);
4086 SkipUntil(tok::r_paren, StopAtSemi);
4087 return;
4088 }
4089 SmallString<16> SpellingBuffer;
4090 SpellingBuffer.resize(Tok.getLength() + 1);
4091 bool Invalid = false;
4092 StringRef TokSpelling = PP.getSpelling(Tok, SpellingBuffer, &Invalid);
4093 if (Invalid) {
4094 SkipUntil(tok::r_paren, StopAtSemi);
4095 return;
4096 }
4097 StrBuffer += TokSpelling;
4098 ConsumeAnyToken();
4099 }
4100 StrBuffer += "\"";
4101
4102 if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) {
4103 Diag(Tok, diag::err_attribute_uuid_malformed_guid);
4104 ConsumeParen();
4105 return;
4106 }
4107
4108 // Pretend the user wrote the appropriate string literal here.
4109 // ActOnStringLiteral() copies the string data into the literal, so it's
4110 // ok that the Token points to StrBuffer.
4111 Token Toks[1];
4112 Toks[0].startToken();
4113 Toks[0].setKind(tok::string_literal);
4114 Toks[0].setLocation(StartLoc);
4115 Toks[0].setLiteralData(StrBuffer.data());
4116 Toks[0].setLength(StrBuffer.size());
4117 StringLiteral *UuidString =
4118 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
4119 ArgExprs.push_back(UuidString);
4120 }
4121
4122 if (!T.consumeClose()) {
4123 // FIXME: Warn that this syntax is deprecated, with a Fix-It suggesting
4124 // using __declspec(uuid()) instead.
4125 Attrs.addNew(UuidIdent, SourceRange(UuidLoc, T.getCloseLocation()), nullptr,
4126 SourceLocation(), ArgExprs.data(), ArgExprs.size(),
4127 AttributeList::AS_Microsoft);
4128 }
4129}
4130
David Majnemere4752e752015-07-08 05:55:00 +00004131/// ParseMicrosoftAttributes - Parse Microsoft attributes [Attr]
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00004132///
4133/// [MS] ms-attribute:
4134/// '[' token-seq ']'
4135///
4136/// [MS] ms-attribute-seq:
4137/// ms-attribute[opt]
4138/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00004139void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
4140 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00004141 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
4142
Saleem Abdulrasool425efcf2015-06-15 20:57:04 +00004143 do {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004144 // FIXME: If this is actually a C++11 attribute, parse it as one.
Saleem Abdulrasool425efcf2015-06-15 20:57:04 +00004145 BalancedDelimiterTracker T(*this, tok::l_square);
4146 T.consumeOpen();
Nico Weber05e1dad2016-09-03 03:25:22 +00004147
4148 // Skip most ms attributes except for a whitelist.
4149 while (true) {
4150 SkipUntil(tok::r_square, tok::identifier, StopAtSemi | StopBeforeMatch);
4151 if (Tok.isNot(tok::identifier)) // ']', but also eof
4152 break;
4153 if (Tok.getIdentifierInfo()->getName() == "uuid")
4154 ParseMicrosoftUuidAttributeArgs(attrs);
4155 else
4156 ConsumeToken();
4157 }
4158
Saleem Abdulrasool425efcf2015-06-15 20:57:04 +00004159 T.consumeClose();
4160 if (endLoc)
4161 *endLoc = T.getCloseLocation();
4162 } while (Tok.is(tok::l_square));
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00004163}
Francois Pichet8f981d52011-05-25 10:19:49 +00004164
4165void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
4166 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00004167 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00004168 if (ParseMicrosoftIfExistsCondition(Result))
4169 return;
4170
Douglas Gregor43edb322011-10-24 22:31:10 +00004171 BalancedDelimiterTracker Braces(*this, tok::l_brace);
4172 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00004173 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet8f981d52011-05-25 10:19:49 +00004174 return;
4175 }
Francois Pichet8f981d52011-05-25 10:19:49 +00004176
Douglas Gregor43edb322011-10-24 22:31:10 +00004177 switch (Result.Behavior) {
4178 case IEB_Parse:
4179 // Parse the declarations below.
4180 break;
4181
4182 case IEB_Dependent:
4183 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
4184 << Result.IsIfExists;
4185 // Fall through to skip.
4186
4187 case IEB_Skip:
4188 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00004189 return;
4190 }
4191
Richard Smith34f30512013-11-23 04:06:09 +00004192 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00004193 // __if_exists, __if_not_exists can nest.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00004194 if (Tok.isOneOf(tok::kw___if_exists, tok::kw___if_not_exists)) {
Francois Pichet8f981d52011-05-25 10:19:49 +00004195 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
4196 continue;
4197 }
4198
4199 // Check for extraneous top-level semicolon.
4200 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00004201 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00004202 continue;
4203 }
4204
4205 AccessSpecifier AS = getAccessSpecifierIfPresent();
4206 if (AS != AS_none) {
4207 // Current token is a C++ access specifier.
4208 CurAS = AS;
4209 SourceLocation ASLoc = Tok.getLocation();
4210 ConsumeToken();
4211 if (Tok.is(tok::colon))
4212 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
4213 else
Alp Toker35d87032013-12-30 23:29:50 +00004214 Diag(Tok, diag::err_expected) << tok::colon;
Francois Pichet8f981d52011-05-25 10:19:49 +00004215 ConsumeToken();
4216 continue;
4217 }
4218
4219 // Parse all the comma separated declarators.
Craig Topper161e4db2014-05-21 06:02:52 +00004220 ParseCXXClassMemberDeclaration(CurAS, nullptr);
Francois Pichet8f981d52011-05-25 10:19:49 +00004221 }
Douglas Gregor43edb322011-10-24 22:31:10 +00004222
4223 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00004224}