blob: 0a88d202b8beb9579f35edbe3c78933b0d8009ce [file] [log] [blame]
Chris Lattnera5235172007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
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"
Chris Lattnera5235172007-08-25 06:57:03 +000029using namespace clang;
30
31/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redl67667942010-08-27 23:12:46 +000032/// may either be a top level namespace or a block-level namespace alias. If
33/// there was an inline keyword, it has already been parsed.
Chris Lattnera5235172007-08-25 06:57:03 +000034///
35/// namespace-definition: [C++ 7.3: basic.namespace]
36/// named-namespace-definition
37/// unnamed-namespace-definition
38///
39/// unnamed-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000040/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000041///
42/// named-namespace-definition:
43/// original-namespace-definition
44/// extension-namespace-definition
45///
46/// original-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000047/// 'inline'[opt] 'namespace' identifier attributes[opt]
48/// '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000049///
50/// extension-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000051/// 'inline'[opt] 'namespace' original-namespace-name
52/// '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000053///
Chris Lattnera5235172007-08-25 06:57:03 +000054/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
55/// 'namespace' identifier '=' qualified-namespace-specifier ';'
56///
John McCall48871652010-08-21 09:40:31 +000057Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redl67667942010-08-27 23:12:46 +000058 SourceLocation &DeclEnd,
59 SourceLocation InlineLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000060 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000061 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian4bf82622011-08-22 17:59:19 +000062 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000063
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000064 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000065 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000066 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +000067 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000068 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000069
Chris Lattnera5235172007-08-25 06:57:03 +000070 SourceLocation IdentLoc;
Craig Topper161e4db2014-05-21 06:02:52 +000071 IdentifierInfo *Ident = nullptr;
Richard Trieu61384cb2011-05-26 20:11:09 +000072 std::vector<SourceLocation> ExtraIdentLoc;
73 std::vector<IdentifierInfo*> ExtraIdent;
74 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000075
Aaron Ballman730476b2014-11-08 15:33:35 +000076 ParsedAttributesWithRange attrs(AttrFactory);
77 SourceLocation attrLoc;
78 if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) {
79 if (!getLangOpts().CPlusPlus1z)
Aaron Ballmanc0ae7df2014-11-08 17:07:15 +000080 Diag(Tok.getLocation(), diag::warn_cxx14_compat_attribute)
81 << 0 /*namespace*/;
Aaron Ballman730476b2014-11-08 15:33:35 +000082 attrLoc = Tok.getLocation();
83 ParseCXX11Attributes(attrs);
84 }
Mike Stump11289f42009-09-09 15:08:12 +000085
Chris Lattner76c72282007-10-09 17:33:22 +000086 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000087 Ident = Tok.getIdentifierInfo();
88 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieu61384cb2011-05-26 20:11:09 +000089 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
90 ExtraNamespaceLoc.push_back(ConsumeToken());
91 ExtraIdent.push_back(Tok.getIdentifierInfo());
92 ExtraIdentLoc.push_back(ConsumeToken());
93 }
Chris Lattnera5235172007-08-25 06:57:03 +000094 }
Mike Stump11289f42009-09-09 15:08:12 +000095
Aaron Ballmanc0ae7df2014-11-08 17:07:15 +000096 // A nested namespace definition cannot have attributes.
97 if (!ExtraNamespaceLoc.empty() && attrLoc.isValid())
98 Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute);
99
Chris Lattnera5235172007-08-25 06:57:03 +0000100 // Read label attributes, if present.
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000101 if (Tok.is(tok::kw___attribute)) {
Aaron Ballman730476b2014-11-08 15:33:35 +0000102 attrLoc = Tok.getLocation();
John McCall53fa7142010-12-24 02:08:15 +0000103 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000104 }
Mike Stump11289f42009-09-09 15:08:12 +0000105
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000106 if (Tok.is(tok::equal)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000107 if (!Ident) {
Alp Tokerec543272013-12-24 09:48:30 +0000108 Diag(Tok, diag::err_expected) << tok::identifier;
Nico Weber729f1e22012-10-27 23:44:27 +0000109 // Skip to end of the definition and eat the ';'.
110 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000111 return nullptr;
Nico Weber729f1e22012-10-27 23:44:27 +0000112 }
Aaron Ballman730476b2014-11-08 15:33:35 +0000113 if (attrLoc.isValid())
114 Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +0000115 if (InlineLoc.isValid())
116 Diag(InlineLoc, diag::err_inline_namespace_alias)
117 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000118 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000119 }
Mike Stump11289f42009-09-09 15:08:12 +0000120
Richard Trieu61384cb2011-05-26 20:11:09 +0000121
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000122 BalancedDelimiterTracker T(*this, tok::l_brace);
123 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000124 if (Ident)
125 Diag(Tok, diag::err_expected) << tok::l_brace;
126 else
127 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Craig Topper161e4db2014-05-21 06:02:52 +0000128 return nullptr;
Chris Lattnera5235172007-08-25 06:57:03 +0000129 }
Mike Stump11289f42009-09-09 15:08:12 +0000130
Douglas Gregor0be31a22010-07-02 17:43:08 +0000131 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
132 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
133 getCurScope()->getFnParent()) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000134 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000135 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +0000136 return nullptr;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000137 }
138
Richard Smith13307f52014-11-08 05:37:34 +0000139 if (ExtraIdent.empty()) {
140 // Normal namespace definition, not a nested-namespace-definition.
141 } else if (InlineLoc.isValid()) {
142 Diag(InlineLoc, diag::err_inline_nested_namespace_definition);
143 } else if (getLangOpts().CPlusPlus1z) {
144 Diag(ExtraNamespaceLoc[0],
145 diag::warn_cxx14_compat_nested_namespace_definition);
146 } else {
Richard Trieu61384cb2011-05-26 20:11:09 +0000147 TentativeParsingAction TPA(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000148 SkipUntil(tok::r_brace, StopBeforeMatch);
Richard Trieu61384cb2011-05-26 20:11:09 +0000149 Token rBraceToken = Tok;
150 TPA.Revert();
151
152 if (!rBraceToken.is(tok::r_brace)) {
Richard Smith13307f52014-11-08 05:37:34 +0000153 Diag(ExtraNamespaceLoc[0], diag::ext_nested_namespace_definition)
Richard Trieu61384cb2011-05-26 20:11:09 +0000154 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
155 } else {
Benjamin Kramerf546f412011-05-26 21:32:30 +0000156 std::string NamespaceFix;
Richard Trieu61384cb2011-05-26 20:11:09 +0000157 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
158 E = ExtraIdent.end(); I != E; ++I) {
159 NamespaceFix += " { namespace ";
160 NamespaceFix += (*I)->getName();
161 }
Benjamin Kramerf546f412011-05-26 21:32:30 +0000162
Richard Trieu61384cb2011-05-26 20:11:09 +0000163 std::string RBraces;
Benjamin Kramerf546f412011-05-26 21:32:30 +0000164 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieu61384cb2011-05-26 20:11:09 +0000165 RBraces += "} ";
Benjamin Kramerf546f412011-05-26 21:32:30 +0000166
Richard Smith13307f52014-11-08 05:37:34 +0000167 Diag(ExtraNamespaceLoc[0], diag::ext_nested_namespace_definition)
Richard Trieu61384cb2011-05-26 20:11:09 +0000168 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
169 ExtraIdentLoc.back()),
170 NamespaceFix)
171 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
172 }
173 }
174
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000175 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith5d164bc2011-10-15 05:09:34 +0000176 if (InlineLoc.isValid())
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000177 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000178 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000179
Chris Lattner4de55aa2009-03-29 14:02:43 +0000180 // Enter a scope for the namespace.
181 ParseScope NamespaceScope(this, Scope::DeclScope);
182
John McCall48871652010-08-21 09:40:31 +0000183 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000184 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000185 IdentLoc, Ident, T.getOpenLocation(),
186 attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000187
John McCallfaf5fb42010-08-26 23:41:50 +0000188 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
189 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000190
Richard Trieu61384cb2011-05-26 20:11:09 +0000191 // Parse the contents of the namespace. This includes parsing recovery on
192 // any improperly nested namespaces.
193 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000194 InlineLoc, attrs, T);
Mike Stump11289f42009-09-09 15:08:12 +0000195
Chris Lattner4de55aa2009-03-29 14:02:43 +0000196 // Leave the namespace scope.
197 NamespaceScope.Exit();
198
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000199 DeclEnd = T.getCloseLocation();
200 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000201
202 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000203}
Chris Lattner38376f12008-01-12 07:05:38 +0000204
Richard Trieu61384cb2011-05-26 20:11:09 +0000205/// ParseInnerNamespace - Parse the contents of a namespace.
Richard Smith13307f52014-11-08 05:37:34 +0000206void Parser::ParseInnerNamespace(std::vector<SourceLocation> &IdentLoc,
207 std::vector<IdentifierInfo *> &Ident,
208 std::vector<SourceLocation> &NamespaceLoc,
209 unsigned int index, SourceLocation &InlineLoc,
210 ParsedAttributes &attrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000211 BalancedDelimiterTracker &Tracker) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000212 if (index == Ident.size()) {
Richard Smith34f30512013-11-23 04:06:09 +0000213 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000214 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000215 MaybeParseCXX11Attributes(attrs);
Richard Trieu61384cb2011-05-26 20:11:09 +0000216 MaybeParseMicrosoftAttributes(attrs);
217 ParseExternalDeclaration(attrs);
218 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000219
220 // The caller is what called check -- we are simply calling
221 // the close for it.
222 Tracker.consumeClose();
Richard Trieu61384cb2011-05-26 20:11:09 +0000223
224 return;
225 }
226
Richard Smith13307f52014-11-08 05:37:34 +0000227 // Handle a nested namespace definition.
228 // FIXME: Preserve the source information through to the AST rather than
229 // desugaring it here.
Richard Trieu61384cb2011-05-26 20:11:09 +0000230 ParseScope NamespaceScope(this, Scope::DeclScope);
231 Decl *NamespcDecl =
232 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
233 NamespaceLoc[index], IdentLoc[index],
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000234 Ident[index], Tracker.getOpenLocation(),
235 attrs.getList());
Richard Trieu61384cb2011-05-26 20:11:09 +0000236
237 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000238 attrs, Tracker);
Richard Trieu61384cb2011-05-26 20:11:09 +0000239
240 NamespaceScope.Exit();
241
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000242 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieu61384cb2011-05-26 20:11:09 +0000243}
244
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000245/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
246/// alias definition.
247///
John McCall48871652010-08-21 09:40:31 +0000248Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000249 SourceLocation AliasLoc,
250 IdentifierInfo *Alias,
251 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000252 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000253
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000254 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000255
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000256 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000257 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000258 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000259 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000260 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000261
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000262 CXXScopeSpec SS;
263 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000264 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000265
266 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
267 Diag(Tok, diag::err_expected_namespace_name);
268 // Skip to end of the definition and eat the ';'.
269 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000270 return nullptr;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000271 }
272
273 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000274 IdentifierInfo *Ident = Tok.getIdentifierInfo();
275 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000276
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000277 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000278 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000279 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
280 SkipUntil(tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000281
Douglas Gregor0be31a22010-07-02 17:43:08 +0000282 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000283 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000284}
285
Chris Lattner38376f12008-01-12 07:05:38 +0000286/// ParseLinkage - We know that the current token is a string_literal
287/// and just before that, that extern was seen.
288///
289/// linkage-specification: [C++ 7.5p2: dcl.link]
290/// 'extern' string-literal '{' declaration-seq[opt] '}'
291/// 'extern' string-literal declaration
292///
Chris Lattner8ea64422010-11-09 20:15:55 +0000293Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Richard Smith4ee696d2014-02-17 23:25:27 +0000294 assert(isTokenStringLiteral() && "Not a string literal!");
295 ExprResult Lang = ParseStringLiteralExpression(false);
Chris Lattner38376f12008-01-12 07:05:38 +0000296
Douglas Gregor07665a62009-01-05 19:45:36 +0000297 ParseScope LinkageScope(this, Scope::DeclScope);
Richard Smith4ee696d2014-02-17 23:25:27 +0000298 Decl *LinkageSpec =
299 Lang.isInvalid()
Craig Topper161e4db2014-05-21 06:02:52 +0000300 ? nullptr
Richard Smith4ee696d2014-02-17 23:25:27 +0000301 : Actions.ActOnStartLinkageSpecification(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000302 getCurScope(), DS.getSourceRange().getBegin(), Lang.get(),
Richard Smith4ee696d2014-02-17 23:25:27 +0000303 Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
Douglas Gregor07665a62009-01-05 19:45:36 +0000304
John McCall084e83d2011-03-24 11:26:52 +0000305 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000306 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000307 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000308
Douglas Gregor07665a62009-01-05 19:45:36 +0000309 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara4d423992011-05-01 16:25:54 +0000310 // Reset the source range in DS, as the leading "extern"
311 // does not really belong to the inner declaration ...
312 DS.SetRangeStart(SourceLocation());
313 DS.SetRangeEnd(SourceLocation());
314 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000315 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000316 ParseExternalDeclaration(attrs, &DS);
Richard Smith4ee696d2014-02-17 23:25:27 +0000317 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
318 getCurScope(), LinkageSpec, SourceLocation())
Craig Topper161e4db2014-05-21 06:02:52 +0000319 : nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000320 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000321
Douglas Gregorb65a9132010-02-07 08:38:28 +0000322 DS.abort();
323
John McCall53fa7142010-12-24 02:08:15 +0000324 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000325
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000326 BalancedDelimiterTracker T(*this, tok::l_brace);
327 T.consumeOpen();
Richard Smith77944862014-03-02 05:58:18 +0000328
329 unsigned NestedModules = 0;
330 while (true) {
331 switch (Tok.getKind()) {
332 case tok::annot_module_begin:
333 ++NestedModules;
334 ParseTopLevelDecl();
335 continue;
336
337 case tok::annot_module_end:
338 if (!NestedModules)
339 break;
340 --NestedModules;
341 ParseTopLevelDecl();
342 continue;
343
344 case tok::annot_module_include:
345 ParseTopLevelDecl();
346 continue;
347
348 case tok::eof:
349 break;
350
351 case tok::r_brace:
352 if (!NestedModules)
353 break;
354 // Fall through.
355 default:
356 ParsedAttributesWithRange attrs(AttrFactory);
357 MaybeParseCXX11Attributes(attrs);
358 MaybeParseMicrosoftAttributes(attrs);
359 ParseExternalDeclaration(attrs);
360 continue;
361 }
362
363 break;
Chris Lattner38376f12008-01-12 07:05:38 +0000364 }
365
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000366 T.consumeClose();
Richard Smith4ee696d2014-02-17 23:25:27 +0000367 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
368 getCurScope(), LinkageSpec, T.getCloseLocation())
Craig Topper161e4db2014-05-21 06:02:52 +0000369 : nullptr;
Chris Lattner38376f12008-01-12 07:05:38 +0000370}
Douglas Gregor556877c2008-04-13 21:30:24 +0000371
Douglas Gregord7c4d982008-12-30 03:27:21 +0000372/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
373/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000374Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000375 const ParsedTemplateInfo &TemplateInfo,
376 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000377 ParsedAttributesWithRange &attrs,
378 Decl **OwnedType) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000379 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000380 ObjCDeclContextSwitch ObjCDC(*this);
381
Douglas Gregord7c4d982008-12-30 03:27:21 +0000382 // Eat 'using'.
383 SourceLocation UsingLoc = ConsumeToken();
384
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000385 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000386 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000387 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000388 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000389 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000390
John McCall9b72f892010-11-10 02:40:36 +0000391 // 'using namespace' means this is a using-directive.
392 if (Tok.is(tok::kw_namespace)) {
393 // Template parameters are always an error here.
394 if (TemplateInfo.Kind) {
395 SourceRange R = TemplateInfo.getSourceRange();
396 Diag(UsingLoc, diag::err_templated_using_directive)
397 << R << FixItHint::CreateRemoval(R);
398 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000399
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000400 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000401 }
402
Richard Smithdda56e42011-04-15 14:24:37 +0000403 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000404
405 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000406 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000407
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000408 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000409 AS_none, OwnedType);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000410}
411
412/// ParseUsingDirective - Parse C++ using-directive, assumes
413/// that current token is 'namespace' and 'using' was already parsed.
414///
415/// using-directive: [C++ 7.3.p4: namespace.udir]
416/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
417/// namespace-name ;
418/// [GNU] using-directive:
419/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
420/// namespace-name attributes[opt] ;
421///
John McCall48871652010-08-21 09:40:31 +0000422Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000423 SourceLocation UsingLoc,
424 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000425 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000426 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
427
428 // Eat 'namespace'.
429 SourceLocation NamespcLoc = ConsumeToken();
430
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000431 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000432 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000433 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000434 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000435 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000436
Douglas Gregord7c4d982008-12-30 03:27:21 +0000437 CXXScopeSpec SS;
438 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000439 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000440
Craig Topper161e4db2014-05-21 06:02:52 +0000441 IdentifierInfo *NamespcName = nullptr;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000442 SourceLocation IdentLoc = SourceLocation();
443
444 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000445 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000446 Diag(Tok, diag::err_expected_namespace_name);
447 // If there was invalid namespace name, skip to end of decl, and eat ';'.
448 SkipUntil(tok::semi);
449 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Craig Topper161e4db2014-05-21 06:02:52 +0000450 return nullptr;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000451 }
Mike Stump11289f42009-09-09 15:08:12 +0000452
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000453 // Parse identifier.
454 NamespcName = Tok.getIdentifierInfo();
455 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000456
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000457 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000458 bool GNUAttr = false;
459 if (Tok.is(tok::kw___attribute)) {
460 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000461 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000462 }
Mike Stump11289f42009-09-09 15:08:12 +0000463
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000464 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000465 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000466 if (ExpectAndConsume(tok::semi,
467 GNUAttr ? diag::err_expected_semi_after_attribute_list
468 : diag::err_expected_semi_after_namespace_name))
469 SkipUntil(tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000470
Douglas Gregor0be31a22010-07-02 17:43:08 +0000471 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000472 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000473}
474
Richard Smithdda56e42011-04-15 14:24:37 +0000475/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
476/// Assumes that 'using' was already seen.
Douglas Gregord7c4d982008-12-30 03:27:21 +0000477///
478/// using-declaration: [C++ 7.3.p3: namespace.udecl]
479/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000480/// unqualified-id
481/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000482///
Richard Smith810ad3e2013-01-29 10:02:16 +0000483/// alias-declaration: C++11 [dcl.dcl]p1
484/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smithdda56e42011-04-15 14:24:37 +0000485///
John McCall48871652010-08-21 09:40:31 +0000486Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000487 const ParsedTemplateInfo &TemplateInfo,
488 SourceLocation UsingLoc,
489 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000490 AccessSpecifier AS,
491 Decl **OwnedType) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000492 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000493 SourceLocation TypenameLoc;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000494 bool HasTypenameKeyword = false;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000495
Richard Smithc2c8bb82013-10-15 01:34:54 +0000496 // Check for misplaced attributes before the identifier in an
497 // alias-declaration.
498 ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
499 MaybeParseCXX11Attributes(MisplacedAttrs);
Douglas Gregorfec52632009-06-20 00:51:54 +0000500
501 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000502 // FIXME: This is wrong; we should parse this as a typename-specifier.
Alp Toker97650562014-01-10 11:19:30 +0000503 if (TryConsumeToken(tok::kw_typename, TypenameLoc))
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000504 HasTypenameKeyword = true;
Douglas Gregorfec52632009-06-20 00:51:54 +0000505
Nikola Smiljanic67860242014-09-26 00:28:20 +0000506 if (Tok.is(tok::kw___super)) {
507 Diag(Tok.getLocation(), diag::err_super_in_using_declaration);
508 SkipUntil(tok::semi);
509 return nullptr;
510 }
511
Douglas Gregorfec52632009-06-20 00:51:54 +0000512 // Parse nested-name-specifier.
Craig Topper161e4db2014-05-21 06:02:52 +0000513 IdentifierInfo *LastII = nullptr;
Richard Smith7447af42013-03-26 01:15:19 +0000514 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
Craig Topper161e4db2014-05-21 06:02:52 +0000515 /*MayBePseudoDtor=*/nullptr,
516 /*IsTypename=*/false,
Richard Smith7447af42013-03-26 01:15:19 +0000517 /*LastII=*/&LastII);
Douglas Gregorfec52632009-06-20 00:51:54 +0000518
Douglas Gregorfec52632009-06-20 00:51:54 +0000519 // Check nested-name specifier.
520 if (SS.isInvalid()) {
521 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000522 return nullptr;
Douglas Gregorfec52632009-06-20 00:51:54 +0000523 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000524
Richard Smith7447af42013-03-26 01:15:19 +0000525 SourceLocation TemplateKWLoc;
526 UnqualifiedId Name;
527
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000528 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000529 // destructor names and allow the action module to diagnose any semantic
530 // errors.
Richard Smith7447af42013-03-26 01:15:19 +0000531 //
532 // C++11 [class.qual]p2:
533 // [...] in a using-declaration that is a member-declaration, if the name
534 // specified after the nested-name-specifier is the same as the identifier
535 // or the simple-template-id's template-name in the last component of the
536 // nested-name-specifier, the name is [...] considered to name the
537 // constructor.
538 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
539 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
540 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
541 !SS.getScopeRep()->getAsNamespace() &&
542 !SS.getScopeRep()->getAsNamespaceAlias()) {
543 SourceLocation IdLoc = ConsumeToken();
544 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
545 Name.setConstructorName(Type, IdLoc, IdLoc);
546 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
547 /*AllowDestructorName=*/ true,
548 /*AllowConstructorName=*/ true, ParsedType(),
549 TemplateKWLoc, Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000550 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000551 return nullptr;
Douglas Gregorfec52632009-06-20 00:51:54 +0000552 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000553
Richard Smithc2c8bb82013-10-15 01:34:54 +0000554 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith37a45dd2013-10-24 01:21:09 +0000555 MaybeParseGNUAttributes(Attrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000556 MaybeParseCXX11Attributes(Attrs);
Richard Smithdda56e42011-04-15 14:24:37 +0000557
558 // Maybe this is an alias-declaration.
Richard Smithdda56e42011-04-15 14:24:37 +0000559 TypeResult TypeAlias;
Richard Smithc2c8bb82013-10-15 01:34:54 +0000560 bool IsAliasDecl = Tok.is(tok::equal);
Richard Smithdda56e42011-04-15 14:24:37 +0000561 if (IsAliasDecl) {
Richard Smithc2c8bb82013-10-15 01:34:54 +0000562 // If we had any misplaced attributes from earlier, this is where they
563 // should have been written.
564 if (MisplacedAttrs.Range.isValid()) {
565 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
566 << FixItHint::CreateInsertionFromRange(
567 Tok.getLocation(),
568 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
569 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
570 Attrs.takeAllFrom(MisplacedAttrs);
571 }
572
Richard Smithdda56e42011-04-15 14:24:37 +0000573 ConsumeToken();
574
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000575 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000576 diag::warn_cxx98_compat_alias_declaration :
577 diag::ext_alias_declaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000578
Richard Smith3f1b5d02011-05-05 21:57:07 +0000579 // Type alias templates cannot be specialized.
580 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000581 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
582 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000583 SpecKind = 0;
584 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
585 SpecKind = 1;
586 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
587 SpecKind = 2;
588 if (SpecKind != -1) {
589 SourceRange Range;
590 if (SpecKind == 0)
591 Range = SourceRange(Name.TemplateId->LAngleLoc,
592 Name.TemplateId->RAngleLoc);
593 else
594 Range = TemplateInfo.getSourceRange();
595 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
596 << SpecKind << Range;
597 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000598 return nullptr;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000599 }
600
Richard Smithdda56e42011-04-15 14:24:37 +0000601 // Name must be an identifier.
602 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
603 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
604 // No removal fixit: can't recover from this.
605 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000606 return nullptr;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000607 } else if (HasTypenameKeyword)
Richard Smithdda56e42011-04-15 14:24:37 +0000608 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
609 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
610 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
611 else if (SS.isNotEmpty())
612 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
613 << FixItHint::CreateRemoval(SS.getRange());
614
Craig Topper161e4db2014-05-21 06:02:52 +0000615 TypeAlias = ParseTypeName(nullptr, TemplateInfo.Kind ?
Richard Smith3f1b5d02011-05-05 21:57:07 +0000616 Declarator::AliasTemplateContext :
Richard Smith54ecd982013-02-20 19:22:51 +0000617 Declarator::AliasDeclContext, AS, OwnedType,
618 &Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000619 } else {
620 // C++11 attributes are not allowed on a using-declaration, but GNU ones
621 // are.
Richard Smithc2c8bb82013-10-15 01:34:54 +0000622 ProhibitAttributes(MisplacedAttrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000623 ProhibitAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000624
Richard Smithdda56e42011-04-15 14:24:37 +0000625 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith54ecd982013-02-20 19:22:51 +0000626 MaybeParseGNUAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000627 }
Mike Stump11289f42009-09-09 15:08:12 +0000628
Douglas Gregorfec52632009-06-20 00:51:54 +0000629 // Eat ';'.
630 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000631 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
632 !Attrs.empty() ? "attributes list"
633 : IsAliasDecl ? "alias declaration"
634 : "using declaration"))
635 SkipUntil(tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000636
John McCall9b72f892010-11-10 02:40:36 +0000637 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000638 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000639 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000640 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-11-10 02:40:36 +0000641 SourceRange R = TemplateInfo.getSourceRange();
642 Diag(UsingLoc, diag::err_templated_using_declaration)
643 << R << FixItHint::CreateRemoval(R);
644
645 // Unfortunately, we have to bail out instead of recovering by
646 // ignoring the parameters, just in case the nested name specifier
647 // depends on the parameters.
Craig Topper161e4db2014-05-21 06:02:52 +0000648 return nullptr;
John McCall9b72f892010-11-10 02:40:36 +0000649 }
650
Douglas Gregor882a61a2011-09-26 14:30:28 +0000651 // "typename" keyword is allowed for identifiers only,
652 // because it may be a type definition.
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000653 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor882a61a2011-09-26 14:30:28 +0000654 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
655 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000656 // Proceed parsing, but reset the HasTypenameKeyword flag.
657 HasTypenameKeyword = false;
Douglas Gregor882a61a2011-09-26 14:30:28 +0000658 }
659
Richard Smith3f1b5d02011-05-05 21:57:07 +0000660 if (IsAliasDecl) {
661 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000662 MultiTemplateParamsArg TemplateParamsArg(
Craig Topper161e4db2014-05-21 06:02:52 +0000663 TemplateParams ? TemplateParams->data() : nullptr,
Richard Smith3f1b5d02011-05-05 21:57:07 +0000664 TemplateParams ? TemplateParams->size() : 0);
665 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith54ecd982013-02-20 19:22:51 +0000666 UsingLoc, Name, Attrs.getList(),
667 TypeAlias);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000668 }
Richard Smithdda56e42011-04-15 14:24:37 +0000669
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000670 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
671 /* HasUsingKeyword */ true, UsingLoc,
672 SS, Name, Attrs.getList(),
673 HasTypenameKeyword, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000674}
675
Benjamin Kramere56f3932011-12-23 17:00:35 +0000676/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000677///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000678/// [C++0x] static_assert-declaration:
679/// static_assert ( constant-expression , string-literal ) ;
680///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000681/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000682/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000683///
John McCall48871652010-08-21 09:40:31 +0000684Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000685 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
686 "Not a static_assert declaration");
687
David Blaikiebbafb8a2012-03-11 07:00:24 +0000688 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000689 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000690 if (Tok.is(tok::kw_static_assert))
691 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000692
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000693 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000694
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000695 BalancedDelimiterTracker T(*this, tok::l_paren);
696 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000697 Diag(Tok, diag::err_expected) << tok::l_paren;
Richard Smith76965712012-09-13 19:12:50 +0000698 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000699 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000700 }
Mike Stump11289f42009-09-09 15:08:12 +0000701
John McCalldadc5752010-08-24 06:29:42 +0000702 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000703 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000704 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000705 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000706 }
Mike Stump11289f42009-09-09 15:08:12 +0000707
Richard Smith085a64f2014-06-20 19:57:12 +0000708 ExprResult AssertMessage;
709 if (Tok.is(tok::r_paren)) {
710 Diag(Tok, getLangOpts().CPlusPlus1z
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000711 ? diag::warn_cxx14_compat_static_assert_no_message
Richard Smith085a64f2014-06-20 19:57:12 +0000712 : diag::ext_static_assert_no_message)
713 << (getLangOpts().CPlusPlus1z
714 ? FixItHint()
715 : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\""));
716 } else {
717 if (ExpectAndConsume(tok::comma)) {
718 SkipUntil(tok::semi);
719 return nullptr;
720 }
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000721
Richard Smith085a64f2014-06-20 19:57:12 +0000722 if (!isTokenStringLiteral()) {
723 Diag(Tok, diag::err_expected_string_literal)
724 << /*Source='static_assert'*/1;
725 SkipMalformedDecl();
726 return nullptr;
727 }
Mike Stump11289f42009-09-09 15:08:12 +0000728
Richard Smith085a64f2014-06-20 19:57:12 +0000729 AssertMessage = ParseStringLiteralExpression();
730 if (AssertMessage.isInvalid()) {
731 SkipMalformedDecl();
732 return nullptr;
733 }
Richard Smithd67aea22012-03-06 03:21:47 +0000734 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000735
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000736 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000737
Chris Lattner49836b42009-04-02 04:16:50 +0000738 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000739 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000740
John McCallb268a282010-08-23 23:25:46 +0000741 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000742 AssertExpr.get(),
743 AssertMessage.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000744 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000745}
746
Richard Smith74aeef52013-04-26 16:15:35 +0000747/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000748///
749/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000750/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000751///
David Blaikie15a430a2011-12-04 05:04:18 +0000752SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
753 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
754 && "Not a decltype specifier");
755
David Blaikie15a430a2011-12-04 05:04:18 +0000756 ExprResult Result;
757 SourceLocation StartLoc = Tok.getLocation();
758 SourceLocation EndLoc;
759
760 if (Tok.is(tok::annot_decltype)) {
761 Result = getExprAnnotation(Tok);
762 EndLoc = Tok.getAnnotationEndLoc();
763 ConsumeToken();
764 if (Result.isInvalid()) {
765 DS.SetTypeSpecError();
766 return EndLoc;
767 }
768 } else {
Richard Smith324df552012-02-24 22:30:04 +0000769 if (Tok.getIdentifierInfo()->isStr("decltype"))
770 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000771
David Blaikie15a430a2011-12-04 05:04:18 +0000772 ConsumeToken();
773
774 BalancedDelimiterTracker T(*this, tok::l_paren);
775 if (T.expectAndConsume(diag::err_expected_lparen_after,
776 "decltype", tok::r_paren)) {
777 DS.SetTypeSpecError();
778 return T.getOpenLocation() == Tok.getLocation() ?
779 StartLoc : T.getOpenLocation();
780 }
781
Richard Smith74aeef52013-04-26 16:15:35 +0000782 // Check for C++1y 'decltype(auto)'.
783 if (Tok.is(tok::kw_auto)) {
784 // No need to disambiguate here: an expression can't start with 'auto',
785 // because the typename-specifier in a function-style cast operation can't
786 // be 'auto'.
787 Diag(Tok.getLocation(),
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000788 getLangOpts().CPlusPlus14
Richard Smith74aeef52013-04-26 16:15:35 +0000789 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
790 : diag::ext_decltype_auto_type_specifier);
791 ConsumeToken();
792 } else {
793 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000794
Richard Smith74aeef52013-04-26 16:15:35 +0000795 // C++11 [dcl.type.simple]p4:
796 // The operand of the decltype specifier is an unevaluated operand.
797 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
Craig Topper161e4db2014-05-21 06:02:52 +0000798 nullptr,/*IsDecltype=*/true);
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000799 Result = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Richard Smith74aeef52013-04-26 16:15:35 +0000800 if (Result.isInvalid()) {
801 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000802 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smith74aeef52013-04-26 16:15:35 +0000803 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000804 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000805 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
806 // Backtrack to get the location of the last token before the semi.
807 PP.RevertCachedTokens(2);
808 ConsumeToken(); // the semi.
809 EndLoc = ConsumeAnyToken();
810 assert(Tok.is(tok::semi));
811 } else {
812 EndLoc = Tok.getLocation();
813 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000814 }
Richard Smith74aeef52013-04-26 16:15:35 +0000815 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000816 }
Richard Smith74aeef52013-04-26 16:15:35 +0000817
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000818 Result = Actions.ActOnDecltypeExpression(Result.get());
David Blaikie15a430a2011-12-04 05:04:18 +0000819 }
820
821 // Match the ')'
822 T.consumeClose();
823 if (T.getCloseLocation().isInvalid()) {
824 DS.SetTypeSpecError();
825 // FIXME: this should return the location of the last token
826 // that was consumed (by "consumeClose()")
827 return T.getCloseLocation();
828 }
829
Richard Smithfd555f62012-02-22 02:04:18 +0000830 if (Result.isInvalid()) {
831 DS.SetTypeSpecError();
832 return T.getCloseLocation();
833 }
834
David Blaikie15a430a2011-12-04 05:04:18 +0000835 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000836 }
Richard Smith74aeef52013-04-26 16:15:35 +0000837 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000838
Craig Topper161e4db2014-05-21 06:02:52 +0000839 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +0000840 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000841 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Anders Carlsson74948d02009-06-24 17:47:40 +0000842 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000843 if (Result.get()
844 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000845 DiagID, Result.get(), Policy)
Richard Smith74aeef52013-04-26 16:15:35 +0000846 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000847 DiagID, Policy)) {
John McCall49bfce42009-08-03 20:12:06 +0000848 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000849 DS.SetTypeSpecError();
850 }
851 return EndLoc;
852}
853
854void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
855 SourceLocation StartLoc,
856 SourceLocation EndLoc) {
857 // make sure we have a token we can turn into an annotation token
858 if (PP.isBacktrackEnabled())
859 PP.RevertCachedTokens(1);
860 else
861 PP.EnterToken(Tok);
862
863 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000864 setExprAnnotation(Tok,
865 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
866 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
867 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000868 Tok.setAnnotationEndLoc(EndLoc);
869 Tok.setLocation(StartLoc);
870 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000871}
872
Alexis Hunt4a257072011-05-19 05:37:45 +0000873void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
874 assert(Tok.is(tok::kw___underlying_type) &&
875 "Not an underlying type specifier");
876
877 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000878 BalancedDelimiterTracker T(*this, tok::l_paren);
879 if (T.expectAndConsume(diag::err_expected_lparen_after,
880 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +0000881 return;
882 }
883
884 TypeResult Result = ParseTypeName();
885 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000886 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt4a257072011-05-19 05:37:45 +0000887 return;
888 }
889
890 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000891 T.consumeClose();
892 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000893 return;
894
Craig Topper161e4db2014-05-21 06:02:52 +0000895 const char *PrevSpec = nullptr;
Alexis Hunt4a257072011-05-19 05:37:45 +0000896 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000897 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000898 DiagID, Result.get(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000899 Actions.getASTContext().getPrintingPolicy()))
Alexis Hunt4a257072011-05-19 05:37:45 +0000900 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanellaa90af722013-07-06 18:54:58 +0000901 DS.setTypeofParensRange(T.getRange());
Alexis Hunt4a257072011-05-19 05:37:45 +0000902}
903
David Blaikie00ee7a082011-10-25 15:01:20 +0000904/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
905/// class name or decltype-specifier. Note that we only check that the result
906/// names a type; semantic analysis will need to verify that the type names a
907/// class. The result is either a type or null, depending on whether a type
908/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +0000909///
Richard Smith4c96e992013-02-19 23:47:15 +0000910/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000911/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +0000912/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000913/// nested-name-specifier[opt] class-name
914/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +0000915/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +0000916/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000917/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000918///
Richard Smith4c96e992013-02-19 23:47:15 +0000919/// In C++98, instead of base-type-specifier, we have:
920///
921/// ::[opt] nested-name-specifier[opt] class-name
Craig Topper9ad7e262014-10-31 06:57:07 +0000922TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
923 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +0000924 // Ignore attempts to use typename
925 if (Tok.is(tok::kw_typename)) {
926 Diag(Tok, diag::err_expected_class_name_not_template)
927 << FixItHint::CreateRemoval(Tok.getLocation());
928 ConsumeToken();
929 }
930
David Blaikieafa155f2011-10-25 18:17:58 +0000931 // Parse optional nested-name-specifier
932 CXXScopeSpec SS;
933 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
934
935 BaseLoc = Tok.getLocation();
936
David Blaikie1cd50022011-10-25 17:10:12 +0000937 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +0000938 // tok == kw_decltype is just error recovery, it can only happen when SS
939 // isn't empty
940 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +0000941 if (SS.isNotEmpty())
942 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
943 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +0000944 // Fake up a Declarator to use with ActOnTypeName.
945 DeclSpec DS(AttrFactory);
946
David Blaikie7491e732011-12-08 04:53:15 +0000947 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +0000948
949 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
950 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
951 }
952
Douglas Gregord54dfb82009-02-25 23:52:28 +0000953 // Check whether we have a template-id that names a type.
954 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000955 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +0000956 if (TemplateId->Kind == TNK_Type_template ||
957 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000958 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000959
960 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000961 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000962 EndLocation = Tok.getAnnotationEndLoc();
963 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000964
965 if (Type)
966 return Type;
967 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000968 }
969
970 // Fall through to produce an error below.
971 }
972
Douglas Gregor831c93f2008-11-05 20:51:48 +0000973 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000974 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000975 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000976 }
977
Douglas Gregor18473f32010-01-12 21:28:44 +0000978 IdentifierInfo *Id = Tok.getIdentifierInfo();
979 SourceLocation IdLoc = ConsumeToken();
980
981 if (Tok.is(tok::less)) {
982 // It looks the user intended to write a template-id here, but the
983 // template-name was wrong. Try to fix that.
984 TemplateNameKind TNK = TNK_Type_template;
985 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000986 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000987 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000988 Diag(IdLoc, diag::err_unknown_template_name)
989 << Id;
990 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000991
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000992 if (!Template) {
993 TemplateArgList TemplateArgs;
994 SourceLocation LAngleLoc, RAngleLoc;
995 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
996 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor18473f32010-01-12 21:28:44 +0000997 return true;
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000998 }
Douglas Gregor18473f32010-01-12 21:28:44 +0000999
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001000 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +00001001 UnqualifiedId TemplateName;
1002 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001003
Douglas Gregor18473f32010-01-12 21:28:44 +00001004 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001005 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1006 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +00001007 return true;
1008 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +00001009 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001010
Douglas Gregor18473f32010-01-12 21:28:44 +00001011 // If we didn't end up with a typename token, there's nothing more we
1012 // can do.
1013 if (Tok.isNot(tok::annot_typename))
1014 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001015
Douglas Gregor18473f32010-01-12 21:28:44 +00001016 // Retrieve the type from the annotation token, consume that token, and
1017 // return.
1018 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +00001019 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +00001020 ConsumeToken();
1021 return Type;
1022 }
1023
Douglas Gregor831c93f2008-11-05 20:51:48 +00001024 // We have an identifier; check whether it is actually a type.
Craig Topper161e4db2014-05-21 06:02:52 +00001025 IdentifierInfo *CorrectedII = nullptr;
Douglas Gregore7c20652011-03-02 00:47:37 +00001026 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +00001027 false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00001028 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +00001029 /*NonTrivialTypeSourceInfo=*/true,
1030 &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001031 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001032 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001033 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +00001034 }
1035
1036 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +00001037 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001038
1039 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +00001040 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +00001041 DS.SetRangeStart(IdLoc);
1042 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +00001043 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001044
Craig Topper161e4db2014-05-21 06:02:52 +00001045 const char *PrevSpec = nullptr;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001046 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001047 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
1048 Actions.getASTContext().getPrintingPolicy());
Nick Lewycky19b9f952010-07-26 16:56:01 +00001049
1050 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1051 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001052}
1053
John McCall8d32c052012-05-22 21:28:12 +00001054void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
1055 while (Tok.is(tok::kw___single_inheritance) ||
1056 Tok.is(tok::kw___multiple_inheritance) ||
1057 Tok.is(tok::kw___virtual_inheritance)) {
1058 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1059 SourceLocation AttrNameLoc = ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001060 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Aaron Ballman8edb5c22013-12-18 23:44:18 +00001061 AttributeList::AS_Keyword);
John McCall8d32c052012-05-22 21:28:12 +00001062 }
1063}
1064
Richard Smith369b9f92012-06-25 21:37:02 +00001065/// Determine whether the following tokens are valid after a type-specifier
1066/// which could be a standalone declaration. This will conservatively return
1067/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +00001068bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +00001069 // This switch enumerates the valid "follow" set for type-specifiers.
1070 switch (Tok.getKind()) {
1071 default: break;
1072 case tok::semi: // struct foo {...} ;
1073 case tok::star: // struct foo {...} * P;
1074 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +00001075 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +00001076 case tok::identifier: // struct foo {...} V ;
1077 case tok::r_paren: //(struct foo {...} ) {4}
1078 case tok::annot_cxxscope: // struct foo {...} a:: b;
1079 case tok::annot_typename: // struct foo {...} a ::b;
1080 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1081 case tok::l_paren: // struct foo {...} ( x);
1082 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001083 case tok::kw_operator: // struct foo operator ++() {...}
Alp Tokerd3f79c52013-11-24 20:24:54 +00001084 case tok::kw___declspec: // struct foo {...} __declspec(...)
Richard Smith843f18f2014-08-13 02:13:15 +00001085 case tok::l_square: // void f(struct f [ 3])
1086 case tok::ellipsis: // void f(struct f ... [Ns])
Abramo Bagnara152eb392014-08-16 08:29:27 +00001087 // FIXME: we should emit semantic diagnostic when declaration
1088 // attribute is in type attribute position.
1089 case tok::kw___attribute: // struct foo __attribute__((used)) x;
Richard Smith369b9f92012-06-25 21:37:02 +00001090 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001091 case tok::colon:
1092 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smith369b9f92012-06-25 21:37:02 +00001093 // Type qualifiers
1094 case tok::kw_const: // struct foo {...} const x;
1095 case tok::kw_volatile: // struct foo {...} volatile x;
1096 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith843f18f2014-08-13 02:13:15 +00001097 case tok::kw__Atomic: // struct foo {...} _Atomic x;
Nico Rieck3e1ee832014-12-04 23:30:25 +00001098 case tok::kw___unaligned: // struct foo {...} __unaligned *x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001099 // Function specifiers
1100 // Note, no 'explicit'. An explicit function must be either a conversion
1101 // operator or a constructor. Either way, it can't have a return type.
1102 case tok::kw_inline: // struct foo inline f();
1103 case tok::kw_virtual: // struct foo virtual f();
1104 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001105 // Storage-class specifiers
1106 case tok::kw_static: // struct foo {...} static x;
1107 case tok::kw_extern: // struct foo {...} extern x;
1108 case tok::kw_typedef: // struct foo {...} typedef x;
1109 case tok::kw_register: // struct foo {...} register x;
1110 case tok::kw_auto: // struct foo {...} auto x;
1111 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001112 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001113 case tok::kw_constexpr: // struct foo {...} constexpr x;
1114 // As shown above, type qualifiers and storage class specifiers absolutely
1115 // can occur after class specifiers according to the grammar. However,
1116 // almost no one actually writes code like this. If we see one of these,
1117 // it is much more likely that someone missed a semi colon and the
1118 // type/storage class specifier we're seeing is part of the *next*
1119 // intended declaration, as in:
1120 //
1121 // struct foo { ... }
1122 // typedef int X;
1123 //
1124 // We'd really like to emit a missing semicolon error instead of emitting
1125 // an error on the 'int' saying that you can't have two type specifiers in
1126 // the same declaration of X. Because of this, we look ahead past this
1127 // token to see if it's a type specifier. If so, we know the code is
1128 // otherwise invalid, so we can produce the expected semi error.
1129 if (!isKnownToBeTypeSpecifier(NextToken()))
1130 return true;
1131 break;
1132 case tok::r_brace: // struct bar { struct foo {...} }
1133 // Missing ';' at end of struct is accepted as an extension in C mode.
1134 if (!getLangOpts().CPlusPlus)
1135 return true;
1136 break;
Richard Smith52c5b872013-01-29 04:13:32 +00001137 case tok::greater:
1138 // template<class T = class X>
1139 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001140 }
1141 return false;
1142}
1143
Douglas Gregor556877c2008-04-13 21:30:24 +00001144/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1145/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1146/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001147/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001148///
1149/// class-specifier: [C++ class]
1150/// class-head '{' member-specification[opt] '}'
1151/// class-head '{' member-specification[opt] '}' attributes[opt]
1152/// class-head:
1153/// class-key identifier[opt] base-clause[opt]
1154/// class-key nested-name-specifier identifier base-clause[opt]
1155/// class-key nested-name-specifier[opt] simple-template-id
1156/// base-clause[opt]
1157/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001158/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001159/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001160/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001161/// simple-template-id base-clause[opt]
1162/// class-key:
1163/// 'class'
1164/// 'struct'
1165/// 'union'
1166///
1167/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001168/// class-key ::[opt] nested-name-specifier[opt] identifier
1169/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1170/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001171///
1172/// Note that the C++ class-specifier and elaborated-type-specifier,
1173/// together, subsume the C99 struct-or-union-specifier:
1174///
1175/// struct-or-union-specifier: [C99 6.7.2.1]
1176/// struct-or-union identifier[opt] '{' struct-contents '}'
1177/// struct-or-union identifier
1178/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1179/// '}' attributes[opt]
1180/// [GNU] struct-or-union attributes[opt] identifier
1181/// struct-or-union:
1182/// 'struct'
1183/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001184void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1185 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001186 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001187 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001188 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001189 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001190 DeclSpec::TST TagType;
1191 if (TagTokKind == tok::kw_struct)
1192 TagType = DeclSpec::TST_struct;
1193 else if (TagTokKind == tok::kw___interface)
1194 TagType = DeclSpec::TST_interface;
1195 else if (TagTokKind == tok::kw_class)
1196 TagType = DeclSpec::TST_class;
1197 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001198 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1199 TagType = DeclSpec::TST_union;
1200 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001201
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001202 if (Tok.is(tok::code_completion)) {
1203 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001204 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001205 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001206 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001207
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001208 // C++03 [temp.explicit] 14.7.2/8:
1209 // The usual access checking rules do not apply to names used to specify
1210 // explicit instantiations.
1211 //
1212 // As an extension we do not perform access checking on the names used to
1213 // specify explicit specializations either. This is important to allow
1214 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001215 //
1216 // Note that we don't suppress if this turns out to be an elaborated
1217 // type specifier.
1218 bool shouldDelayDiagsInTag =
1219 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1220 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1221 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001222
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001223 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001224 // If attributes exist after tag, parse them.
Richard Smith37a45dd2013-10-24 01:21:09 +00001225 MaybeParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +00001226
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001227 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +00001228 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +00001229 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001230
John McCall8d32c052012-05-22 21:28:12 +00001231 // Parse inheritance specifiers.
1232 if (Tok.is(tok::kw___single_inheritance) ||
1233 Tok.is(tok::kw___multiple_inheritance) ||
1234 Tok.is(tok::kw___virtual_inheritance))
Richard Smith37a45dd2013-10-24 01:21:09 +00001235 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCall8d32c052012-05-22 21:28:12 +00001236
Alexis Hunt96d5c762009-11-21 08:43:09 +00001237 // If C++0x attributes exist here, parse them.
1238 // FIXME: Are we consistent with the ordering of parsing of different
1239 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001240 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001241
Michael Han309af292013-01-07 16:57:11 +00001242 // Source location used by FIXIT to insert misplaced
1243 // C++11 attributes
1244 SourceLocation AttrFixitLoc = Tok.getLocation();
1245
Nico Weber7c3c5be2014-09-23 04:09:56 +00001246 if (TagType == DeclSpec::TST_struct &&
David Majnemer86330af2014-12-29 02:14:26 +00001247 Tok.isNot(tok::identifier) &&
1248 !Tok.isAnnotation() &&
Nico Weber7c3c5be2014-09-23 04:09:56 +00001249 Tok.getIdentifierInfo() &&
Nico Weberb10c9202014-09-24 03:28:54 +00001250 (Tok.is(tok::kw___is_abstract) ||
1251 Tok.is(tok::kw___is_arithmetic) ||
1252 Tok.is(tok::kw___is_array) ||
1253 Tok.is(tok::kw___is_base_of) ||
1254 Tok.is(tok::kw___is_class) ||
1255 Tok.is(tok::kw___is_complete_type) ||
1256 Tok.is(tok::kw___is_compound) ||
1257 Tok.is(tok::kw___is_const) ||
1258 Tok.is(tok::kw___is_constructible) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001259 Tok.is(tok::kw___is_convertible) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001260 Tok.is(tok::kw___is_convertible_to) ||
1261 Tok.is(tok::kw___is_destructible) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001262 Tok.is(tok::kw___is_empty) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001263 Tok.is(tok::kw___is_enum) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001264 Tok.is(tok::kw___is_floating_point) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001265 Tok.is(tok::kw___is_final) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001266 Tok.is(tok::kw___is_function) ||
1267 Tok.is(tok::kw___is_fundamental) ||
1268 Tok.is(tok::kw___is_integral) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001269 Tok.is(tok::kw___is_interface_class) ||
1270 Tok.is(tok::kw___is_literal) ||
1271 Tok.is(tok::kw___is_lvalue_expr) ||
1272 Tok.is(tok::kw___is_lvalue_reference) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001273 Tok.is(tok::kw___is_member_function_pointer) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001274 Tok.is(tok::kw___is_member_object_pointer) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001275 Tok.is(tok::kw___is_member_pointer) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001276 Tok.is(tok::kw___is_nothrow_assignable) ||
1277 Tok.is(tok::kw___is_nothrow_constructible) ||
1278 Tok.is(tok::kw___is_nothrow_destructible) ||
1279 Tok.is(tok::kw___is_object) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001280 Tok.is(tok::kw___is_pod) ||
1281 Tok.is(tok::kw___is_pointer) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001282 Tok.is(tok::kw___is_polymorphic) ||
1283 Tok.is(tok::kw___is_reference) ||
1284 Tok.is(tok::kw___is_rvalue_expr) ||
1285 Tok.is(tok::kw___is_rvalue_reference) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001286 Tok.is(tok::kw___is_same) ||
1287 Tok.is(tok::kw___is_scalar) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001288 Tok.is(tok::kw___is_sealed) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001289 Tok.is(tok::kw___is_signed) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001290 Tok.is(tok::kw___is_standard_layout) ||
1291 Tok.is(tok::kw___is_trivial) ||
1292 Tok.is(tok::kw___is_trivially_assignable) ||
1293 Tok.is(tok::kw___is_trivially_constructible) ||
1294 Tok.is(tok::kw___is_trivially_copyable) ||
1295 Tok.is(tok::kw___is_union) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001296 Tok.is(tok::kw___is_unsigned) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001297 Tok.is(tok::kw___is_void) ||
1298 Tok.is(tok::kw___is_volatile)))
Nico Weber7c3c5be2014-09-23 04:09:56 +00001299 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
1300 // name of struct templates, but some are keywords in GCC >= 4.3
1301 // and Clang. Therefore, when we see the token sequence "struct
1302 // X", make X into a normal identifier rather than a keyword, to
1303 // allow libstdc++ 4.2 and libc++ to work properly.
1304 TryKeywordIdentFallback(true);
Mike Stump11289f42009-09-09 15:08:12 +00001305
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001306 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001307 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001308 if (getLangOpts().CPlusPlus) {
Serge Pavlov458ea762014-07-16 05:16:52 +00001309 // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it
1310 // is a base-specifier-list.
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001311 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001312
Nico Webercfaa4cd2015-02-15 07:26:13 +00001313 CXXScopeSpec Spec;
1314 bool HasValidSpec = true;
1315 if (ParseOptionalCXXScopeSpecifier(Spec, ParsedType(), EnteringContext)) {
John McCall413021a2010-07-30 06:26:29 +00001316 DS.SetTypeSpecError();
Nico Webercfaa4cd2015-02-15 07:26:13 +00001317 HasValidSpec = false;
1318 }
1319 if (Spec.isSet())
1320 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) {
Alp Tokerec543272013-12-24 09:48:30 +00001321 Diag(Tok, diag::err_expected) << tok::identifier;
Nico Webercfaa4cd2015-02-15 07:26:13 +00001322 HasValidSpec = false;
1323 }
1324 if (HasValidSpec)
1325 SS = Spec;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001326 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001327
Douglas Gregor916462b2009-10-30 21:46:58 +00001328 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1329
Douglas Gregor67a65642009-02-17 23:15:12 +00001330 // Parse the (optional) class name or simple-template-id.
Craig Topper161e4db2014-05-21 06:02:52 +00001331 IdentifierInfo *Name = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001332 SourceLocation NameLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001333 TemplateIdAnnotation *TemplateId = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001334 if (Tok.is(tok::identifier)) {
1335 Name = Tok.getIdentifierInfo();
1336 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001337
David Blaikiebbafb8a2012-03-11 07:00:24 +00001338 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001339 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001340 // Eat the template argument list and try to continue parsing this as
1341 // a class (or template thereof).
1342 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001343 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +00001344 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +00001345 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001346 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001347 // We couldn't parse the template argument list at all, so don't
1348 // try to give any location information for the list.
1349 LAngleLoc = RAngleLoc = SourceLocation();
1350 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001351
Douglas Gregor916462b2009-10-30 21:46:58 +00001352 Diag(NameLoc, diag::err_explicit_spec_non_template)
Alp Toker01d65e12014-01-06 12:54:41 +00001353 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1354 << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
Joao Matose9a3ed42012-08-31 22:18:20 +00001355
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001356 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001357 // we've removed its template argument list.
1358 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1359 if (TemplateParams && TemplateParams->size() > 1) {
1360 TemplateParams->pop_back();
1361 } else {
Craig Topper161e4db2014-05-21 06:02:52 +00001362 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001363 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001364 = ParsedTemplateInfo::NonTemplate;
1365 }
1366 } else if (TemplateInfo.Kind
1367 == ParsedTemplateInfo::ExplicitInstantiation) {
1368 // Pretend this is just a forward declaration.
Craig Topper161e4db2014-05-21 06:02:52 +00001369 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001370 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001371 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001372 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001373 = SourceLocation();
1374 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1375 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001376 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001377 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001378 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001379 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001380 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001381
Douglas Gregore7c20652011-03-02 00:47:37 +00001382 if (TemplateId->Kind != TNK_Type_template &&
1383 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001384 // The template-name in the simple-template-id refers to
1385 // something other than a class template. Give an appropriate
1386 // error message and skip to the ';'.
1387 SourceRange Range(NameLoc);
1388 if (SS.isNotEmpty())
1389 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001390
Richard Smith72bfbd82013-12-04 00:28:23 +00001391 // FIXME: Name may be null here.
Douglas Gregor7f741122009-02-25 19:37:18 +00001392 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001393 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001394
Douglas Gregor7f741122009-02-25 19:37:18 +00001395 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001396 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor7f741122009-02-25 19:37:18 +00001397 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001398 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001399 }
1400
Richard Smithbfdb1082012-03-12 08:56:40 +00001401 // There are four options here.
1402 // - If we are in a trailing return type, this is always just a reference,
1403 // and we must not try to parse a definition. For instance,
1404 // [] () -> struct S { };
1405 // does not define a type.
1406 // - If we have 'struct foo {...', 'struct foo :...',
1407 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1408 // - If we have 'struct foo;', then this is either a forward declaration
1409 // or a friend declaration, which have to be treated differently.
1410 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001411 //
1412 // We also detect these erroneous cases to provide better diagnostic for
1413 // C++11 attributes parsing.
1414 // - attributes follow class name:
1415 // struct foo [[]] {};
1416 // - attributes appear before or after 'final':
1417 // struct foo [[]] final [[]] {};
1418 //
Richard Smithc5b05522012-03-12 07:56:15 +00001419 // However, in type-specifier-seq's, things look like declarations but are
1420 // just references, e.g.
1421 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001422 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001423 // &T::operator struct s;
Richard Smith649c7b062014-01-08 00:56:48 +00001424 // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
Michael Han9407e502012-11-26 22:54:45 +00001425
1426 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001427 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001428
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001429 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
John McCallfaf5fb42010-08-26 23:41:50 +00001430 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001431 if (DSC == DSC_trailing)
1432 TUK = Sema::TUK_Reference;
1433 else if (Tok.is(tok::l_brace) ||
1434 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001435 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001436 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001437 if (DS.isFriendSpecified()) {
1438 // C++ [class.friend]p2:
1439 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001440 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001441 << SourceRange(DS.getFriendSpecLoc());
1442
1443 // Skip everything up to the semicolon, so that this looks like a proper
1444 // friend class (or template thereof) declaration.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001445 SkipUntil(tok::semi, StopBeforeMatch);
John McCallfaf5fb42010-08-26 23:41:50 +00001446 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001447 } else {
1448 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001449 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001450 }
Richard Smith434516c2013-02-22 06:46:23 +00001451 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1452 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001453 // We can't tell if this is a definition or reference
1454 // until we skipped the 'final' and C++11 attribute specifiers.
1455 TentativeParsingAction PA(*this);
1456
1457 // Skip the 'final' keyword.
1458 ConsumeToken();
1459
1460 // Skip C++11 attribute specifiers.
1461 while (true) {
1462 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1463 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001464 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001465 break;
Richard Smith434516c2013-02-22 06:46:23 +00001466 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001467 ConsumeToken();
1468 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001469 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001470 break;
1471 } else {
1472 break;
1473 }
1474 }
1475
1476 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1477 TUK = Sema::TUK_Definition;
1478 else
1479 TUK = Sema::TUK_Reference;
1480
1481 PA.Revert();
Richard Smith649c7b062014-01-08 00:56:48 +00001482 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00001483 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001484 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001485 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001486 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001487 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Joao Matose9a3ed42012-08-31 22:18:20 +00001488 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00001489 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001490 DeclSpec::getSpecifierName(TagType, PPol));
Joao Matose9a3ed42012-08-31 22:18:20 +00001491 PP.EnterToken(Tok);
1492 Tok.setKind(tok::semi);
1493 }
Richard Smith369b9f92012-06-25 21:37:02 +00001494 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001495 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001496
Michael Han9407e502012-11-26 22:54:45 +00001497 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1498 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001499 if (TUK != Sema::TUK_Reference) {
1500 // If this is not a reference, then the only possible
1501 // valid place for C++11 attributes to appear here
1502 // is between class-key and class-name. If there are
1503 // any attributes after class-name, we try a fixit to move
1504 // them to the right place.
1505 SourceRange AttrRange = Attributes.Range;
1506 if (AttrRange.isValid()) {
1507 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1508 << AttrRange
1509 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1510 CharSourceRange(AttrRange, true))
1511 << FixItHint::CreateRemoval(AttrRange);
1512
1513 // Recover by adding misplaced attributes to the attribute list
1514 // of the class so they can be applied on the class later.
1515 attrs.takeAllFrom(Attributes);
1516 }
1517 }
Michael Han9407e502012-11-26 22:54:45 +00001518
John McCall6347b682012-05-07 06:16:58 +00001519 // If this is an elaborated type specifier, and we delayed
1520 // diagnostics before, just merge them into the current pool.
1521 if (shouldDelayDiagsInTag) {
1522 diagsFromTag.done();
1523 if (TUK == Sema::TUK_Reference)
1524 diagsFromTag.redelay();
1525 }
1526
John McCall413021a2010-07-30 06:26:29 +00001527 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001528 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001529 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1530 // We have a declaration or reference to an anonymous class.
1531 Diag(StartLoc, diag::err_anon_type_definition)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001532 << DeclSpec::getSpecifierName(TagType, Policy);
John McCall413021a2010-07-30 06:26:29 +00001533 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001534
David Majnemer3252fd02013-12-05 01:36:53 +00001535 // If we are parsing a definition and stop at a base-clause, continue on
1536 // until the semicolon. Continuing from the comma will just trick us into
1537 // thinking we are seeing a variable declaration.
1538 if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1539 SkipUntil(tok::semi, StopBeforeMatch);
1540 else
1541 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor556877c2008-04-13 21:30:24 +00001542 return;
1543 }
1544
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001545 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001546 DeclResult TagOrTempResult = true; // invalid
1547 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001548
Douglas Gregord6ab8742009-05-28 23:31:59 +00001549 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +00001550 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001551 // Explicit specialization, class template partial specialization,
1552 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001553 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001554 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001555 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001556 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001557 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001558 ProhibitAttributes(attrs);
1559
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001560 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001561 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001562 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001563 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001564 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001565 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001566 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001567 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001568 TemplateId->TemplateNameLoc,
1569 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001570 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001571 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001572 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001573
1574 // Friend template-ids are treated as references unless
1575 // they have template headers, in which case they're ill-formed
1576 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1577 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001578 } else if (TUK == Sema::TUK_Reference ||
1579 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001580 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001581 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001582 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001583 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001584 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001585 TemplateId->Template,
1586 TemplateId->TemplateNameLoc,
1587 TemplateId->LAngleLoc,
1588 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001589 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001590 } else {
1591 // This is an explicit specialization or a class template
1592 // partial specialization.
1593 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001594 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1595 // This looks like an explicit instantiation, because we have
1596 // something like
1597 //
1598 // template class Foo<X>
1599 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001600 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001601 // meant to be an explicit specialization, but the user forgot
1602 // the '<>' after 'template'.
Richard Smith003c5e12013-11-08 19:03:29 +00001603 // It this is friend declaration however, since it cannot have a
1604 // template header, it is most likely that the user meant to
1605 // remove the 'template' keyword.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001606 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith003c5e12013-11-08 19:03:29 +00001607 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001608
Richard Smith003c5e12013-11-08 19:03:29 +00001609 if (TUK == Sema::TUK_Friend) {
1610 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
Craig Topper161e4db2014-05-21 06:02:52 +00001611 TemplateParams = nullptr;
Richard Smith003c5e12013-11-08 19:03:29 +00001612 } else {
1613 SourceLocation LAngleLoc =
1614 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1615 Diag(TemplateId->TemplateNameLoc,
1616 diag::err_explicit_instantiation_with_definition)
1617 << SourceRange(TemplateInfo.TemplateLoc)
1618 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1619
1620 // Create a fake template parameter list that contains only
1621 // "template<>", so that we treat this construct as a class
1622 // template specialization.
1623 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
Craig Topper161e4db2014-05-21 06:02:52 +00001624 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, nullptr,
1625 0, LAngleLoc));
Richard Smith003c5e12013-11-08 19:03:29 +00001626 TemplateParams = &FakedParamLists;
1627 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001628 }
1629
1630 // Build the class template specialization.
Richard Smith4b55a9c2014-04-17 03:29:33 +00001631 TagOrTempResult = Actions.ActOnClassTemplateSpecialization(
1632 getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(),
1633 *TemplateId, attrs.getList(),
Craig Topper161e4db2014-05-21 06:02:52 +00001634 MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0]
1635 : nullptr,
Richard Smith4b55a9c2014-04-17 03:29:33 +00001636 TemplateParams ? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001637 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001638 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001639 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001640 // Explicit instantiation of a member of a class template
1641 // specialization, e.g.,
1642 //
1643 // template struct Outer<int>::Inner;
1644 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001645 ProhibitAttributes(attrs);
1646
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001647 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001648 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001649 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001650 TemplateInfo.TemplateLoc,
1651 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001652 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001653 } else if (TUK == Sema::TUK_Friend &&
1654 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001655 ProhibitAttributes(attrs);
1656
John McCallace48cd2010-10-19 01:40:49 +00001657 TagOrTempResult =
1658 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1659 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001660 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001661 MultiTemplateParamsArg(
Craig Topper161e4db2014-05-21 06:02:52 +00001662 TemplateParams? &(*TemplateParams)[0]
1663 : nullptr,
John McCallace48cd2010-10-19 01:40:49 +00001664 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001665 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001666 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1667 ProhibitAttributes(attrs);
Richard Smith003c5e12013-11-08 19:03:29 +00001668
Larisse Voufo725de3e2013-06-21 00:08:46 +00001669 if (TUK == Sema::TUK_Definition &&
1670 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1671 // If the declarator-id is not a template-id, issue a diagnostic and
1672 // recover by ignoring the 'template' keyword.
1673 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1674 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Craig Topper161e4db2014-05-21 06:02:52 +00001675 TemplateParams = nullptr;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001676 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001677
John McCall7f41d982009-09-11 04:59:25 +00001678 bool IsDependent = false;
1679
John McCall32723e92010-10-19 18:40:57 +00001680 // Don't pass down template parameter lists if this is just a tag
1681 // reference. For example, we don't need the template parameters here:
1682 // template <class T> class A *makeA(T t);
1683 MultiTemplateParamsArg TParams;
1684 if (TUK != Sema::TUK_Reference && TemplateParams)
1685 TParams =
1686 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1687
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001688 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001689 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001690 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001691 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001692 TParams, Owned, IsDependent,
1693 SourceLocation(), false,
Richard Smith649c7b062014-01-08 00:56:48 +00001694 clang::TypeResult(),
1695 DSC == DSC_type_specifier);
John McCall7f41d982009-09-11 04:59:25 +00001696
1697 // If ActOnTag said the type was dependent, try again with the
1698 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001699 if (IsDependent) {
1700 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001701 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001702 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001703 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001704 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001705
Douglas Gregor556877c2008-04-13 21:30:24 +00001706 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001707 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001708 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001709 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001710 isCXX11FinalKeyword());
David Blaikiebbafb8a2012-03-11 07:00:24 +00001711 if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001712 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1713 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001714 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001715 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001716 }
1717
Craig Topper161e4db2014-05-21 06:02:52 +00001718 const char *PrevSpec = nullptr;
John McCallba7bf592010-08-24 05:47:05 +00001719 unsigned DiagID;
1720 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001721 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001722 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1723 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001724 PrevSpec, DiagID, TypeResult.get(), Policy);
John McCall7f41d982009-09-11 04:59:25 +00001725 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001726 Result = DS.SetTypeSpecType(TagType, StartLoc,
1727 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001728 PrevSpec, DiagID, TagOrTempResult.get(), Owned,
1729 Policy);
John McCall7f41d982009-09-11 04:59:25 +00001730 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001731 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001732 return;
1733 }
Mike Stump11289f42009-09-09 15:08:12 +00001734
John McCallba7bf592010-08-24 05:47:05 +00001735 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001736 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001737
Chris Lattnercf251412010-02-02 01:23:29 +00001738 // At this point, we've successfully parsed a class-specifier in 'definition'
1739 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1740 // going to look at what comes after it to improve error recovery. If an
1741 // impossible token occurs next, we assume that the programmer forgot a ; at
1742 // the end of the declaration and recover that way.
1743 //
Richard Smith369b9f92012-06-25 21:37:02 +00001744 // Also enforce C++ [temp]p3:
1745 // In a template-declaration which defines a class, no declarator
1746 // is permitted.
Richard Smith843f18f2014-08-13 02:13:15 +00001747 //
1748 // After a type-specifier, we don't expect a semicolon. This only happens in
1749 // C, since definitions are not permitted in this context in C++.
Joao Matose9a3ed42012-08-31 22:18:20 +00001750 if (TUK == Sema::TUK_Definition &&
Richard Smith843f18f2014-08-13 02:13:15 +00001751 (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) &&
Joao Matose9a3ed42012-08-31 22:18:20 +00001752 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001753 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001754 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Alp Toker383d2c42014-01-01 03:08:43 +00001755 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001756 DeclSpec::getSpecifierName(TagType, PPol));
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001757 // Push this token back into the preprocessor and change our current token
1758 // to ';' so that the rest of the code recovers as though there were an
1759 // ';' after the definition.
1760 PP.EnterToken(Tok);
1761 Tok.setKind(tok::semi);
1762 }
Chris Lattnercf251412010-02-02 01:23:29 +00001763 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001764}
1765
Mike Stump11289f42009-09-09 15:08:12 +00001766/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001767///
1768/// base-clause : [C++ class.derived]
1769/// ':' base-specifier-list
1770/// base-specifier-list:
1771/// base-specifier '...'[opt]
1772/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001773void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001774 assert(Tok.is(tok::colon) && "Not a base clause");
1775 ConsumeToken();
1776
Douglas Gregor29a92472008-10-22 17:49:05 +00001777 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001778 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001779
Douglas Gregor556877c2008-04-13 21:30:24 +00001780 while (true) {
1781 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001782 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001783 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001784 // Skip the rest of this base specifier, up until the comma or
1785 // opening brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001786 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor29a92472008-10-22 17:49:05 +00001787 } else {
1788 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001789 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001790 }
1791
1792 // If the next token is a comma, consume it and keep reading
1793 // base-specifiers.
Alp Toker97650562014-01-10 11:19:30 +00001794 if (!TryConsumeToken(tok::comma))
1795 break;
Douglas Gregor556877c2008-04-13 21:30:24 +00001796 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001797
1798 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001799 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001800}
1801
1802/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1803/// one entry in the base class list of a class specifier, for example:
1804/// class foo : public bar, virtual private baz {
1805/// 'public bar' and 'virtual private baz' are each base-specifiers.
1806///
1807/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001808/// attribute-specifier-seq[opt] base-type-specifier
1809/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1810/// base-type-specifier
1811/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1812/// base-type-specifier
Craig Topper9ad7e262014-10-31 06:57:07 +00001813BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001814 bool IsVirtual = false;
1815 SourceLocation StartLoc = Tok.getLocation();
1816
Richard Smith4c96e992013-02-19 23:47:15 +00001817 ParsedAttributesWithRange Attributes(AttrFactory);
1818 MaybeParseCXX11Attributes(Attributes);
1819
Douglas Gregor556877c2008-04-13 21:30:24 +00001820 // Parse the 'virtual' keyword.
Alp Toker97650562014-01-10 11:19:30 +00001821 if (TryConsumeToken(tok::kw_virtual))
Douglas Gregor556877c2008-04-13 21:30:24 +00001822 IsVirtual = true;
Douglas Gregor556877c2008-04-13 21:30:24 +00001823
Richard Smith4c96e992013-02-19 23:47:15 +00001824 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1825
Douglas Gregor556877c2008-04-13 21:30:24 +00001826 // Parse an (optional) access specifier.
1827 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001828 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001829 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001830
Richard Smith4c96e992013-02-19 23:47:15 +00001831 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1832
Douglas Gregor556877c2008-04-13 21:30:24 +00001833 // Parse the 'virtual' keyword (again!), in case it came after the
1834 // access specifier.
1835 if (Tok.is(tok::kw_virtual)) {
1836 SourceLocation VirtualLoc = ConsumeToken();
1837 if (IsVirtual) {
1838 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001839 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001840 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001841 }
1842
1843 IsVirtual = true;
1844 }
1845
Richard Smith4c96e992013-02-19 23:47:15 +00001846 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1847
Douglas Gregor831c93f2008-11-05 20:51:48 +00001848 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001849 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001850 SourceLocation BaseLoc;
1851 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001852 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001853 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001854
Douglas Gregor752a5952011-01-03 22:36:02 +00001855 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1856 // actually part of the base-specifier-list grammar productions, but we
1857 // parse it here for convenience.
1858 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00001859 TryConsumeToken(tok::ellipsis, EllipsisLoc);
1860
Mike Stump11289f42009-09-09 15:08:12 +00001861 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001862 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001863
Douglas Gregor556877c2008-04-13 21:30:24 +00001864 // Notify semantic analysis that we have parsed a complete
1865 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00001866 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1867 Access, BaseType.get(), BaseLoc,
1868 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001869}
1870
1871/// getAccessSpecifierIfPresent - Determine whether the next token is
1872/// a C++ access-specifier.
1873///
1874/// access-specifier: [C++ class.derived]
1875/// 'private'
1876/// 'protected'
1877/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001878AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001879 switch (Tok.getKind()) {
1880 default: return AS_none;
1881 case tok::kw_private: return AS_private;
1882 case tok::kw_protected: return AS_protected;
1883 case tok::kw_public: return AS_public;
1884 }
1885}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001886
Douglas Gregor433e0532012-04-16 18:27:27 +00001887/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith0b3a4622014-11-13 20:01:57 +00001888/// delayed, e.g., default arguments or an exception-specification, create a
1889/// late-parsed method declaration record to handle the parsing at the end of
1890/// the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00001891void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1892 Decl *ThisDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001893 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001894 = DeclaratorInfo.getFunctionTypeInfo();
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001895 // If there was a late-parsed exception-specification, we'll need a
1896 // late parse
1897 bool NeedLateParse = FTI.getExceptionSpecType() == EST_Unparsed;
Douglas Gregor433e0532012-04-16 18:27:27 +00001898
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001899 if (!NeedLateParse)
1900 // Look ahead to see if there are any default args
1901 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx)
1902 if (FTI.Params[ParamIdx].DefaultArgTokens) {
1903 NeedLateParse = true;
1904 break;
1905 }
1906
1907 if (NeedLateParse) {
Richard Smith0b3a4622014-11-13 20:01:57 +00001908 // Push this method onto the stack of late-parsed method
1909 // declarations.
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001910 auto LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
Richard Smith0b3a4622014-11-13 20:01:57 +00001911 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
1912 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
1913
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001914 // Stash the exception-specification tokens in the late-pased method.
Richard Smith0b3a4622014-11-13 20:01:57 +00001915 LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens;
1916 FTI.ExceptionSpecTokens = 0;
1917
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001918 // Push tokens for each parameter. Those that do not have
1919 // defaults will be NULL.
Richard Smith0b3a4622014-11-13 20:01:57 +00001920 LateMethod->DefaultArgs.reserve(FTI.NumParams);
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001921 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx)
Alp Tokerc5350722014-02-26 22:27:52 +00001922 LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001923 FTI.Params[ParamIdx].Param, FTI.Params[ParamIdx].DefaultArgTokens));
Eli Friedman3af2a772009-07-22 21:45:50 +00001924 }
1925}
1926
Richard Smith89645bc2013-01-02 12:01:23 +00001927/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001928/// virt-specifier.
1929///
1930/// virt-specifier:
1931/// override
1932/// final
Richard Smith89645bc2013-01-02 12:01:23 +00001933VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001934 if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001935 return VirtSpecifiers::VS_None;
1936
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001937 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001938
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001939 // Initialize the contextual keywords.
1940 if (!Ident_final) {
1941 Ident_final = &PP.getIdentifierTable().get("final");
1942 if (getLangOpts().MicrosoftExt)
1943 Ident_sealed = &PP.getIdentifierTable().get("sealed");
1944 Ident_override = &PP.getIdentifierTable().get("override");
Anders Carlsson56104902011-01-17 03:05:47 +00001945 }
1946
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001947 if (II == Ident_override)
1948 return VirtSpecifiers::VS_Override;
1949
1950 if (II == Ident_sealed)
1951 return VirtSpecifiers::VS_Sealed;
1952
1953 if (II == Ident_final)
1954 return VirtSpecifiers::VS_Final;
1955
Anders Carlsson56104902011-01-17 03:05:47 +00001956 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001957}
1958
Richard Smith89645bc2013-01-02 12:01:23 +00001959/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001960///
1961/// virt-specifier-seq:
1962/// virt-specifier
1963/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00001964void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
Richard Smith3d1a94c2014-08-12 00:22:39 +00001965 bool IsInterface,
1966 SourceLocation FriendLoc) {
Anders Carlsson56104902011-01-17 03:05:47 +00001967 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00001968 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001969 if (Specifier == VirtSpecifiers::VS_None)
1970 return;
1971
Richard Smith3d1a94c2014-08-12 00:22:39 +00001972 if (FriendLoc.isValid()) {
1973 Diag(Tok.getLocation(), diag::err_friend_decl_spec)
1974 << VirtSpecifiers::getSpecifierName(Specifier)
1975 << FixItHint::CreateRemoval(Tok.getLocation())
1976 << SourceRange(FriendLoc, FriendLoc);
1977 ConsumeToken();
1978 continue;
1979 }
1980
Anders Carlsson56104902011-01-17 03:05:47 +00001981 // C++ [class.mem]p8:
1982 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Craig Topper161e4db2014-05-21 06:02:52 +00001983 const char *PrevSpec = nullptr;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001984 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001985 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1986 << PrevSpec
1987 << FixItHint::CreateRemoval(Tok.getLocation());
1988
David Majnemera5433082013-10-18 00:33:31 +00001989 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
1990 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalldb632ac2012-09-25 07:32:39 +00001991 Diag(Tok.getLocation(), diag::err_override_control_interface)
1992 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemera5433082013-10-18 00:33:31 +00001993 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
1994 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalldb632ac2012-09-25 07:32:39 +00001995 } else {
David Majnemera5433082013-10-18 00:33:31 +00001996 Diag(Tok.getLocation(),
1997 getLangOpts().CPlusPlus11
1998 ? diag::warn_cxx98_compat_override_control_keyword
1999 : diag::ext_override_control_keyword)
2000 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalldb632ac2012-09-25 07:32:39 +00002001 }
Anders Carlsson56104902011-01-17 03:05:47 +00002002 ConsumeToken();
2003 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002004}
2005
Richard Smith89645bc2013-01-02 12:01:23 +00002006/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002007/// 'final' or Microsoft 'sealed' contextual keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00002008bool Parser::isCXX11FinalKeyword() const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002009 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
2010 return Specifier == VirtSpecifiers::VS_Final ||
2011 Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002012}
2013
Richard Smith72553fc2014-01-23 23:53:27 +00002014/// \brief Parse a C++ member-declarator up to, but not including, the optional
2015/// brace-or-equal-initializer or pure-specifier.
Nico Weberd89e6f72015-01-16 19:34:13 +00002016bool Parser::ParseCXXMemberDeclaratorBeforeInitializer(
Richard Smith72553fc2014-01-23 23:53:27 +00002017 Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
2018 LateParsedAttrList &LateParsedAttrs) {
2019 // member-declarator:
2020 // declarator pure-specifier[opt]
2021 // declarator brace-or-equal-initializer[opt]
2022 // identifier[opt] ':' constant-expression
Serge Pavlov458ea762014-07-16 05:16:52 +00002023 if (Tok.isNot(tok::colon))
Richard Smith72553fc2014-01-23 23:53:27 +00002024 ParseDeclarator(DeclaratorInfo);
Richard Smith3d1a94c2014-08-12 00:22:39 +00002025 else
2026 DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation());
Richard Smith72553fc2014-01-23 23:53:27 +00002027
2028 if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002029 assert(DeclaratorInfo.isPastIdentifier() &&
2030 "don't know where identifier would go yet?");
Richard Smith72553fc2014-01-23 23:53:27 +00002031 BitfieldSize = ParseConstantExpression();
2032 if (BitfieldSize.isInvalid())
2033 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2034 } else
Richard Smith3d1a94c2014-08-12 00:22:39 +00002035 ParseOptionalCXX11VirtSpecifierSeq(
2036 VS, getCurrentClass().IsInterface,
2037 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Richard Smith72553fc2014-01-23 23:53:27 +00002038
2039 // If a simple-asm-expr is present, parse it.
2040 if (Tok.is(tok::kw_asm)) {
2041 SourceLocation Loc;
2042 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
2043 if (AsmLabel.isInvalid())
2044 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2045
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002046 DeclaratorInfo.setAsmLabel(AsmLabel.get());
Richard Smith72553fc2014-01-23 23:53:27 +00002047 DeclaratorInfo.SetRangeEnd(Loc);
2048 }
2049
2050 // If attributes exist after the declarator, but before an '{', parse them.
2051 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Richard Smith4b5a9492014-01-24 22:34:35 +00002052
2053 // For compatibility with code written to older Clang, also accept a
2054 // virt-specifier *after* the GNU attributes.
Aaron Ballman5d153e32014-08-04 17:03:51 +00002055 if (BitfieldSize.isUnset() && VS.isUnset()) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002056 ParseOptionalCXX11VirtSpecifierSeq(
2057 VS, getCurrentClass().IsInterface,
2058 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Aaron Ballman5d153e32014-08-04 17:03:51 +00002059 if (!VS.isUnset()) {
2060 // If we saw any GNU-style attributes that are known to GCC followed by a
2061 // virt-specifier, issue a GCC-compat warning.
2062 const AttributeList *Attr = DeclaratorInfo.getAttributes();
2063 while (Attr) {
2064 if (Attr->isKnownToGCC() && !Attr->isCXX11Attribute())
2065 Diag(Attr->getLoc(), diag::warn_gcc_attribute_location);
2066 Attr = Attr->getNext();
2067 }
2068 }
2069 }
Nico Weberd89e6f72015-01-16 19:34:13 +00002070
2071 // If this has neither a name nor a bit width, something has gone seriously
2072 // wrong. Skip until the semi-colon or }.
2073 if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
2074 // If so, skip until the semi-colon or a }.
2075 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2076 return true;
2077 }
2078 return false;
Richard Smith72553fc2014-01-23 23:53:27 +00002079}
2080
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002081/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
2082///
2083/// member-declaration:
2084/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
2085/// function-definition ';'[opt]
2086/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
2087/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00002088/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002089/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002090/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002091///
2092/// member-declarator-list:
2093/// member-declarator
2094/// member-declarator-list ',' member-declarator
2095///
2096/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002097/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002098/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00002099/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002100/// identifier[opt] ':' constant-expression
2101///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002102/// virt-specifier-seq:
2103/// virt-specifier
2104/// virt-specifier-seq virt-specifier
2105///
2106/// virt-specifier:
2107/// override
2108/// final
David Majnemera5433082013-10-18 00:33:31 +00002109/// [MS] sealed
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002110///
Sebastian Redl42e92c42009-04-12 17:16:29 +00002111/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002112/// '= 0'
2113///
2114/// constant-initializer:
2115/// '=' constant-expression
2116///
Douglas Gregor3447e762009-08-20 22:52:58 +00002117void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002118 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00002119 const ParsedTemplateInfo &TemplateInfo,
2120 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00002121 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002122 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00002123 Diag(Tok, diag::err_at_defs_cxx);
2124 else
2125 Diag(Tok, diag::err_at_in_class);
Richard Smithda35e962013-11-09 04:52:51 +00002126
Douglas Gregor23c84762011-04-14 17:21:19 +00002127 ConsumeToken();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002128 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor23c84762011-04-14 17:21:19 +00002129 return;
2130 }
Richard Smithda35e962013-11-09 04:52:51 +00002131
Serge Pavlov458ea762014-07-16 05:16:52 +00002132 // Turn on colon protection early, while parsing declspec, although there is
2133 // nothing to protect there. It prevents from false errors if error recovery
2134 // incorrectly determines where the declspec ends, as in the example:
2135 // struct A { enum class B { C }; };
2136 // const int C = 4;
2137 // struct D { A::B : C; };
2138 ColonProtectionRAIIObject X(*this);
2139
John McCalla0097262009-12-11 02:10:03 +00002140 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00002141 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00002142 if (!TemplateInfo.Kind &&
Nikola Smiljanic67860242014-09-26 00:28:20 +00002143 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2144 Tok.is(tok::kw___super))) {
Richard Smith45855df2012-05-09 08:23:23 +00002145 if (TryAnnotateCXXScopeToken())
2146 MalformedTypeSpec = true;
2147
2148 bool isAccessDecl;
2149 if (Tok.isNot(tok::annot_cxxscope))
2150 isAccessDecl = false;
2151 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00002152 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
2153 else
2154 isAccessDecl = NextToken().is(tok::kw_operator);
2155
2156 if (isAccessDecl) {
2157 // Collect the scope specifier token we annotated earlier.
2158 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002159 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2160 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00002161
Nico Weberef03e702014-09-10 00:59:37 +00002162 if (SS.isInvalid()) {
2163 SkipUntil(tok::semi);
2164 return;
2165 }
2166
John McCalla0097262009-12-11 02:10:03 +00002167 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00002168 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00002169 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +00002170 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
2171 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00002172 SkipUntil(tok::semi);
2173 return;
2174 }
2175
2176 // TODO: recover from mistakenly-qualified operator declarations.
Alp Toker383d2c42014-01-01 03:08:43 +00002177 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2178 "access declaration")) {
2179 SkipUntil(tok::semi);
John McCalla0097262009-12-11 02:10:03 +00002180 return;
Alp Toker383d2c42014-01-01 03:08:43 +00002181 }
John McCalla0097262009-12-11 02:10:03 +00002182
Douglas Gregor0be31a22010-07-02 17:43:08 +00002183 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002184 /* HasUsingKeyword */ false,
2185 SourceLocation(),
John McCalla0097262009-12-11 02:10:03 +00002186 SS, Name,
Craig Topper161e4db2014-05-21 06:02:52 +00002187 /* AttrList */ nullptr,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002188 /* HasTypenameKeyword */ false,
John McCalla0097262009-12-11 02:10:03 +00002189 SourceLocation());
2190 return;
2191 }
2192 }
2193
Aaron Ballmane7c544d2014-08-04 20:28:35 +00002194 // static_assert-declaration. A templated static_assert declaration is
2195 // diagnosed in Parser::ParseSingleDeclarationAfterTemplate.
2196 if (!TemplateInfo.Kind &&
2197 (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert))) {
Chris Lattner49836b42009-04-02 04:16:50 +00002198 SourceLocation DeclEnd;
2199 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002200 return;
2201 }
Mike Stump11289f42009-09-09 15:08:12 +00002202
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002203 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00002204 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00002205 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00002206 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00002207 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002208 AS, AccessAttrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002209 return;
2210 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002211
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002212 // Handle: member-declaration ::= '__extension__' member-declaration
2213 if (Tok.is(tok::kw___extension__)) {
2214 // __extension__ silences extension warnings in the subexpression.
2215 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2216 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002217 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2218 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002219 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002220
John McCall084e83d2011-03-24 11:26:52 +00002221 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002222 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002223 // Optional C++11 attribute-specifier
2224 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002225 // We need to keep these attributes for future diagnostic
2226 // before they are taken over by declaration specifier.
2227 FnAttrs.addAll(attrs.getList());
2228 FnAttrs.Range = attrs.Range;
2229
John McCall53fa7142010-12-24 02:08:15 +00002230 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002231
Douglas Gregorfec52632009-06-20 00:51:54 +00002232 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002233 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002234
Douglas Gregorfec52632009-06-20 00:51:54 +00002235 // Eat 'using'.
2236 SourceLocation UsingLoc = ConsumeToken();
2237
2238 if (Tok.is(tok::kw_namespace)) {
2239 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002240 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002241 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00002242 SourceLocation DeclEnd;
Richard Smith3f1b5d02011-05-05 21:57:07 +00002243 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +00002244 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2245 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00002246 }
2247 return;
2248 }
2249
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002250 // Hold late-parsed attributes so we can attach a Decl to them later.
2251 LateParsedAttrList CommonLateParsedAttrs;
2252
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002253 // decl-specifier-seq:
2254 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002255 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002256 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002257 if (MalformedTypeSpec)
2258 DS.SetTypeSpecError();
Richard Smith72553fc2014-01-23 23:53:27 +00002259
Serge Pavlov458ea762014-07-16 05:16:52 +00002260 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2261 &CommonLateParsedAttrs);
2262
2263 // Turn off colon protection that was set for declspec.
2264 X.restore();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002265
Richard Smith404dfb42013-11-19 22:47:36 +00002266 // If we had a free-standing type definition with a missing semicolon, we
2267 // may get this far before the problem becomes obvious.
2268 if (DS.hasTagDefinition() &&
2269 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2270 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2271 &CommonLateParsedAttrs))
2272 return;
2273
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002274 MultiTemplateParamsArg TemplateParams(
Craig Topper161e4db2014-05-21 06:02:52 +00002275 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data()
2276 : nullptr,
John McCall11083da2009-09-16 22:47:08 +00002277 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2278
Alp Toker35d87032013-12-30 23:29:50 +00002279 if (TryConsumeToken(tok::semi)) {
Michael Handdc016d2012-11-28 23:17:40 +00002280 if (DS.isFriendSpecified())
2281 ProhibitAttributes(FnAttrs);
2282
John McCall48871652010-08-21 09:40:31 +00002283 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00002284 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00002285 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00002286 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002287 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002288
John McCall28a6aea2009-11-04 02:18:39 +00002289 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002290 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002291
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002292 // Hold late-parsed attributes so we can attach a Decl to them later.
2293 LateParsedAttrList LateParsedAttrs;
2294
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002295 SourceLocation EqualLoc;
2296 bool HasInitializer = false;
2297 ExprResult Init;
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002298
Richard Smith72553fc2014-01-23 23:53:27 +00002299 SmallVector<Decl *, 8> DeclsInGroup;
2300 ExprResult BitfieldSize;
2301 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002302
Richard Smith72553fc2014-01-23 23:53:27 +00002303 // Parse the first declarator.
Nico Weberd89e6f72015-01-16 19:34:13 +00002304 if (ParseCXXMemberDeclaratorBeforeInitializer(
2305 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) {
Richard Smith72553fc2014-01-23 23:53:27 +00002306 TryConsumeToken(tok::semi);
2307 return;
2308 }
John Thompson5bc5cbe2009-11-25 22:58:06 +00002309
Richard Smith72553fc2014-01-23 23:53:27 +00002310 // Check for a member function definition.
Richard Smith4b5a9492014-01-24 22:34:35 +00002311 if (BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002312 // MSVC permits pure specifier on inline functions defined at class scope.
Francois Pichet3abc9b82011-05-11 02:14:46 +00002313 // Hence check for =0 before checking for function definition.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002314 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Richard Smith72553fc2014-01-23 23:53:27 +00002315 DeclaratorInfo.isFunctionDeclarator() &&
Francois Pichet3abc9b82011-05-11 02:14:46 +00002316 NextToken().is(tok::numeric_constant)) {
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002317 EqualLoc = ConsumeToken();
Francois Pichet3abc9b82011-05-11 02:14:46 +00002318 Init = ParseInitializer();
2319 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002320 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002321 else
2322 HasInitializer = true;
Francois Pichet3abc9b82011-05-11 02:14:46 +00002323 }
2324
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002325 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002326 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002327 //
2328 // In C++11, a non-function declarator followed by an open brace is a
2329 // braced-init-list for an in-class member initialization, not an
2330 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002331 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002332 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002333 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002334 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002335 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002336 } else if (Tok.is(tok::equal)) {
2337 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002338 if (KW.is(tok::kw_default))
2339 DefinitionKind = FDK_Defaulted;
2340 else if (KW.is(tok::kw_delete))
2341 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002342 }
2343 }
2344
Michael Handdc016d2012-11-28 23:17:40 +00002345 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2346 // to a friend declaration, that declaration shall be a definition.
2347 if (DeclaratorInfo.isFunctionDeclarator() &&
2348 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2349 // Diagnose attributes that appear before decl specifier:
2350 // [[]] friend int foo();
2351 ProhibitAttributes(FnAttrs);
2352 }
2353
Nico Webera7f137d2015-01-16 19:35:01 +00002354 if (DefinitionKind != FDK_Declaration) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002355 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002356 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002357 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002358 SkipUntil(tok::r_brace);
Michael Handdc016d2012-11-28 23:17:40 +00002359
Douglas Gregor8a4db832011-01-19 16:41:58 +00002360 // Consume the optional ';'
Alp Toker35d87032013-12-30 23:29:50 +00002361 TryConsumeToken(tok::semi);
2362
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002363 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002364 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002365
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002366 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002367 Diag(DeclaratorInfo.getIdentifierLoc(),
2368 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002369
Richard Smith2603b092012-11-15 22:54:20 +00002370 // Recover by treating the 'typedef' as spurious.
2371 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002372 }
2373
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002374 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002375 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002376 VS, DefinitionKind, Init);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002377
David Majnemer23252a32013-08-01 04:22:55 +00002378 if (FunDecl) {
2379 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2380 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2381 }
2382 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2383 LateParsedAttrs[i]->addDecl(FunDecl);
2384 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002385 }
2386 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002387
2388 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002389 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002390 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002391
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002392 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002393 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002394 }
2395
2396 // member-declarator-list:
2397 // member-declarator
2398 // member-declarator-list ',' member-declarator
2399
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002400 while (1) {
Richard Smith2b013182012-06-10 03:12:00 +00002401 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002402 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith938f40b2011-06-11 17:19:42 +00002403 if (BitfieldSize.get()) {
2404 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002405 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith938f40b2011-06-11 17:19:42 +00002406 } else {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002407 HasInitializer = true;
Richard Smith2b013182012-06-10 03:12:00 +00002408 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2409 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smith2b013182012-06-10 03:12:00 +00002410 != DeclSpec::SCS_typedef)
2411 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002412 }
2413 }
2414
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002415 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002416 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002417 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002418
Craig Topper161e4db2014-05-21 06:02:52 +00002419 NamedDecl *ThisDecl = nullptr;
John McCall07e91c02009-08-06 02:15:43 +00002420 if (DS.isFriendSpecified()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002421 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
Michael Handdc016d2012-11-28 23:17:40 +00002422 // to a friend declaration, that declaration shall be a definition.
2423 //
Richard Smith72553fc2014-01-23 23:53:27 +00002424 // Diagnose attributes that appear in a friend member function declarator:
2425 // friend int foo [[]] ();
Michael Handdc016d2012-11-28 23:17:40 +00002426 SmallVector<SourceRange, 4> Ranges;
2427 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
Richard Smith72553fc2014-01-23 23:53:27 +00002428 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
2429 E = Ranges.end(); I != E; ++I)
2430 Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
Michael Handdc016d2012-11-28 23:17:40 +00002431
Douglas Gregor0be31a22010-07-02 17:43:08 +00002432 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002433 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002434 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002435 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002436 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002437 TemplateParams,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002438 BitfieldSize.get(),
Richard Smith2b013182012-06-10 03:12:00 +00002439 VS, HasInClassInit);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002440
2441 if (VarTemplateDecl *VT =
Craig Topper161e4db2014-05-21 06:02:52 +00002442 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr)
Larisse Voufo39a1e502013-08-06 01:03:05 +00002443 // Re-direct this decl to refer to the templated decl so that we can
2444 // initialize it.
2445 ThisDecl = VT->getTemplatedDecl();
2446
David Majnemer23252a32013-08-01 04:22:55 +00002447 if (ThisDecl && AccessAttrs)
Richard Smithf8a75c32013-08-29 00:47:48 +00002448 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00002449 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002450
Douglas Gregor728d00b2011-10-10 14:49:18 +00002451 // Handle the initializer.
David Blaikie35506f82013-01-30 01:22:18 +00002452 if (HasInClassInit != ICIS_NoInit &&
2453 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2454 DeclSpec::SCS_static) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002455 // The initializer was deferred; parse it and cache the tokens.
David Majnemer23252a32013-08-01 04:22:55 +00002456 Diag(Tok, getLangOpts().CPlusPlus11
2457 ? diag::warn_cxx98_compat_nonstatic_member_init
2458 : diag::ext_nonstatic_member_init);
Richard Smith5d164bc2011-10-15 05:09:34 +00002459
Richard Smith938f40b2011-06-11 17:19:42 +00002460 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002461 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2462 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002463 //
2464 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002465 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002466 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002467 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemer23252a32013-08-01 04:22:55 +00002468
2469 // Avoid later warnings about a class member of incomplete type.
David Blaikiecdd91db2012-02-14 09:00:46 +00002470 if (ThisDecl)
David Blaikiecdd91db2012-02-14 09:00:46 +00002471 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002472 } else
2473 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002474 } else if (HasInitializer) {
2475 // Normal initializer.
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002476 if (!Init.isUsable())
David Majnemer23252a32013-08-01 04:22:55 +00002477 Init = ParseCXXMemberInitializer(
2478 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2479
Douglas Gregor728d00b2011-10-10 14:49:18 +00002480 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002481 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002482 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002483 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smith74aeef52013-04-26 16:15:35 +00002484 DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002485 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor728d00b2011-10-10 14:49:18 +00002486 // No initializer.
Richard Smith74aeef52013-04-26 16:15:35 +00002487 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002488
Douglas Gregor728d00b2011-10-10 14:49:18 +00002489 if (ThisDecl) {
David Majnemer23252a32013-08-01 04:22:55 +00002490 if (!ThisDecl->isInvalidDecl()) {
2491 // Set the Decl for any late parsed attributes
2492 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2493 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2494
2495 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2496 LateParsedAttrs[i]->addDecl(ThisDecl);
2497 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002498 Actions.FinalizeDeclaration(ThisDecl);
2499 DeclsInGroup.push_back(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002500
2501 if (DeclaratorInfo.isFunctionDeclarator() &&
2502 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2503 DeclSpec::SCS_typedef)
2504 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002505 }
David Majnemer23252a32013-08-01 04:22:55 +00002506 LateParsedAttrs.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002507
2508 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002509
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002510 // If we don't have a comma, it is either the end of the list (a ';')
2511 // or an error, bail out.
Alp Toker094e5212014-01-05 03:27:11 +00002512 SourceLocation CommaLoc;
2513 if (!TryConsumeToken(tok::comma, CommaLoc))
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002514 break;
Mike Stump11289f42009-09-09 15:08:12 +00002515
Richard Smithc8a79032012-01-09 22:31:44 +00002516 if (Tok.isAtStartOfLine() &&
2517 !MightBeDeclarator(Declarator::MemberContext)) {
2518 // This comma was followed by a line-break and something which can't be
2519 // the start of a declarator. The comma was probably a typo for a
2520 // semicolon.
2521 Diag(CommaLoc, diag::err_expected_semi_declaration)
2522 << FixItHint::CreateReplacement(CommaLoc, ";");
2523 ExpectSemi = false;
2524 break;
2525 }
Mike Stump11289f42009-09-09 15:08:12 +00002526
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002527 // Parse the next declarator.
2528 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002529 VS.clear();
Nico Weberf56c85b2015-01-17 02:26:40 +00002530 BitfieldSize = ExprResult(/*Invalid=*/false);
2531 Init = ExprResult(/*Invalid=*/false);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002532 HasInitializer = false;
Richard Smith8d06f422012-01-12 23:53:29 +00002533 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002534
Richard Smith72553fc2014-01-23 23:53:27 +00002535 // GNU attributes are allowed before the second and subsequent declarator.
John McCall53fa7142010-12-24 02:08:15 +00002536 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002537
Nico Weberd89e6f72015-01-16 19:34:13 +00002538 if (ParseCXXMemberDeclaratorBeforeInitializer(
2539 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs))
2540 break;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002541 }
2542
Richard Smithc8a79032012-01-09 22:31:44 +00002543 if (ExpectSemi &&
2544 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002545 // Skip to end of block or statement.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002546 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002547 // If we stopped at a ';', eat it.
Alp Toker35d87032013-12-30 23:29:50 +00002548 TryConsumeToken(tok::semi);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002549 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002550 }
2551
Rafael Espindolaab417692013-07-09 12:05:01 +00002552 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002553}
2554
Richard Smith938f40b2011-06-11 17:19:42 +00002555/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2556/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2557/// function definition. The location of the '=', if any, will be placed in
2558/// EqualLoc.
2559///
2560/// pure-specifier:
2561/// '= 0'
Sebastian Redleef474c2012-02-22 10:50:08 +00002562///
Richard Smith938f40b2011-06-11 17:19:42 +00002563/// brace-or-equal-initializer:
2564/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002565/// braced-init-list
2566///
Richard Smith938f40b2011-06-11 17:19:42 +00002567/// initializer-clause:
2568/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002569/// braced-init-list
2570///
Richard Smithda35e962013-11-09 04:52:51 +00002571/// defaulted/deleted function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002572/// '=' 'default'
2573/// '=' 'delete'
2574///
2575/// Prior to C++0x, the assignment-expression in an initializer-clause must
2576/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002577ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002578 SourceLocation &EqualLoc) {
2579 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2580 && "Data member initializer not starting with '=' or '{'");
2581
Douglas Gregor926410d2012-02-21 02:22:07 +00002582 EnterExpressionEvaluationContext Context(Actions,
2583 Sema::PotentiallyEvaluated,
2584 D);
Alp Toker094e5212014-01-05 03:27:11 +00002585 if (TryConsumeToken(tok::equal, EqualLoc)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002586 if (Tok.is(tok::kw_delete)) {
2587 // In principle, an initializer of '= delete p;' is legal, but it will
2588 // never type-check. It's better to diagnose it as an ill-formed expression
2589 // than as an ill-formed deleted non-function member.
2590 // An initializer of '= delete p, foo' will never be parsed, because
2591 // a top-level comma always ends the initializer expression.
2592 const Token &Next = NextToken();
2593 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
Richard Smith34f30512013-11-23 04:06:09 +00002594 Next.is(tok::eof)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002595 if (IsFunction)
2596 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2597 << 1 /* delete */;
2598 else
2599 Diag(ConsumeToken(), diag::err_deleted_non_function);
Richard Smithedcb26e2014-06-11 00:49:52 +00002600 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002601 }
2602 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002603 if (IsFunction)
2604 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2605 << 0 /* default */;
2606 else
2607 Diag(ConsumeToken(), diag::err_default_special_members);
Richard Smithedcb26e2014-06-11 00:49:52 +00002608 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002609 }
David Majnemer87ff66c2014-12-13 11:34:16 +00002610 }
2611 if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) {
2612 Diag(Tok, diag::err_ms_property_initializer) << PD;
2613 return ExprError();
Sebastian Redleef474c2012-02-22 10:50:08 +00002614 }
2615 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002616}
2617
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002618/// ParseCXXMemberSpecification - Parse the class definition.
2619///
2620/// member-specification:
2621/// member-declaration member-specification[opt]
2622/// access-specifier ':' member-specification[opt]
2623///
Joao Matose9a3ed42012-08-31 22:18:20 +00002624void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002625 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00002626 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002627 unsigned TagType, Decl *TagDecl) {
2628 assert((TagType == DeclSpec::TST_struct ||
2629 TagType == DeclSpec::TST_interface ||
2630 TagType == DeclSpec::TST_union ||
2631 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2632
John McCallfaf5fb42010-08-26 23:41:50 +00002633 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2634 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00002635
Douglas Gregoredf8f392010-01-16 20:52:59 +00002636 // Determine whether this is a non-nested class. Note that local
2637 // classes are *not* considered to be nested classes.
2638 bool NonNestedClass = true;
2639 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002640 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002641 if (S->isClassScope()) {
2642 // We're inside a class scope, so this is a nested class.
2643 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00002644
2645 // The Microsoft extension __interface does not permit nested classes.
2646 if (getCurrentClass().IsInterface) {
2647 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2648 << /*ErrorType=*/6
2649 << (isa<NamedDecl>(TagDecl)
2650 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
David Blaikieabe1a392014-04-02 05:58:29 +00002651 : "(anonymous)");
John McCalldb632ac2012-09-25 07:32:39 +00002652 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00002653 break;
2654 }
2655
2656 if ((S->getFlags() & Scope::FnScope)) {
2657 // If we're in a function or function template declared in the
2658 // body of a class, then this is a local class rather than a
2659 // nested class.
2660 const Scope *Parent = S->getParent();
2661 if (Parent->isTemplateParamScope())
2662 Parent = Parent->getParent();
2663 if (Parent->isClassScope())
2664 break;
2665 }
2666 }
2667 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002668
2669 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00002670 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002671
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002672 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00002673 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2674 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002675
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002676 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002677 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002678
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002679 SourceLocation FinalLoc;
David Majnemera5433082013-10-18 00:33:31 +00002680 bool IsFinalSpelledSealed = false;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002681
2682 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002683 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemera5433082013-10-18 00:33:31 +00002684 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2685 assert((Specifier == VirtSpecifiers::VS_Final ||
2686 Specifier == VirtSpecifiers::VS_Sealed) &&
2687 "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00002688 FinalLoc = ConsumeToken();
David Majnemera5433082013-10-18 00:33:31 +00002689 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002690
David Majnemera5433082013-10-18 00:33:31 +00002691 if (TagType == DeclSpec::TST_interface)
John McCalldb632ac2012-09-25 07:32:39 +00002692 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemera5433082013-10-18 00:33:31 +00002693 << VirtSpecifiers::getSpecifierName(Specifier);
2694 else if (Specifier == VirtSpecifiers::VS_Final)
2695 Diag(FinalLoc, getLangOpts().CPlusPlus11
2696 ? diag::warn_cxx98_compat_override_control_keyword
2697 : diag::ext_override_control_keyword)
2698 << VirtSpecifiers::getSpecifierName(Specifier);
2699 else if (Specifier == VirtSpecifiers::VS_Sealed)
2700 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han9407e502012-11-26 22:54:45 +00002701
Michael Han309af292013-01-07 16:57:11 +00002702 // Parse any C++11 attributes after 'final' keyword.
2703 // These attributes are not allowed to appear here,
2704 // and the only possible place for them to appertain
2705 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00002706 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Nico Weber4b4be842014-12-29 06:56:50 +00002707
2708 // ParseClassSpecifier() does only a superficial check for attributes before
2709 // deciding to call this method. For example, for
2710 // `class C final alignas ([l) {` it will decide that this looks like a
2711 // misplaced attribute since it sees `alignas '(' ')'`. But the actual
2712 // attribute parsing code will try to parse the '[' as a constexpr lambda
2713 // and consume enough tokens that the alignas parsing code will eat the
2714 // opening '{'. So bail out if the next token isn't one we expect.
Nico Weber36de3a22014-12-29 21:56:22 +00002715 if (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)) {
2716 if (TagDecl)
2717 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
Nico Weber4b4be842014-12-29 06:56:50 +00002718 return;
Nico Weber36de3a22014-12-29 21:56:22 +00002719 }
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002720 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002721
John McCall2d814c32009-12-19 21:48:58 +00002722 if (Tok.is(tok::colon)) {
2723 ParseBaseClause(TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002724 if (!Tok.is(tok::l_brace)) {
Ismail Pazarbasi129c44c2014-09-25 21:13:02 +00002725 bool SuggestFixIt = false;
2726 SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation);
2727 if (Tok.isAtStartOfLine()) {
2728 switch (Tok.getKind()) {
2729 case tok::kw_private:
2730 case tok::kw_protected:
2731 case tok::kw_public:
2732 SuggestFixIt = NextToken().getKind() == tok::colon;
2733 break;
2734 case tok::kw_static_assert:
2735 case tok::r_brace:
2736 case tok::kw_using:
2737 // base-clause can have simple-template-id; 'template' can't be there
2738 case tok::kw_template:
2739 SuggestFixIt = true;
2740 break;
2741 case tok::identifier:
2742 SuggestFixIt = isConstructorDeclarator(true);
2743 break;
2744 default:
2745 SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
2746 break;
2747 }
2748 }
2749 DiagnosticBuilder LBraceDiag =
2750 Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers);
2751 if (SuggestFixIt) {
2752 LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {");
2753 // Try recovering from missing { after base-clause.
2754 PP.EnterToken(Tok);
2755 Tok.setKind(tok::l_brace);
2756 } else {
2757 if (TagDecl)
2758 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
2759 return;
2760 }
John McCall2d814c32009-12-19 21:48:58 +00002761 }
2762 }
2763
2764 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002765 BalancedDelimiterTracker T(*this, tok::l_brace);
2766 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00002767
John McCall08bede42010-05-28 08:11:17 +00002768 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00002769 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemera5433082013-10-18 00:33:31 +00002770 IsFinalSpelledSealed,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002771 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00002772
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002773 // C++ 11p3: Members of a class defined with the keyword class are private
2774 // by default. Members of a class defined with the keywords struct or union
2775 // are public by default.
2776 AccessSpecifier CurAS;
2777 if (TagType == DeclSpec::TST_class)
2778 CurAS = AS_private;
2779 else
2780 CurAS = AS_public;
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002781 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002782
Douglas Gregor9377c822010-06-21 22:31:09 +00002783 if (TagDecl) {
2784 // While we still have something to read, read the member-declarations.
Richard Smith34f30512013-11-23 04:06:09 +00002785 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Douglas Gregor9377c822010-06-21 22:31:09 +00002786 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002787
David Blaikiebbafb8a2012-03-11 07:00:24 +00002788 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet8f981d52011-05-25 10:19:49 +00002789 Tok.is(tok::kw___if_not_exists))) {
2790 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2791 continue;
2792 }
2793
Douglas Gregor9377c822010-06-21 22:31:09 +00002794 // Check for extraneous top-level semicolon.
2795 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002796 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor9377c822010-06-21 22:31:09 +00002797 continue;
2798 }
2799
Eli Friedmanec52f922012-02-23 23:47:16 +00002800 if (Tok.is(tok::annot_pragma_vis)) {
2801 HandlePragmaVisibility();
2802 continue;
2803 }
2804
2805 if (Tok.is(tok::annot_pragma_pack)) {
2806 HandlePragmaPack();
2807 continue;
2808 }
2809
Argyrios Kyrtzidis5c2021b2012-10-12 17:39:59 +00002810 if (Tok.is(tok::annot_pragma_align)) {
2811 HandlePragmaAlign();
2812 continue;
2813 }
2814
Alexey Bataeva769e072013-03-22 06:34:35 +00002815 if (Tok.is(tok::annot_pragma_openmp)) {
2816 ParseOpenMPDeclarativeDirective();
2817 continue;
2818 }
2819
David Majnemer4bb09802014-02-10 19:50:15 +00002820 if (Tok.is(tok::annot_pragma_ms_pointers_to_members)) {
2821 HandlePragmaMSPointersToMembers();
2822 continue;
2823 }
2824
Warren Huntc3b18962014-04-08 22:30:47 +00002825 if (Tok.is(tok::annot_pragma_ms_pragma)) {
2826 HandlePragmaMSPragma();
2827 continue;
2828 }
2829
Richard Smithda35e962013-11-09 04:52:51 +00002830 // If we see a namespace here, a close brace was missing somewhere.
2831 if (Tok.is(tok::kw_namespace)) {
Richard Smith2ac43ad2013-11-15 23:00:02 +00002832 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
Richard Smithda35e962013-11-09 04:52:51 +00002833 break;
2834 }
2835
Douglas Gregor9377c822010-06-21 22:31:09 +00002836 AccessSpecifier AS = getAccessSpecifierIfPresent();
2837 if (AS != AS_none) {
2838 // Current token is a C++ access specifier.
2839 CurAS = AS;
2840 SourceLocation ASLoc = Tok.getLocation();
David Blaikieeba32c22011-10-13 06:08:43 +00002841 unsigned TokLength = Tok.getLength();
Douglas Gregor9377c822010-06-21 22:31:09 +00002842 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002843 AccessAttrs.clear();
2844 MaybeParseGNUAttributes(AccessAttrs);
2845
David Blaikieeba32c22011-10-13 06:08:43 +00002846 SourceLocation EndLoc;
Alp Toker35d87032013-12-30 23:29:50 +00002847 if (TryConsumeToken(tok::colon, EndLoc)) {
2848 } else if (TryConsumeToken(tok::semi, EndLoc)) {
2849 Diag(EndLoc, diag::err_expected)
2850 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002851 } else {
2852 EndLoc = ASLoc.getLocWithOffset(TokLength);
Alp Toker35d87032013-12-30 23:29:50 +00002853 Diag(EndLoc, diag::err_expected)
2854 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002855 }
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002856
John McCalldb632ac2012-09-25 07:32:39 +00002857 // The Microsoft extension __interface does not permit non-public
2858 // access specifiers.
2859 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2860 Diag(ASLoc, diag::err_access_specifier_interface)
2861 << (CurAS == AS_protected);
2862 }
2863
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002864 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2865 AccessAttrs.getList())) {
2866 // found another attribute than only annotations
2867 AccessAttrs.clear();
2868 }
2869
Douglas Gregor9377c822010-06-21 22:31:09 +00002870 continue;
2871 }
2872
Douglas Gregor9377c822010-06-21 22:31:09 +00002873 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002874 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002875 }
2876
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002877 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00002878 } else {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002879 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002880 }
Mike Stump11289f42009-09-09 15:08:12 +00002881
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002882 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002883 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002884 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002885
John McCall08bede42010-05-28 08:11:17 +00002886 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002887 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002888 T.getOpenLocation(),
2889 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002890 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002891
Douglas Gregor433e0532012-04-16 18:27:27 +00002892 // C++11 [class.mem]p2:
2893 // Within the class member-specification, the class is regarded as complete
Richard Smith0b3a4622014-11-13 20:01:57 +00002894 // within function bodies, default arguments, exception-specifications, and
Douglas Gregor433e0532012-04-16 18:27:27 +00002895 // brace-or-equal-initializers for non-static data members (including such
2896 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00002897 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002898 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00002899 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002900 // declarations and the lexed inline method definitions, along with any
2901 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00002902 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002903 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002904 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00002905
2906 // We've finished with all pending member declarations.
2907 Actions.ActOnFinishCXXMemberDecls();
2908
Richard Smith938f40b2011-06-11 17:19:42 +00002909 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002910 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00002911 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002912 }
2913
John McCall08bede42010-05-28 08:11:17 +00002914 if (TagDecl)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002915 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2916 T.getCloseLocation());
John McCall2ff380a2010-03-17 00:38:33 +00002917
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002918 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002919 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002920 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002921}
Douglas Gregore8381c02008-11-05 04:29:56 +00002922
Richard Smith2ac43ad2013-11-15 23:00:02 +00002923void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithda35e962013-11-09 04:52:51 +00002924 assert(Tok.is(tok::kw_namespace));
2925
2926 // FIXME: Suggest where the close brace should have gone by looking
2927 // at indentation changes within the definition body.
Richard Smith2ac43ad2013-11-15 23:00:02 +00002928 Diag(D->getLocation(),
2929 diag::err_missing_end_of_definition) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002930 Diag(Tok.getLocation(),
Richard Smith2ac43ad2013-11-15 23:00:02 +00002931 diag::note_missing_end_of_definition_before) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002932
2933 // Push '};' onto the token stream to recover.
2934 PP.EnterToken(Tok);
2935
2936 Tok.startToken();
2937 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
2938 Tok.setKind(tok::semi);
2939 PP.EnterToken(Tok);
2940
2941 Tok.setKind(tok::r_brace);
2942}
2943
Douglas Gregore8381c02008-11-05 04:29:56 +00002944/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2945/// which explicitly initializes the members or base classes of a
2946/// class (C++ [class.base.init]). For example, the three initializers
2947/// after the ':' in the Derived constructor below:
2948///
2949/// @code
2950/// class Base { };
2951/// class Derived : Base {
2952/// int x;
2953/// float f;
2954/// public:
2955/// Derived(float f) : Base(), x(17), f(f) { }
2956/// };
2957/// @endcode
2958///
Mike Stump11289f42009-09-09 15:08:12 +00002959/// [C++] ctor-initializer:
2960/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00002961///
Mike Stump11289f42009-09-09 15:08:12 +00002962/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00002963/// mem-initializer ...[opt]
2964/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00002965void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00002966 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2967
John Wiegley1c0675e2011-04-28 01:08:34 +00002968 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2969 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00002970 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002971
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002972 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002973 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002974
Douglas Gregore8381c02008-11-05 04:29:56 +00002975 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002976 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00002977 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2978 MemInitializers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002979 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002980 } else {
2981 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2982 if (!MemInit.isInvalid())
2983 MemInitializers.push_back(MemInit.get());
2984 else
2985 AnyErrors = true;
2986 }
2987
Douglas Gregore8381c02008-11-05 04:29:56 +00002988 if (Tok.is(tok::comma))
2989 ConsumeToken();
2990 else if (Tok.is(tok::l_brace))
2991 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00002992 // If the next token looks like a base or member initializer, assume that
2993 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00002994 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2995 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2996 Diag(Loc, diag::err_ctor_init_missing_comma)
2997 << FixItHint::CreateInsertion(Loc, ", ");
2998 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00002999 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alp Tokerec543272013-12-24 09:48:30 +00003000 Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
3001 << tok::comma;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003002 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregore8381c02008-11-05 04:29:56 +00003003 break;
3004 }
3005 } while (true);
3006
David Blaikie3fc2f912013-01-17 05:26:25 +00003007 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00003008 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00003009}
3010
3011/// ParseMemInitializer - Parse a C++ member initializer, which is
3012/// part of a constructor initializer that explicitly initializes one
3013/// member or base class (C++ [class.base.init]). See
3014/// ParseConstructorInitializer for an example.
3015///
3016/// [C++] mem-initializer:
3017/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00003018/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00003019///
Douglas Gregore8381c02008-11-05 04:29:56 +00003020/// [C++] mem-initializer-id:
3021/// '::'[opt] nested-name-specifier[opt] class-name
3022/// identifier
Craig Topper9ad7e262014-10-31 06:57:07 +00003023MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00003024 // parse '::'[opt] nested-name-specifier[opt]
3025 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00003026 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00003027 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003028 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00003029 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00003030 if (TemplateId->Kind == TNK_Type_template ||
3031 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00003032 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003033 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00003034 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003035 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003036 }
David Blaikie186a8892012-01-24 06:03:59 +00003037 // Uses of decltype will already have been converted to annot_decltype by
3038 // ParseOptionalCXXScopeSpecifier at this point.
3039 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
3040 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00003041 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00003042 return true;
3043 }
Mike Stump11289f42009-09-09 15:08:12 +00003044
Craig Topper161e4db2014-05-21 06:02:52 +00003045 IdentifierInfo *II = nullptr;
David Blaikie186a8892012-01-24 06:03:59 +00003046 DeclSpec DS(AttrFactory);
3047 SourceLocation IdLoc = Tok.getLocation();
3048 if (Tok.is(tok::annot_decltype)) {
3049 // Get the decltype expression, if there is one.
3050 ParseDecltypeSpecifier(DS);
3051 } else {
3052 if (Tok.is(tok::identifier))
3053 // Get the identifier. This may be a member name or a class name,
3054 // but we'll let the semantic analysis determine which it is.
3055 II = Tok.getIdentifierInfo();
3056 ConsumeToken();
3057 }
3058
Douglas Gregore8381c02008-11-05 04:29:56 +00003059
3060 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003061 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003062 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3063
Sebastian Redla74948d2011-09-24 17:48:25 +00003064 ExprResult InitList = ParseBraceInitializer();
3065 if (InitList.isInvalid())
3066 return true;
3067
3068 SourceLocation EllipsisLoc;
Alp Toker094e5212014-01-05 03:27:11 +00003069 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redla74948d2011-09-24 17:48:25 +00003070
3071 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00003072 TemplateTypeTy, DS, IdLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003073 InitList.get(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00003074 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003075 BalancedDelimiterTracker T(*this, tok::l_paren);
3076 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00003077
Sebastian Redl3da34892011-06-05 12:23:16 +00003078 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00003079 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00003080 CommaLocsTy CommaLocs;
3081 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003082 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00003083 return true;
3084 }
3085
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003086 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00003087
3088 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00003089 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00003090
3091 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00003092 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00003093 T.getOpenLocation(), ArgExprs,
3094 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00003095 }
3096
Alp Tokerec543272013-12-24 09:48:30 +00003097 if (getLangOpts().CPlusPlus11)
3098 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
3099 else
3100 return Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregore8381c02008-11-05 04:29:56 +00003101}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003102
Sebastian Redl965b0e32011-03-05 14:45:16 +00003103/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003104///
Douglas Gregor356513d2008-12-01 18:00:20 +00003105/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00003106/// dynamic-exception-specification
3107/// noexcept-specification
3108///
3109/// noexcept-specification:
3110/// 'noexcept'
3111/// 'noexcept' '(' constant-expression ')'
3112ExceptionSpecificationType
Richard Smith0b3a4622014-11-13 20:01:57 +00003113Parser::tryParseExceptionSpecification(bool Delayed,
Douglas Gregor433e0532012-04-16 18:27:27 +00003114 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003115 SmallVectorImpl<ParsedType> &DynamicExceptions,
3116 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith0b3a4622014-11-13 20:01:57 +00003117 ExprResult &NoexceptExpr,
3118 CachedTokens *&ExceptionSpecTokens) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003119 ExceptionSpecificationType Result = EST_None;
Richard Smith0b3a4622014-11-13 20:01:57 +00003120 ExceptionSpecTokens = 0;
3121
3122 // Handle delayed parsing of exception-specifications.
3123 if (Delayed) {
3124 if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept))
3125 return EST_None;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003126
Richard Smith0b3a4622014-11-13 20:01:57 +00003127 // Consume and cache the starting token.
3128 bool IsNoexcept = Tok.is(tok::kw_noexcept);
3129 Token StartTok = Tok;
3130 SpecificationRange = SourceRange(ConsumeToken());
3131
3132 // Check for a '('.
3133 if (!Tok.is(tok::l_paren)) {
3134 // If this is a bare 'noexcept', we're done.
3135 if (IsNoexcept) {
3136 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3137 NoexceptExpr = 0;
3138 return EST_BasicNoexcept;
3139 }
3140
3141 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3142 return EST_DynamicNone;
3143 }
3144
3145 // Cache the tokens for the exception-specification.
3146 ExceptionSpecTokens = new CachedTokens;
3147 ExceptionSpecTokens->push_back(StartTok); // 'throw' or 'noexcept'
3148 ExceptionSpecTokens->push_back(Tok); // '('
3149 SpecificationRange.setEnd(ConsumeParen()); // '('
Richard Smithb1c217e2015-01-13 02:24:58 +00003150
3151 ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens,
3152 /*StopAtSemi=*/true,
3153 /*ConsumeFinalToken=*/true);
Richard Smith0b3a4622014-11-13 20:01:57 +00003154 SpecificationRange.setEnd(Tok.getLocation());
Richard Smith0b3a4622014-11-13 20:01:57 +00003155 return EST_Unparsed;
3156 }
3157
Sebastian Redl965b0e32011-03-05 14:45:16 +00003158 // See if there's a dynamic specification.
3159 if (Tok.is(tok::kw_throw)) {
3160 Result = ParseDynamicExceptionSpecification(SpecificationRange,
3161 DynamicExceptions,
3162 DynamicExceptionRanges);
3163 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
3164 "Produced different number of exception types and ranges.");
3165 }
3166
3167 // If there's no noexcept specification, we're done.
3168 if (Tok.isNot(tok::kw_noexcept))
3169 return Result;
3170
Richard Smithb15c11c2011-10-17 23:06:20 +00003171 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3172
Sebastian Redl965b0e32011-03-05 14:45:16 +00003173 // If we already had a dynamic specification, parse the noexcept for,
3174 // recovery, but emit a diagnostic and don't store the results.
3175 SourceRange NoexceptRange;
3176 ExceptionSpecificationType NoexceptType = EST_None;
3177
3178 SourceLocation KeywordLoc = ConsumeToken();
3179 if (Tok.is(tok::l_paren)) {
3180 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003181 BalancedDelimiterTracker T(*this, tok::l_paren);
3182 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003183 NoexceptType = EST_ComputedNoexcept;
3184 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003185 // The argument must be contextually convertible to bool. We use
3186 // ActOnBooleanCondition for this purpose.
3187 if (!NoexceptExpr.isInvalid())
3188 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
3189 NoexceptExpr.get());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003190 T.consumeClose();
3191 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl965b0e32011-03-05 14:45:16 +00003192 } else {
3193 // There is no argument.
3194 NoexceptType = EST_BasicNoexcept;
3195 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
3196 }
3197
3198 if (Result == EST_None) {
3199 SpecificationRange = NoexceptRange;
3200 Result = NoexceptType;
3201
3202 // If there's a dynamic specification after a noexcept specification,
3203 // parse that and ignore the results.
3204 if (Tok.is(tok::kw_throw)) {
3205 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3206 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
3207 DynamicExceptionRanges);
3208 }
3209 } else {
3210 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3211 }
3212
3213 return Result;
3214}
3215
Richard Smith8ca78a12013-06-13 02:02:51 +00003216static void diagnoseDynamicExceptionSpecification(
3217 Parser &P, const SourceRange &Range, bool IsNoexcept) {
3218 if (P.getLangOpts().CPlusPlus11) {
3219 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
3220 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
3221 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
3222 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
3223 }
3224}
3225
Sebastian Redl965b0e32011-03-05 14:45:16 +00003226/// ParseDynamicExceptionSpecification - Parse a C++
3227/// dynamic-exception-specification (C++ [except.spec]).
3228///
3229/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00003230/// 'throw' '(' type-id-list [opt] ')'
3231/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00003232///
Douglas Gregor356513d2008-12-01 18:00:20 +00003233/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00003234/// type-id ... [opt]
3235/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003236///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003237ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
3238 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003239 SmallVectorImpl<ParsedType> &Exceptions,
3240 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003241 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00003242
Sebastian Redl965b0e32011-03-05 14:45:16 +00003243 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003244 BalancedDelimiterTracker T(*this, tok::l_paren);
3245 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003246 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3247 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003248 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003249 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003250
Douglas Gregor356513d2008-12-01 18:00:20 +00003251 // Parse throw(...), a Microsoft extension that means "this function
3252 // can throw anything".
3253 if (Tok.is(tok::ellipsis)) {
3254 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003255 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00003256 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003257 T.consumeClose();
3258 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003259 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003260 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00003261 }
3262
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003263 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00003264 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003265 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00003266 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00003267
Douglas Gregor830837d2010-12-20 23:57:46 +00003268 if (Tok.is(tok::ellipsis)) {
3269 // C++0x [temp.variadic]p5:
3270 // - In a dynamic-exception-specification (15.4); the pattern is a
3271 // type-id.
3272 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003273 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00003274 if (!Res.isInvalid())
3275 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3276 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003277
Sebastian Redld6434562009-05-29 18:02:33 +00003278 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003279 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00003280 Ranges.push_back(Range);
3281 }
Alp Toker97650562014-01-10 11:19:30 +00003282
3283 if (!TryConsumeToken(tok::comma))
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003284 break;
3285 }
3286
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003287 T.consumeClose();
3288 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003289 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3290 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003291 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003292}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003293
Douglas Gregor7fb25412010-10-01 18:44:50 +00003294/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3295/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003296TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00003297 assert(Tok.is(tok::arrow) && "expected arrow");
3298
3299 ConsumeToken();
3300
Richard Smithbfdb1082012-03-12 08:56:40 +00003301 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003302}
3303
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003304/// \brief We have just started parsing the definition of a new class,
3305/// so push that class onto our stack of classes that is currently
3306/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003307Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003308Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3309 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003310 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003311 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003312 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003313 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003314}
3315
3316/// \brief Deallocate the given parsed class and all of its nested
3317/// classes.
3318void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003319 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3320 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003321 delete Class;
3322}
3323
3324/// \brief Pop the top class of the stack of classes that are
3325/// currently being parsed.
3326///
3327/// This routine should be called when we have finished parsing the
3328/// definition of a class, but have not yet popped the Scope
3329/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003330void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003331 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003332
John McCallc1465822011-02-14 07:13:47 +00003333 Actions.PopParsingClass(state);
3334
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003335 ParsingClass *Victim = ClassStack.top();
3336 ClassStack.pop();
3337 if (Victim->TopLevelClass) {
3338 // Deallocate all of the nested classes of this class,
3339 // recursively: we don't need to keep any of this information.
3340 DeallocateParsedClasses(Victim);
3341 return;
Mike Stump11289f42009-09-09 15:08:12 +00003342 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003343 assert(!ClassStack.empty() && "Missing top-level class?");
3344
Douglas Gregorefc46952010-10-12 16:25:54 +00003345 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003346 // The victim is a nested class, but we will not need to perform
3347 // any processing after the definition of this class since it has
3348 // no members whose handling was delayed. Therefore, we can just
3349 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003350 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003351 return;
3352 }
3353
3354 // This nested class has some members that will need to be processed
3355 // after the top-level class is completely defined. Therefore, add
3356 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003357 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003358 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003359 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003360}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003361
Richard Smith3dff2512012-04-10 03:25:07 +00003362/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3363///
3364/// \return the parsed identifier on success, and 0 if the next token is not an
3365/// attribute-token.
3366///
3367/// C++11 [dcl.attr.grammar]p3:
3368/// If a keyword or an alternative token that satisfies the syntactic
3369/// requirements of an identifier is contained in an attribute-token,
3370/// it is considered an identifier.
3371IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3372 switch (Tok.getKind()) {
3373 default:
3374 // Identifiers and keywords have identifier info attached.
David Majnemerd5271992015-01-09 18:09:39 +00003375 if (!Tok.isAnnotation()) {
3376 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3377 Loc = ConsumeToken();
3378 return II;
3379 }
Richard Smith3dff2512012-04-10 03:25:07 +00003380 }
Craig Topper161e4db2014-05-21 06:02:52 +00003381 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003382
3383 case tok::ampamp: // 'and'
3384 case tok::pipe: // 'bitor'
3385 case tok::pipepipe: // 'or'
3386 case tok::caret: // 'xor'
3387 case tok::tilde: // 'compl'
3388 case tok::amp: // 'bitand'
3389 case tok::ampequal: // 'and_eq'
3390 case tok::pipeequal: // 'or_eq'
3391 case tok::caretequal: // 'xor_eq'
3392 case tok::exclaim: // 'not'
3393 case tok::exclaimequal: // 'not_eq'
3394 // Alternative tokens do not have identifier info, but their spelling
3395 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003396 SmallString<8> SpellingBuf;
Richard Smith3dff2512012-04-10 03:25:07 +00003397 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003398 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003399 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003400 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003401 }
Craig Topper161e4db2014-05-21 06:02:52 +00003402 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003403 }
3404}
3405
Michael Han23214e52012-10-03 01:56:22 +00003406static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3407 IdentifierInfo *ScopeName) {
3408 switch (AttributeList::getKind(AttrName, ScopeName,
3409 AttributeList::AS_CXX11)) {
3410 case AttributeList::AT_CarriesDependency:
Aaron Ballman35f94212014-04-14 16:03:22 +00003411 case AttributeList::AT_Deprecated:
Michael Han23214e52012-10-03 01:56:22 +00003412 case AttributeList::AT_FallThrough:
Richard Smith10876ef2013-01-17 01:30:42 +00003413 case AttributeList::AT_CXX11NoReturn: {
Michael Han23214e52012-10-03 01:56:22 +00003414 return true;
3415 }
3416
3417 default:
3418 return false;
3419 }
3420}
3421
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003422/// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause.
3423///
3424/// [C++11] attribute-argument-clause:
3425/// '(' balanced-token-seq ')'
3426///
3427/// [C++11] balanced-token-seq:
3428/// balanced-token
3429/// balanced-token-seq balanced-token
3430///
3431/// [C++11] balanced-token:
3432/// '(' balanced-token-seq ')'
3433/// '[' balanced-token-seq ']'
3434/// '{' balanced-token-seq '}'
3435/// any token but '(', ')', '[', ']', '{', or '}'
3436bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName,
3437 SourceLocation AttrNameLoc,
3438 ParsedAttributes &Attrs,
3439 SourceLocation *EndLoc,
3440 IdentifierInfo *ScopeName,
3441 SourceLocation ScopeLoc) {
3442 assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
Aaron Ballman35f94212014-04-14 16:03:22 +00003443 SourceLocation LParenLoc = Tok.getLocation();
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003444
3445 // If the attribute isn't known, we will not attempt to parse any
3446 // arguments.
3447 if (!hasAttribute(AttrSyntax::CXX, ScopeName, AttrName,
3448 getTargetInfo().getTriple(), getLangOpts())) {
3449 // Eat the left paren, then skip to the ending right paren.
3450 ConsumeParen();
3451 SkipUntil(tok::r_paren);
3452 return false;
3453 }
3454
3455 if (ScopeName && ScopeName->getName() == "gnu")
3456 // GNU-scoped attributes have some special cases to handle GNU-specific
3457 // behaviors.
3458 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
Craig Topper161e4db2014-05-21 06:02:52 +00003459 ScopeLoc, AttributeList::AS_CXX11, nullptr);
Aaron Ballman35f94212014-04-14 16:03:22 +00003460 else {
3461 unsigned NumArgs =
3462 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
3463 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3464
3465 const AttributeList *Attr = Attrs.getList();
3466 if (Attr && IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) {
3467 // If the attribute is a standard or built-in attribute and we are
3468 // parsing an argument list, we need to determine whether this attribute
3469 // was allowed to have an argument list (such as [[deprecated]]), and how
3470 // many arguments were parsed (so we can diagnose on [[deprecated()]]).
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003471 if (Attr->getMaxArgs() && !NumArgs) {
3472 // The attribute was allowed to have arguments, but none were provided
3473 // even though the attribute parsed successfully. This is an error.
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003474 Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
3475 return false;
3476 } else if (!Attr->getMaxArgs()) {
3477 // The attribute parsed successfully, but was not allowed to have any
3478 // arguments. It doesn't matter whether any were provided -- the
Aaron Ballman35f94212014-04-14 16:03:22 +00003479 // presence of the argument list (even if empty) is diagnosed.
3480 Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
Aaron Ballman9b7cee62014-12-19 18:37:22 +00003481 << AttrName
3482 << FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc));
Aaron Ballman35f94212014-04-14 16:03:22 +00003483 return false;
3484 }
3485 }
3486 }
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003487 return true;
3488}
3489
3490/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003491///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003492/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003493/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003494/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003495///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003496/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003497/// attribute[opt]
3498/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003499/// attribute '...'
3500/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003501///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003502/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003503/// attribute-token attribute-argument-clause[opt]
3504///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003505/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003506/// identifier
3507/// attribute-scoped-token
3508///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003509/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003510/// attribute-namespace '::' identifier
3511///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003512/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003513/// identifier
Richard Smith3dff2512012-04-10 03:25:07 +00003514void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003515 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003516 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003517 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003518 ParseAlignmentSpecifier(attrs, endLoc);
3519 return;
3520 }
3521
Alexis Hunt96d5c762009-11-21 08:43:09 +00003522 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003523 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003524
Richard Smithf679b5b2011-10-14 20:48:27 +00003525 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3526
Alexis Hunt96d5c762009-11-21 08:43:09 +00003527 ConsumeBracket();
3528 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003529
Richard Smith10876ef2013-01-17 01:30:42 +00003530 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3531
Richard Smith3dff2512012-04-10 03:25:07 +00003532 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003533 // attribute not present
Alp Toker97650562014-01-10 11:19:30 +00003534 if (TryConsumeToken(tok::comma))
Alexis Hunt96d5c762009-11-21 08:43:09 +00003535 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003536
Richard Smith3dff2512012-04-10 03:25:07 +00003537 SourceLocation ScopeLoc, AttrLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00003538 IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003539
3540 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3541 if (!AttrName)
3542 // Break out to the "expected ']'" diagnostic.
3543 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003544
Alexis Hunt96d5c762009-11-21 08:43:09 +00003545 // scoped attribute
Alp Toker97650562014-01-10 11:19:30 +00003546 if (TryConsumeToken(tok::coloncolon)) {
Richard Smith3dff2512012-04-10 03:25:07 +00003547 ScopeName = AttrName;
3548 ScopeLoc = AttrLoc;
3549
3550 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3551 if (!AttrName) {
Alp Tokerec543272013-12-24 09:48:30 +00003552 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003553 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003554 continue;
3555 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003556 }
3557
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003558 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003559 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003560
Richard Smith10876ef2013-01-17 01:30:42 +00003561 if (StandardAttr &&
3562 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3563 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003564 << AttrName << SourceRange(SeenAttrs[AttrName]);
Richard Smith10876ef2013-01-17 01:30:42 +00003565
Michael Han23214e52012-10-03 01:56:22 +00003566 // Parse attribute arguments
Aaron Ballman35f94212014-04-14 16:03:22 +00003567 if (Tok.is(tok::l_paren))
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003568 AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3569 ScopeName, ScopeLoc);
Michael Han23214e52012-10-03 01:56:22 +00003570
3571 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003572 attrs.addNew(AttrName,
3573 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3574 AttrLoc),
Craig Topper161e4db2014-05-21 06:02:52 +00003575 ScopeName, ScopeLoc, nullptr, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003576
Alp Toker97650562014-01-10 11:19:30 +00003577 if (TryConsumeToken(tok::ellipsis))
Michael Han23214e52012-10-03 01:56:22 +00003578 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3579 << AttrName->getName();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003580 }
3581
Alp Toker383d2c42014-01-01 03:08:43 +00003582 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003583 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003584 if (endLoc)
3585 *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003586 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003587 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003588}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003589
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003590/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003591///
3592/// attribute-specifier-seq:
3593/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003594void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003595 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003596 assert(getLangOpts().CPlusPlus11);
3597
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003598 SourceLocation StartLoc = Tok.getLocation(), Loc;
3599 if (!endLoc)
3600 endLoc = &Loc;
3601
Douglas Gregor6f981002011-10-07 20:35:25 +00003602 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003603 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003604 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003605
3606 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003607}
3608
Richard Smithc2c8bb82013-10-15 01:34:54 +00003609void Parser::DiagnoseAndSkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00003610 // Start and end location of an attribute or an attribute list.
3611 SourceLocation StartLoc = Tok.getLocation();
Richard Smith955bf012014-06-19 11:42:00 +00003612 SourceLocation EndLoc = SkipCXX11Attributes();
3613
3614 if (EndLoc.isValid()) {
3615 SourceRange Range(StartLoc, EndLoc);
3616 Diag(StartLoc, diag::err_attributes_not_allowed)
3617 << Range;
3618 }
3619}
3620
3621SourceLocation Parser::SkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00003622 SourceLocation EndLoc;
3623
Richard Smith955bf012014-06-19 11:42:00 +00003624 if (!isCXX11AttributeSpecifier())
3625 return EndLoc;
3626
Richard Smithc2c8bb82013-10-15 01:34:54 +00003627 do {
3628 if (Tok.is(tok::l_square)) {
3629 BalancedDelimiterTracker T(*this, tok::l_square);
3630 T.consumeOpen();
3631 T.skipToEnd();
3632 EndLoc = T.getCloseLocation();
3633 } else {
3634 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3635 ConsumeToken();
3636 BalancedDelimiterTracker T(*this, tok::l_paren);
3637 if (!T.consumeOpen())
3638 T.skipToEnd();
3639 EndLoc = T.getCloseLocation();
3640 }
3641 } while (isCXX11AttributeSpecifier());
3642
Richard Smith955bf012014-06-19 11:42:00 +00003643 return EndLoc;
Richard Smithc2c8bb82013-10-15 01:34:54 +00003644}
3645
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003646/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3647///
3648/// [MS] ms-attribute:
3649/// '[' token-seq ']'
3650///
3651/// [MS] ms-attribute-seq:
3652/// ms-attribute[opt]
3653/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00003654void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3655 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003656 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3657
3658 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003659 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003660 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003661 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
John McCall53fa7142010-12-24 02:08:15 +00003662 if (endLoc) *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003663 ExpectAndConsume(tok::r_square);
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003664 }
3665}
Francois Pichet8f981d52011-05-25 10:19:49 +00003666
3667void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3668 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00003669 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00003670 if (ParseMicrosoftIfExistsCondition(Result))
3671 return;
3672
Douglas Gregor43edb322011-10-24 22:31:10 +00003673 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3674 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00003675 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet8f981d52011-05-25 10:19:49 +00003676 return;
3677 }
Francois Pichet8f981d52011-05-25 10:19:49 +00003678
Douglas Gregor43edb322011-10-24 22:31:10 +00003679 switch (Result.Behavior) {
3680 case IEB_Parse:
3681 // Parse the declarations below.
3682 break;
3683
3684 case IEB_Dependent:
3685 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3686 << Result.IsIfExists;
3687 // Fall through to skip.
3688
3689 case IEB_Skip:
3690 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00003691 return;
3692 }
3693
Richard Smith34f30512013-11-23 04:06:09 +00003694 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003695 // __if_exists, __if_not_exists can nest.
3696 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3697 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3698 continue;
3699 }
3700
3701 // Check for extraneous top-level semicolon.
3702 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003703 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00003704 continue;
3705 }
3706
3707 AccessSpecifier AS = getAccessSpecifierIfPresent();
3708 if (AS != AS_none) {
3709 // Current token is a C++ access specifier.
3710 CurAS = AS;
3711 SourceLocation ASLoc = Tok.getLocation();
3712 ConsumeToken();
3713 if (Tok.is(tok::colon))
3714 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3715 else
Alp Toker35d87032013-12-30 23:29:50 +00003716 Diag(Tok, diag::err_expected) << tok::colon;
Francois Pichet8f981d52011-05-25 10:19:49 +00003717 ConsumeToken();
3718 continue;
3719 }
3720
3721 // Parse all the comma separated declarators.
Craig Topper161e4db2014-05-21 06:02:52 +00003722 ParseCXXClassMemberDeclaration(CurAS, nullptr);
Francois Pichet8f981d52011-05-25 10:19:49 +00003723 }
Douglas Gregor43edb322011-10-24 22:31:10 +00003724
3725 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00003726}