blob: aac7d630954828fa4ac49a68e2c9c34373a01e5c [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);
David Majnemerf9bde282015-03-11 06:45:39 +0000561 Decl *DeclFromDeclSpec = nullptr;
Richard Smithdda56e42011-04-15 14:24:37 +0000562 if (IsAliasDecl) {
Richard Smithc2c8bb82013-10-15 01:34:54 +0000563 // If we had any misplaced attributes from earlier, this is where they
564 // should have been written.
565 if (MisplacedAttrs.Range.isValid()) {
566 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
567 << FixItHint::CreateInsertionFromRange(
568 Tok.getLocation(),
569 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
570 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
571 Attrs.takeAllFrom(MisplacedAttrs);
572 }
573
Richard Smithdda56e42011-04-15 14:24:37 +0000574 ConsumeToken();
575
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000576 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000577 diag::warn_cxx98_compat_alias_declaration :
578 diag::ext_alias_declaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000579
Richard Smith3f1b5d02011-05-05 21:57:07 +0000580 // Type alias templates cannot be specialized.
581 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000582 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
583 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000584 SpecKind = 0;
585 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
586 SpecKind = 1;
587 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
588 SpecKind = 2;
589 if (SpecKind != -1) {
590 SourceRange Range;
591 if (SpecKind == 0)
592 Range = SourceRange(Name.TemplateId->LAngleLoc,
593 Name.TemplateId->RAngleLoc);
594 else
595 Range = TemplateInfo.getSourceRange();
596 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
597 << SpecKind << Range;
598 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000599 return nullptr;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000600 }
601
Richard Smithdda56e42011-04-15 14:24:37 +0000602 // Name must be an identifier.
603 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
604 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
605 // No removal fixit: can't recover from this.
606 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000607 return nullptr;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000608 } else if (HasTypenameKeyword)
Richard Smithdda56e42011-04-15 14:24:37 +0000609 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
610 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
611 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
612 else if (SS.isNotEmpty())
613 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
614 << FixItHint::CreateRemoval(SS.getRange());
615
David Majnemerf9bde282015-03-11 06:45:39 +0000616 TypeAlias = ParseTypeName(nullptr, TemplateInfo.Kind
617 ? Declarator::AliasTemplateContext
618 : Declarator::AliasDeclContext,
619 AS, &DeclFromDeclSpec, &Attrs);
620 if (OwnedType)
621 *OwnedType = DeclFromDeclSpec;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000622 } else {
623 // C++11 attributes are not allowed on a using-declaration, but GNU ones
624 // are.
Richard Smithc2c8bb82013-10-15 01:34:54 +0000625 ProhibitAttributes(MisplacedAttrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000626 ProhibitAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000627
Richard Smithdda56e42011-04-15 14:24:37 +0000628 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith54ecd982013-02-20 19:22:51 +0000629 MaybeParseGNUAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000630 }
Mike Stump11289f42009-09-09 15:08:12 +0000631
Douglas Gregorfec52632009-06-20 00:51:54 +0000632 // Eat ';'.
633 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000634 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
635 !Attrs.empty() ? "attributes list"
636 : IsAliasDecl ? "alias declaration"
637 : "using declaration"))
638 SkipUntil(tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000639
John McCall9b72f892010-11-10 02:40:36 +0000640 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000641 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000642 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000643 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-11-10 02:40:36 +0000644 SourceRange R = TemplateInfo.getSourceRange();
645 Diag(UsingLoc, diag::err_templated_using_declaration)
646 << R << FixItHint::CreateRemoval(R);
647
648 // Unfortunately, we have to bail out instead of recovering by
649 // ignoring the parameters, just in case the nested name specifier
650 // depends on the parameters.
Craig Topper161e4db2014-05-21 06:02:52 +0000651 return nullptr;
John McCall9b72f892010-11-10 02:40:36 +0000652 }
653
Douglas Gregor882a61a2011-09-26 14:30:28 +0000654 // "typename" keyword is allowed for identifiers only,
655 // because it may be a type definition.
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000656 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor882a61a2011-09-26 14:30:28 +0000657 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
658 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000659 // Proceed parsing, but reset the HasTypenameKeyword flag.
660 HasTypenameKeyword = false;
Douglas Gregor882a61a2011-09-26 14:30:28 +0000661 }
662
Richard Smith3f1b5d02011-05-05 21:57:07 +0000663 if (IsAliasDecl) {
664 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000665 MultiTemplateParamsArg TemplateParamsArg(
Craig Topper161e4db2014-05-21 06:02:52 +0000666 TemplateParams ? TemplateParams->data() : nullptr,
Richard Smith3f1b5d02011-05-05 21:57:07 +0000667 TemplateParams ? TemplateParams->size() : 0);
668 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith54ecd982013-02-20 19:22:51 +0000669 UsingLoc, Name, Attrs.getList(),
David Majnemerf9bde282015-03-11 06:45:39 +0000670 TypeAlias, DeclFromDeclSpec);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000671 }
Richard Smithdda56e42011-04-15 14:24:37 +0000672
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000673 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
674 /* HasUsingKeyword */ true, UsingLoc,
675 SS, Name, Attrs.getList(),
676 HasTypenameKeyword, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000677}
678
Benjamin Kramere56f3932011-12-23 17:00:35 +0000679/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000680///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000681/// [C++0x] static_assert-declaration:
682/// static_assert ( constant-expression , string-literal ) ;
683///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000684/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000685/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000686///
John McCall48871652010-08-21 09:40:31 +0000687Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000688 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
689 "Not a static_assert declaration");
690
David Blaikiebbafb8a2012-03-11 07:00:24 +0000691 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000692 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000693 if (Tok.is(tok::kw_static_assert))
694 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000695
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000696 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000697
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000698 BalancedDelimiterTracker T(*this, tok::l_paren);
699 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000700 Diag(Tok, diag::err_expected) << tok::l_paren;
Richard Smith76965712012-09-13 19:12:50 +0000701 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000702 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000703 }
Mike Stump11289f42009-09-09 15:08:12 +0000704
John McCalldadc5752010-08-24 06:29:42 +0000705 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000706 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000707 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000708 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000709 }
Mike Stump11289f42009-09-09 15:08:12 +0000710
Richard Smith085a64f2014-06-20 19:57:12 +0000711 ExprResult AssertMessage;
712 if (Tok.is(tok::r_paren)) {
713 Diag(Tok, getLangOpts().CPlusPlus1z
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000714 ? diag::warn_cxx14_compat_static_assert_no_message
Richard Smith085a64f2014-06-20 19:57:12 +0000715 : diag::ext_static_assert_no_message)
716 << (getLangOpts().CPlusPlus1z
717 ? FixItHint()
718 : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\""));
719 } else {
720 if (ExpectAndConsume(tok::comma)) {
721 SkipUntil(tok::semi);
722 return nullptr;
723 }
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000724
Richard Smith085a64f2014-06-20 19:57:12 +0000725 if (!isTokenStringLiteral()) {
726 Diag(Tok, diag::err_expected_string_literal)
727 << /*Source='static_assert'*/1;
728 SkipMalformedDecl();
729 return nullptr;
730 }
Mike Stump11289f42009-09-09 15:08:12 +0000731
Richard Smith085a64f2014-06-20 19:57:12 +0000732 AssertMessage = ParseStringLiteralExpression();
733 if (AssertMessage.isInvalid()) {
734 SkipMalformedDecl();
735 return nullptr;
736 }
Richard Smithd67aea22012-03-06 03:21:47 +0000737 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000738
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000739 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000740
Chris Lattner49836b42009-04-02 04:16:50 +0000741 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000742 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000743
John McCallb268a282010-08-23 23:25:46 +0000744 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000745 AssertExpr.get(),
746 AssertMessage.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000747 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000748}
749
Richard Smith74aeef52013-04-26 16:15:35 +0000750/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000751///
752/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000753/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000754///
David Blaikie15a430a2011-12-04 05:04:18 +0000755SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
756 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
757 && "Not a decltype specifier");
758
David Blaikie15a430a2011-12-04 05:04:18 +0000759 ExprResult Result;
760 SourceLocation StartLoc = Tok.getLocation();
761 SourceLocation EndLoc;
762
763 if (Tok.is(tok::annot_decltype)) {
764 Result = getExprAnnotation(Tok);
765 EndLoc = Tok.getAnnotationEndLoc();
766 ConsumeToken();
767 if (Result.isInvalid()) {
768 DS.SetTypeSpecError();
769 return EndLoc;
770 }
771 } else {
Richard Smith324df552012-02-24 22:30:04 +0000772 if (Tok.getIdentifierInfo()->isStr("decltype"))
773 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000774
David Blaikie15a430a2011-12-04 05:04:18 +0000775 ConsumeToken();
776
777 BalancedDelimiterTracker T(*this, tok::l_paren);
778 if (T.expectAndConsume(diag::err_expected_lparen_after,
779 "decltype", tok::r_paren)) {
780 DS.SetTypeSpecError();
781 return T.getOpenLocation() == Tok.getLocation() ?
782 StartLoc : T.getOpenLocation();
783 }
784
Richard Smith74aeef52013-04-26 16:15:35 +0000785 // Check for C++1y 'decltype(auto)'.
786 if (Tok.is(tok::kw_auto)) {
787 // No need to disambiguate here: an expression can't start with 'auto',
788 // because the typename-specifier in a function-style cast operation can't
789 // be 'auto'.
790 Diag(Tok.getLocation(),
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000791 getLangOpts().CPlusPlus14
Richard Smith74aeef52013-04-26 16:15:35 +0000792 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
793 : diag::ext_decltype_auto_type_specifier);
794 ConsumeToken();
795 } else {
796 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000797
Richard Smith74aeef52013-04-26 16:15:35 +0000798 // C++11 [dcl.type.simple]p4:
799 // The operand of the decltype specifier is an unevaluated operand.
800 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
Craig Topper161e4db2014-05-21 06:02:52 +0000801 nullptr,/*IsDecltype=*/true);
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000802 Result = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Richard Smith74aeef52013-04-26 16:15:35 +0000803 if (Result.isInvalid()) {
804 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000805 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smith74aeef52013-04-26 16:15:35 +0000806 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000807 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000808 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
809 // Backtrack to get the location of the last token before the semi.
810 PP.RevertCachedTokens(2);
811 ConsumeToken(); // the semi.
812 EndLoc = ConsumeAnyToken();
813 assert(Tok.is(tok::semi));
814 } else {
815 EndLoc = Tok.getLocation();
816 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000817 }
Richard Smith74aeef52013-04-26 16:15:35 +0000818 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000819 }
Richard Smith74aeef52013-04-26 16:15:35 +0000820
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000821 Result = Actions.ActOnDecltypeExpression(Result.get());
David Blaikie15a430a2011-12-04 05:04:18 +0000822 }
823
824 // Match the ')'
825 T.consumeClose();
826 if (T.getCloseLocation().isInvalid()) {
827 DS.SetTypeSpecError();
828 // FIXME: this should return the location of the last token
829 // that was consumed (by "consumeClose()")
830 return T.getCloseLocation();
831 }
832
Richard Smithfd555f62012-02-22 02:04:18 +0000833 if (Result.isInvalid()) {
834 DS.SetTypeSpecError();
835 return T.getCloseLocation();
836 }
837
David Blaikie15a430a2011-12-04 05:04:18 +0000838 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000839 }
Richard Smith74aeef52013-04-26 16:15:35 +0000840 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000841
Craig Topper161e4db2014-05-21 06:02:52 +0000842 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +0000843 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000844 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Anders Carlsson74948d02009-06-24 17:47:40 +0000845 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000846 if (Result.get()
847 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000848 DiagID, Result.get(), Policy)
Richard Smith74aeef52013-04-26 16:15:35 +0000849 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000850 DiagID, Policy)) {
John McCall49bfce42009-08-03 20:12:06 +0000851 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000852 DS.SetTypeSpecError();
853 }
854 return EndLoc;
855}
856
857void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
858 SourceLocation StartLoc,
859 SourceLocation EndLoc) {
860 // make sure we have a token we can turn into an annotation token
861 if (PP.isBacktrackEnabled())
862 PP.RevertCachedTokens(1);
863 else
864 PP.EnterToken(Tok);
865
866 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000867 setExprAnnotation(Tok,
868 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
869 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
870 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000871 Tok.setAnnotationEndLoc(EndLoc);
872 Tok.setLocation(StartLoc);
873 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000874}
875
Alexis Hunt4a257072011-05-19 05:37:45 +0000876void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
877 assert(Tok.is(tok::kw___underlying_type) &&
878 "Not an underlying type specifier");
879
880 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000881 BalancedDelimiterTracker T(*this, tok::l_paren);
882 if (T.expectAndConsume(diag::err_expected_lparen_after,
883 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +0000884 return;
885 }
886
887 TypeResult Result = ParseTypeName();
888 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000889 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt4a257072011-05-19 05:37:45 +0000890 return;
891 }
892
893 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000894 T.consumeClose();
895 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000896 return;
897
Craig Topper161e4db2014-05-21 06:02:52 +0000898 const char *PrevSpec = nullptr;
Alexis Hunt4a257072011-05-19 05:37:45 +0000899 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000900 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000901 DiagID, Result.get(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000902 Actions.getASTContext().getPrintingPolicy()))
Alexis Hunt4a257072011-05-19 05:37:45 +0000903 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanellaa90af722013-07-06 18:54:58 +0000904 DS.setTypeofParensRange(T.getRange());
Alexis Hunt4a257072011-05-19 05:37:45 +0000905}
906
David Blaikie00ee7a082011-10-25 15:01:20 +0000907/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
908/// class name or decltype-specifier. Note that we only check that the result
909/// names a type; semantic analysis will need to verify that the type names a
910/// class. The result is either a type or null, depending on whether a type
911/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +0000912///
Richard Smith4c96e992013-02-19 23:47:15 +0000913/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000914/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +0000915/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000916/// nested-name-specifier[opt] class-name
917/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +0000918/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +0000919/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000920/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000921///
Richard Smith4c96e992013-02-19 23:47:15 +0000922/// In C++98, instead of base-type-specifier, we have:
923///
924/// ::[opt] nested-name-specifier[opt] class-name
Craig Topper9ad7e262014-10-31 06:57:07 +0000925TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
926 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +0000927 // Ignore attempts to use typename
928 if (Tok.is(tok::kw_typename)) {
929 Diag(Tok, diag::err_expected_class_name_not_template)
930 << FixItHint::CreateRemoval(Tok.getLocation());
931 ConsumeToken();
932 }
933
David Blaikieafa155f2011-10-25 18:17:58 +0000934 // Parse optional nested-name-specifier
935 CXXScopeSpec SS;
936 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
937
938 BaseLoc = Tok.getLocation();
939
David Blaikie1cd50022011-10-25 17:10:12 +0000940 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +0000941 // tok == kw_decltype is just error recovery, it can only happen when SS
942 // isn't empty
943 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +0000944 if (SS.isNotEmpty())
945 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
946 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +0000947 // Fake up a Declarator to use with ActOnTypeName.
948 DeclSpec DS(AttrFactory);
949
David Blaikie7491e732011-12-08 04:53:15 +0000950 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +0000951
952 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
953 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
954 }
955
Douglas Gregord54dfb82009-02-25 23:52:28 +0000956 // Check whether we have a template-id that names a type.
957 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000958 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +0000959 if (TemplateId->Kind == TNK_Type_template ||
960 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000961 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000962
963 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000964 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000965 EndLocation = Tok.getAnnotationEndLoc();
966 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000967
968 if (Type)
969 return Type;
970 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000971 }
972
973 // Fall through to produce an error below.
974 }
975
Douglas Gregor831c93f2008-11-05 20:51:48 +0000976 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000977 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000978 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000979 }
980
Douglas Gregor18473f32010-01-12 21:28:44 +0000981 IdentifierInfo *Id = Tok.getIdentifierInfo();
982 SourceLocation IdLoc = ConsumeToken();
983
984 if (Tok.is(tok::less)) {
985 // It looks the user intended to write a template-id here, but the
986 // template-name was wrong. Try to fix that.
987 TemplateNameKind TNK = TNK_Type_template;
988 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000989 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000990 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000991 Diag(IdLoc, diag::err_unknown_template_name)
992 << Id;
993 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000994
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000995 if (!Template) {
996 TemplateArgList TemplateArgs;
997 SourceLocation LAngleLoc, RAngleLoc;
998 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
999 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor18473f32010-01-12 21:28:44 +00001000 return true;
Serge Pavlovb716b3c2013-08-10 05:54:47 +00001001 }
Douglas Gregor18473f32010-01-12 21:28:44 +00001002
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001003 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +00001004 UnqualifiedId TemplateName;
1005 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001006
Douglas Gregor18473f32010-01-12 21:28:44 +00001007 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001008 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1009 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +00001010 return true;
1011 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +00001012 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001013
Douglas Gregor18473f32010-01-12 21:28:44 +00001014 // If we didn't end up with a typename token, there's nothing more we
1015 // can do.
1016 if (Tok.isNot(tok::annot_typename))
1017 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001018
Douglas Gregor18473f32010-01-12 21:28:44 +00001019 // Retrieve the type from the annotation token, consume that token, and
1020 // return.
1021 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +00001022 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +00001023 ConsumeToken();
1024 return Type;
1025 }
1026
Douglas Gregor831c93f2008-11-05 20:51:48 +00001027 // We have an identifier; check whether it is actually a type.
Craig Topper161e4db2014-05-21 06:02:52 +00001028 IdentifierInfo *CorrectedII = nullptr;
Douglas Gregore7c20652011-03-02 00:47:37 +00001029 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +00001030 false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00001031 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +00001032 /*NonTrivialTypeSourceInfo=*/true,
1033 &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001034 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001035 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001036 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +00001037 }
1038
1039 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +00001040 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001041
1042 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +00001043 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +00001044 DS.SetRangeStart(IdLoc);
1045 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +00001046 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001047
Craig Topper161e4db2014-05-21 06:02:52 +00001048 const char *PrevSpec = nullptr;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001049 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001050 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
1051 Actions.getASTContext().getPrintingPolicy());
Nick Lewycky19b9f952010-07-26 16:56:01 +00001052
1053 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1054 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001055}
1056
John McCall8d32c052012-05-22 21:28:12 +00001057void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
1058 while (Tok.is(tok::kw___single_inheritance) ||
1059 Tok.is(tok::kw___multiple_inheritance) ||
1060 Tok.is(tok::kw___virtual_inheritance)) {
1061 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1062 SourceLocation AttrNameLoc = ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001063 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Aaron Ballman8edb5c22013-12-18 23:44:18 +00001064 AttributeList::AS_Keyword);
John McCall8d32c052012-05-22 21:28:12 +00001065 }
1066}
1067
Richard Smith369b9f92012-06-25 21:37:02 +00001068/// Determine whether the following tokens are valid after a type-specifier
1069/// which could be a standalone declaration. This will conservatively return
1070/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +00001071bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +00001072 // This switch enumerates the valid "follow" set for type-specifiers.
1073 switch (Tok.getKind()) {
1074 default: break;
1075 case tok::semi: // struct foo {...} ;
1076 case tok::star: // struct foo {...} * P;
1077 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +00001078 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +00001079 case tok::identifier: // struct foo {...} V ;
1080 case tok::r_paren: //(struct foo {...} ) {4}
1081 case tok::annot_cxxscope: // struct foo {...} a:: b;
1082 case tok::annot_typename: // struct foo {...} a ::b;
1083 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1084 case tok::l_paren: // struct foo {...} ( x);
1085 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001086 case tok::kw_operator: // struct foo operator ++() {...}
Alp Tokerd3f79c52013-11-24 20:24:54 +00001087 case tok::kw___declspec: // struct foo {...} __declspec(...)
Richard Smith843f18f2014-08-13 02:13:15 +00001088 case tok::l_square: // void f(struct f [ 3])
1089 case tok::ellipsis: // void f(struct f ... [Ns])
Abramo Bagnara152eb392014-08-16 08:29:27 +00001090 // FIXME: we should emit semantic diagnostic when declaration
1091 // attribute is in type attribute position.
1092 case tok::kw___attribute: // struct foo __attribute__((used)) x;
Richard Smith369b9f92012-06-25 21:37:02 +00001093 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001094 case tok::colon:
1095 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smith369b9f92012-06-25 21:37:02 +00001096 // Type qualifiers
1097 case tok::kw_const: // struct foo {...} const x;
1098 case tok::kw_volatile: // struct foo {...} volatile x;
1099 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith843f18f2014-08-13 02:13:15 +00001100 case tok::kw__Atomic: // struct foo {...} _Atomic x;
Nico Rieck3e1ee832014-12-04 23:30:25 +00001101 case tok::kw___unaligned: // struct foo {...} __unaligned *x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001102 // Function specifiers
1103 // Note, no 'explicit'. An explicit function must be either a conversion
1104 // operator or a constructor. Either way, it can't have a return type.
1105 case tok::kw_inline: // struct foo inline f();
1106 case tok::kw_virtual: // struct foo virtual f();
1107 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001108 // Storage-class specifiers
1109 case tok::kw_static: // struct foo {...} static x;
1110 case tok::kw_extern: // struct foo {...} extern x;
1111 case tok::kw_typedef: // struct foo {...} typedef x;
1112 case tok::kw_register: // struct foo {...} register x;
1113 case tok::kw_auto: // struct foo {...} auto x;
1114 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001115 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001116 case tok::kw_constexpr: // struct foo {...} constexpr x;
1117 // As shown above, type qualifiers and storage class specifiers absolutely
1118 // can occur after class specifiers according to the grammar. However,
1119 // almost no one actually writes code like this. If we see one of these,
1120 // it is much more likely that someone missed a semi colon and the
1121 // type/storage class specifier we're seeing is part of the *next*
1122 // intended declaration, as in:
1123 //
1124 // struct foo { ... }
1125 // typedef int X;
1126 //
1127 // We'd really like to emit a missing semicolon error instead of emitting
1128 // an error on the 'int' saying that you can't have two type specifiers in
1129 // the same declaration of X. Because of this, we look ahead past this
1130 // token to see if it's a type specifier. If so, we know the code is
1131 // otherwise invalid, so we can produce the expected semi error.
1132 if (!isKnownToBeTypeSpecifier(NextToken()))
1133 return true;
1134 break;
1135 case tok::r_brace: // struct bar { struct foo {...} }
1136 // Missing ';' at end of struct is accepted as an extension in C mode.
1137 if (!getLangOpts().CPlusPlus)
1138 return true;
1139 break;
Richard Smith52c5b872013-01-29 04:13:32 +00001140 case tok::greater:
1141 // template<class T = class X>
1142 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001143 }
1144 return false;
1145}
1146
Douglas Gregor556877c2008-04-13 21:30:24 +00001147/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1148/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1149/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001150/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001151///
1152/// class-specifier: [C++ class]
1153/// class-head '{' member-specification[opt] '}'
1154/// class-head '{' member-specification[opt] '}' attributes[opt]
1155/// class-head:
1156/// class-key identifier[opt] base-clause[opt]
1157/// class-key nested-name-specifier identifier base-clause[opt]
1158/// class-key nested-name-specifier[opt] simple-template-id
1159/// base-clause[opt]
1160/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001161/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001162/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001163/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001164/// simple-template-id base-clause[opt]
1165/// class-key:
1166/// 'class'
1167/// 'struct'
1168/// 'union'
1169///
1170/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001171/// class-key ::[opt] nested-name-specifier[opt] identifier
1172/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1173/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001174///
1175/// Note that the C++ class-specifier and elaborated-type-specifier,
1176/// together, subsume the C99 struct-or-union-specifier:
1177///
1178/// struct-or-union-specifier: [C99 6.7.2.1]
1179/// struct-or-union identifier[opt] '{' struct-contents '}'
1180/// struct-or-union identifier
1181/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1182/// '}' attributes[opt]
1183/// [GNU] struct-or-union attributes[opt] identifier
1184/// struct-or-union:
1185/// 'struct'
1186/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001187void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1188 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001189 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001190 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001191 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001192 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001193 DeclSpec::TST TagType;
1194 if (TagTokKind == tok::kw_struct)
1195 TagType = DeclSpec::TST_struct;
1196 else if (TagTokKind == tok::kw___interface)
1197 TagType = DeclSpec::TST_interface;
1198 else if (TagTokKind == tok::kw_class)
1199 TagType = DeclSpec::TST_class;
1200 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001201 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1202 TagType = DeclSpec::TST_union;
1203 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001204
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001205 if (Tok.is(tok::code_completion)) {
1206 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001207 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001208 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001209 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001210
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001211 // C++03 [temp.explicit] 14.7.2/8:
1212 // The usual access checking rules do not apply to names used to specify
1213 // explicit instantiations.
1214 //
1215 // As an extension we do not perform access checking on the names used to
1216 // specify explicit specializations either. This is important to allow
1217 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001218 //
1219 // Note that we don't suppress if this turns out to be an elaborated
1220 // type specifier.
1221 bool shouldDelayDiagsInTag =
1222 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1223 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1224 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001225
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001226 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001227 // If attributes exist after tag, parse them.
Richard Smith37a45dd2013-10-24 01:21:09 +00001228 MaybeParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +00001229
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001230 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +00001231 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +00001232 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001233
John McCall8d32c052012-05-22 21:28:12 +00001234 // Parse inheritance specifiers.
1235 if (Tok.is(tok::kw___single_inheritance) ||
1236 Tok.is(tok::kw___multiple_inheritance) ||
1237 Tok.is(tok::kw___virtual_inheritance))
Richard Smith37a45dd2013-10-24 01:21:09 +00001238 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCall8d32c052012-05-22 21:28:12 +00001239
Alexis Hunt96d5c762009-11-21 08:43:09 +00001240 // If C++0x attributes exist here, parse them.
1241 // FIXME: Are we consistent with the ordering of parsing of different
1242 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001243 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001244
Michael Han309af292013-01-07 16:57:11 +00001245 // Source location used by FIXIT to insert misplaced
1246 // C++11 attributes
1247 SourceLocation AttrFixitLoc = Tok.getLocation();
1248
Nico Weber7c3c5be2014-09-23 04:09:56 +00001249 if (TagType == DeclSpec::TST_struct &&
David Majnemer86330af2014-12-29 02:14:26 +00001250 Tok.isNot(tok::identifier) &&
1251 !Tok.isAnnotation() &&
Nico Weber7c3c5be2014-09-23 04:09:56 +00001252 Tok.getIdentifierInfo() &&
Nico Weberb10c9202014-09-24 03:28:54 +00001253 (Tok.is(tok::kw___is_abstract) ||
1254 Tok.is(tok::kw___is_arithmetic) ||
1255 Tok.is(tok::kw___is_array) ||
1256 Tok.is(tok::kw___is_base_of) ||
1257 Tok.is(tok::kw___is_class) ||
1258 Tok.is(tok::kw___is_complete_type) ||
1259 Tok.is(tok::kw___is_compound) ||
1260 Tok.is(tok::kw___is_const) ||
1261 Tok.is(tok::kw___is_constructible) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001262 Tok.is(tok::kw___is_convertible) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001263 Tok.is(tok::kw___is_convertible_to) ||
1264 Tok.is(tok::kw___is_destructible) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001265 Tok.is(tok::kw___is_empty) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001266 Tok.is(tok::kw___is_enum) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001267 Tok.is(tok::kw___is_floating_point) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001268 Tok.is(tok::kw___is_final) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001269 Tok.is(tok::kw___is_function) ||
1270 Tok.is(tok::kw___is_fundamental) ||
1271 Tok.is(tok::kw___is_integral) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001272 Tok.is(tok::kw___is_interface_class) ||
1273 Tok.is(tok::kw___is_literal) ||
1274 Tok.is(tok::kw___is_lvalue_expr) ||
1275 Tok.is(tok::kw___is_lvalue_reference) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001276 Tok.is(tok::kw___is_member_function_pointer) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001277 Tok.is(tok::kw___is_member_object_pointer) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001278 Tok.is(tok::kw___is_member_pointer) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001279 Tok.is(tok::kw___is_nothrow_assignable) ||
1280 Tok.is(tok::kw___is_nothrow_constructible) ||
1281 Tok.is(tok::kw___is_nothrow_destructible) ||
1282 Tok.is(tok::kw___is_object) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001283 Tok.is(tok::kw___is_pod) ||
1284 Tok.is(tok::kw___is_pointer) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001285 Tok.is(tok::kw___is_polymorphic) ||
1286 Tok.is(tok::kw___is_reference) ||
1287 Tok.is(tok::kw___is_rvalue_expr) ||
1288 Tok.is(tok::kw___is_rvalue_reference) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001289 Tok.is(tok::kw___is_same) ||
1290 Tok.is(tok::kw___is_scalar) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001291 Tok.is(tok::kw___is_sealed) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001292 Tok.is(tok::kw___is_signed) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001293 Tok.is(tok::kw___is_standard_layout) ||
1294 Tok.is(tok::kw___is_trivial) ||
1295 Tok.is(tok::kw___is_trivially_assignable) ||
1296 Tok.is(tok::kw___is_trivially_constructible) ||
1297 Tok.is(tok::kw___is_trivially_copyable) ||
1298 Tok.is(tok::kw___is_union) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001299 Tok.is(tok::kw___is_unsigned) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001300 Tok.is(tok::kw___is_void) ||
1301 Tok.is(tok::kw___is_volatile)))
Nico Weber7c3c5be2014-09-23 04:09:56 +00001302 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
1303 // name of struct templates, but some are keywords in GCC >= 4.3
1304 // and Clang. Therefore, when we see the token sequence "struct
1305 // X", make X into a normal identifier rather than a keyword, to
1306 // allow libstdc++ 4.2 and libc++ to work properly.
1307 TryKeywordIdentFallback(true);
Mike Stump11289f42009-09-09 15:08:12 +00001308
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001309 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001310 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001311 if (getLangOpts().CPlusPlus) {
Serge Pavlov458ea762014-07-16 05:16:52 +00001312 // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it
1313 // is a base-specifier-list.
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001314 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001315
Nico Webercfaa4cd2015-02-15 07:26:13 +00001316 CXXScopeSpec Spec;
1317 bool HasValidSpec = true;
1318 if (ParseOptionalCXXScopeSpecifier(Spec, ParsedType(), EnteringContext)) {
John McCall413021a2010-07-30 06:26:29 +00001319 DS.SetTypeSpecError();
Nico Webercfaa4cd2015-02-15 07:26:13 +00001320 HasValidSpec = false;
1321 }
1322 if (Spec.isSet())
1323 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) {
Alp Tokerec543272013-12-24 09:48:30 +00001324 Diag(Tok, diag::err_expected) << tok::identifier;
Nico Webercfaa4cd2015-02-15 07:26:13 +00001325 HasValidSpec = false;
1326 }
1327 if (HasValidSpec)
1328 SS = Spec;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001329 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001330
Douglas Gregor916462b2009-10-30 21:46:58 +00001331 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1332
Douglas Gregor67a65642009-02-17 23:15:12 +00001333 // Parse the (optional) class name or simple-template-id.
Craig Topper161e4db2014-05-21 06:02:52 +00001334 IdentifierInfo *Name = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001335 SourceLocation NameLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001336 TemplateIdAnnotation *TemplateId = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001337 if (Tok.is(tok::identifier)) {
1338 Name = Tok.getIdentifierInfo();
1339 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001340
David Blaikiebbafb8a2012-03-11 07:00:24 +00001341 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001342 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001343 // Eat the template argument list and try to continue parsing this as
1344 // a class (or template thereof).
1345 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001346 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +00001347 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +00001348 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001349 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001350 // We couldn't parse the template argument list at all, so don't
1351 // try to give any location information for the list.
1352 LAngleLoc = RAngleLoc = SourceLocation();
1353 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001354
Douglas Gregor916462b2009-10-30 21:46:58 +00001355 Diag(NameLoc, diag::err_explicit_spec_non_template)
Alp Toker01d65e12014-01-06 12:54:41 +00001356 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1357 << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
Joao Matose9a3ed42012-08-31 22:18:20 +00001358
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001359 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001360 // we've removed its template argument list.
1361 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1362 if (TemplateParams && TemplateParams->size() > 1) {
1363 TemplateParams->pop_back();
1364 } else {
Craig Topper161e4db2014-05-21 06:02:52 +00001365 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001366 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001367 = ParsedTemplateInfo::NonTemplate;
1368 }
1369 } else if (TemplateInfo.Kind
1370 == ParsedTemplateInfo::ExplicitInstantiation) {
1371 // Pretend this is just a forward declaration.
Craig Topper161e4db2014-05-21 06:02:52 +00001372 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001373 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001374 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001375 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001376 = SourceLocation();
1377 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1378 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001379 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001380 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001381 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001382 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001383 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001384
Douglas Gregore7c20652011-03-02 00:47:37 +00001385 if (TemplateId->Kind != TNK_Type_template &&
1386 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001387 // The template-name in the simple-template-id refers to
1388 // something other than a class template. Give an appropriate
1389 // error message and skip to the ';'.
1390 SourceRange Range(NameLoc);
1391 if (SS.isNotEmpty())
1392 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001393
Richard Smith72bfbd82013-12-04 00:28:23 +00001394 // FIXME: Name may be null here.
Douglas Gregor7f741122009-02-25 19:37:18 +00001395 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001396 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001397
Douglas Gregor7f741122009-02-25 19:37:18 +00001398 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001399 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor7f741122009-02-25 19:37:18 +00001400 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001401 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001402 }
1403
Richard Smithbfdb1082012-03-12 08:56:40 +00001404 // There are four options here.
1405 // - If we are in a trailing return type, this is always just a reference,
1406 // and we must not try to parse a definition. For instance,
1407 // [] () -> struct S { };
1408 // does not define a type.
1409 // - If we have 'struct foo {...', 'struct foo :...',
1410 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1411 // - If we have 'struct foo;', then this is either a forward declaration
1412 // or a friend declaration, which have to be treated differently.
1413 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001414 //
1415 // We also detect these erroneous cases to provide better diagnostic for
1416 // C++11 attributes parsing.
1417 // - attributes follow class name:
1418 // struct foo [[]] {};
1419 // - attributes appear before or after 'final':
1420 // struct foo [[]] final [[]] {};
1421 //
Richard Smithc5b05522012-03-12 07:56:15 +00001422 // However, in type-specifier-seq's, things look like declarations but are
1423 // just references, e.g.
1424 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001425 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001426 // &T::operator struct s;
Richard Smith649c7b062014-01-08 00:56:48 +00001427 // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
Michael Han9407e502012-11-26 22:54:45 +00001428
1429 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001430 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001431
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001432 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
John McCallfaf5fb42010-08-26 23:41:50 +00001433 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001434 if (DSC == DSC_trailing)
1435 TUK = Sema::TUK_Reference;
1436 else if (Tok.is(tok::l_brace) ||
1437 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001438 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001439 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001440 if (DS.isFriendSpecified()) {
1441 // C++ [class.friend]p2:
1442 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001443 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001444 << SourceRange(DS.getFriendSpecLoc());
1445
1446 // Skip everything up to the semicolon, so that this looks like a proper
1447 // friend class (or template thereof) declaration.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001448 SkipUntil(tok::semi, StopBeforeMatch);
John McCallfaf5fb42010-08-26 23:41:50 +00001449 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001450 } else {
1451 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001452 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001453 }
Richard Smith434516c2013-02-22 06:46:23 +00001454 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1455 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001456 // We can't tell if this is a definition or reference
1457 // until we skipped the 'final' and C++11 attribute specifiers.
1458 TentativeParsingAction PA(*this);
1459
1460 // Skip the 'final' keyword.
1461 ConsumeToken();
1462
1463 // Skip C++11 attribute specifiers.
1464 while (true) {
1465 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1466 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001467 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001468 break;
Richard Smith434516c2013-02-22 06:46:23 +00001469 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001470 ConsumeToken();
1471 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001472 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001473 break;
1474 } else {
1475 break;
1476 }
1477 }
1478
1479 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1480 TUK = Sema::TUK_Definition;
1481 else
1482 TUK = Sema::TUK_Reference;
1483
1484 PA.Revert();
Richard Smith649c7b062014-01-08 00:56:48 +00001485 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00001486 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001487 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001488 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001489 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001490 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Joao Matose9a3ed42012-08-31 22:18:20 +00001491 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00001492 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001493 DeclSpec::getSpecifierName(TagType, PPol));
Joao Matose9a3ed42012-08-31 22:18:20 +00001494 PP.EnterToken(Tok);
1495 Tok.setKind(tok::semi);
1496 }
Richard Smith369b9f92012-06-25 21:37:02 +00001497 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001498 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001499
Michael Han9407e502012-11-26 22:54:45 +00001500 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1501 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001502 if (TUK != Sema::TUK_Reference) {
1503 // If this is not a reference, then the only possible
1504 // valid place for C++11 attributes to appear here
1505 // is between class-key and class-name. If there are
1506 // any attributes after class-name, we try a fixit to move
1507 // them to the right place.
1508 SourceRange AttrRange = Attributes.Range;
1509 if (AttrRange.isValid()) {
1510 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1511 << AttrRange
1512 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1513 CharSourceRange(AttrRange, true))
1514 << FixItHint::CreateRemoval(AttrRange);
1515
1516 // Recover by adding misplaced attributes to the attribute list
1517 // of the class so they can be applied on the class later.
1518 attrs.takeAllFrom(Attributes);
1519 }
1520 }
Michael Han9407e502012-11-26 22:54:45 +00001521
John McCall6347b682012-05-07 06:16:58 +00001522 // If this is an elaborated type specifier, and we delayed
1523 // diagnostics before, just merge them into the current pool.
1524 if (shouldDelayDiagsInTag) {
1525 diagsFromTag.done();
1526 if (TUK == Sema::TUK_Reference)
1527 diagsFromTag.redelay();
1528 }
1529
John McCall413021a2010-07-30 06:26:29 +00001530 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001531 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001532 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1533 // We have a declaration or reference to an anonymous class.
1534 Diag(StartLoc, diag::err_anon_type_definition)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001535 << DeclSpec::getSpecifierName(TagType, Policy);
John McCall413021a2010-07-30 06:26:29 +00001536 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001537
David Majnemer3252fd02013-12-05 01:36:53 +00001538 // If we are parsing a definition and stop at a base-clause, continue on
1539 // until the semicolon. Continuing from the comma will just trick us into
1540 // thinking we are seeing a variable declaration.
1541 if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1542 SkipUntil(tok::semi, StopBeforeMatch);
1543 else
1544 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor556877c2008-04-13 21:30:24 +00001545 return;
1546 }
1547
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001548 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001549 DeclResult TagOrTempResult = true; // invalid
1550 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001551
Douglas Gregord6ab8742009-05-28 23:31:59 +00001552 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +00001553 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001554 // Explicit specialization, class template partial specialization,
1555 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001556 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001557 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001558 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001559 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001560 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001561 ProhibitAttributes(attrs);
1562
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001563 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001564 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001565 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001566 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001567 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001568 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001569 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001570 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001571 TemplateId->TemplateNameLoc,
1572 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001573 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001574 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001575 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001576
1577 // Friend template-ids are treated as references unless
1578 // they have template headers, in which case they're ill-formed
1579 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1580 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001581 } else if (TUK == Sema::TUK_Reference ||
1582 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001583 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001584 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001585 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001586 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001587 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001588 TemplateId->Template,
1589 TemplateId->TemplateNameLoc,
1590 TemplateId->LAngleLoc,
1591 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001592 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001593 } else {
1594 // This is an explicit specialization or a class template
1595 // partial specialization.
1596 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001597 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1598 // This looks like an explicit instantiation, because we have
1599 // something like
1600 //
1601 // template class Foo<X>
1602 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001603 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001604 // meant to be an explicit specialization, but the user forgot
1605 // the '<>' after 'template'.
Richard Smith003c5e12013-11-08 19:03:29 +00001606 // It this is friend declaration however, since it cannot have a
1607 // template header, it is most likely that the user meant to
1608 // remove the 'template' keyword.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001609 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith003c5e12013-11-08 19:03:29 +00001610 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001611
Richard Smith003c5e12013-11-08 19:03:29 +00001612 if (TUK == Sema::TUK_Friend) {
1613 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
Craig Topper161e4db2014-05-21 06:02:52 +00001614 TemplateParams = nullptr;
Richard Smith003c5e12013-11-08 19:03:29 +00001615 } else {
1616 SourceLocation LAngleLoc =
1617 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1618 Diag(TemplateId->TemplateNameLoc,
1619 diag::err_explicit_instantiation_with_definition)
1620 << SourceRange(TemplateInfo.TemplateLoc)
1621 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1622
1623 // Create a fake template parameter list that contains only
1624 // "template<>", so that we treat this construct as a class
1625 // template specialization.
1626 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
Craig Topper161e4db2014-05-21 06:02:52 +00001627 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, nullptr,
1628 0, LAngleLoc));
Richard Smith003c5e12013-11-08 19:03:29 +00001629 TemplateParams = &FakedParamLists;
1630 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001631 }
1632
1633 // Build the class template specialization.
Richard Smith4b55a9c2014-04-17 03:29:33 +00001634 TagOrTempResult = Actions.ActOnClassTemplateSpecialization(
1635 getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(),
1636 *TemplateId, attrs.getList(),
Craig Topper161e4db2014-05-21 06:02:52 +00001637 MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0]
1638 : nullptr,
Richard Smith4b55a9c2014-04-17 03:29:33 +00001639 TemplateParams ? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001640 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001641 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001642 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001643 // Explicit instantiation of a member of a class template
1644 // specialization, e.g.,
1645 //
1646 // template struct Outer<int>::Inner;
1647 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001648 ProhibitAttributes(attrs);
1649
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001650 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001651 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001652 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001653 TemplateInfo.TemplateLoc,
1654 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001655 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001656 } else if (TUK == Sema::TUK_Friend &&
1657 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001658 ProhibitAttributes(attrs);
1659
John McCallace48cd2010-10-19 01:40:49 +00001660 TagOrTempResult =
1661 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1662 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001663 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001664 MultiTemplateParamsArg(
Craig Topper161e4db2014-05-21 06:02:52 +00001665 TemplateParams? &(*TemplateParams)[0]
1666 : nullptr,
John McCallace48cd2010-10-19 01:40:49 +00001667 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001668 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001669 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1670 ProhibitAttributes(attrs);
Richard Smith003c5e12013-11-08 19:03:29 +00001671
Larisse Voufo725de3e2013-06-21 00:08:46 +00001672 if (TUK == Sema::TUK_Definition &&
1673 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1674 // If the declarator-id is not a template-id, issue a diagnostic and
1675 // recover by ignoring the 'template' keyword.
1676 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1677 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Craig Topper161e4db2014-05-21 06:02:52 +00001678 TemplateParams = nullptr;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001679 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001680
John McCall7f41d982009-09-11 04:59:25 +00001681 bool IsDependent = false;
1682
John McCall32723e92010-10-19 18:40:57 +00001683 // Don't pass down template parameter lists if this is just a tag
1684 // reference. For example, we don't need the template parameters here:
1685 // template <class T> class A *makeA(T t);
1686 MultiTemplateParamsArg TParams;
1687 if (TUK != Sema::TUK_Reference && TemplateParams)
1688 TParams =
1689 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1690
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001691 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001692 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001693 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001694 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001695 TParams, Owned, IsDependent,
1696 SourceLocation(), false,
Richard Smith649c7b062014-01-08 00:56:48 +00001697 clang::TypeResult(),
1698 DSC == DSC_type_specifier);
John McCall7f41d982009-09-11 04:59:25 +00001699
1700 // If ActOnTag said the type was dependent, try again with the
1701 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001702 if (IsDependent) {
1703 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001704 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001705 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001706 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001707 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001708
Douglas Gregor556877c2008-04-13 21:30:24 +00001709 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001710 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001711 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001712 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001713 isCXX11FinalKeyword());
David Blaikiebbafb8a2012-03-11 07:00:24 +00001714 if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001715 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1716 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001717 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001718 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001719 }
1720
Craig Topper161e4db2014-05-21 06:02:52 +00001721 const char *PrevSpec = nullptr;
John McCallba7bf592010-08-24 05:47:05 +00001722 unsigned DiagID;
1723 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001724 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001725 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1726 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001727 PrevSpec, DiagID, TypeResult.get(), Policy);
John McCall7f41d982009-09-11 04:59:25 +00001728 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001729 Result = DS.SetTypeSpecType(TagType, StartLoc,
1730 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001731 PrevSpec, DiagID, TagOrTempResult.get(), Owned,
1732 Policy);
John McCall7f41d982009-09-11 04:59:25 +00001733 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001734 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001735 return;
1736 }
Mike Stump11289f42009-09-09 15:08:12 +00001737
John McCallba7bf592010-08-24 05:47:05 +00001738 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001739 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001740
Chris Lattnercf251412010-02-02 01:23:29 +00001741 // At this point, we've successfully parsed a class-specifier in 'definition'
1742 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1743 // going to look at what comes after it to improve error recovery. If an
1744 // impossible token occurs next, we assume that the programmer forgot a ; at
1745 // the end of the declaration and recover that way.
1746 //
Richard Smith369b9f92012-06-25 21:37:02 +00001747 // Also enforce C++ [temp]p3:
1748 // In a template-declaration which defines a class, no declarator
1749 // is permitted.
Richard Smith843f18f2014-08-13 02:13:15 +00001750 //
1751 // After a type-specifier, we don't expect a semicolon. This only happens in
1752 // C, since definitions are not permitted in this context in C++.
Joao Matose9a3ed42012-08-31 22:18:20 +00001753 if (TUK == Sema::TUK_Definition &&
Richard Smith843f18f2014-08-13 02:13:15 +00001754 (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) &&
Joao Matose9a3ed42012-08-31 22:18:20 +00001755 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001756 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001757 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Alp Toker383d2c42014-01-01 03:08:43 +00001758 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001759 DeclSpec::getSpecifierName(TagType, PPol));
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001760 // Push this token back into the preprocessor and change our current token
1761 // to ';' so that the rest of the code recovers as though there were an
1762 // ';' after the definition.
1763 PP.EnterToken(Tok);
1764 Tok.setKind(tok::semi);
1765 }
Chris Lattnercf251412010-02-02 01:23:29 +00001766 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001767}
1768
Mike Stump11289f42009-09-09 15:08:12 +00001769/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001770///
1771/// base-clause : [C++ class.derived]
1772/// ':' base-specifier-list
1773/// base-specifier-list:
1774/// base-specifier '...'[opt]
1775/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001776void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001777 assert(Tok.is(tok::colon) && "Not a base clause");
1778 ConsumeToken();
1779
Douglas Gregor29a92472008-10-22 17:49:05 +00001780 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001781 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001782
Douglas Gregor556877c2008-04-13 21:30:24 +00001783 while (true) {
1784 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001785 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001786 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001787 // Skip the rest of this base specifier, up until the comma or
1788 // opening brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001789 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor29a92472008-10-22 17:49:05 +00001790 } else {
1791 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001792 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001793 }
1794
1795 // If the next token is a comma, consume it and keep reading
1796 // base-specifiers.
Alp Toker97650562014-01-10 11:19:30 +00001797 if (!TryConsumeToken(tok::comma))
1798 break;
Douglas Gregor556877c2008-04-13 21:30:24 +00001799 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001800
1801 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001802 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001803}
1804
1805/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1806/// one entry in the base class list of a class specifier, for example:
1807/// class foo : public bar, virtual private baz {
1808/// 'public bar' and 'virtual private baz' are each base-specifiers.
1809///
1810/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001811/// attribute-specifier-seq[opt] base-type-specifier
1812/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1813/// base-type-specifier
1814/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1815/// base-type-specifier
Craig Topper9ad7e262014-10-31 06:57:07 +00001816BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001817 bool IsVirtual = false;
1818 SourceLocation StartLoc = Tok.getLocation();
1819
Richard Smith4c96e992013-02-19 23:47:15 +00001820 ParsedAttributesWithRange Attributes(AttrFactory);
1821 MaybeParseCXX11Attributes(Attributes);
1822
Douglas Gregor556877c2008-04-13 21:30:24 +00001823 // Parse the 'virtual' keyword.
Alp Toker97650562014-01-10 11:19:30 +00001824 if (TryConsumeToken(tok::kw_virtual))
Douglas Gregor556877c2008-04-13 21:30:24 +00001825 IsVirtual = true;
Douglas Gregor556877c2008-04-13 21:30:24 +00001826
Richard Smith4c96e992013-02-19 23:47:15 +00001827 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1828
Douglas Gregor556877c2008-04-13 21:30:24 +00001829 // Parse an (optional) access specifier.
1830 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001831 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001832 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001833
Richard Smith4c96e992013-02-19 23:47:15 +00001834 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1835
Douglas Gregor556877c2008-04-13 21:30:24 +00001836 // Parse the 'virtual' keyword (again!), in case it came after the
1837 // access specifier.
1838 if (Tok.is(tok::kw_virtual)) {
1839 SourceLocation VirtualLoc = ConsumeToken();
1840 if (IsVirtual) {
1841 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001842 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001843 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001844 }
1845
1846 IsVirtual = true;
1847 }
1848
Richard Smith4c96e992013-02-19 23:47:15 +00001849 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1850
Douglas Gregor831c93f2008-11-05 20:51:48 +00001851 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001852 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001853 SourceLocation BaseLoc;
1854 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001855 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001856 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001857
Douglas Gregor752a5952011-01-03 22:36:02 +00001858 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1859 // actually part of the base-specifier-list grammar productions, but we
1860 // parse it here for convenience.
1861 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00001862 TryConsumeToken(tok::ellipsis, EllipsisLoc);
1863
Mike Stump11289f42009-09-09 15:08:12 +00001864 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001865 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001866
Douglas Gregor556877c2008-04-13 21:30:24 +00001867 // Notify semantic analysis that we have parsed a complete
1868 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00001869 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1870 Access, BaseType.get(), BaseLoc,
1871 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001872}
1873
1874/// getAccessSpecifierIfPresent - Determine whether the next token is
1875/// a C++ access-specifier.
1876///
1877/// access-specifier: [C++ class.derived]
1878/// 'private'
1879/// 'protected'
1880/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001881AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001882 switch (Tok.getKind()) {
1883 default: return AS_none;
1884 case tok::kw_private: return AS_private;
1885 case tok::kw_protected: return AS_protected;
1886 case tok::kw_public: return AS_public;
1887 }
1888}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001889
Douglas Gregor433e0532012-04-16 18:27:27 +00001890/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith0b3a4622014-11-13 20:01:57 +00001891/// delayed, e.g., default arguments or an exception-specification, create a
1892/// late-parsed method declaration record to handle the parsing at the end of
1893/// the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00001894void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1895 Decl *ThisDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001896 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001897 = DeclaratorInfo.getFunctionTypeInfo();
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001898 // If there was a late-parsed exception-specification, we'll need a
1899 // late parse
1900 bool NeedLateParse = FTI.getExceptionSpecType() == EST_Unparsed;
Douglas Gregor433e0532012-04-16 18:27:27 +00001901
Nathan Sidwell5bb231c2015-02-19 14:03:22 +00001902 if (!NeedLateParse) {
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001903 // Look ahead to see if there are any default args
Nathan Sidwell5bb231c2015-02-19 14:03:22 +00001904 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
1905 auto Param = cast<ParmVarDecl>(FTI.Params[ParamIdx].Param);
1906 if (Param->hasUnparsedDefaultArg()) {
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001907 NeedLateParse = true;
1908 break;
1909 }
Nathan Sidwell5bb231c2015-02-19 14:03:22 +00001910 }
1911 }
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001912
1913 if (NeedLateParse) {
Richard Smith0b3a4622014-11-13 20:01:57 +00001914 // Push this method onto the stack of late-parsed method
1915 // declarations.
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001916 auto LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
Richard Smith0b3a4622014-11-13 20:01:57 +00001917 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
1918 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
1919
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001920 // Stash the exception-specification tokens in the late-pased method.
Richard Smith0b3a4622014-11-13 20:01:57 +00001921 LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens;
1922 FTI.ExceptionSpecTokens = 0;
1923
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001924 // Push tokens for each parameter. Those that do not have
1925 // defaults will be NULL.
Richard Smith0b3a4622014-11-13 20:01:57 +00001926 LateMethod->DefaultArgs.reserve(FTI.NumParams);
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001927 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx)
Alp Tokerc5350722014-02-26 22:27:52 +00001928 LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001929 FTI.Params[ParamIdx].Param, FTI.Params[ParamIdx].DefaultArgTokens));
Eli Friedman3af2a772009-07-22 21:45:50 +00001930 }
1931}
1932
Richard Smith89645bc2013-01-02 12:01:23 +00001933/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001934/// virt-specifier.
1935///
1936/// virt-specifier:
1937/// override
1938/// final
Richard Smith89645bc2013-01-02 12:01:23 +00001939VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001940 if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001941 return VirtSpecifiers::VS_None;
1942
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001943 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001944
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001945 // Initialize the contextual keywords.
1946 if (!Ident_final) {
1947 Ident_final = &PP.getIdentifierTable().get("final");
1948 if (getLangOpts().MicrosoftExt)
1949 Ident_sealed = &PP.getIdentifierTable().get("sealed");
1950 Ident_override = &PP.getIdentifierTable().get("override");
Anders Carlsson56104902011-01-17 03:05:47 +00001951 }
1952
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001953 if (II == Ident_override)
1954 return VirtSpecifiers::VS_Override;
1955
1956 if (II == Ident_sealed)
1957 return VirtSpecifiers::VS_Sealed;
1958
1959 if (II == Ident_final)
1960 return VirtSpecifiers::VS_Final;
1961
Anders Carlsson56104902011-01-17 03:05:47 +00001962 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001963}
1964
Richard Smith89645bc2013-01-02 12:01:23 +00001965/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001966///
1967/// virt-specifier-seq:
1968/// virt-specifier
1969/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00001970void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
Richard Smith3d1a94c2014-08-12 00:22:39 +00001971 bool IsInterface,
1972 SourceLocation FriendLoc) {
Anders Carlsson56104902011-01-17 03:05:47 +00001973 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00001974 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001975 if (Specifier == VirtSpecifiers::VS_None)
1976 return;
1977
Richard Smith3d1a94c2014-08-12 00:22:39 +00001978 if (FriendLoc.isValid()) {
1979 Diag(Tok.getLocation(), diag::err_friend_decl_spec)
1980 << VirtSpecifiers::getSpecifierName(Specifier)
1981 << FixItHint::CreateRemoval(Tok.getLocation())
1982 << SourceRange(FriendLoc, FriendLoc);
1983 ConsumeToken();
1984 continue;
1985 }
1986
Anders Carlsson56104902011-01-17 03:05:47 +00001987 // C++ [class.mem]p8:
1988 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Craig Topper161e4db2014-05-21 06:02:52 +00001989 const char *PrevSpec = nullptr;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001990 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001991 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1992 << PrevSpec
1993 << FixItHint::CreateRemoval(Tok.getLocation());
1994
David Majnemera5433082013-10-18 00:33:31 +00001995 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
1996 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalldb632ac2012-09-25 07:32:39 +00001997 Diag(Tok.getLocation(), diag::err_override_control_interface)
1998 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemera5433082013-10-18 00:33:31 +00001999 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
2000 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalldb632ac2012-09-25 07:32:39 +00002001 } else {
David Majnemera5433082013-10-18 00:33:31 +00002002 Diag(Tok.getLocation(),
2003 getLangOpts().CPlusPlus11
2004 ? diag::warn_cxx98_compat_override_control_keyword
2005 : diag::ext_override_control_keyword)
2006 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalldb632ac2012-09-25 07:32:39 +00002007 }
Anders Carlsson56104902011-01-17 03:05:47 +00002008 ConsumeToken();
2009 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002010}
2011
Richard Smith89645bc2013-01-02 12:01:23 +00002012/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002013/// 'final' or Microsoft 'sealed' contextual keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00002014bool Parser::isCXX11FinalKeyword() const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002015 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
2016 return Specifier == VirtSpecifiers::VS_Final ||
2017 Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002018}
2019
Richard Smith72553fc2014-01-23 23:53:27 +00002020/// \brief Parse a C++ member-declarator up to, but not including, the optional
2021/// brace-or-equal-initializer or pure-specifier.
Nico Weberd89e6f72015-01-16 19:34:13 +00002022bool Parser::ParseCXXMemberDeclaratorBeforeInitializer(
Richard Smith72553fc2014-01-23 23:53:27 +00002023 Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
2024 LateParsedAttrList &LateParsedAttrs) {
2025 // member-declarator:
2026 // declarator pure-specifier[opt]
2027 // declarator brace-or-equal-initializer[opt]
2028 // identifier[opt] ':' constant-expression
Serge Pavlov458ea762014-07-16 05:16:52 +00002029 if (Tok.isNot(tok::colon))
Richard Smith72553fc2014-01-23 23:53:27 +00002030 ParseDeclarator(DeclaratorInfo);
Richard Smith3d1a94c2014-08-12 00:22:39 +00002031 else
2032 DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation());
Richard Smith72553fc2014-01-23 23:53:27 +00002033
2034 if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002035 assert(DeclaratorInfo.isPastIdentifier() &&
2036 "don't know where identifier would go yet?");
Richard Smith72553fc2014-01-23 23:53:27 +00002037 BitfieldSize = ParseConstantExpression();
2038 if (BitfieldSize.isInvalid())
2039 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002040 } else {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002041 ParseOptionalCXX11VirtSpecifierSeq(
2042 VS, getCurrentClass().IsInterface,
2043 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002044 if (!VS.isUnset())
2045 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
2046 }
Richard Smith72553fc2014-01-23 23:53:27 +00002047
2048 // If a simple-asm-expr is present, parse it.
2049 if (Tok.is(tok::kw_asm)) {
2050 SourceLocation Loc;
2051 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
2052 if (AsmLabel.isInvalid())
2053 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2054
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002055 DeclaratorInfo.setAsmLabel(AsmLabel.get());
Richard Smith72553fc2014-01-23 23:53:27 +00002056 DeclaratorInfo.SetRangeEnd(Loc);
2057 }
2058
2059 // If attributes exist after the declarator, but before an '{', parse them.
2060 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Richard Smith4b5a9492014-01-24 22:34:35 +00002061
2062 // For compatibility with code written to older Clang, also accept a
2063 // virt-specifier *after* the GNU attributes.
Aaron Ballman5d153e32014-08-04 17:03:51 +00002064 if (BitfieldSize.isUnset() && VS.isUnset()) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002065 ParseOptionalCXX11VirtSpecifierSeq(
2066 VS, getCurrentClass().IsInterface,
2067 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Aaron Ballman5d153e32014-08-04 17:03:51 +00002068 if (!VS.isUnset()) {
2069 // If we saw any GNU-style attributes that are known to GCC followed by a
2070 // virt-specifier, issue a GCC-compat warning.
2071 const AttributeList *Attr = DeclaratorInfo.getAttributes();
2072 while (Attr) {
2073 if (Attr->isKnownToGCC() && !Attr->isCXX11Attribute())
2074 Diag(Attr->getLoc(), diag::warn_gcc_attribute_location);
2075 Attr = Attr->getNext();
2076 }
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002077 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
Aaron Ballman5d153e32014-08-04 17:03:51 +00002078 }
2079 }
Nico Weberd89e6f72015-01-16 19:34:13 +00002080
2081 // If this has neither a name nor a bit width, something has gone seriously
2082 // wrong. Skip until the semi-colon or }.
2083 if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
2084 // If so, skip until the semi-colon or a }.
2085 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2086 return true;
2087 }
2088 return false;
Richard Smith72553fc2014-01-23 23:53:27 +00002089}
2090
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002091/// \brief Look for declaration specifiers possibly occurring after C++11
2092/// virt-specifier-seq and diagnose them.
2093void Parser::MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(
2094 Declarator &D,
2095 VirtSpecifiers &VS) {
2096 DeclSpec DS(AttrFactory);
2097
2098 // GNU-style and C++11 attributes are not allowed here, but they will be
2099 // handled by the caller. Diagnose everything else.
2100 ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed, false);
2101 D.ExtendWithDeclSpec(DS);
2102
2103 if (D.isFunctionDeclarator()) {
2104 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2105 auto DeclSpecCheck = [&] (DeclSpec::TQ TypeQual,
2106 const char *FixItName,
2107 SourceLocation SpecLoc,
2108 unsigned* QualifierLoc) {
2109 FixItHint Insertion;
2110 auto &Function = D.getFunctionTypeInfo();
2111 if (DS.getTypeQualifiers() & TypeQual) {
2112 if (!(Function.TypeQuals & TypeQual)) {
2113 std::string Name(FixItName);
2114 Name += " ";
2115 Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name.c_str());
2116 Function.TypeQuals |= TypeQual;
2117 *QualifierLoc = SpecLoc.getRawEncoding();
2118 }
2119 Diag(SpecLoc, diag::err_declspec_after_virtspec)
2120 << FixItName
2121 << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
2122 << FixItHint::CreateRemoval(SpecLoc)
2123 << Insertion;
2124 }
2125 };
2126 auto &Function = D.getFunctionTypeInfo();
2127 DeclSpecCheck(DeclSpec::TQ_const, "const", DS.getConstSpecLoc(),
2128 &Function.ConstQualifierLoc);
2129 DeclSpecCheck(DeclSpec::TQ_volatile, "volatile", DS.getVolatileSpecLoc(),
2130 &Function.VolatileQualifierLoc);
2131 DeclSpecCheck(DeclSpec::TQ_restrict, "restrict", DS.getRestrictSpecLoc(),
2132 &Function.RestrictQualifierLoc);
2133 }
2134 }
2135}
2136
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002137/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
2138///
2139/// member-declaration:
2140/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
2141/// function-definition ';'[opt]
2142/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
2143/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00002144/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002145/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002146/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002147///
2148/// member-declarator-list:
2149/// member-declarator
2150/// member-declarator-list ',' member-declarator
2151///
2152/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002153/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002154/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00002155/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002156/// identifier[opt] ':' constant-expression
2157///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002158/// virt-specifier-seq:
2159/// virt-specifier
2160/// virt-specifier-seq virt-specifier
2161///
2162/// virt-specifier:
2163/// override
2164/// final
David Majnemera5433082013-10-18 00:33:31 +00002165/// [MS] sealed
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002166///
Sebastian Redl42e92c42009-04-12 17:16:29 +00002167/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002168/// '= 0'
2169///
2170/// constant-initializer:
2171/// '=' constant-expression
2172///
Douglas Gregor3447e762009-08-20 22:52:58 +00002173void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002174 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00002175 const ParsedTemplateInfo &TemplateInfo,
2176 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00002177 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002178 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00002179 Diag(Tok, diag::err_at_defs_cxx);
2180 else
2181 Diag(Tok, diag::err_at_in_class);
Richard Smithda35e962013-11-09 04:52:51 +00002182
Douglas Gregor23c84762011-04-14 17:21:19 +00002183 ConsumeToken();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002184 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor23c84762011-04-14 17:21:19 +00002185 return;
2186 }
Richard Smithda35e962013-11-09 04:52:51 +00002187
Serge Pavlov458ea762014-07-16 05:16:52 +00002188 // Turn on colon protection early, while parsing declspec, although there is
2189 // nothing to protect there. It prevents from false errors if error recovery
2190 // incorrectly determines where the declspec ends, as in the example:
2191 // struct A { enum class B { C }; };
2192 // const int C = 4;
2193 // struct D { A::B : C; };
2194 ColonProtectionRAIIObject X(*this);
2195
John McCalla0097262009-12-11 02:10:03 +00002196 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00002197 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00002198 if (!TemplateInfo.Kind &&
Nikola Smiljanic67860242014-09-26 00:28:20 +00002199 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2200 Tok.is(tok::kw___super))) {
Richard Smith45855df2012-05-09 08:23:23 +00002201 if (TryAnnotateCXXScopeToken())
2202 MalformedTypeSpec = true;
2203
2204 bool isAccessDecl;
2205 if (Tok.isNot(tok::annot_cxxscope))
2206 isAccessDecl = false;
2207 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00002208 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
2209 else
2210 isAccessDecl = NextToken().is(tok::kw_operator);
2211
2212 if (isAccessDecl) {
2213 // Collect the scope specifier token we annotated earlier.
2214 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002215 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2216 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00002217
Nico Weberef03e702014-09-10 00:59:37 +00002218 if (SS.isInvalid()) {
2219 SkipUntil(tok::semi);
2220 return;
2221 }
2222
John McCalla0097262009-12-11 02:10:03 +00002223 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00002224 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00002225 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +00002226 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
2227 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00002228 SkipUntil(tok::semi);
2229 return;
2230 }
2231
2232 // TODO: recover from mistakenly-qualified operator declarations.
Alp Toker383d2c42014-01-01 03:08:43 +00002233 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2234 "access declaration")) {
2235 SkipUntil(tok::semi);
John McCalla0097262009-12-11 02:10:03 +00002236 return;
Alp Toker383d2c42014-01-01 03:08:43 +00002237 }
John McCalla0097262009-12-11 02:10:03 +00002238
Douglas Gregor0be31a22010-07-02 17:43:08 +00002239 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002240 /* HasUsingKeyword */ false,
2241 SourceLocation(),
John McCalla0097262009-12-11 02:10:03 +00002242 SS, Name,
Craig Topper161e4db2014-05-21 06:02:52 +00002243 /* AttrList */ nullptr,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002244 /* HasTypenameKeyword */ false,
John McCalla0097262009-12-11 02:10:03 +00002245 SourceLocation());
2246 return;
2247 }
2248 }
2249
Aaron Ballmane7c544d2014-08-04 20:28:35 +00002250 // static_assert-declaration. A templated static_assert declaration is
2251 // diagnosed in Parser::ParseSingleDeclarationAfterTemplate.
2252 if (!TemplateInfo.Kind &&
2253 (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert))) {
Chris Lattner49836b42009-04-02 04:16:50 +00002254 SourceLocation DeclEnd;
2255 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002256 return;
2257 }
Mike Stump11289f42009-09-09 15:08:12 +00002258
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002259 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00002260 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00002261 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00002262 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00002263 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002264 AS, AccessAttrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002265 return;
2266 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002267
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002268 // Handle: member-declaration ::= '__extension__' member-declaration
2269 if (Tok.is(tok::kw___extension__)) {
2270 // __extension__ silences extension warnings in the subexpression.
2271 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2272 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002273 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2274 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002275 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002276
John McCall084e83d2011-03-24 11:26:52 +00002277 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002278 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002279 // Optional C++11 attribute-specifier
2280 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002281 // We need to keep these attributes for future diagnostic
2282 // before they are taken over by declaration specifier.
2283 FnAttrs.addAll(attrs.getList());
2284 FnAttrs.Range = attrs.Range;
2285
John McCall53fa7142010-12-24 02:08:15 +00002286 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002287
Douglas Gregorfec52632009-06-20 00:51:54 +00002288 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002289 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002290
Douglas Gregorfec52632009-06-20 00:51:54 +00002291 // Eat 'using'.
2292 SourceLocation UsingLoc = ConsumeToken();
2293
2294 if (Tok.is(tok::kw_namespace)) {
2295 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002296 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002297 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00002298 SourceLocation DeclEnd;
Richard Smith3f1b5d02011-05-05 21:57:07 +00002299 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +00002300 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2301 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00002302 }
2303 return;
2304 }
2305
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002306 // Hold late-parsed attributes so we can attach a Decl to them later.
2307 LateParsedAttrList CommonLateParsedAttrs;
2308
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002309 // decl-specifier-seq:
2310 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002311 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002312 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002313 if (MalformedTypeSpec)
2314 DS.SetTypeSpecError();
Richard Smith72553fc2014-01-23 23:53:27 +00002315
Serge Pavlov458ea762014-07-16 05:16:52 +00002316 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2317 &CommonLateParsedAttrs);
2318
2319 // Turn off colon protection that was set for declspec.
2320 X.restore();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002321
Richard Smith404dfb42013-11-19 22:47:36 +00002322 // If we had a free-standing type definition with a missing semicolon, we
2323 // may get this far before the problem becomes obvious.
2324 if (DS.hasTagDefinition() &&
2325 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2326 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2327 &CommonLateParsedAttrs))
2328 return;
2329
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002330 MultiTemplateParamsArg TemplateParams(
Craig Topper161e4db2014-05-21 06:02:52 +00002331 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data()
2332 : nullptr,
John McCall11083da2009-09-16 22:47:08 +00002333 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2334
Alp Toker35d87032013-12-30 23:29:50 +00002335 if (TryConsumeToken(tok::semi)) {
Michael Handdc016d2012-11-28 23:17:40 +00002336 if (DS.isFriendSpecified())
2337 ProhibitAttributes(FnAttrs);
2338
John McCall48871652010-08-21 09:40:31 +00002339 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00002340 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00002341 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00002342 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002343 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002344
John McCall28a6aea2009-11-04 02:18:39 +00002345 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002346 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002347
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002348 // Hold late-parsed attributes so we can attach a Decl to them later.
2349 LateParsedAttrList LateParsedAttrs;
2350
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002351 SourceLocation EqualLoc;
2352 bool HasInitializer = false;
2353 ExprResult Init;
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002354
Richard Smith72553fc2014-01-23 23:53:27 +00002355 SmallVector<Decl *, 8> DeclsInGroup;
2356 ExprResult BitfieldSize;
2357 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002358
Richard Smith72553fc2014-01-23 23:53:27 +00002359 // Parse the first declarator.
Nico Weberd89e6f72015-01-16 19:34:13 +00002360 if (ParseCXXMemberDeclaratorBeforeInitializer(
2361 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) {
Richard Smith72553fc2014-01-23 23:53:27 +00002362 TryConsumeToken(tok::semi);
2363 return;
2364 }
John Thompson5bc5cbe2009-11-25 22:58:06 +00002365
Richard Smith72553fc2014-01-23 23:53:27 +00002366 // Check for a member function definition.
Richard Smith4b5a9492014-01-24 22:34:35 +00002367 if (BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002368 // MSVC permits pure specifier on inline functions defined at class scope.
Francois Pichet3abc9b82011-05-11 02:14:46 +00002369 // Hence check for =0 before checking for function definition.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002370 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Richard Smith72553fc2014-01-23 23:53:27 +00002371 DeclaratorInfo.isFunctionDeclarator() &&
Francois Pichet3abc9b82011-05-11 02:14:46 +00002372 NextToken().is(tok::numeric_constant)) {
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002373 EqualLoc = ConsumeToken();
Francois Pichet3abc9b82011-05-11 02:14:46 +00002374 Init = ParseInitializer();
2375 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002376 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002377 else
2378 HasInitializer = true;
Francois Pichet3abc9b82011-05-11 02:14:46 +00002379 }
2380
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002381 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002382 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002383 //
2384 // In C++11, a non-function declarator followed by an open brace is a
2385 // braced-init-list for an in-class member initialization, not an
2386 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002387 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002388 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002389 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002390 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002391 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002392 } else if (Tok.is(tok::equal)) {
2393 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002394 if (KW.is(tok::kw_default))
2395 DefinitionKind = FDK_Defaulted;
2396 else if (KW.is(tok::kw_delete))
2397 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002398 }
2399 }
Eli Bendersky41842222015-03-23 23:49:41 +00002400 DeclaratorInfo.setFunctionDefinitionKind(DefinitionKind);
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002401
Michael Handdc016d2012-11-28 23:17:40 +00002402 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2403 // to a friend declaration, that declaration shall be a definition.
2404 if (DeclaratorInfo.isFunctionDeclarator() &&
2405 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2406 // Diagnose attributes that appear before decl specifier:
2407 // [[]] friend int foo();
2408 ProhibitAttributes(FnAttrs);
2409 }
2410
Nico Webera7f137d2015-01-16 19:35:01 +00002411 if (DefinitionKind != FDK_Declaration) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002412 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002413 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002414 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002415 SkipUntil(tok::r_brace);
Michael Handdc016d2012-11-28 23:17:40 +00002416
Douglas Gregor8a4db832011-01-19 16:41:58 +00002417 // Consume the optional ';'
Alp Toker35d87032013-12-30 23:29:50 +00002418 TryConsumeToken(tok::semi);
2419
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002420 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002421 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002422
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002423 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002424 Diag(DeclaratorInfo.getIdentifierLoc(),
2425 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002426
Richard Smith2603b092012-11-15 22:54:20 +00002427 // Recover by treating the 'typedef' as spurious.
2428 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002429 }
2430
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002431 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002432 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Eli Bendersky41842222015-03-23 23:49:41 +00002433 VS, Init);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002434
David Majnemer23252a32013-08-01 04:22:55 +00002435 if (FunDecl) {
2436 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2437 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2438 }
2439 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2440 LateParsedAttrs[i]->addDecl(FunDecl);
2441 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002442 }
2443 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002444
2445 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002446 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002447 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002448
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002449 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002450 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002451 }
2452
2453 // member-declarator-list:
2454 // member-declarator
2455 // member-declarator-list ',' member-declarator
2456
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002457 while (1) {
Richard Smith2b013182012-06-10 03:12:00 +00002458 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002459 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith938f40b2011-06-11 17:19:42 +00002460 if (BitfieldSize.get()) {
2461 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002462 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith938f40b2011-06-11 17:19:42 +00002463 } else {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002464 HasInitializer = true;
Richard Smith2b013182012-06-10 03:12:00 +00002465 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2466 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smith2b013182012-06-10 03:12:00 +00002467 != DeclSpec::SCS_typedef)
2468 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002469 }
2470 }
2471
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002472 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002473 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002474 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002475
Craig Topper161e4db2014-05-21 06:02:52 +00002476 NamedDecl *ThisDecl = nullptr;
John McCall07e91c02009-08-06 02:15:43 +00002477 if (DS.isFriendSpecified()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002478 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
Michael Handdc016d2012-11-28 23:17:40 +00002479 // to a friend declaration, that declaration shall be a definition.
2480 //
Richard Smith72553fc2014-01-23 23:53:27 +00002481 // Diagnose attributes that appear in a friend member function declarator:
2482 // friend int foo [[]] ();
Michael Handdc016d2012-11-28 23:17:40 +00002483 SmallVector<SourceRange, 4> Ranges;
2484 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
Richard Smith72553fc2014-01-23 23:53:27 +00002485 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
2486 E = Ranges.end(); I != E; ++I)
2487 Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
Michael Handdc016d2012-11-28 23:17:40 +00002488
Douglas Gregor0be31a22010-07-02 17:43:08 +00002489 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002490 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002491 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002492 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002493 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002494 TemplateParams,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002495 BitfieldSize.get(),
Richard Smith2b013182012-06-10 03:12:00 +00002496 VS, HasInClassInit);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002497
2498 if (VarTemplateDecl *VT =
Craig Topper161e4db2014-05-21 06:02:52 +00002499 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr)
Larisse Voufo39a1e502013-08-06 01:03:05 +00002500 // Re-direct this decl to refer to the templated decl so that we can
2501 // initialize it.
2502 ThisDecl = VT->getTemplatedDecl();
2503
David Majnemer23252a32013-08-01 04:22:55 +00002504 if (ThisDecl && AccessAttrs)
Richard Smithf8a75c32013-08-29 00:47:48 +00002505 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00002506 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002507
Douglas Gregor728d00b2011-10-10 14:49:18 +00002508 // Handle the initializer.
David Blaikie35506f82013-01-30 01:22:18 +00002509 if (HasInClassInit != ICIS_NoInit &&
2510 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2511 DeclSpec::SCS_static) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002512 // The initializer was deferred; parse it and cache the tokens.
David Majnemer23252a32013-08-01 04:22:55 +00002513 Diag(Tok, getLangOpts().CPlusPlus11
2514 ? diag::warn_cxx98_compat_nonstatic_member_init
2515 : diag::ext_nonstatic_member_init);
Richard Smith5d164bc2011-10-15 05:09:34 +00002516
Richard Smith938f40b2011-06-11 17:19:42 +00002517 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002518 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2519 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002520 //
2521 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002522 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002523 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002524 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemer23252a32013-08-01 04:22:55 +00002525
2526 // Avoid later warnings about a class member of incomplete type.
David Blaikiecdd91db2012-02-14 09:00:46 +00002527 if (ThisDecl)
David Blaikiecdd91db2012-02-14 09:00:46 +00002528 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002529 } else
2530 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002531 } else if (HasInitializer) {
2532 // Normal initializer.
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002533 if (!Init.isUsable())
David Majnemer23252a32013-08-01 04:22:55 +00002534 Init = ParseCXXMemberInitializer(
2535 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2536
Douglas Gregor728d00b2011-10-10 14:49:18 +00002537 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002538 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002539 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002540 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smith74aeef52013-04-26 16:15:35 +00002541 DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002542 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor728d00b2011-10-10 14:49:18 +00002543 // No initializer.
Richard Smith74aeef52013-04-26 16:15:35 +00002544 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002545
Douglas Gregor728d00b2011-10-10 14:49:18 +00002546 if (ThisDecl) {
David Majnemer23252a32013-08-01 04:22:55 +00002547 if (!ThisDecl->isInvalidDecl()) {
2548 // Set the Decl for any late parsed attributes
2549 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2550 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2551
2552 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2553 LateParsedAttrs[i]->addDecl(ThisDecl);
2554 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002555 Actions.FinalizeDeclaration(ThisDecl);
2556 DeclsInGroup.push_back(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002557
2558 if (DeclaratorInfo.isFunctionDeclarator() &&
2559 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2560 DeclSpec::SCS_typedef)
2561 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002562 }
David Majnemer23252a32013-08-01 04:22:55 +00002563 LateParsedAttrs.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002564
2565 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002566
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002567 // If we don't have a comma, it is either the end of the list (a ';')
2568 // or an error, bail out.
Alp Toker094e5212014-01-05 03:27:11 +00002569 SourceLocation CommaLoc;
2570 if (!TryConsumeToken(tok::comma, CommaLoc))
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002571 break;
Mike Stump11289f42009-09-09 15:08:12 +00002572
Richard Smithc8a79032012-01-09 22:31:44 +00002573 if (Tok.isAtStartOfLine() &&
2574 !MightBeDeclarator(Declarator::MemberContext)) {
2575 // This comma was followed by a line-break and something which can't be
2576 // the start of a declarator. The comma was probably a typo for a
2577 // semicolon.
2578 Diag(CommaLoc, diag::err_expected_semi_declaration)
2579 << FixItHint::CreateReplacement(CommaLoc, ";");
2580 ExpectSemi = false;
2581 break;
2582 }
Mike Stump11289f42009-09-09 15:08:12 +00002583
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002584 // Parse the next declarator.
2585 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002586 VS.clear();
Nico Weberf56c85b2015-01-17 02:26:40 +00002587 BitfieldSize = ExprResult(/*Invalid=*/false);
2588 Init = ExprResult(/*Invalid=*/false);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002589 HasInitializer = false;
Richard Smith8d06f422012-01-12 23:53:29 +00002590 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002591
Richard Smith72553fc2014-01-23 23:53:27 +00002592 // GNU attributes are allowed before the second and subsequent declarator.
John McCall53fa7142010-12-24 02:08:15 +00002593 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002594
Nico Weberd89e6f72015-01-16 19:34:13 +00002595 if (ParseCXXMemberDeclaratorBeforeInitializer(
2596 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs))
2597 break;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002598 }
2599
Richard Smithc8a79032012-01-09 22:31:44 +00002600 if (ExpectSemi &&
2601 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002602 // Skip to end of block or statement.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002603 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002604 // If we stopped at a ';', eat it.
Alp Toker35d87032013-12-30 23:29:50 +00002605 TryConsumeToken(tok::semi);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002606 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002607 }
2608
Rafael Espindolaab417692013-07-09 12:05:01 +00002609 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002610}
2611
Richard Smith938f40b2011-06-11 17:19:42 +00002612/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2613/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2614/// function definition. The location of the '=', if any, will be placed in
2615/// EqualLoc.
2616///
2617/// pure-specifier:
2618/// '= 0'
Sebastian Redleef474c2012-02-22 10:50:08 +00002619///
Richard Smith938f40b2011-06-11 17:19:42 +00002620/// brace-or-equal-initializer:
2621/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002622/// braced-init-list
2623///
Richard Smith938f40b2011-06-11 17:19:42 +00002624/// initializer-clause:
2625/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002626/// braced-init-list
2627///
Richard Smithda35e962013-11-09 04:52:51 +00002628/// defaulted/deleted function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002629/// '=' 'default'
2630/// '=' 'delete'
2631///
2632/// Prior to C++0x, the assignment-expression in an initializer-clause must
2633/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002634ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002635 SourceLocation &EqualLoc) {
2636 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2637 && "Data member initializer not starting with '=' or '{'");
2638
Douglas Gregor926410d2012-02-21 02:22:07 +00002639 EnterExpressionEvaluationContext Context(Actions,
2640 Sema::PotentiallyEvaluated,
2641 D);
Alp Toker094e5212014-01-05 03:27:11 +00002642 if (TryConsumeToken(tok::equal, EqualLoc)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002643 if (Tok.is(tok::kw_delete)) {
2644 // In principle, an initializer of '= delete p;' is legal, but it will
2645 // never type-check. It's better to diagnose it as an ill-formed expression
2646 // than as an ill-formed deleted non-function member.
2647 // An initializer of '= delete p, foo' will never be parsed, because
2648 // a top-level comma always ends the initializer expression.
2649 const Token &Next = NextToken();
2650 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
Richard Smith34f30512013-11-23 04:06:09 +00002651 Next.is(tok::eof)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002652 if (IsFunction)
2653 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2654 << 1 /* delete */;
2655 else
2656 Diag(ConsumeToken(), diag::err_deleted_non_function);
Richard Smithedcb26e2014-06-11 00:49:52 +00002657 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002658 }
2659 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002660 if (IsFunction)
2661 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2662 << 0 /* default */;
2663 else
2664 Diag(ConsumeToken(), diag::err_default_special_members);
Richard Smithedcb26e2014-06-11 00:49:52 +00002665 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002666 }
David Majnemer87ff66c2014-12-13 11:34:16 +00002667 }
2668 if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) {
2669 Diag(Tok, diag::err_ms_property_initializer) << PD;
2670 return ExprError();
Sebastian Redleef474c2012-02-22 10:50:08 +00002671 }
2672 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002673}
2674
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002675/// ParseCXXMemberSpecification - Parse the class definition.
2676///
2677/// member-specification:
2678/// member-declaration member-specification[opt]
2679/// access-specifier ':' member-specification[opt]
2680///
Joao Matose9a3ed42012-08-31 22:18:20 +00002681void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002682 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00002683 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002684 unsigned TagType, Decl *TagDecl) {
2685 assert((TagType == DeclSpec::TST_struct ||
2686 TagType == DeclSpec::TST_interface ||
2687 TagType == DeclSpec::TST_union ||
2688 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2689
John McCallfaf5fb42010-08-26 23:41:50 +00002690 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2691 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00002692
Douglas Gregoredf8f392010-01-16 20:52:59 +00002693 // Determine whether this is a non-nested class. Note that local
2694 // classes are *not* considered to be nested classes.
2695 bool NonNestedClass = true;
2696 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002697 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002698 if (S->isClassScope()) {
2699 // We're inside a class scope, so this is a nested class.
2700 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00002701
2702 // The Microsoft extension __interface does not permit nested classes.
2703 if (getCurrentClass().IsInterface) {
2704 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2705 << /*ErrorType=*/6
2706 << (isa<NamedDecl>(TagDecl)
2707 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
David Blaikieabe1a392014-04-02 05:58:29 +00002708 : "(anonymous)");
John McCalldb632ac2012-09-25 07:32:39 +00002709 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00002710 break;
2711 }
2712
2713 if ((S->getFlags() & Scope::FnScope)) {
2714 // If we're in a function or function template declared in the
2715 // body of a class, then this is a local class rather than a
2716 // nested class.
2717 const Scope *Parent = S->getParent();
2718 if (Parent->isTemplateParamScope())
2719 Parent = Parent->getParent();
2720 if (Parent->isClassScope())
2721 break;
2722 }
2723 }
2724 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002725
2726 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00002727 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002728
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002729 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00002730 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2731 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002732
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002733 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002734 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002735
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002736 SourceLocation FinalLoc;
David Majnemera5433082013-10-18 00:33:31 +00002737 bool IsFinalSpelledSealed = false;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002738
2739 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002740 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemera5433082013-10-18 00:33:31 +00002741 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2742 assert((Specifier == VirtSpecifiers::VS_Final ||
2743 Specifier == VirtSpecifiers::VS_Sealed) &&
2744 "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00002745 FinalLoc = ConsumeToken();
David Majnemera5433082013-10-18 00:33:31 +00002746 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002747
David Majnemera5433082013-10-18 00:33:31 +00002748 if (TagType == DeclSpec::TST_interface)
John McCalldb632ac2012-09-25 07:32:39 +00002749 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemera5433082013-10-18 00:33:31 +00002750 << VirtSpecifiers::getSpecifierName(Specifier);
2751 else if (Specifier == VirtSpecifiers::VS_Final)
2752 Diag(FinalLoc, getLangOpts().CPlusPlus11
2753 ? diag::warn_cxx98_compat_override_control_keyword
2754 : diag::ext_override_control_keyword)
2755 << VirtSpecifiers::getSpecifierName(Specifier);
2756 else if (Specifier == VirtSpecifiers::VS_Sealed)
2757 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han9407e502012-11-26 22:54:45 +00002758
Michael Han309af292013-01-07 16:57:11 +00002759 // Parse any C++11 attributes after 'final' keyword.
2760 // These attributes are not allowed to appear here,
2761 // and the only possible place for them to appertain
2762 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00002763 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Nico Weber4b4be842014-12-29 06:56:50 +00002764
2765 // ParseClassSpecifier() does only a superficial check for attributes before
2766 // deciding to call this method. For example, for
2767 // `class C final alignas ([l) {` it will decide that this looks like a
2768 // misplaced attribute since it sees `alignas '(' ')'`. But the actual
2769 // attribute parsing code will try to parse the '[' as a constexpr lambda
2770 // and consume enough tokens that the alignas parsing code will eat the
2771 // opening '{'. So bail out if the next token isn't one we expect.
Nico Weber36de3a22014-12-29 21:56:22 +00002772 if (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)) {
2773 if (TagDecl)
2774 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
Nico Weber4b4be842014-12-29 06:56:50 +00002775 return;
Nico Weber36de3a22014-12-29 21:56:22 +00002776 }
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002777 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002778
John McCall2d814c32009-12-19 21:48:58 +00002779 if (Tok.is(tok::colon)) {
2780 ParseBaseClause(TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002781 if (!Tok.is(tok::l_brace)) {
Ismail Pazarbasi129c44c2014-09-25 21:13:02 +00002782 bool SuggestFixIt = false;
2783 SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation);
2784 if (Tok.isAtStartOfLine()) {
2785 switch (Tok.getKind()) {
2786 case tok::kw_private:
2787 case tok::kw_protected:
2788 case tok::kw_public:
2789 SuggestFixIt = NextToken().getKind() == tok::colon;
2790 break;
2791 case tok::kw_static_assert:
2792 case tok::r_brace:
2793 case tok::kw_using:
2794 // base-clause can have simple-template-id; 'template' can't be there
2795 case tok::kw_template:
2796 SuggestFixIt = true;
2797 break;
2798 case tok::identifier:
2799 SuggestFixIt = isConstructorDeclarator(true);
2800 break;
2801 default:
2802 SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
2803 break;
2804 }
2805 }
2806 DiagnosticBuilder LBraceDiag =
2807 Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers);
2808 if (SuggestFixIt) {
2809 LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {");
2810 // Try recovering from missing { after base-clause.
2811 PP.EnterToken(Tok);
2812 Tok.setKind(tok::l_brace);
2813 } else {
2814 if (TagDecl)
2815 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
2816 return;
2817 }
John McCall2d814c32009-12-19 21:48:58 +00002818 }
2819 }
2820
2821 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002822 BalancedDelimiterTracker T(*this, tok::l_brace);
2823 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00002824
John McCall08bede42010-05-28 08:11:17 +00002825 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00002826 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemera5433082013-10-18 00:33:31 +00002827 IsFinalSpelledSealed,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002828 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00002829
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002830 // C++ 11p3: Members of a class defined with the keyword class are private
2831 // by default. Members of a class defined with the keywords struct or union
2832 // are public by default.
2833 AccessSpecifier CurAS;
2834 if (TagType == DeclSpec::TST_class)
2835 CurAS = AS_private;
2836 else
2837 CurAS = AS_public;
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002838 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002839
Douglas Gregor9377c822010-06-21 22:31:09 +00002840 if (TagDecl) {
2841 // While we still have something to read, read the member-declarations.
Richard Smith34f30512013-11-23 04:06:09 +00002842 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Douglas Gregor9377c822010-06-21 22:31:09 +00002843 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002844
David Blaikiebbafb8a2012-03-11 07:00:24 +00002845 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet8f981d52011-05-25 10:19:49 +00002846 Tok.is(tok::kw___if_not_exists))) {
2847 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2848 continue;
2849 }
2850
Douglas Gregor9377c822010-06-21 22:31:09 +00002851 // Check for extraneous top-level semicolon.
2852 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002853 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor9377c822010-06-21 22:31:09 +00002854 continue;
2855 }
2856
Eli Friedmanec52f922012-02-23 23:47:16 +00002857 if (Tok.is(tok::annot_pragma_vis)) {
2858 HandlePragmaVisibility();
2859 continue;
2860 }
2861
2862 if (Tok.is(tok::annot_pragma_pack)) {
2863 HandlePragmaPack();
2864 continue;
2865 }
2866
Argyrios Kyrtzidis5c2021b2012-10-12 17:39:59 +00002867 if (Tok.is(tok::annot_pragma_align)) {
2868 HandlePragmaAlign();
2869 continue;
2870 }
2871
Alexey Bataeva769e072013-03-22 06:34:35 +00002872 if (Tok.is(tok::annot_pragma_openmp)) {
2873 ParseOpenMPDeclarativeDirective();
2874 continue;
2875 }
2876
David Majnemer4bb09802014-02-10 19:50:15 +00002877 if (Tok.is(tok::annot_pragma_ms_pointers_to_members)) {
2878 HandlePragmaMSPointersToMembers();
2879 continue;
2880 }
2881
Warren Huntc3b18962014-04-08 22:30:47 +00002882 if (Tok.is(tok::annot_pragma_ms_pragma)) {
2883 HandlePragmaMSPragma();
2884 continue;
2885 }
2886
Richard Smithda35e962013-11-09 04:52:51 +00002887 // If we see a namespace here, a close brace was missing somewhere.
2888 if (Tok.is(tok::kw_namespace)) {
Richard Smith2ac43ad2013-11-15 23:00:02 +00002889 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
Richard Smithda35e962013-11-09 04:52:51 +00002890 break;
2891 }
2892
Douglas Gregor9377c822010-06-21 22:31:09 +00002893 AccessSpecifier AS = getAccessSpecifierIfPresent();
2894 if (AS != AS_none) {
2895 // Current token is a C++ access specifier.
2896 CurAS = AS;
2897 SourceLocation ASLoc = Tok.getLocation();
David Blaikieeba32c22011-10-13 06:08:43 +00002898 unsigned TokLength = Tok.getLength();
Douglas Gregor9377c822010-06-21 22:31:09 +00002899 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002900 AccessAttrs.clear();
2901 MaybeParseGNUAttributes(AccessAttrs);
2902
David Blaikieeba32c22011-10-13 06:08:43 +00002903 SourceLocation EndLoc;
Alp Toker35d87032013-12-30 23:29:50 +00002904 if (TryConsumeToken(tok::colon, EndLoc)) {
2905 } else if (TryConsumeToken(tok::semi, EndLoc)) {
2906 Diag(EndLoc, diag::err_expected)
2907 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002908 } else {
2909 EndLoc = ASLoc.getLocWithOffset(TokLength);
Alp Toker35d87032013-12-30 23:29:50 +00002910 Diag(EndLoc, diag::err_expected)
2911 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002912 }
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002913
John McCalldb632ac2012-09-25 07:32:39 +00002914 // The Microsoft extension __interface does not permit non-public
2915 // access specifiers.
2916 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2917 Diag(ASLoc, diag::err_access_specifier_interface)
2918 << (CurAS == AS_protected);
2919 }
2920
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002921 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2922 AccessAttrs.getList())) {
2923 // found another attribute than only annotations
2924 AccessAttrs.clear();
2925 }
2926
Douglas Gregor9377c822010-06-21 22:31:09 +00002927 continue;
2928 }
2929
Douglas Gregor9377c822010-06-21 22:31:09 +00002930 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002931 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002932 }
2933
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002934 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00002935 } else {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002936 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002937 }
Mike Stump11289f42009-09-09 15:08:12 +00002938
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002939 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002940 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002941 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002942
John McCall08bede42010-05-28 08:11:17 +00002943 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002944 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002945 T.getOpenLocation(),
2946 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002947 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002948
Douglas Gregor433e0532012-04-16 18:27:27 +00002949 // C++11 [class.mem]p2:
2950 // Within the class member-specification, the class is regarded as complete
Richard Smith0b3a4622014-11-13 20:01:57 +00002951 // within function bodies, default arguments, exception-specifications, and
Douglas Gregor433e0532012-04-16 18:27:27 +00002952 // brace-or-equal-initializers for non-static data members (including such
2953 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00002954 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002955 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00002956 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002957 // declarations and the lexed inline method definitions, along with any
2958 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00002959 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002960 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002961 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00002962
2963 // We've finished with all pending member declarations.
2964 Actions.ActOnFinishCXXMemberDecls();
2965
Richard Smith938f40b2011-06-11 17:19:42 +00002966 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002967 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00002968 PrevTokLocation = SavedPrevTokLocation;
Reid Klecknerbba3cb92015-03-17 19:00:50 +00002969
2970 // We've finished parsing everything, including default argument
2971 // initializers.
Reid Kleckner93f661a2015-03-17 21:51:43 +00002972 Actions.ActOnFinishCXXMemberDefaultArgs(TagDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002973 }
2974
John McCall08bede42010-05-28 08:11:17 +00002975 if (TagDecl)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002976 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2977 T.getCloseLocation());
John McCall2ff380a2010-03-17 00:38:33 +00002978
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002979 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002980 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002981 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002982}
Douglas Gregore8381c02008-11-05 04:29:56 +00002983
Richard Smith2ac43ad2013-11-15 23:00:02 +00002984void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithda35e962013-11-09 04:52:51 +00002985 assert(Tok.is(tok::kw_namespace));
2986
2987 // FIXME: Suggest where the close brace should have gone by looking
2988 // at indentation changes within the definition body.
Richard Smith2ac43ad2013-11-15 23:00:02 +00002989 Diag(D->getLocation(),
2990 diag::err_missing_end_of_definition) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002991 Diag(Tok.getLocation(),
Richard Smith2ac43ad2013-11-15 23:00:02 +00002992 diag::note_missing_end_of_definition_before) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002993
2994 // Push '};' onto the token stream to recover.
2995 PP.EnterToken(Tok);
2996
2997 Tok.startToken();
2998 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
2999 Tok.setKind(tok::semi);
3000 PP.EnterToken(Tok);
3001
3002 Tok.setKind(tok::r_brace);
3003}
3004
Douglas Gregore8381c02008-11-05 04:29:56 +00003005/// ParseConstructorInitializer - Parse a C++ constructor initializer,
3006/// which explicitly initializes the members or base classes of a
3007/// class (C++ [class.base.init]). For example, the three initializers
3008/// after the ':' in the Derived constructor below:
3009///
3010/// @code
3011/// class Base { };
3012/// class Derived : Base {
3013/// int x;
3014/// float f;
3015/// public:
3016/// Derived(float f) : Base(), x(17), f(f) { }
3017/// };
3018/// @endcode
3019///
Mike Stump11289f42009-09-09 15:08:12 +00003020/// [C++] ctor-initializer:
3021/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00003022///
Mike Stump11289f42009-09-09 15:08:12 +00003023/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00003024/// mem-initializer ...[opt]
3025/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00003026void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Nico Weber3b00fdc2015-03-07 19:52:39 +00003027 assert(Tok.is(tok::colon) &&
3028 "Constructor initializer always starts with ':'");
Douglas Gregore8381c02008-11-05 04:29:56 +00003029
Nico Weber3b00fdc2015-03-07 19:52:39 +00003030 // Poison the SEH identifiers so they are flagged as illegal in constructor
3031 // initializers.
John Wiegley1c0675e2011-04-28 01:08:34 +00003032 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00003033 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003034
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003035 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00003036 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003037
Douglas Gregore8381c02008-11-05 04:29:56 +00003038 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00003039 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00003040 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
3041 MemInitializers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003042 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00003043 } else {
3044 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
3045 if (!MemInit.isInvalid())
3046 MemInitializers.push_back(MemInit.get());
3047 else
3048 AnyErrors = true;
3049 }
3050
Douglas Gregore8381c02008-11-05 04:29:56 +00003051 if (Tok.is(tok::comma))
3052 ConsumeToken();
3053 else if (Tok.is(tok::l_brace))
3054 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00003055 // If the next token looks like a base or member initializer, assume that
3056 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00003057 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
3058 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3059 Diag(Loc, diag::err_ctor_init_missing_comma)
3060 << FixItHint::CreateInsertion(Loc, ", ");
3061 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00003062 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alp Tokerec543272013-12-24 09:48:30 +00003063 Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
3064 << tok::comma;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003065 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregore8381c02008-11-05 04:29:56 +00003066 break;
3067 }
3068 } while (true);
3069
David Blaikie3fc2f912013-01-17 05:26:25 +00003070 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00003071 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00003072}
3073
3074/// ParseMemInitializer - Parse a C++ member initializer, which is
3075/// part of a constructor initializer that explicitly initializes one
3076/// member or base class (C++ [class.base.init]). See
3077/// ParseConstructorInitializer for an example.
3078///
3079/// [C++] mem-initializer:
3080/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00003081/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00003082///
Douglas Gregore8381c02008-11-05 04:29:56 +00003083/// [C++] mem-initializer-id:
3084/// '::'[opt] nested-name-specifier[opt] class-name
3085/// identifier
Craig Topper9ad7e262014-10-31 06:57:07 +00003086MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00003087 // parse '::'[opt] nested-name-specifier[opt]
3088 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00003089 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00003090 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003091 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00003092 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00003093 if (TemplateId->Kind == TNK_Type_template ||
3094 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00003095 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003096 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00003097 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003098 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003099 }
David Blaikie186a8892012-01-24 06:03:59 +00003100 // Uses of decltype will already have been converted to annot_decltype by
3101 // ParseOptionalCXXScopeSpecifier at this point.
3102 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
3103 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00003104 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00003105 return true;
3106 }
Mike Stump11289f42009-09-09 15:08:12 +00003107
Craig Topper161e4db2014-05-21 06:02:52 +00003108 IdentifierInfo *II = nullptr;
David Blaikie186a8892012-01-24 06:03:59 +00003109 DeclSpec DS(AttrFactory);
3110 SourceLocation IdLoc = Tok.getLocation();
3111 if (Tok.is(tok::annot_decltype)) {
3112 // Get the decltype expression, if there is one.
3113 ParseDecltypeSpecifier(DS);
3114 } else {
3115 if (Tok.is(tok::identifier))
3116 // Get the identifier. This may be a member name or a class name,
3117 // but we'll let the semantic analysis determine which it is.
3118 II = Tok.getIdentifierInfo();
3119 ConsumeToken();
3120 }
3121
Douglas Gregore8381c02008-11-05 04:29:56 +00003122
3123 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003124 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003125 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3126
Sebastian Redla74948d2011-09-24 17:48:25 +00003127 ExprResult InitList = ParseBraceInitializer();
3128 if (InitList.isInvalid())
3129 return true;
3130
3131 SourceLocation EllipsisLoc;
Alp Toker094e5212014-01-05 03:27:11 +00003132 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redla74948d2011-09-24 17:48:25 +00003133
3134 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00003135 TemplateTypeTy, DS, IdLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003136 InitList.get(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00003137 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003138 BalancedDelimiterTracker T(*this, tok::l_paren);
3139 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00003140
Sebastian Redl3da34892011-06-05 12:23:16 +00003141 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00003142 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00003143 CommaLocsTy CommaLocs;
3144 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003145 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00003146 return true;
3147 }
3148
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003149 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00003150
3151 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00003152 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00003153
3154 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00003155 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00003156 T.getOpenLocation(), ArgExprs,
3157 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00003158 }
3159
Alp Tokerec543272013-12-24 09:48:30 +00003160 if (getLangOpts().CPlusPlus11)
3161 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
3162 else
3163 return Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregore8381c02008-11-05 04:29:56 +00003164}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003165
Sebastian Redl965b0e32011-03-05 14:45:16 +00003166/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003167///
Douglas Gregor356513d2008-12-01 18:00:20 +00003168/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00003169/// dynamic-exception-specification
3170/// noexcept-specification
3171///
3172/// noexcept-specification:
3173/// 'noexcept'
3174/// 'noexcept' '(' constant-expression ')'
3175ExceptionSpecificationType
Richard Smith0b3a4622014-11-13 20:01:57 +00003176Parser::tryParseExceptionSpecification(bool Delayed,
Douglas Gregor433e0532012-04-16 18:27:27 +00003177 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003178 SmallVectorImpl<ParsedType> &DynamicExceptions,
3179 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith0b3a4622014-11-13 20:01:57 +00003180 ExprResult &NoexceptExpr,
3181 CachedTokens *&ExceptionSpecTokens) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003182 ExceptionSpecificationType Result = EST_None;
Richard Smith0b3a4622014-11-13 20:01:57 +00003183 ExceptionSpecTokens = 0;
3184
3185 // Handle delayed parsing of exception-specifications.
3186 if (Delayed) {
3187 if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept))
3188 return EST_None;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003189
Richard Smith0b3a4622014-11-13 20:01:57 +00003190 // Consume and cache the starting token.
3191 bool IsNoexcept = Tok.is(tok::kw_noexcept);
3192 Token StartTok = Tok;
3193 SpecificationRange = SourceRange(ConsumeToken());
3194
3195 // Check for a '('.
3196 if (!Tok.is(tok::l_paren)) {
3197 // If this is a bare 'noexcept', we're done.
3198 if (IsNoexcept) {
3199 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3200 NoexceptExpr = 0;
3201 return EST_BasicNoexcept;
3202 }
3203
3204 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3205 return EST_DynamicNone;
3206 }
3207
3208 // Cache the tokens for the exception-specification.
3209 ExceptionSpecTokens = new CachedTokens;
3210 ExceptionSpecTokens->push_back(StartTok); // 'throw' or 'noexcept'
3211 ExceptionSpecTokens->push_back(Tok); // '('
3212 SpecificationRange.setEnd(ConsumeParen()); // '('
Richard Smithb1c217e2015-01-13 02:24:58 +00003213
3214 ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens,
3215 /*StopAtSemi=*/true,
3216 /*ConsumeFinalToken=*/true);
Richard Smith0b3a4622014-11-13 20:01:57 +00003217 SpecificationRange.setEnd(Tok.getLocation());
Richard Smith0b3a4622014-11-13 20:01:57 +00003218 return EST_Unparsed;
3219 }
3220
Sebastian Redl965b0e32011-03-05 14:45:16 +00003221 // See if there's a dynamic specification.
3222 if (Tok.is(tok::kw_throw)) {
3223 Result = ParseDynamicExceptionSpecification(SpecificationRange,
3224 DynamicExceptions,
3225 DynamicExceptionRanges);
3226 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
3227 "Produced different number of exception types and ranges.");
3228 }
3229
3230 // If there's no noexcept specification, we're done.
3231 if (Tok.isNot(tok::kw_noexcept))
3232 return Result;
3233
Richard Smithb15c11c2011-10-17 23:06:20 +00003234 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3235
Sebastian Redl965b0e32011-03-05 14:45:16 +00003236 // If we already had a dynamic specification, parse the noexcept for,
3237 // recovery, but emit a diagnostic and don't store the results.
3238 SourceRange NoexceptRange;
3239 ExceptionSpecificationType NoexceptType = EST_None;
3240
3241 SourceLocation KeywordLoc = ConsumeToken();
3242 if (Tok.is(tok::l_paren)) {
3243 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003244 BalancedDelimiterTracker T(*this, tok::l_paren);
3245 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003246 NoexceptType = EST_ComputedNoexcept;
3247 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003248 // The argument must be contextually convertible to bool. We use
3249 // ActOnBooleanCondition for this purpose.
3250 if (!NoexceptExpr.isInvalid())
3251 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
3252 NoexceptExpr.get());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003253 T.consumeClose();
3254 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl965b0e32011-03-05 14:45:16 +00003255 } else {
3256 // There is no argument.
3257 NoexceptType = EST_BasicNoexcept;
3258 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
3259 }
3260
3261 if (Result == EST_None) {
3262 SpecificationRange = NoexceptRange;
3263 Result = NoexceptType;
3264
3265 // If there's a dynamic specification after a noexcept specification,
3266 // parse that and ignore the results.
3267 if (Tok.is(tok::kw_throw)) {
3268 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3269 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
3270 DynamicExceptionRanges);
3271 }
3272 } else {
3273 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3274 }
3275
3276 return Result;
3277}
3278
Richard Smith8ca78a12013-06-13 02:02:51 +00003279static void diagnoseDynamicExceptionSpecification(
3280 Parser &P, const SourceRange &Range, bool IsNoexcept) {
3281 if (P.getLangOpts().CPlusPlus11) {
3282 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
3283 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
3284 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
3285 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
3286 }
3287}
3288
Sebastian Redl965b0e32011-03-05 14:45:16 +00003289/// ParseDynamicExceptionSpecification - Parse a C++
3290/// dynamic-exception-specification (C++ [except.spec]).
3291///
3292/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00003293/// 'throw' '(' type-id-list [opt] ')'
3294/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00003295///
Douglas Gregor356513d2008-12-01 18:00:20 +00003296/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00003297/// type-id ... [opt]
3298/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003299///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003300ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
3301 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003302 SmallVectorImpl<ParsedType> &Exceptions,
3303 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003304 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00003305
Sebastian Redl965b0e32011-03-05 14:45:16 +00003306 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003307 BalancedDelimiterTracker T(*this, tok::l_paren);
3308 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003309 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3310 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003311 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003312 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003313
Douglas Gregor356513d2008-12-01 18:00:20 +00003314 // Parse throw(...), a Microsoft extension that means "this function
3315 // can throw anything".
3316 if (Tok.is(tok::ellipsis)) {
3317 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003318 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00003319 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003320 T.consumeClose();
3321 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003322 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003323 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00003324 }
3325
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003326 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00003327 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003328 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00003329 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00003330
Douglas Gregor830837d2010-12-20 23:57:46 +00003331 if (Tok.is(tok::ellipsis)) {
3332 // C++0x [temp.variadic]p5:
3333 // - In a dynamic-exception-specification (15.4); the pattern is a
3334 // type-id.
3335 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003336 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00003337 if (!Res.isInvalid())
3338 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3339 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003340
Sebastian Redld6434562009-05-29 18:02:33 +00003341 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003342 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00003343 Ranges.push_back(Range);
3344 }
Alp Toker97650562014-01-10 11:19:30 +00003345
3346 if (!TryConsumeToken(tok::comma))
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003347 break;
3348 }
3349
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003350 T.consumeClose();
3351 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003352 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3353 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003354 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003355}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003356
Douglas Gregor7fb25412010-10-01 18:44:50 +00003357/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3358/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003359TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00003360 assert(Tok.is(tok::arrow) && "expected arrow");
3361
3362 ConsumeToken();
3363
Richard Smithbfdb1082012-03-12 08:56:40 +00003364 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003365}
3366
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003367/// \brief We have just started parsing the definition of a new class,
3368/// so push that class onto our stack of classes that is currently
3369/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003370Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003371Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3372 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003373 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003374 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003375 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003376 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003377}
3378
3379/// \brief Deallocate the given parsed class and all of its nested
3380/// classes.
3381void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003382 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3383 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003384 delete Class;
3385}
3386
3387/// \brief Pop the top class of the stack of classes that are
3388/// currently being parsed.
3389///
3390/// This routine should be called when we have finished parsing the
3391/// definition of a class, but have not yet popped the Scope
3392/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003393void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003394 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003395
John McCallc1465822011-02-14 07:13:47 +00003396 Actions.PopParsingClass(state);
3397
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003398 ParsingClass *Victim = ClassStack.top();
3399 ClassStack.pop();
3400 if (Victim->TopLevelClass) {
3401 // Deallocate all of the nested classes of this class,
3402 // recursively: we don't need to keep any of this information.
3403 DeallocateParsedClasses(Victim);
3404 return;
Mike Stump11289f42009-09-09 15:08:12 +00003405 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003406 assert(!ClassStack.empty() && "Missing top-level class?");
3407
Douglas Gregorefc46952010-10-12 16:25:54 +00003408 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003409 // The victim is a nested class, but we will not need to perform
3410 // any processing after the definition of this class since it has
3411 // no members whose handling was delayed. Therefore, we can just
3412 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003413 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003414 return;
3415 }
3416
3417 // This nested class has some members that will need to be processed
3418 // after the top-level class is completely defined. Therefore, add
3419 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003420 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003421 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003422 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003423}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003424
Richard Smith3dff2512012-04-10 03:25:07 +00003425/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3426///
3427/// \return the parsed identifier on success, and 0 if the next token is not an
3428/// attribute-token.
3429///
3430/// C++11 [dcl.attr.grammar]p3:
3431/// If a keyword or an alternative token that satisfies the syntactic
3432/// requirements of an identifier is contained in an attribute-token,
3433/// it is considered an identifier.
3434IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3435 switch (Tok.getKind()) {
3436 default:
3437 // Identifiers and keywords have identifier info attached.
David Majnemerd5271992015-01-09 18:09:39 +00003438 if (!Tok.isAnnotation()) {
3439 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3440 Loc = ConsumeToken();
3441 return II;
3442 }
Richard Smith3dff2512012-04-10 03:25:07 +00003443 }
Craig Topper161e4db2014-05-21 06:02:52 +00003444 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003445
3446 case tok::ampamp: // 'and'
3447 case tok::pipe: // 'bitor'
3448 case tok::pipepipe: // 'or'
3449 case tok::caret: // 'xor'
3450 case tok::tilde: // 'compl'
3451 case tok::amp: // 'bitand'
3452 case tok::ampequal: // 'and_eq'
3453 case tok::pipeequal: // 'or_eq'
3454 case tok::caretequal: // 'xor_eq'
3455 case tok::exclaim: // 'not'
3456 case tok::exclaimequal: // 'not_eq'
3457 // Alternative tokens do not have identifier info, but their spelling
3458 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003459 SmallString<8> SpellingBuf;
Richard Smith3dff2512012-04-10 03:25:07 +00003460 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003461 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003462 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003463 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003464 }
Craig Topper161e4db2014-05-21 06:02:52 +00003465 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003466 }
3467}
3468
Michael Han23214e52012-10-03 01:56:22 +00003469static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3470 IdentifierInfo *ScopeName) {
3471 switch (AttributeList::getKind(AttrName, ScopeName,
3472 AttributeList::AS_CXX11)) {
3473 case AttributeList::AT_CarriesDependency:
Aaron Ballman35f94212014-04-14 16:03:22 +00003474 case AttributeList::AT_Deprecated:
Michael Han23214e52012-10-03 01:56:22 +00003475 case AttributeList::AT_FallThrough:
Richard Smith10876ef2013-01-17 01:30:42 +00003476 case AttributeList::AT_CXX11NoReturn: {
Michael Han23214e52012-10-03 01:56:22 +00003477 return true;
3478 }
3479
3480 default:
3481 return false;
3482 }
3483}
3484
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003485/// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause.
3486///
3487/// [C++11] attribute-argument-clause:
3488/// '(' balanced-token-seq ')'
3489///
3490/// [C++11] balanced-token-seq:
3491/// balanced-token
3492/// balanced-token-seq balanced-token
3493///
3494/// [C++11] balanced-token:
3495/// '(' balanced-token-seq ')'
3496/// '[' balanced-token-seq ']'
3497/// '{' balanced-token-seq '}'
3498/// any token but '(', ')', '[', ']', '{', or '}'
3499bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName,
3500 SourceLocation AttrNameLoc,
3501 ParsedAttributes &Attrs,
3502 SourceLocation *EndLoc,
3503 IdentifierInfo *ScopeName,
3504 SourceLocation ScopeLoc) {
3505 assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
Aaron Ballman35f94212014-04-14 16:03:22 +00003506 SourceLocation LParenLoc = Tok.getLocation();
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003507
3508 // If the attribute isn't known, we will not attempt to parse any
3509 // arguments.
3510 if (!hasAttribute(AttrSyntax::CXX, ScopeName, AttrName,
3511 getTargetInfo().getTriple(), getLangOpts())) {
3512 // Eat the left paren, then skip to the ending right paren.
3513 ConsumeParen();
3514 SkipUntil(tok::r_paren);
3515 return false;
3516 }
3517
3518 if (ScopeName && ScopeName->getName() == "gnu")
3519 // GNU-scoped attributes have some special cases to handle GNU-specific
3520 // behaviors.
3521 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
Craig Topper161e4db2014-05-21 06:02:52 +00003522 ScopeLoc, AttributeList::AS_CXX11, nullptr);
Aaron Ballman35f94212014-04-14 16:03:22 +00003523 else {
3524 unsigned NumArgs =
3525 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
3526 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3527
3528 const AttributeList *Attr = Attrs.getList();
3529 if (Attr && IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) {
3530 // If the attribute is a standard or built-in attribute and we are
3531 // parsing an argument list, we need to determine whether this attribute
3532 // was allowed to have an argument list (such as [[deprecated]]), and how
3533 // many arguments were parsed (so we can diagnose on [[deprecated()]]).
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003534 if (Attr->getMaxArgs() && !NumArgs) {
3535 // The attribute was allowed to have arguments, but none were provided
3536 // even though the attribute parsed successfully. This is an error.
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003537 Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003538 } else if (!Attr->getMaxArgs()) {
3539 // The attribute parsed successfully, but was not allowed to have any
3540 // arguments. It doesn't matter whether any were provided -- the
Aaron Ballman35f94212014-04-14 16:03:22 +00003541 // presence of the argument list (even if empty) is diagnosed.
3542 Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
Aaron Ballman9b7cee62014-12-19 18:37:22 +00003543 << AttrName
3544 << FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc));
Aaron Ballman35f94212014-04-14 16:03:22 +00003545 }
3546 }
3547 }
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003548 return true;
3549}
3550
3551/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003552///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003553/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003554/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003555/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003556///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003557/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003558/// attribute[opt]
3559/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003560/// attribute '...'
3561/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003562///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003563/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003564/// attribute-token attribute-argument-clause[opt]
3565///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003566/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003567/// identifier
3568/// attribute-scoped-token
3569///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003570/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003571/// attribute-namespace '::' identifier
3572///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003573/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003574/// identifier
Richard Smith3dff2512012-04-10 03:25:07 +00003575void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003576 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003577 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003578 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003579 ParseAlignmentSpecifier(attrs, endLoc);
3580 return;
3581 }
3582
Alexis Hunt96d5c762009-11-21 08:43:09 +00003583 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003584 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003585
Richard Smithf679b5b2011-10-14 20:48:27 +00003586 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3587
Alexis Hunt96d5c762009-11-21 08:43:09 +00003588 ConsumeBracket();
3589 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003590
Richard Smith10876ef2013-01-17 01:30:42 +00003591 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3592
Richard Smith3dff2512012-04-10 03:25:07 +00003593 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003594 // attribute not present
Alp Toker97650562014-01-10 11:19:30 +00003595 if (TryConsumeToken(tok::comma))
Alexis Hunt96d5c762009-11-21 08:43:09 +00003596 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003597
Richard Smith3dff2512012-04-10 03:25:07 +00003598 SourceLocation ScopeLoc, AttrLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00003599 IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003600
3601 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3602 if (!AttrName)
3603 // Break out to the "expected ']'" diagnostic.
3604 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003605
Alexis Hunt96d5c762009-11-21 08:43:09 +00003606 // scoped attribute
Alp Toker97650562014-01-10 11:19:30 +00003607 if (TryConsumeToken(tok::coloncolon)) {
Richard Smith3dff2512012-04-10 03:25:07 +00003608 ScopeName = AttrName;
3609 ScopeLoc = AttrLoc;
3610
3611 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3612 if (!AttrName) {
Alp Tokerec543272013-12-24 09:48:30 +00003613 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003614 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003615 continue;
3616 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003617 }
3618
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003619 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003620 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003621
Richard Smith10876ef2013-01-17 01:30:42 +00003622 if (StandardAttr &&
3623 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3624 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003625 << AttrName << SourceRange(SeenAttrs[AttrName]);
Richard Smith10876ef2013-01-17 01:30:42 +00003626
Michael Han23214e52012-10-03 01:56:22 +00003627 // Parse attribute arguments
Aaron Ballman35f94212014-04-14 16:03:22 +00003628 if (Tok.is(tok::l_paren))
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003629 AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3630 ScopeName, ScopeLoc);
Michael Han23214e52012-10-03 01:56:22 +00003631
3632 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003633 attrs.addNew(AttrName,
3634 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3635 AttrLoc),
Craig Topper161e4db2014-05-21 06:02:52 +00003636 ScopeName, ScopeLoc, nullptr, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003637
Alp Toker97650562014-01-10 11:19:30 +00003638 if (TryConsumeToken(tok::ellipsis))
Michael Han23214e52012-10-03 01:56:22 +00003639 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3640 << AttrName->getName();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003641 }
3642
Alp Toker383d2c42014-01-01 03:08:43 +00003643 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003644 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003645 if (endLoc)
3646 *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003647 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003648 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003649}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003650
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003651/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003652///
3653/// attribute-specifier-seq:
3654/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003655void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003656 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003657 assert(getLangOpts().CPlusPlus11);
3658
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003659 SourceLocation StartLoc = Tok.getLocation(), Loc;
3660 if (!endLoc)
3661 endLoc = &Loc;
3662
Douglas Gregor6f981002011-10-07 20:35:25 +00003663 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003664 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003665 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003666
3667 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003668}
3669
Richard Smithc2c8bb82013-10-15 01:34:54 +00003670void Parser::DiagnoseAndSkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00003671 // Start and end location of an attribute or an attribute list.
3672 SourceLocation StartLoc = Tok.getLocation();
Richard Smith955bf012014-06-19 11:42:00 +00003673 SourceLocation EndLoc = SkipCXX11Attributes();
3674
3675 if (EndLoc.isValid()) {
3676 SourceRange Range(StartLoc, EndLoc);
3677 Diag(StartLoc, diag::err_attributes_not_allowed)
3678 << Range;
3679 }
3680}
3681
3682SourceLocation Parser::SkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00003683 SourceLocation EndLoc;
3684
Richard Smith955bf012014-06-19 11:42:00 +00003685 if (!isCXX11AttributeSpecifier())
3686 return EndLoc;
3687
Richard Smithc2c8bb82013-10-15 01:34:54 +00003688 do {
3689 if (Tok.is(tok::l_square)) {
3690 BalancedDelimiterTracker T(*this, tok::l_square);
3691 T.consumeOpen();
3692 T.skipToEnd();
3693 EndLoc = T.getCloseLocation();
3694 } else {
3695 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3696 ConsumeToken();
3697 BalancedDelimiterTracker T(*this, tok::l_paren);
3698 if (!T.consumeOpen())
3699 T.skipToEnd();
3700 EndLoc = T.getCloseLocation();
3701 }
3702 } while (isCXX11AttributeSpecifier());
3703
Richard Smith955bf012014-06-19 11:42:00 +00003704 return EndLoc;
Richard Smithc2c8bb82013-10-15 01:34:54 +00003705}
3706
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003707/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3708///
3709/// [MS] ms-attribute:
3710/// '[' token-seq ']'
3711///
3712/// [MS] ms-attribute-seq:
3713/// ms-attribute[opt]
3714/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00003715void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3716 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003717 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3718
3719 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003720 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003721 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003722 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
John McCall53fa7142010-12-24 02:08:15 +00003723 if (endLoc) *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003724 ExpectAndConsume(tok::r_square);
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003725 }
3726}
Francois Pichet8f981d52011-05-25 10:19:49 +00003727
3728void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3729 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00003730 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00003731 if (ParseMicrosoftIfExistsCondition(Result))
3732 return;
3733
Douglas Gregor43edb322011-10-24 22:31:10 +00003734 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3735 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00003736 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet8f981d52011-05-25 10:19:49 +00003737 return;
3738 }
Francois Pichet8f981d52011-05-25 10:19:49 +00003739
Douglas Gregor43edb322011-10-24 22:31:10 +00003740 switch (Result.Behavior) {
3741 case IEB_Parse:
3742 // Parse the declarations below.
3743 break;
3744
3745 case IEB_Dependent:
3746 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3747 << Result.IsIfExists;
3748 // Fall through to skip.
3749
3750 case IEB_Skip:
3751 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00003752 return;
3753 }
3754
Richard Smith34f30512013-11-23 04:06:09 +00003755 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003756 // __if_exists, __if_not_exists can nest.
3757 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3758 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3759 continue;
3760 }
3761
3762 // Check for extraneous top-level semicolon.
3763 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003764 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00003765 continue;
3766 }
3767
3768 AccessSpecifier AS = getAccessSpecifierIfPresent();
3769 if (AS != AS_none) {
3770 // Current token is a C++ access specifier.
3771 CurAS = AS;
3772 SourceLocation ASLoc = Tok.getLocation();
3773 ConsumeToken();
3774 if (Tok.is(tok::colon))
3775 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3776 else
Alp Toker35d87032013-12-30 23:29:50 +00003777 Diag(Tok, diag::err_expected) << tok::colon;
Francois Pichet8f981d52011-05-25 10:19:49 +00003778 ConsumeToken();
3779 continue;
3780 }
3781
3782 // Parse all the comma separated declarators.
Craig Topper161e4db2014-05-21 06:02:52 +00003783 ParseCXXClassMemberDeclaration(CurAS, nullptr);
Francois Pichet8f981d52011-05-25 10:19:49 +00003784 }
Douglas Gregor43edb322011-10-24 22:31:10 +00003785
3786 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00003787}