blob: ec19f352532fc91a1f0668d0b81eca0906a3af49 [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070016#include "clang/AST/ASTContext.h"
Larisse Voufoef4579c2013-08-06 01:03:05 +000017#include "clang/AST/DeclTemplate.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070018#include "clang/Basic/Attributes.h"
19#include "clang/Basic/CharInfo.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070020#include "clang/Basic/OperatorKinds.h"
Stephen Hines0e2c34f2015-03-23 12:09:02 -070021#include "clang/Basic/TargetInfo.h"
Chris Lattner500d3292009-01-29 05:15:15 +000022#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000023#include "clang/Sema/DeclSpec.h"
John McCall19510852010-08-20 18:27:03 +000024#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000025#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000026#include "clang/Sema/Scope.h"
John McCalle402e722012-09-25 07:32:39 +000027#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000028#include "llvm/ADT/SmallString.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000029using namespace clang;
30
31/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-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 Lattner8f08cb72007-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 Redld078e642010-08-27 23:12:46 +000040/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000041///
42/// named-namespace-definition:
43/// original-namespace-definition
44/// extension-namespace-definition
45///
46/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000047/// 'inline'[opt] 'namespace' identifier attributes[opt]
48/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000049///
50/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000051/// 'inline'[opt] 'namespace' original-namespace-name
52/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000053///
Chris Lattner8f08cb72007-08-25 06:57:03 +000054/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
55/// 'namespace' identifier '=' qualified-namespace-specifier ';'
56///
John McCalld226f652010-08-21 09:40:31 +000057Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000058 SourceLocation &DeclEnd,
59 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000060 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000061 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +000062 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000063
Douglas Gregor49f40bd2009-09-18 19:03:04 +000064 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000065 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000066 cutOffParsing();
Stephen Hines6bcf27b2014-05-29 04:14:42 -070067 return nullptr;
Douglas Gregor49f40bd2009-09-18 19:03:04 +000068 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000069
Chris Lattner8f08cb72007-08-25 06:57:03 +000070 SourceLocation IdentLoc;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070071 IdentifierInfo *Ident = nullptr;
Richard Trieuf858bd82011-05-26 20:11:09 +000072 std::vector<SourceLocation> ExtraIdentLoc;
73 std::vector<IdentifierInfo*> ExtraIdent;
74 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000075
Stephen Hines176edba2014-12-01 14:53:08 -080076 ParsedAttributesWithRange attrs(AttrFactory);
77 SourceLocation attrLoc;
78 if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) {
79 if (!getLangOpts().CPlusPlus1z)
80 Diag(Tok.getLocation(), diag::warn_cxx14_compat_attribute)
81 << 0 /*namespace*/;
82 attrLoc = Tok.getLocation();
83 ParseCXX11Attributes(attrs);
84 }
Mike Stump1eb44332009-09-09 15:08:12 +000085
Chris Lattner04d66662007-10-09 17:33:22 +000086 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000087 Ident = Tok.getIdentifierInfo();
88 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieuf858bd82011-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 Lattner8f08cb72007-08-25 06:57:03 +000094 }
Mike Stump1eb44332009-09-09 15:08:12 +000095
Stephen Hines176edba2014-12-01 14:53:08 -080096 // A nested namespace definition cannot have attributes.
97 if (!ExtraNamespaceLoc.empty() && attrLoc.isValid())
98 Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute);
99
Chris Lattner8f08cb72007-08-25 06:57:03 +0000100 // Read label attributes, if present.
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000101 if (Tok.is(tok::kw___attribute)) {
Stephen Hines176edba2014-12-01 14:53:08 -0800102 attrLoc = Tok.getLocation();
John McCall7f040a92010-12-24 02:08:15 +0000103 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000104 }
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000106 if (Tok.is(tok::equal)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700107 if (!Ident) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700108 Diag(Tok, diag::err_expected) << tok::identifier;
Nico Webere1bb3292012-10-27 23:44:27 +0000109 // Skip to end of the definition and eat the ';'.
110 SkipUntil(tok::semi);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700111 return nullptr;
Nico Webere1bb3292012-10-27 23:44:27 +0000112 }
Stephen Hines176edba2014-12-01 14:53:08 -0800113 if (attrLoc.isValid())
114 Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +0000115 if (InlineLoc.isValid())
116 Diag(InlineLoc, diag::err_inline_namespace_alias)
117 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000118 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000119 }
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Richard Trieuf858bd82011-05-26 20:11:09 +0000121
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000122 BalancedDelimiterTracker T(*this, tok::l_brace);
123 if (T.consumeOpen()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700124 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;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700128 return nullptr;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000129 }
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Douglas Gregor23c94db2010-07-02 17:43:08 +0000131 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
132 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
133 getCurScope()->getFnParent()) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000134 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000135 SkipUntil(tok::r_brace);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700136 return nullptr;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000137 }
138
Stephen Hines176edba2014-12-01 14:53:08 -0800139 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 Trieuf858bd82011-05-26 20:11:09 +0000147 TentativeParsingAction TPA(*this);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000148 SkipUntil(tok::r_brace, StopBeforeMatch);
Richard Trieuf858bd82011-05-26 20:11:09 +0000149 Token rBraceToken = Tok;
150 TPA.Revert();
151
152 if (!rBraceToken.is(tok::r_brace)) {
Stephen Hines176edba2014-12-01 14:53:08 -0800153 Diag(ExtraNamespaceLoc[0], diag::ext_nested_namespace_definition)
Richard Trieuf858bd82011-05-26 20:11:09 +0000154 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
155 } else {
Benjamin Kramer9910df02011-05-26 21:32:30 +0000156 std::string NamespaceFix;
Richard Trieuf858bd82011-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 Kramer9910df02011-05-26 21:32:30 +0000162
Richard Trieuf858bd82011-05-26 20:11:09 +0000163 std::string RBraces;
Benjamin Kramer9910df02011-05-26 21:32:30 +0000164 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieuf858bd82011-05-26 20:11:09 +0000165 RBraces += "} ";
Benjamin Kramer9910df02011-05-26 21:32:30 +0000166
Stephen Hines176edba2014-12-01 14:53:08 -0800167 Diag(ExtraNamespaceLoc[0], diag::ext_nested_namespace_definition)
Richard Trieuf858bd82011-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 Redl88e64ca2010-08-31 00:36:45 +0000175 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith7fe62082011-10-15 05:09:34 +0000176 if (InlineLoc.isValid())
Richard Smith80ad52f2013-01-02 11:42:31 +0000177 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000178 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000179
Chris Lattner51448322009-03-29 14:02:43 +0000180 // Enter a scope for the namespace.
181 ParseScope NamespaceScope(this, Scope::DeclScope);
182
John McCalld226f652010-08-21 09:40:31 +0000183 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000184 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000185 IdentLoc, Ident, T.getOpenLocation(),
186 attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000187
John McCallf312b1e2010-08-26 23:41:50 +0000188 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
189 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Richard Trieuf858bd82011-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 Gregor4a8dfb52011-10-12 16:37:45 +0000194 InlineLoc, attrs, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Chris Lattner51448322009-03-29 14:02:43 +0000196 // Leave the namespace scope.
197 NamespaceScope.Exit();
198
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000199 DeclEnd = T.getCloseLocation();
200 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner51448322009-03-29 14:02:43 +0000201
202 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000203}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000204
Richard Trieuf858bd82011-05-26 20:11:09 +0000205/// ParseInnerNamespace - Parse the contents of a namespace.
Stephen Hines176edba2014-12-01 14:53:08 -0800206void 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 Gregor4a8dfb52011-10-12 16:37:45 +0000211 BalancedDelimiterTracker &Tracker) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000212 if (index == Ident.size()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700213 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000214 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000215 MaybeParseCXX11Attributes(attrs);
Richard Trieuf858bd82011-05-26 20:11:09 +0000216 MaybeParseMicrosoftAttributes(attrs);
217 ParseExternalDeclaration(attrs);
218 }
Douglas Gregor4a8dfb52011-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 Trieuf858bd82011-05-26 20:11:09 +0000223
224 return;
225 }
226
Stephen Hines176edba2014-12-01 14:53:08 -0800227 // Handle a nested namespace definition.
228 // FIXME: Preserve the source information through to the AST rather than
229 // desugaring it here.
Richard Trieuf858bd82011-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 Gregor4a8dfb52011-10-12 16:37:45 +0000234 Ident[index], Tracker.getOpenLocation(),
235 attrs.getList());
Richard Trieuf858bd82011-05-26 20:11:09 +0000236
237 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000238 attrs, Tracker);
Richard Trieuf858bd82011-05-26 20:11:09 +0000239
240 NamespaceScope.Exit();
241
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000242 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieuf858bd82011-05-26 20:11:09 +0000243}
244
Anders Carlssonf67606a2009-03-28 04:07:16 +0000245/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
246/// alias definition.
247///
John McCalld226f652010-08-21 09:40:31 +0000248Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000249 SourceLocation AliasLoc,
250 IdentifierInfo *Alias,
251 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000252 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Anders Carlssonf67606a2009-03-28 04:07:16 +0000254 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000256 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000257 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000258 cutOffParsing();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700259 return nullptr;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000260 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000261
Anders Carlssonf67606a2009-03-28 04:07:16 +0000262 CXXScopeSpec SS;
263 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000264 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlssonf67606a2009-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);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700270 return nullptr;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000271 }
272
273 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000274 IdentifierInfo *Ident = Tok.getIdentifierInfo();
275 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Anders Carlssonf67606a2009-03-28 04:07:16 +0000277 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000278 DeclEnd = Tok.getLocation();
Stephen Hines651f13c2014-04-23 16:59:28 -0700279 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
280 SkipUntil(tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Douglas Gregor23c94db2010-07-02 17:43:08 +0000282 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000283 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000284}
285
Chris Lattnerc6fdc342008-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 Lattner7d642712010-11-09 20:15:55 +0000293Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700294 assert(isTokenStringLiteral() && "Not a string literal!");
295 ExprResult Lang = ParseStringLiteralExpression(false);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000296
Douglas Gregor074149e2009-01-05 19:45:36 +0000297 ParseScope LinkageScope(this, Scope::DeclScope);
Stephen Hines651f13c2014-04-23 16:59:28 -0700298 Decl *LinkageSpec =
299 Lang.isInvalid()
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700300 ? nullptr
Stephen Hines651f13c2014-04-23 16:59:28 -0700301 : Actions.ActOnStartLinkageSpecification(
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700302 getCurScope(), DS.getSourceRange().getBegin(), Lang.get(),
Stephen Hines651f13c2014-04-23 16:59:28 -0700303 Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
Douglas Gregor074149e2009-01-05 19:45:36 +0000304
John McCall0b7e6782011-03-24 11:26:52 +0000305 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000306 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000307 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000308
Douglas Gregor074149e2009-01-05 19:45:36 +0000309 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-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 Bagnara35f9a192010-07-30 16:47:02 +0000315 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000316 ParseExternalDeclaration(attrs, &DS);
Stephen Hines651f13c2014-04-23 16:59:28 -0700317 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
318 getCurScope(), LinkageSpec, SourceLocation())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700319 : nullptr;
Mike Stump1eb44332009-09-09 15:08:12 +0000320 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000321
Douglas Gregor63a01132010-02-07 08:38:28 +0000322 DS.abort();
323
John McCall7f040a92010-12-24 02:08:15 +0000324 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000325
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000326 BalancedDelimiterTracker T(*this, tok::l_brace);
327 T.consumeOpen();
Stephen Hines651f13c2014-04-23 16:59:28 -0700328
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 Lattnerc6fdc342008-01-12 07:05:38 +0000364 }
365
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000366 T.consumeClose();
Stephen Hines651f13c2014-04-23 16:59:28 -0700367 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
368 getCurScope(), LinkageSpec, T.getCloseLocation())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700369 : nullptr;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000370}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000371
Douglas Gregorf780abc2008-12-30 03:27:21 +0000372/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
373/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000374Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000375 const ParsedTemplateInfo &TemplateInfo,
376 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000377 ParsedAttributesWithRange &attrs,
378 Decl **OwnedType) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000379 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000380 ObjCDeclContextSwitch ObjCDC(*this);
381
Douglas Gregorf780abc2008-12-30 03:27:21 +0000382 // Eat 'using'.
383 SourceLocation UsingLoc = ConsumeToken();
384
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000385 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000386 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000387 cutOffParsing();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700388 return nullptr;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000389 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000390
John McCall78b81052010-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 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000399
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000400 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000401 }
402
Richard Smith162e1c12011-04-15 14:24:37 +0000403 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000404
405 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000406 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000407
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000408 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000409 AS_none, OwnedType);
Douglas Gregorf780abc2008-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 McCalld226f652010-08-21 09:40:31 +0000422Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000423 SourceLocation UsingLoc,
424 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000425 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-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 Gregor49f40bd2009-09-18 19:03:04 +0000431 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000432 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000433 cutOffParsing();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700434 return nullptr;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000435 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000436
Douglas Gregorf780abc2008-12-30 03:27:21 +0000437 CXXScopeSpec SS;
438 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000439 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000440
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700441 IdentifierInfo *NamespcName = nullptr;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000442 SourceLocation IdentLoc = SourceLocation();
443
444 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000445 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-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?
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700450 return nullptr;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Chris Lattner823c44e2009-01-06 07:27:21 +0000453 // Parse identifier.
454 NamespcName = Tok.getIdentifierInfo();
455 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Chris Lattner823c44e2009-01-06 07:27:21 +0000457 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000458 bool GNUAttr = false;
459 if (Tok.is(tok::kw___attribute)) {
460 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000461 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000462 }
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Chris Lattner823c44e2009-01-06 07:27:21 +0000464 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000465 DeclEnd = Tok.getLocation();
Stephen Hines651f13c2014-04-23 16:59:28 -0700466 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 Gregorf780abc2008-12-30 03:27:21 +0000470
Douglas Gregor23c94db2010-07-02 17:43:08 +0000471 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000472 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000473}
474
Richard Smith162e1c12011-04-15 14:24:37 +0000475/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
476/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000477///
478/// using-declaration: [C++ 7.3.p3: namespace.udecl]
479/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000480/// unqualified-id
481/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000482///
Richard Smithd03de6a2013-01-29 10:02:16 +0000483/// alias-declaration: C++11 [dcl.dcl]p1
484/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smith162e1c12011-04-15 14:24:37 +0000485///
John McCalld226f652010-08-21 09:40:31 +0000486Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000487 const ParsedTemplateInfo &TemplateInfo,
488 SourceLocation UsingLoc,
489 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000490 AccessSpecifier AS,
491 Decl **OwnedType) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000492 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000493 SourceLocation TypenameLoc;
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000494 bool HasTypenameKeyword = false;
Sean Hunt2edf0a22012-06-23 05:07:58 +0000495
Richard Smith5eed7e02013-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 Gregor9cfbe482009-06-20 00:51:54 +0000500
501 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000502 // FIXME: This is wrong; we should parse this as a typename-specifier.
Stephen Hines651f13c2014-04-23 16:59:28 -0700503 if (TryConsumeToken(tok::kw_typename, TypenameLoc))
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000504 HasTypenameKeyword = true;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000505
Stephen Hines176edba2014-12-01 14:53:08 -0800506 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 Gregor9cfbe482009-06-20 00:51:54 +0000512 // Parse nested-name-specifier.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700513 IdentifierInfo *LastII = nullptr;
Richard Smith2db075b2013-03-26 01:15:19 +0000514 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700515 /*MayBePseudoDtor=*/nullptr,
516 /*IsTypename=*/false,
Richard Smith2db075b2013-03-26 01:15:19 +0000517 /*LastII=*/&LastII);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000518
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000519 // Check nested-name specifier.
520 if (SS.isInvalid()) {
521 SkipUntil(tok::semi);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700522 return nullptr;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000523 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000524
Richard Smith2db075b2013-03-26 01:15:19 +0000525 SourceLocation TemplateKWLoc;
526 UnqualifiedId Name;
527
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000528 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000529 // destructor names and allow the action module to diagnose any semantic
530 // errors.
Richard Smith2db075b2013-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 Gregor9cfbe482009-06-20 00:51:54 +0000550 SkipUntil(tok::semi);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700551 return nullptr;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000552 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000553
Richard Smith5eed7e02013-10-15 01:34:54 +0000554 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smithdf1cce52013-10-24 01:21:09 +0000555 MaybeParseGNUAttributes(Attrs);
Richard Smith6b3d3e52013-02-20 19:22:51 +0000556 MaybeParseCXX11Attributes(Attrs);
Richard Smith162e1c12011-04-15 14:24:37 +0000557
558 // Maybe this is an alias-declaration.
Richard Smith162e1c12011-04-15 14:24:37 +0000559 TypeResult TypeAlias;
Richard Smith5eed7e02013-10-15 01:34:54 +0000560 bool IsAliasDecl = Tok.is(tok::equal);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700561 Decl *DeclFromDeclSpec = nullptr;
Richard Smith162e1c12011-04-15 14:24:37 +0000562 if (IsAliasDecl) {
Richard Smith5eed7e02013-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 Smith162e1c12011-04-15 14:24:37 +0000574 ConsumeToken();
575
Richard Smith80ad52f2013-01-02 11:42:31 +0000576 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000577 diag::warn_cxx98_compat_alias_declaration :
578 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000579
Richard Smith3e4c6c42011-05-05 21:57:07 +0000580 // Type alias templates cannot be specialized.
581 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000582 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
583 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-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);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700599 return nullptr;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000600 }
601
Richard Smith162e1c12011-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);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700607 return nullptr;
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000608 } else if (HasTypenameKeyword)
Richard Smith162e1c12011-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
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700616 TypeAlias = ParseTypeName(nullptr, TemplateInfo.Kind
617 ? Declarator::AliasTemplateContext
618 : Declarator::AliasDeclContext,
619 AS, &DeclFromDeclSpec, &Attrs);
620 if (OwnedType)
621 *OwnedType = DeclFromDeclSpec;
Sean Hunt2edf0a22012-06-23 05:07:58 +0000622 } else {
623 // C++11 attributes are not allowed on a using-declaration, but GNU ones
624 // are.
Richard Smith5eed7e02013-10-15 01:34:54 +0000625 ProhibitAttributes(MisplacedAttrs);
Richard Smith6b3d3e52013-02-20 19:22:51 +0000626 ProhibitAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000627
Richard Smith162e1c12011-04-15 14:24:37 +0000628 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith6b3d3e52013-02-20 19:22:51 +0000629 MaybeParseGNUAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000630 }
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000632 // Eat ';'.
633 DeclEnd = Tok.getLocation();
Stephen Hines651f13c2014-04-23 16:59:28 -0700634 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 Gregor9cfbe482009-06-20 00:51:54 +0000639
John McCall78b81052010-11-10 02:40:36 +0000640 // Diagnose an attempt to declare a templated using-declaration.
Richard Smithd03de6a2013-01-29 10:02:16 +0000641 // In C++11, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000642 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000643 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-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.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700651 return nullptr;
John McCall78b81052010-11-10 02:40:36 +0000652 }
653
Douglas Gregor480b53c2011-09-26 14:30:28 +0000654 // "typename" keyword is allowed for identifiers only,
655 // because it may be a type definition.
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000656 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor480b53c2011-09-26 14:30:28 +0000657 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
658 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000659 // Proceed parsing, but reset the HasTypenameKeyword flag.
660 HasTypenameKeyword = false;
Douglas Gregor480b53c2011-09-26 14:30:28 +0000661 }
662
Richard Smith3e4c6c42011-05-05 21:57:07 +0000663 if (IsAliasDecl) {
664 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramer5354e772012-08-23 23:38:35 +0000665 MultiTemplateParamsArg TemplateParamsArg(
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700666 TemplateParams ? TemplateParams->data() : nullptr,
Richard Smith3e4c6c42011-05-05 21:57:07 +0000667 TemplateParams ? TemplateParams->size() : 0);
668 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000669 UsingLoc, Name, Attrs.getList(),
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700670 TypeAlias, DeclFromDeclSpec);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000671 }
Richard Smith162e1c12011-04-15 14:24:37 +0000672
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000673 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
674 /* HasUsingKeyword */ true, UsingLoc,
675 SS, Name, Attrs.getList(),
676 HasTypenameKeyword, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000677}
678
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000679/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000680///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000681/// [C++0x] static_assert-declaration:
682/// static_assert ( constant-expression , string-literal ) ;
683///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000684/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000685/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000686///
John McCalld226f652010-08-21 09:40:31 +0000687Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-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 Blaikie4e4d0842012-03-11 07:00:24 +0000691 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000692 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000693 if (Tok.is(tok::kw_static_assert))
694 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000695
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000696 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000698 BalancedDelimiterTracker T(*this, tok::l_paren);
699 if (T.consumeOpen()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700700 Diag(Tok, diag::err_expected) << tok::l_paren;
Richard Smith3686c712012-09-13 19:12:50 +0000701 SkipMalformedDecl();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700702 return nullptr;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000703 }
Mike Stump1eb44332009-09-09 15:08:12 +0000704
John McCall60d7b3a2010-08-24 06:29:42 +0000705 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000706 if (AssertExpr.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000707 SkipMalformedDecl();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700708 return nullptr;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700711 ExprResult AssertMessage;
712 if (Tok.is(tok::r_paren)) {
713 Diag(Tok, getLangOpts().CPlusPlus1z
Stephen Hines176edba2014-12-01 14:53:08 -0800714 ? diag::warn_cxx14_compat_static_assert_no_message
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700715 : 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 Carlssonad5f9602009-03-13 23:29:20 +0000724
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700725 if (!isTokenStringLiteral()) {
726 Diag(Tok, diag::err_expected_string_literal)
727 << /*Source='static_assert'*/1;
728 SkipMalformedDecl();
729 return nullptr;
730 }
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700732 AssertMessage = ParseStringLiteralExpression();
733 if (AssertMessage.isInvalid()) {
734 SkipMalformedDecl();
735 return nullptr;
736 }
Richard Smith99831e42012-03-06 03:21:47 +0000737 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000738
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000739 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Chris Lattner97144fc2009-04-02 04:16:50 +0000741 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000742 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000743
John McCall9ae2f072010-08-23 23:25:46 +0000744 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700745 AssertExpr.get(),
746 AssertMessage.get(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000747 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000748}
749
Richard Smitha2c36462013-04-26 16:15:35 +0000750/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000751///
752/// 'decltype' ( expression )
Richard Smitha2c36462013-04-26 16:15:35 +0000753/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000754///
David Blaikie42d6d0c2011-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 Blaikie42d6d0c2011-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 Smithc7b55432012-02-24 22:30:04 +0000772 if (Tok.getIdentifierInfo()->isStr("decltype"))
773 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000774
David Blaikie42d6d0c2011-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 Smitha2c36462013-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(),
Stephen Hines176edba2014-12-01 14:53:08 -0800791 getLangOpts().CPlusPlus14
Richard Smitha2c36462013-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 Blaikie42d6d0c2011-12-04 05:04:18 +0000797
Richard Smitha2c36462013-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,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700801 nullptr,/*IsDecltype=*/true);
Stephen Hines176edba2014-12-01 14:53:08 -0800802 Result = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Richard Smitha2c36462013-04-26 16:15:35 +0000803 if (Result.isInvalid()) {
804 DS.SetTypeSpecError();
Alexey Bataev8fe24752013-11-18 08:17:37 +0000805 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smitha2c36462013-04-26 16:15:35 +0000806 EndLoc = ConsumeParen();
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000807 } else {
Richard Smitha2c36462013-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 Kyrtzidis1e584692012-10-26 22:53:44 +0000817 }
Richard Smitha2c36462013-04-26 16:15:35 +0000818 return EndLoc;
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000819 }
Richard Smitha2c36462013-04-26 16:15:35 +0000820
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700821 Result = Actions.ActOnDecltypeExpression(Result.get());
David Blaikie42d6d0c2011-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 Smith76f3f692012-02-22 02:04:18 +0000833 if (Result.isInvalid()) {
834 DS.SetTypeSpecError();
835 return T.getCloseLocation();
836 }
837
David Blaikie42d6d0c2011-12-04 05:04:18 +0000838 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000839 }
Richard Smitha2c36462013-04-26 16:15:35 +0000840 assert(!Result.isInvalid());
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700842 const char *PrevSpec = nullptr;
John McCallfec54012009-08-03 20:12:06 +0000843 unsigned DiagID;
Stephen Hines651f13c2014-04-23 16:59:28 -0700844 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000845 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smitha2c36462013-04-26 16:15:35 +0000846 if (Result.get()
847 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700848 DiagID, Result.get(), Policy)
Richard Smitha2c36462013-04-26 16:15:35 +0000849 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
Stephen Hines651f13c2014-04-23 16:59:28 -0700850 DiagID, Policy)) {
John McCallfec54012009-08-03 20:12:06 +0000851 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-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 Smitha2c36462013-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 Blaikie42d6d0c2011-12-04 05:04:18 +0000871 Tok.setAnnotationEndLoc(EndLoc);
872 Tok.setLocation(StartLoc);
873 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000874}
875
Sean Huntdb5d44b2011-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 Gregor4a8dfb52011-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)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000884 return;
885 }
886
887 TypeResult Result = ParseTypeName();
888 if (Result.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000889 SkipUntil(tok::r_paren, StopAtSemi);
Sean Huntdb5d44b2011-05-19 05:37:45 +0000890 return;
891 }
892
893 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000894 T.consumeClose();
895 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000896 return;
897
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700898 const char *PrevSpec = nullptr;
Sean Huntdb5d44b2011-05-19 05:37:45 +0000899 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000900 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700901 DiagID, Result.get(),
Stephen Hines651f13c2014-04-23 16:59:28 -0700902 Actions.getASTContext().getPrintingPolicy()))
Sean Huntdb5d44b2011-05-19 05:37:45 +0000903 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanella2d776342013-07-06 18:54:58 +0000904 DS.setTypeofParensRange(T.getRange());
Sean Huntdb5d44b2011-05-19 05:37:45 +0000905}
906
David Blaikie09048df2011-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 Gregor42a552f2008-11-05 20:51:48 +0000912///
Richard Smith05321402013-02-19 23:47:15 +0000913/// base-type-specifier: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000914/// class-or-decltype
Richard Smith05321402013-02-19 23:47:15 +0000915/// class-or-decltype: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000916/// nested-name-specifier[opt] class-name
917/// decltype-specifier
Richard Smith05321402013-02-19 23:47:15 +0000918/// class-name: [C++ class.name]
Douglas Gregor42a552f2008-11-05 20:51:48 +0000919/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000920/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000921///
Richard Smith05321402013-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
Stephen Hines176edba2014-12-01 14:53:08 -0800925TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
926 SourceLocation &EndLocation) {
David Blaikie7fe38782011-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 Blaikie152aa4b2011-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 Blaikie22216eb2011-10-25 17:10:12 +0000940 // Parse decltype-specifier
David Blaikie42d6d0c2011-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 Blaikie152aa4b2011-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 Blaikie22216eb2011-10-25 17:10:12 +0000947 // Fake up a Declarator to use with ActOnTypeName.
948 DeclSpec DS(AttrFactory);
949
David Blaikieb5777572011-12-08 04:53:15 +0000950 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000951
952 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
953 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
954 }
955
Douglas Gregor7f43d672009-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 Kyrtzidis25a76762011-06-22 06:09:49 +0000958 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000959 if (TemplateId->Kind == TNK_Type_template ||
960 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000961 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000962
963 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000964 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000965 EndLocation = Tok.getAnnotationEndLoc();
966 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000967
968 if (Type)
969 return Type;
970 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000971 }
972
973 // Fall through to produce an error below.
974 }
975
Douglas Gregor42a552f2008-11-05 20:51:48 +0000976 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000977 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000978 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000979 }
980
Douglas Gregor84d0a192010-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 Gregor23c94db2010-07-02 17:43:08 +0000989 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000990 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000991 Diag(IdLoc, diag::err_unknown_template_name)
992 << Id;
993 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000994
Serge Pavlov62f675c2013-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 Gregor84d0a192010-01-12 21:28:44 +00001000 return true;
Serge Pavlov62f675c2013-08-10 05:54:47 +00001001 }
Douglas Gregor84d0a192010-01-12 21:28:44 +00001002
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001003 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +00001004 UnqualifiedId TemplateName;
1005 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001006
Douglas Gregor84d0a192010-01-12 21:28:44 +00001007 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001008 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1009 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +00001010 return true;
1011 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +00001012 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001013
Douglas Gregor84d0a192010-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 Rajaratnam19357542010-03-13 10:17:05 +00001018
Douglas Gregor84d0a192010-01-12 21:28:44 +00001019 // Retrieve the type from the annotation token, consume that token, and
1020 // return.
1021 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +00001022 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +00001023 ConsumeToken();
1024 return Type;
1025 }
1026
Douglas Gregor42a552f2008-11-05 20:51:48 +00001027 // We have an identifier; check whether it is actually a type.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001028 IdentifierInfo *CorrectedII = nullptr;
Douglas Gregor059101f2011-03-02 00:47:37 +00001029 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +00001030 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00001031 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +00001032 /*NonTrivialTypeSourceInfo=*/true,
1033 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001034 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +00001035 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001036 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +00001037 }
1038
1039 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +00001040 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +00001041
1042 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +00001043 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +00001044 DS.SetRangeStart(IdLoc);
1045 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +00001046 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +00001047
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001048 const char *PrevSpec = nullptr;
Nick Lewycky56062202010-07-26 16:56:01 +00001049 unsigned DiagID;
Stephen Hines651f13c2014-04-23 16:59:28 -07001050 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
1051 Actions.getASTContext().getPrintingPolicy());
Nick Lewycky56062202010-07-26 16:56:01 +00001052
1053 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1054 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001055}
1056
John McCallc052dbb2012-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();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001063 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Stephen Hines651f13c2014-04-23 16:59:28 -07001064 AttributeList::AS_Keyword);
John McCallc052dbb2012-05-22 21:28:12 +00001065 }
1066}
1067
Richard Smithc9f35172012-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 Smith139be702012-07-02 19:14:01 +00001071bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-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 Smithba65f502013-01-19 03:48:05 +00001078 case tok::ampamp: // struct foo {...} && R = ...
Richard Smithc9f35172012-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 Smithba65f502013-01-19 03:48:05 +00001086 case tok::kw_operator: // struct foo operator ++() {...}
Stephen Hines651f13c2014-04-23 16:59:28 -07001087 case tok::kw___declspec: // struct foo {...} __declspec(...)
Stephen Hines176edba2014-12-01 14:53:08 -08001088 case tok::l_square: // void f(struct f [ 3])
1089 case tok::ellipsis: // void f(struct f ... [Ns])
1090 // 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 Smithc9f35172012-06-25 21:37:02 +00001093 return true;
Richard Smith139be702012-07-02 19:14:01 +00001094 case tok::colon:
1095 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-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;
Stephen Hines176edba2014-12-01 14:53:08 -08001100 case tok::kw__Atomic: // struct foo {...} _Atomic x;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001101 case tok::kw___unaligned: // struct foo {...} __unaligned *x;
Richard Smithba65f502013-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 Smithc9f35172012-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 Smithba65f502013-01-19 03:48:05 +00001115 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smithc9f35172012-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 Smith8338a9d2013-01-29 04:13:32 +00001140 case tok::greater:
1141 // template<class T = class X>
1142 return getLangOpts().CPlusPlus;
Richard Smithc9f35172012-06-25 21:37:02 +00001143 }
1144 return false;
1145}
1146
Douglas Gregore37ac4f2008-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 Smith69730c12012-03-12 07:56:15 +00001150/// cannot start a definition.
Douglas Gregore37ac4f2008-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 Stump1eb44332009-09-09 15:08:12 +00001161/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001162/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001163/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-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 Stump1eb44332009-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 Gregore37ac4f2008-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 Lattner4c97d762009-04-12 21:49:30 +00001187void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1188 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001189 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001190 AccessSpecifier AS,
Michael Han2e397132012-11-26 22:54:45 +00001191 bool EnteringContext, DeclSpecContext DSC,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001192 ParsedAttributesWithRange &Attributes) {
Joao Matos17d35c32012-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 Lattner4c97d762009-04-12 21:49:30 +00001201 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1202 TagType = DeclSpec::TST_union;
1203 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001204
Douglas Gregor374929f2009-09-18 15:37:17 +00001205 if (Tok.is(tok::code_completion)) {
1206 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001207 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001208 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001209 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001210
Chandler Carruth926c4b42010-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 McCall13489672012-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 Carruth926c4b42010-06-28 08:39:25 +00001225
Sean Hunt2edf0a22012-06-23 05:07:58 +00001226 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001227 // If attributes exist after tag, parse them.
Richard Smithdf1cce52013-10-24 01:21:09 +00001228 MaybeParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001229
Steve Narofff59e17e2008-12-24 20:59:21 +00001230 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001231 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001232 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001233
John McCallc052dbb2012-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 Smithdf1cce52013-10-24 01:21:09 +00001238 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCallc052dbb2012-05-22 21:28:12 +00001239
Sean Huntbbd37c62009-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 Smith4e24f0f2013-01-02 12:01:23 +00001243 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Michael Han07fc1ba2013-01-07 16:57:11 +00001245 // Source location used by FIXIT to insert misplaced
1246 // C++11 attributes
1247 SourceLocation AttrFixitLoc = Tok.getLocation();
1248
Stephen Hines176edba2014-12-01 14:53:08 -08001249 if (TagType == DeclSpec::TST_struct &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001250 Tok.isNot(tok::identifier) &&
1251 !Tok.isAnnotation() &&
Stephen Hines176edba2014-12-01 14:53:08 -08001252 Tok.getIdentifierInfo() &&
1253 (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) ||
1262 Tok.is(tok::kw___is_convertible) ||
1263 Tok.is(tok::kw___is_convertible_to) ||
1264 Tok.is(tok::kw___is_destructible) ||
1265 Tok.is(tok::kw___is_empty) ||
1266 Tok.is(tok::kw___is_enum) ||
1267 Tok.is(tok::kw___is_floating_point) ||
1268 Tok.is(tok::kw___is_final) ||
1269 Tok.is(tok::kw___is_function) ||
1270 Tok.is(tok::kw___is_fundamental) ||
1271 Tok.is(tok::kw___is_integral) ||
1272 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) ||
1276 Tok.is(tok::kw___is_member_function_pointer) ||
1277 Tok.is(tok::kw___is_member_object_pointer) ||
1278 Tok.is(tok::kw___is_member_pointer) ||
1279 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) ||
1283 Tok.is(tok::kw___is_pod) ||
1284 Tok.is(tok::kw___is_pointer) ||
1285 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) ||
1289 Tok.is(tok::kw___is_same) ||
1290 Tok.is(tok::kw___is_scalar) ||
1291 Tok.is(tok::kw___is_sealed) ||
1292 Tok.is(tok::kw___is_signed) ||
1293 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) ||
1299 Tok.is(tok::kw___is_unsigned) ||
1300 Tok.is(tok::kw___is_void) ||
1301 Tok.is(tok::kw___is_volatile)))
1302 // 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 Stump1eb44332009-09-09 15:08:12 +00001308
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001309 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001310 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001311 if (getLangOpts().CPlusPlus) {
Stephen Hines176edba2014-12-01 14:53:08 -08001312 // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it
1313 // is a base-specifier-list.
Chris Lattner08d92ec2009-12-10 00:32:41 +00001314 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001315
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001316 CXXScopeSpec Spec;
1317 bool HasValidSpec = true;
1318 if (ParseOptionalCXXScopeSpecifier(Spec, ParsedType(), EnteringContext)) {
John McCall207014e2010-07-30 06:26:29 +00001319 DS.SetTypeSpecError();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001320 HasValidSpec = false;
1321 }
1322 if (Spec.isSet())
1323 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001324 Diag(Tok, diag::err_expected) << tok::identifier;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001325 HasValidSpec = false;
1326 }
1327 if (HasValidSpec)
1328 SS = Spec;
Chris Lattner08d92ec2009-12-10 00:32:41 +00001329 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001330
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001331 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1332
Douglas Gregorcc636682009-02-17 23:15:12 +00001333 // Parse the (optional) class name or simple-template-id.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001334 IdentifierInfo *Name = nullptr;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001335 SourceLocation NameLoc;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001336 TemplateIdAnnotation *TemplateId = nullptr;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001337 if (Tok.is(tok::identifier)) {
1338 Name = Tok.getIdentifierInfo();
1339 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001340
David Blaikie4e4d0842012-03-11 07:00:24 +00001341 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001342 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-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 Gregor2cc782f2009-10-30 21:46:58 +00001346 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001347 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001348 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001349 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-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 Rajaratnam19357542010-03-13 10:17:05 +00001354
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001355 Diag(NameLoc, diag::err_explicit_spec_non_template)
Stephen Hines651f13c2014-04-23 16:59:28 -07001356 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1357 << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
Joao Matos17d35c32012-08-31 22:18:20 +00001358
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001359 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-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 {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001365 TemplateParams = nullptr;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001366 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-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.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001372 TemplateParams = nullptr;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001373 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001374 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001375 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001376 = SourceLocation();
1377 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1378 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001379 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001380 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001381 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001382 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001383 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001384
Douglas Gregor059101f2011-03-02 00:47:37 +00001385 if (TemplateId->Kind != TNK_Type_template &&
1386 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-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 Gregorcc636682009-02-17 23:15:12 +00001393
Stephen Hines651f13c2014-04-23 16:59:28 -07001394 // FIXME: Name may be null here.
Douglas Gregor39a8de12009-02-25 19:37:18 +00001395 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu6e91f4b2013-06-19 22:25:01 +00001396 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Douglas Gregor39a8de12009-02-25 19:37:18 +00001398 DS.SetTypeSpecError();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001399 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001400 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001401 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001402 }
1403
Richard Smith7796eb52012-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 Han2e397132012-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 Smith69730c12012-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 Redld9bafa72010-02-03 21:21:43 +00001425 // or
Richard Smith69730c12012-03-12 07:56:15 +00001426 // &T::operator struct s;
Stephen Hines651f13c2014-04-23 16:59:28 -07001427 // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
Michael Han2e397132012-11-26 22:54:45 +00001428
1429 // If there are attributes after class name, parse them.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001430 MaybeParseCXX11Attributes(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00001431
Stephen Hines651f13c2014-04-23 16:59:28 -07001432 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
John McCallf312b1e2010-08-26 23:41:50 +00001433 Sema::TagUseKind TUK;
Richard Smith7796eb52012-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 Smith4e24f0f2013-01-02 12:01:23 +00001438 (isCXX11FinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001439 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-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 Smithbdad7a22012-01-10 01:33:14 +00001443 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-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 Bataev8fe24752013-11-18 08:17:37 +00001448 SkipUntil(tok::semi, StopBeforeMatch);
John McCallf312b1e2010-08-26 23:41:50 +00001449 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001450 } else {
1451 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001452 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001453 }
Richard Smith150d8532013-02-22 06:46:23 +00001454 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1455 NextToken().is(tok::kw_alignas))) {
Michael Han2e397132012-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 Bataev8fe24752013-11-18 08:17:37 +00001467 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han2e397132012-11-26 22:54:45 +00001468 break;
Richard Smith150d8532013-02-22 06:46:23 +00001469 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han2e397132012-11-26 22:54:45 +00001470 ConsumeToken();
1471 ConsumeParen();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001472 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han2e397132012-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();
Stephen Hines651f13c2014-04-23 16:59:28 -07001485 } else if (!isTypeSpecifier(DSC) &&
Richard Smithc9f35172012-06-25 21:37:02 +00001486 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001487 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001488 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001489 if (Tok.isNot(tok::semi)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001490 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Joao Matos17d35c32012-08-31 22:18:20 +00001491 // A semicolon was missing after this declaration. Diagnose and recover.
Stephen Hines651f13c2014-04-23 16:59:28 -07001492 ExpectAndConsume(tok::semi, diag::err_expected_after,
1493 DeclSpec::getSpecifierName(TagType, PPol));
Joao Matos17d35c32012-08-31 22:18:20 +00001494 PP.EnterToken(Tok);
1495 Tok.setKind(tok::semi);
1496 }
Richard Smithc9f35172012-06-25 21:37:02 +00001497 } else
John McCallf312b1e2010-08-26 23:41:50 +00001498 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001499
Michael Han2e397132012-11-26 22:54:45 +00001500 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1501 // to caller to handle.
Michael Han07fc1ba2013-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 Han2e397132012-11-26 22:54:45 +00001521
John McCall13489672012-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 McCall207014e2010-07-30 06:26:29 +00001530 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001531 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-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)
Stephen Hines651f13c2014-04-23 16:59:28 -07001535 << DeclSpec::getSpecifierName(TagType, Policy);
John McCall207014e2010-07-30 06:26:29 +00001536 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001537
Bill Wendlinge78e8fc2013-12-23 10:02:34 +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 Gregore37ac4f2008-04-13 21:30:24 +00001545 return;
1546 }
1547
Douglas Gregorddc29e12009-02-06 22:42:48 +00001548 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001549 DeclResult TagOrTempResult = true; // invalid
1550 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001551
Douglas Gregor402abb52009-05-28 23:31:59 +00001552 bool Owned = false;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001553 bool SkipBody = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001554 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001555 // Explicit specialization, class template partial specialization,
1556 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001557 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001558 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001559 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001560 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001561 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001562 ProhibitAttributes(attrs);
1563
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001564 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001565 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001566 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001567 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001568 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001569 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001570 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001571 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001572 TemplateId->TemplateNameLoc,
1573 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001574 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001575 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001576 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001577
1578 // Friend template-ids are treated as references unless
1579 // they have template headers, in which case they're ill-formed
1580 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1581 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001582 } else if (TUK == Sema::TUK_Reference ||
1583 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001584 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001585 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001586 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001587 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001588 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001589 TemplateId->Template,
1590 TemplateId->TemplateNameLoc,
1591 TemplateId->LAngleLoc,
1592 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001593 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001594 } else {
1595 // This is an explicit specialization or a class template
1596 // partial specialization.
1597 TemplateParameterLists FakedParamLists;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001598 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1599 // This looks like an explicit instantiation, because we have
1600 // something like
1601 //
1602 // template class Foo<X>
1603 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001604 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001605 // meant to be an explicit specialization, but the user forgot
1606 // the '<>' after 'template'.
Richard Smith61dfea92013-11-08 19:03:29 +00001607 // It this is friend declaration however, since it cannot have a
1608 // template header, it is most likely that the user meant to
1609 // remove the 'template' keyword.
Larisse Voufo49854292013-06-22 13:56:11 +00001610 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith61dfea92013-11-08 19:03:29 +00001611 "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001612
Richard Smith61dfea92013-11-08 19:03:29 +00001613 if (TUK == Sema::TUK_Friend) {
1614 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001615 TemplateParams = nullptr;
Richard Smith61dfea92013-11-08 19:03:29 +00001616 } else {
1617 SourceLocation LAngleLoc =
1618 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1619 Diag(TemplateId->TemplateNameLoc,
1620 diag::err_explicit_instantiation_with_definition)
1621 << SourceRange(TemplateInfo.TemplateLoc)
1622 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1623
1624 // Create a fake template parameter list that contains only
1625 // "template<>", so that we treat this construct as a class
1626 // template specialization.
1627 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001628 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, nullptr,
1629 0, LAngleLoc));
Richard Smith61dfea92013-11-08 19:03:29 +00001630 TemplateParams = &FakedParamLists;
1631 }
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001632 }
1633
1634 // Build the class template specialization.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001635 TagOrTempResult = Actions.ActOnClassTemplateSpecialization(
1636 getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(),
1637 *TemplateId, attrs.getList(),
1638 MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0]
1639 : nullptr,
1640 TemplateParams ? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001641 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001642 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001643 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001644 // Explicit instantiation of a member of a class template
1645 // specialization, e.g.,
1646 //
1647 // template struct Outer<int>::Inner;
1648 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001649 ProhibitAttributes(attrs);
1650
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001651 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001652 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001653 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001654 TemplateInfo.TemplateLoc,
1655 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001656 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001657 } else if (TUK == Sema::TUK_Friend &&
1658 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001659 ProhibitAttributes(attrs);
1660
John McCall9a34edb2010-10-19 01:40:49 +00001661 TagOrTempResult =
1662 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1663 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001664 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001665 MultiTemplateParamsArg(
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001666 TemplateParams? &(*TemplateParams)[0]
1667 : nullptr,
John McCall9a34edb2010-10-19 01:40:49 +00001668 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001669 } else {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001670 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1671 ProhibitAttributes(attrs);
Richard Smith61dfea92013-11-08 19:03:29 +00001672
Larisse Voufo7c64ef02013-06-21 00:08:46 +00001673 if (TUK == Sema::TUK_Definition &&
1674 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1675 // If the declarator-id is not a template-id, issue a diagnostic and
1676 // recover by ignoring the 'template' keyword.
1677 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1678 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001679 TemplateParams = nullptr;
Larisse Voufo7c64ef02013-06-21 00:08:46 +00001680 }
Sean Hunt2edf0a22012-06-23 05:07:58 +00001681
John McCallc4e70192009-09-11 04:59:25 +00001682 bool IsDependent = false;
1683
John McCalla25c4082010-10-19 18:40:57 +00001684 // Don't pass down template parameter lists if this is just a tag
1685 // reference. For example, we don't need the template parameters here:
1686 // template <class T> class A *makeA(T t);
1687 MultiTemplateParamsArg TParams;
1688 if (TUK != Sema::TUK_Reference && TemplateParams)
1689 TParams =
1690 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1691
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001692 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001693 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001694 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001695 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001696 TParams, Owned, IsDependent,
1697 SourceLocation(), false,
Stephen Hines651f13c2014-04-23 16:59:28 -07001698 clang::TypeResult(),
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001699 DSC == DSC_type_specifier,
1700 &SkipBody);
John McCallc4e70192009-09-11 04:59:25 +00001701
1702 // If ActOnTag said the type was dependent, try again with the
1703 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001704 if (IsDependent) {
1705 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001706 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001707 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001708 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001709 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001710
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001711 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001712 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001713 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001714 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001715 isCXX11FinalKeyword());
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001716 if (SkipBody)
1717 SkipCXXMemberSpecification(StartLoc, AttrFixitLoc, TagType,
1718 TagOrTempResult.get());
1719 else if (getLangOpts().CPlusPlus)
Michael Han07fc1ba2013-01-07 16:57:11 +00001720 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1721 TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001722 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001723 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001724 }
1725
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001726 const char *PrevSpec = nullptr;
John McCallb3d87482010-08-24 05:47:05 +00001727 unsigned DiagID;
1728 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001729 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001730 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1731 NameLoc.isValid() ? NameLoc : StartLoc,
Stephen Hines651f13c2014-04-23 16:59:28 -07001732 PrevSpec, DiagID, TypeResult.get(), Policy);
John McCallc4e70192009-09-11 04:59:25 +00001733 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001734 Result = DS.SetTypeSpecType(TagType, StartLoc,
1735 NameLoc.isValid() ? NameLoc : StartLoc,
Stephen Hines651f13c2014-04-23 16:59:28 -07001736 PrevSpec, DiagID, TagOrTempResult.get(), Owned,
1737 Policy);
John McCallc4e70192009-09-11 04:59:25 +00001738 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001739 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001740 return;
1741 }
Mike Stump1eb44332009-09-09 15:08:12 +00001742
John McCallb3d87482010-08-24 05:47:05 +00001743 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001744 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001745
Chris Lattner4ed5d912010-02-02 01:23:29 +00001746 // At this point, we've successfully parsed a class-specifier in 'definition'
1747 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1748 // going to look at what comes after it to improve error recovery. If an
1749 // impossible token occurs next, we assume that the programmer forgot a ; at
1750 // the end of the declaration and recover that way.
1751 //
Richard Smithc9f35172012-06-25 21:37:02 +00001752 // Also enforce C++ [temp]p3:
1753 // In a template-declaration which defines a class, no declarator
1754 // is permitted.
Stephen Hines176edba2014-12-01 14:53:08 -08001755 //
1756 // After a type-specifier, we don't expect a semicolon. This only happens in
1757 // C, since definitions are not permitted in this context in C++.
Joao Matos17d35c32012-08-31 22:18:20 +00001758 if (TUK == Sema::TUK_Definition &&
Stephen Hines176edba2014-12-01 14:53:08 -08001759 (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) &&
Joao Matos17d35c32012-08-31 22:18:20 +00001760 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidis7d033b22012-12-17 20:10:43 +00001761 if (Tok.isNot(tok::semi)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001762 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
1763 ExpectAndConsume(tok::semi, diag::err_expected_after,
1764 DeclSpec::getSpecifierName(TagType, PPol));
Argyrios Kyrtzidis7d033b22012-12-17 20:10:43 +00001765 // Push this token back into the preprocessor and change our current token
1766 // to ';' so that the rest of the code recovers as though there were an
1767 // ';' after the definition.
1768 PP.EnterToken(Tok);
1769 Tok.setKind(tok::semi);
1770 }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001771 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001772}
1773
Mike Stump1eb44332009-09-09 15:08:12 +00001774/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001775///
1776/// base-clause : [C++ class.derived]
1777/// ':' base-specifier-list
1778/// base-specifier-list:
1779/// base-specifier '...'[opt]
1780/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001781void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001782 assert(Tok.is(tok::colon) && "Not a base clause");
1783 ConsumeToken();
1784
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001785 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001786 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001787
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001788 while (true) {
1789 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001790 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001791 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001792 // Skip the rest of this base specifier, up until the comma or
1793 // opening brace.
Alexey Bataev8fe24752013-11-18 08:17:37 +00001794 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001795 } else {
1796 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001797 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001798 }
1799
1800 // If the next token is a comma, consume it and keep reading
1801 // base-specifiers.
Stephen Hines651f13c2014-04-23 16:59:28 -07001802 if (!TryConsumeToken(tok::comma))
1803 break;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001804 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001805
1806 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001807 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001808}
1809
1810/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1811/// one entry in the base class list of a class specifier, for example:
1812/// class foo : public bar, virtual private baz {
1813/// 'public bar' and 'virtual private baz' are each base-specifiers.
1814///
1815/// base-specifier: [C++ class.derived]
Richard Smith05321402013-02-19 23:47:15 +00001816/// attribute-specifier-seq[opt] base-type-specifier
1817/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1818/// base-type-specifier
1819/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1820/// base-type-specifier
Stephen Hines176edba2014-12-01 14:53:08 -08001821BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001822 bool IsVirtual = false;
1823 SourceLocation StartLoc = Tok.getLocation();
1824
Richard Smith05321402013-02-19 23:47:15 +00001825 ParsedAttributesWithRange Attributes(AttrFactory);
1826 MaybeParseCXX11Attributes(Attributes);
1827
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001828 // Parse the 'virtual' keyword.
Stephen Hines651f13c2014-04-23 16:59:28 -07001829 if (TryConsumeToken(tok::kw_virtual))
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001830 IsVirtual = true;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001831
Richard Smith05321402013-02-19 23:47:15 +00001832 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1833
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001834 // Parse an (optional) access specifier.
1835 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001836 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001837 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Richard Smith05321402013-02-19 23:47:15 +00001839 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1840
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001841 // Parse the 'virtual' keyword (again!), in case it came after the
1842 // access specifier.
1843 if (Tok.is(tok::kw_virtual)) {
1844 SourceLocation VirtualLoc = ConsumeToken();
1845 if (IsVirtual) {
1846 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001847 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001848 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001849 }
1850
1851 IsVirtual = true;
1852 }
1853
Richard Smith05321402013-02-19 23:47:15 +00001854 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1855
Douglas Gregor42a552f2008-11-05 20:51:48 +00001856 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001857 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001858 SourceLocation BaseLoc;
1859 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001860 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001861 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001863 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1864 // actually part of the base-specifier-list grammar productions, but we
1865 // parse it here for convenience.
1866 SourceLocation EllipsisLoc;
Stephen Hines651f13c2014-04-23 16:59:28 -07001867 TryConsumeToken(tok::ellipsis, EllipsisLoc);
1868
Mike Stump1eb44332009-09-09 15:08:12 +00001869 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001870 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001872 // Notify semantic analysis that we have parsed a complete
1873 // base-specifier.
Richard Smith05321402013-02-19 23:47:15 +00001874 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1875 Access, BaseType.get(), BaseLoc,
1876 EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001877}
1878
1879/// getAccessSpecifierIfPresent - Determine whether the next token is
1880/// a C++ access-specifier.
1881///
1882/// access-specifier: [C++ class.derived]
1883/// 'private'
1884/// 'protected'
1885/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001886AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001887 switch (Tok.getKind()) {
1888 default: return AS_none;
1889 case tok::kw_private: return AS_private;
1890 case tok::kw_protected: return AS_protected;
1891 case tok::kw_public: return AS_public;
1892 }
1893}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001894
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001895/// \brief If the given declarator has any parts for which parsing has to be
Stephen Hines176edba2014-12-01 14:53:08 -08001896/// delayed, e.g., default arguments or an exception-specification, create a
1897/// late-parsed method declaration record to handle the parsing at the end of
1898/// the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001899void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1900 Decl *ThisDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001901 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001902 = DeclaratorInfo.getFunctionTypeInfo();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001903 // If there was a late-parsed exception-specification, we'll need a
1904 // late parse
1905 bool NeedLateParse = FTI.getExceptionSpecType() == EST_Unparsed;
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001906
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001907 if (!NeedLateParse) {
1908 // Look ahead to see if there are any default args
1909 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
1910 auto Param = cast<ParmVarDecl>(FTI.Params[ParamIdx].Param);
1911 if (Param->hasUnparsedDefaultArg()) {
1912 NeedLateParse = true;
1913 break;
1914 }
1915 }
1916 }
1917
1918 if (NeedLateParse) {
Stephen Hines176edba2014-12-01 14:53:08 -08001919 // Push this method onto the stack of late-parsed method
1920 // declarations.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001921 auto LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
Stephen Hines176edba2014-12-01 14:53:08 -08001922 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
1923 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
1924
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001925 // Stash the exception-specification tokens in the late-pased method.
Stephen Hines176edba2014-12-01 14:53:08 -08001926 LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens;
1927 FTI.ExceptionSpecTokens = 0;
1928
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001929 // Push tokens for each parameter. Those that do not have
1930 // defaults will be NULL.
Stephen Hines176edba2014-12-01 14:53:08 -08001931 LateMethod->DefaultArgs.reserve(FTI.NumParams);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001932 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx)
Stephen Hines651f13c2014-04-23 16:59:28 -07001933 LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001934 FTI.Params[ParamIdx].Param, FTI.Params[ParamIdx].DefaultArgTokens));
Eli Friedmand33133c2009-07-22 21:45:50 +00001935 }
1936}
1937
Richard Smith4e24f0f2013-01-02 12:01:23 +00001938/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001939/// virt-specifier.
1940///
1941/// virt-specifier:
1942/// override
1943/// final
Richard Smith4e24f0f2013-01-02 12:01:23 +00001944VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
Stephen Hines651f13c2014-04-23 16:59:28 -07001945 if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
Anders Carlssoncc54d592011-01-22 16:56:46 +00001946 return VirtSpecifiers::VS_None;
1947
Stephen Hines651f13c2014-04-23 16:59:28 -07001948 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001949
Stephen Hines651f13c2014-04-23 16:59:28 -07001950 // Initialize the contextual keywords.
1951 if (!Ident_final) {
1952 Ident_final = &PP.getIdentifierTable().get("final");
1953 if (getLangOpts().MicrosoftExt)
1954 Ident_sealed = &PP.getIdentifierTable().get("sealed");
1955 Ident_override = &PP.getIdentifierTable().get("override");
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001956 }
1957
Stephen Hines651f13c2014-04-23 16:59:28 -07001958 if (II == Ident_override)
1959 return VirtSpecifiers::VS_Override;
1960
1961 if (II == Ident_sealed)
1962 return VirtSpecifiers::VS_Sealed;
1963
1964 if (II == Ident_final)
1965 return VirtSpecifiers::VS_Final;
1966
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001967 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001968}
1969
Richard Smith4e24f0f2013-01-02 12:01:23 +00001970/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001971///
1972/// virt-specifier-seq:
1973/// virt-specifier
1974/// virt-specifier-seq virt-specifier
Richard Smith4e24f0f2013-01-02 12:01:23 +00001975void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
Stephen Hines176edba2014-12-01 14:53:08 -08001976 bool IsInterface,
1977 SourceLocation FriendLoc) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001978 while (true) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001979 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001980 if (Specifier == VirtSpecifiers::VS_None)
1981 return;
1982
Stephen Hines176edba2014-12-01 14:53:08 -08001983 if (FriendLoc.isValid()) {
1984 Diag(Tok.getLocation(), diag::err_friend_decl_spec)
1985 << VirtSpecifiers::getSpecifierName(Specifier)
1986 << FixItHint::CreateRemoval(Tok.getLocation())
1987 << SourceRange(FriendLoc, FriendLoc);
1988 ConsumeToken();
1989 continue;
1990 }
1991
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001992 // C++ [class.mem]p8:
1993 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001994 const char *PrevSpec = nullptr;
Anders Carlsson46127a92011-01-22 15:58:16 +00001995 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001996 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1997 << PrevSpec
1998 << FixItHint::CreateRemoval(Tok.getLocation());
1999
David Majnemer7121bdb2013-10-18 00:33:31 +00002000 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
2001 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalle402e722012-09-25 07:32:39 +00002002 Diag(Tok.getLocation(), diag::err_override_control_interface)
2003 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemer7121bdb2013-10-18 00:33:31 +00002004 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
2005 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalle402e722012-09-25 07:32:39 +00002006 } else {
David Majnemer7121bdb2013-10-18 00:33:31 +00002007 Diag(Tok.getLocation(),
2008 getLangOpts().CPlusPlus11
2009 ? diag::warn_cxx98_compat_override_control_keyword
2010 : diag::ext_override_control_keyword)
2011 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalle402e722012-09-25 07:32:39 +00002012 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00002013 ConsumeToken();
2014 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00002015}
2016
Richard Smith4e24f0f2013-01-02 12:01:23 +00002017/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Stephen Hines651f13c2014-04-23 16:59:28 -07002018/// 'final' or Microsoft 'sealed' contextual keyword.
Richard Smith4e24f0f2013-01-02 12:01:23 +00002019bool Parser::isCXX11FinalKeyword() const {
Stephen Hines651f13c2014-04-23 16:59:28 -07002020 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
2021 return Specifier == VirtSpecifiers::VS_Final ||
2022 Specifier == VirtSpecifiers::VS_Sealed;
2023}
Anders Carlssoncc54d592011-01-22 16:56:46 +00002024
Stephen Hines651f13c2014-04-23 16:59:28 -07002025/// \brief Parse a C++ member-declarator up to, but not including, the optional
2026/// brace-or-equal-initializer or pure-specifier.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002027bool Parser::ParseCXXMemberDeclaratorBeforeInitializer(
Stephen Hines651f13c2014-04-23 16:59:28 -07002028 Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
2029 LateParsedAttrList &LateParsedAttrs) {
2030 // member-declarator:
2031 // declarator pure-specifier[opt]
2032 // declarator brace-or-equal-initializer[opt]
2033 // identifier[opt] ':' constant-expression
Stephen Hines176edba2014-12-01 14:53:08 -08002034 if (Tok.isNot(tok::colon))
Stephen Hines651f13c2014-04-23 16:59:28 -07002035 ParseDeclarator(DeclaratorInfo);
Stephen Hines176edba2014-12-01 14:53:08 -08002036 else
2037 DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation());
David Majnemer7121bdb2013-10-18 00:33:31 +00002038
Stephen Hines651f13c2014-04-23 16:59:28 -07002039 if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
Stephen Hines176edba2014-12-01 14:53:08 -08002040 assert(DeclaratorInfo.isPastIdentifier() &&
2041 "don't know where identifier would go yet?");
Stephen Hines651f13c2014-04-23 16:59:28 -07002042 BitfieldSize = ParseConstantExpression();
2043 if (BitfieldSize.isInvalid())
2044 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002045 } else {
Stephen Hines176edba2014-12-01 14:53:08 -08002046 ParseOptionalCXX11VirtSpecifierSeq(
2047 VS, getCurrentClass().IsInterface,
2048 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002049 if (!VS.isUnset())
2050 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
2051 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002052
2053 // If a simple-asm-expr is present, parse it.
2054 if (Tok.is(tok::kw_asm)) {
2055 SourceLocation Loc;
2056 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
2057 if (AsmLabel.isInvalid())
2058 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2059
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002060 DeclaratorInfo.setAsmLabel(AsmLabel.get());
Stephen Hines651f13c2014-04-23 16:59:28 -07002061 DeclaratorInfo.SetRangeEnd(Loc);
2062 }
2063
2064 // If attributes exist after the declarator, but before an '{', parse them.
2065 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
2066
2067 // For compatibility with code written to older Clang, also accept a
2068 // virt-specifier *after* the GNU attributes.
Stephen Hines176edba2014-12-01 14:53:08 -08002069 if (BitfieldSize.isUnset() && VS.isUnset()) {
2070 ParseOptionalCXX11VirtSpecifierSeq(
2071 VS, getCurrentClass().IsInterface,
2072 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
2073 if (!VS.isUnset()) {
2074 // If we saw any GNU-style attributes that are known to GCC followed by a
2075 // virt-specifier, issue a GCC-compat warning.
2076 const AttributeList *Attr = DeclaratorInfo.getAttributes();
2077 while (Attr) {
2078 if (Attr->isKnownToGCC() && !Attr->isCXX11Attribute())
2079 Diag(Attr->getLoc(), diag::warn_gcc_attribute_location);
2080 Attr = Attr->getNext();
2081 }
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002082 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
Stephen Hines176edba2014-12-01 14:53:08 -08002083 }
2084 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002085
2086 // If this has neither a name nor a bit width, something has gone seriously
2087 // wrong. Skip until the semi-colon or }.
2088 if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
2089 // If so, skip until the semi-colon or a }.
2090 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2091 return true;
2092 }
2093 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00002094}
2095
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002096/// \brief Look for declaration specifiers possibly occurring after C++11
2097/// virt-specifier-seq and diagnose them.
2098void Parser::MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(
2099 Declarator &D,
2100 VirtSpecifiers &VS) {
2101 DeclSpec DS(AttrFactory);
2102
2103 // GNU-style and C++11 attributes are not allowed here, but they will be
2104 // handled by the caller. Diagnose everything else.
2105 ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed, false);
2106 D.ExtendWithDeclSpec(DS);
2107
2108 if (D.isFunctionDeclarator()) {
2109 auto &Function = D.getFunctionTypeInfo();
2110 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2111 auto DeclSpecCheck = [&] (DeclSpec::TQ TypeQual,
2112 const char *FixItName,
2113 SourceLocation SpecLoc,
2114 unsigned* QualifierLoc) {
2115 FixItHint Insertion;
2116 if (DS.getTypeQualifiers() & TypeQual) {
2117 if (!(Function.TypeQuals & TypeQual)) {
2118 std::string Name(FixItName);
2119 Name += " ";
2120 Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name.c_str());
2121 Function.TypeQuals |= TypeQual;
2122 *QualifierLoc = SpecLoc.getRawEncoding();
2123 }
2124 Diag(SpecLoc, diag::err_declspec_after_virtspec)
2125 << FixItName
2126 << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
2127 << FixItHint::CreateRemoval(SpecLoc)
2128 << Insertion;
2129 }
2130 };
2131 DeclSpecCheck(DeclSpec::TQ_const, "const", DS.getConstSpecLoc(),
2132 &Function.ConstQualifierLoc);
2133 DeclSpecCheck(DeclSpec::TQ_volatile, "volatile", DS.getVolatileSpecLoc(),
2134 &Function.VolatileQualifierLoc);
2135 DeclSpecCheck(DeclSpec::TQ_restrict, "restrict", DS.getRestrictSpecLoc(),
2136 &Function.RestrictQualifierLoc);
2137 }
2138
2139 // Parse ref-qualifiers.
2140 bool RefQualifierIsLValueRef = true;
2141 SourceLocation RefQualifierLoc;
2142 if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) {
2143 const char *Name = (RefQualifierIsLValueRef ? "& " : "&& ");
2144 FixItHint Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name);
2145 Function.RefQualifierIsLValueRef = RefQualifierIsLValueRef;
2146 Function.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
2147
2148 Diag(RefQualifierLoc, diag::err_declspec_after_virtspec)
2149 << (RefQualifierIsLValueRef ? "&" : "&&")
2150 << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
2151 << FixItHint::CreateRemoval(RefQualifierLoc)
2152 << Insertion;
2153 D.SetRangeEnd(RefQualifierLoc);
2154 }
2155 }
2156}
2157
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002158/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
2159///
2160/// member-declaration:
2161/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
2162/// function-definition ';'[opt]
2163/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
2164/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00002165/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00002166/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00002167/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002168///
2169/// member-declarator-list:
2170/// member-declarator
2171/// member-declarator-list ',' member-declarator
2172///
2173/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00002174/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002175/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002176/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002177/// identifier[opt] ':' constant-expression
2178///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00002179/// virt-specifier-seq:
2180/// virt-specifier
2181/// virt-specifier-seq virt-specifier
2182///
2183/// virt-specifier:
2184/// override
2185/// final
David Majnemer7121bdb2013-10-18 00:33:31 +00002186/// [MS] sealed
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00002187///
Sebastian Redle2b68332009-04-12 17:16:29 +00002188/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002189/// '= 0'
2190///
2191/// constant-initializer:
2192/// '=' constant-expression
2193///
Douglas Gregor37b372b2009-08-20 22:52:58 +00002194void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002195 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00002196 const ParsedTemplateInfo &TemplateInfo,
2197 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00002198 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002199 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00002200 Diag(Tok, diag::err_at_defs_cxx);
2201 else
2202 Diag(Tok, diag::err_at_in_class);
Richard Smithb3104392013-11-09 04:52:51 +00002203
Douglas Gregor8a9013d2011-04-14 17:21:19 +00002204 ConsumeToken();
Alexey Bataev8fe24752013-11-18 08:17:37 +00002205 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor8a9013d2011-04-14 17:21:19 +00002206 return;
2207 }
Richard Smithb3104392013-11-09 04:52:51 +00002208
Stephen Hines176edba2014-12-01 14:53:08 -08002209 // Turn on colon protection early, while parsing declspec, although there is
2210 // nothing to protect there. It prevents from false errors if error recovery
2211 // incorrectly determines where the declspec ends, as in the example:
2212 // struct A { enum class B { C }; };
2213 // const int C = 4;
2214 // struct D { A::B : C; };
2215 ColonProtectionRAIIObject X(*this);
2216
John McCall60fa3cf2009-12-11 02:10:03 +00002217 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00002218 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00002219 if (!TemplateInfo.Kind &&
Stephen Hines176edba2014-12-01 14:53:08 -08002220 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2221 Tok.is(tok::kw___super))) {
Richard Smith83a22ec2012-05-09 08:23:23 +00002222 if (TryAnnotateCXXScopeToken())
2223 MalformedTypeSpec = true;
2224
2225 bool isAccessDecl;
2226 if (Tok.isNot(tok::annot_cxxscope))
2227 isAccessDecl = false;
2228 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00002229 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
2230 else
2231 isAccessDecl = NextToken().is(tok::kw_operator);
2232
2233 if (isAccessDecl) {
2234 // Collect the scope specifier token we annotated earlier.
2235 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002236 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2237 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00002238
Stephen Hines176edba2014-12-01 14:53:08 -08002239 if (SS.isInvalid()) {
2240 SkipUntil(tok::semi);
2241 return;
2242 }
2243
John McCall60fa3cf2009-12-11 02:10:03 +00002244 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002245 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00002246 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002247 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
2248 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00002249 SkipUntil(tok::semi);
2250 return;
2251 }
2252
2253 // TODO: recover from mistakenly-qualified operator declarations.
Stephen Hines651f13c2014-04-23 16:59:28 -07002254 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2255 "access declaration")) {
2256 SkipUntil(tok::semi);
John McCall60fa3cf2009-12-11 02:10:03 +00002257 return;
Stephen Hines651f13c2014-04-23 16:59:28 -07002258 }
John McCall60fa3cf2009-12-11 02:10:03 +00002259
Douglas Gregor23c94db2010-07-02 17:43:08 +00002260 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanella8d030c72013-07-22 10:54:09 +00002261 /* HasUsingKeyword */ false,
2262 SourceLocation(),
John McCall60fa3cf2009-12-11 02:10:03 +00002263 SS, Name,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002264 /* AttrList */ nullptr,
Enea Zaffanella8d030c72013-07-22 10:54:09 +00002265 /* HasTypenameKeyword */ false,
John McCall60fa3cf2009-12-11 02:10:03 +00002266 SourceLocation());
2267 return;
2268 }
2269 }
2270
Stephen Hines176edba2014-12-01 14:53:08 -08002271 // static_assert-declaration. A templated static_assert declaration is
2272 // diagnosed in Parser::ParseSingleDeclarationAfterTemplate.
2273 if (!TemplateInfo.Kind &&
2274 (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert))) {
Chris Lattner97144fc2009-04-02 04:16:50 +00002275 SourceLocation DeclEnd;
2276 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00002277 return;
2278 }
Mike Stump1eb44332009-09-09 15:08:12 +00002279
Chris Lattner682bf922009-03-29 16:50:03 +00002280 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002281 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00002282 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00002283 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00002284 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002285 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00002286 return;
2287 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00002288
Chris Lattnerbc8d5642008-12-18 01:12:00 +00002289 // Handle: member-declaration ::= '__extension__' member-declaration
2290 if (Tok.is(tok::kw___extension__)) {
2291 // __extension__ silences extension warnings in the subexpression.
2292 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2293 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002294 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2295 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00002296 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002297
John McCall0b7e6782011-03-24 11:26:52 +00002298 ParsedAttributesWithRange attrs(AttrFactory);
Michael Han52b501c2012-11-28 23:17:40 +00002299 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00002300 // Optional C++11 attribute-specifier
2301 MaybeParseCXX11Attributes(attrs);
Michael Han52b501c2012-11-28 23:17:40 +00002302 // We need to keep these attributes for future diagnostic
2303 // before they are taken over by declaration specifier.
2304 FnAttrs.addAll(attrs.getList());
2305 FnAttrs.Range = attrs.Range;
2306
John McCall7f040a92010-12-24 02:08:15 +00002307 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002308
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002309 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00002310 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002311
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002312 // Eat 'using'.
2313 SourceLocation UsingLoc = ConsumeToken();
2314
2315 if (Tok.is(tok::kw_namespace)) {
2316 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataev8fe24752013-11-18 08:17:37 +00002317 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattnerae50d502010-02-02 00:43:15 +00002318 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002319 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00002320 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00002321 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2322 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002323 }
2324 return;
2325 }
2326
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002327 // Hold late-parsed attributes so we can attach a Decl to them later.
2328 LateParsedAttrList CommonLateParsedAttrs;
2329
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002330 // decl-specifier-seq:
2331 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00002332 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00002333 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00002334 if (MalformedTypeSpec)
2335 DS.SetTypeSpecError();
Stephen Hines651f13c2014-04-23 16:59:28 -07002336
Stephen Hines176edba2014-12-01 14:53:08 -08002337 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2338 &CommonLateParsedAttrs);
2339
2340 // Turn off colon protection that was set for declspec.
2341 X.restore();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002342
Bill Wendlingf0cc19f2013-11-19 22:56:43 +00002343 // If we had a free-standing type definition with a missing semicolon, we
2344 // may get this far before the problem becomes obvious.
2345 if (DS.hasTagDefinition() &&
2346 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2347 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2348 &CommonLateParsedAttrs))
2349 return;
2350
Benjamin Kramer5354e772012-08-23 23:38:35 +00002351 MultiTemplateParamsArg TemplateParams(
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002352 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data()
2353 : nullptr,
John McCalldd4a3b02009-09-16 22:47:08 +00002354 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2355
Stephen Hines651f13c2014-04-23 16:59:28 -07002356 if (TryConsumeToken(tok::semi)) {
Michael Han52b501c2012-11-28 23:17:40 +00002357 if (DS.isFriendSpecified())
2358 ProhibitAttributes(FnAttrs);
2359
John McCalld226f652010-08-21 09:40:31 +00002360 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00002361 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00002362 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00002363 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002364 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002365
John McCall54abf7d2009-11-04 02:18:39 +00002366 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00002367 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002368
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002369 // Hold late-parsed attributes so we can attach a Decl to them later.
2370 LateParsedAttrList LateParsedAttrs;
2371
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002372 SourceLocation EqualLoc;
2373 bool HasInitializer = false;
2374 ExprResult Init;
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002375
Stephen Hines651f13c2014-04-23 16:59:28 -07002376 SmallVector<Decl *, 8> DeclsInGroup;
2377 ExprResult BitfieldSize;
2378 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002379
Stephen Hines651f13c2014-04-23 16:59:28 -07002380 // Parse the first declarator.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002381 if (ParseCXXMemberDeclaratorBeforeInitializer(
2382 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002383 TryConsumeToken(tok::semi);
2384 return;
2385 }
John Thompson1b2fc0f2009-11-25 22:58:06 +00002386
Stephen Hines651f13c2014-04-23 16:59:28 -07002387 // Check for a member function definition.
2388 if (BitfieldSize.isUnset()) {
2389 // MSVC permits pure specifier on inline functions defined at class scope.
Francois Pichet6a247472011-05-11 02:14:46 +00002390 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00002391 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002392 DeclaratorInfo.isFunctionDeclarator() &&
Francois Pichet6a247472011-05-11 02:14:46 +00002393 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002394 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00002395 Init = ParseInitializer();
2396 if (Init.isInvalid())
Alexey Bataev8fe24752013-11-18 08:17:37 +00002397 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002398 else
2399 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00002400 }
2401
Douglas Gregor45fa5602011-11-07 20:56:01 +00002402 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002403 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002404 //
2405 // In C++11, a non-function declarator followed by an open brace is a
2406 // braced-init-list for an in-class member initialization, not an
2407 // erroneous function definition.
Richard Smith80ad52f2013-01-02 11:42:31 +00002408 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002409 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002410 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00002411 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002412 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002413 } else if (Tok.is(tok::equal)) {
2414 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00002415 if (KW.is(tok::kw_default))
2416 DefinitionKind = FDK_Defaulted;
2417 else if (KW.is(tok::kw_delete))
2418 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00002419 }
2420 }
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002421 DeclaratorInfo.setFunctionDefinitionKind(DefinitionKind);
Sean Hunte4246a62011-05-12 06:15:49 +00002422
Michael Han52b501c2012-11-28 23:17:40 +00002423 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2424 // to a friend declaration, that declaration shall be a definition.
2425 if (DeclaratorInfo.isFunctionDeclarator() &&
2426 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2427 // Diagnose attributes that appear before decl specifier:
2428 // [[]] friend int foo();
2429 ProhibitAttributes(FnAttrs);
2430 }
2431
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002432 if (DefinitionKind != FDK_Declaration) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002433 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002434 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002435 ConsumeBrace();
Alexey Bataev8fe24752013-11-18 08:17:37 +00002436 SkipUntil(tok::r_brace);
Michael Han52b501c2012-11-28 23:17:40 +00002437
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002438 // Consume the optional ';'
Stephen Hines651f13c2014-04-23 16:59:28 -07002439 TryConsumeToken(tok::semi);
2440
Chris Lattner682bf922009-03-29 16:50:03 +00002441 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002442 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002443
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002444 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002445 Diag(DeclaratorInfo.getIdentifierLoc(),
2446 diag::err_function_declared_typedef);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002447
Richard Smith6f9a4452012-11-15 22:54:20 +00002448 // Recover by treating the 'typedef' as spurious.
2449 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002450 }
2451
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002452 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002453 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002454 VS, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002455
David Majnemerfcbe2082013-08-01 04:22:55 +00002456 if (FunDecl) {
2457 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2458 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2459 }
2460 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2461 LateParsedAttrs[i]->addDecl(FunDecl);
2462 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002463 }
2464 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00002465
2466 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002467 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00002468 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002469
Chris Lattner682bf922009-03-29 16:50:03 +00002470 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002471 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002472 }
2473
2474 // member-declarator-list:
2475 // member-declarator
2476 // member-declarator-list ',' member-declarator
2477
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002478 while (1) {
Richard Smithca523302012-06-10 03:12:00 +00002479 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002480 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002481 if (BitfieldSize.get()) {
2482 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataev8fe24752013-11-18 08:17:37 +00002483 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith7a614d82011-06-11 17:19:42 +00002484 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002485 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002486 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2487 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smithca523302012-06-10 03:12:00 +00002488 != DeclSpec::SCS_typedef)
2489 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002490 }
2491 }
2492
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002493 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002494 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002495 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002496
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002497 NamedDecl *ThisDecl = nullptr;
John McCall67d1a672009-08-06 02:15:43 +00002498 if (DS.isFriendSpecified()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002499 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
Michael Han52b501c2012-11-28 23:17:40 +00002500 // to a friend declaration, that declaration shall be a definition.
2501 //
Stephen Hines651f13c2014-04-23 16:59:28 -07002502 // Diagnose attributes that appear in a friend member function declarator:
2503 // friend int foo [[]] ();
Michael Han52b501c2012-11-28 23:17:40 +00002504 SmallVector<SourceRange, 4> Ranges;
2505 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
Stephen Hines651f13c2014-04-23 16:59:28 -07002506 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
2507 E = Ranges.end(); I != E; ++I)
2508 Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
Michael Han52b501c2012-11-28 23:17:40 +00002509
Douglas Gregor23c94db2010-07-02 17:43:08 +00002510 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002511 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002512 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002513 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002514 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002515 TemplateParams,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002516 BitfieldSize.get(),
Richard Smithca523302012-06-10 03:12:00 +00002517 VS, HasInClassInit);
Larisse Voufoef4579c2013-08-06 01:03:05 +00002518
2519 if (VarTemplateDecl *VT =
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002520 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr)
Larisse Voufoef4579c2013-08-06 01:03:05 +00002521 // Re-direct this decl to refer to the templated decl so that we can
2522 // initialize it.
2523 ThisDecl = VT->getTemplatedDecl();
2524
David Majnemerfcbe2082013-08-01 04:22:55 +00002525 if (ThisDecl && AccessAttrs)
Richard Smith4a97b8e2013-08-29 00:47:48 +00002526 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002527 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002528
Douglas Gregor147545d2011-10-10 14:49:18 +00002529 // Handle the initializer.
David Blaikie1d87fba2013-01-30 01:22:18 +00002530 if (HasInClassInit != ICIS_NoInit &&
2531 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2532 DeclSpec::SCS_static) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002533 // The initializer was deferred; parse it and cache the tokens.
David Majnemerfcbe2082013-08-01 04:22:55 +00002534 Diag(Tok, getLangOpts().CPlusPlus11
2535 ? diag::warn_cxx98_compat_nonstatic_member_init
2536 : diag::ext_nonstatic_member_init);
Richard Smith7fe62082011-10-15 05:09:34 +00002537
Richard Smith7a614d82011-06-11 17:19:42 +00002538 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002539 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2540 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002541 //
2542 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002543 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002544 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataev8fe24752013-11-18 08:17:37 +00002545 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemerfcbe2082013-08-01 04:22:55 +00002546
2547 // Avoid later warnings about a class member of incomplete type.
David Blaikie3164c142012-02-14 09:00:46 +00002548 if (ThisDecl)
David Blaikie3164c142012-02-14 09:00:46 +00002549 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002550 } else
2551 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002552 } else if (HasInitializer) {
2553 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002554 if (!Init.isUsable())
David Majnemerfcbe2082013-08-01 04:22:55 +00002555 Init = ParseCXXMemberInitializer(
2556 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2557
Douglas Gregor147545d2011-10-10 14:49:18 +00002558 if (Init.isInvalid())
Alexey Bataev8fe24752013-11-18 08:17:37 +00002559 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor147545d2011-10-10 14:49:18 +00002560 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002561 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smitha2c36462013-04-26 16:15:35 +00002562 DS.containsPlaceholderType());
David Majnemerfcbe2082013-08-01 04:22:55 +00002563 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor147545d2011-10-10 14:49:18 +00002564 // No initializer.
Richard Smitha2c36462013-04-26 16:15:35 +00002565 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemerfcbe2082013-08-01 04:22:55 +00002566
Douglas Gregor147545d2011-10-10 14:49:18 +00002567 if (ThisDecl) {
David Majnemerfcbe2082013-08-01 04:22:55 +00002568 if (!ThisDecl->isInvalidDecl()) {
2569 // Set the Decl for any late parsed attributes
2570 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2571 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2572
2573 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2574 LateParsedAttrs[i]->addDecl(ThisDecl);
2575 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002576 Actions.FinalizeDeclaration(ThisDecl);
2577 DeclsInGroup.push_back(ThisDecl);
David Majnemerfcbe2082013-08-01 04:22:55 +00002578
2579 if (DeclaratorInfo.isFunctionDeclarator() &&
2580 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2581 DeclSpec::SCS_typedef)
2582 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002583 }
David Majnemerfcbe2082013-08-01 04:22:55 +00002584 LateParsedAttrs.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002585
2586 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002587
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002588 // If we don't have a comma, it is either the end of the list (a ';')
2589 // or an error, bail out.
Stephen Hines651f13c2014-04-23 16:59:28 -07002590 SourceLocation CommaLoc;
2591 if (!TryConsumeToken(tok::comma, CommaLoc))
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002592 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002593
Richard Smith1c94c162012-01-09 22:31:44 +00002594 if (Tok.isAtStartOfLine() &&
2595 !MightBeDeclarator(Declarator::MemberContext)) {
2596 // This comma was followed by a line-break and something which can't be
2597 // the start of a declarator. The comma was probably a typo for a
2598 // semicolon.
2599 Diag(CommaLoc, diag::err_expected_semi_declaration)
2600 << FixItHint::CreateReplacement(CommaLoc, ";");
2601 ExpectSemi = false;
2602 break;
2603 }
Mike Stump1eb44332009-09-09 15:08:12 +00002604
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002605 // Parse the next declarator.
2606 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002607 VS.clear();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002608 BitfieldSize = ExprResult(/*Invalid=*/false);
2609 Init = ExprResult(/*Invalid=*/false);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002610 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002611 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002612
Stephen Hines651f13c2014-04-23 16:59:28 -07002613 // GNU attributes are allowed before the second and subsequent declarator.
John McCall7f040a92010-12-24 02:08:15 +00002614 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002615
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002616 if (ParseCXXMemberDeclaratorBeforeInitializer(
2617 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs))
2618 break;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002619 }
2620
Richard Smith1c94c162012-01-09 22:31:44 +00002621 if (ExpectSemi &&
2622 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002623 // Skip to end of block or statement.
Alexey Bataev8fe24752013-11-18 08:17:37 +00002624 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattnerae50d502010-02-02 00:43:15 +00002625 // If we stopped at a ';', eat it.
Stephen Hines651f13c2014-04-23 16:59:28 -07002626 TryConsumeToken(tok::semi);
Chris Lattner682bf922009-03-29 16:50:03 +00002627 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002628 }
2629
Rafael Espindola4549d7f2013-07-09 12:05:01 +00002630 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002631}
2632
Richard Smith7a614d82011-06-11 17:19:42 +00002633/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2634/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2635/// function definition. The location of the '=', if any, will be placed in
2636/// EqualLoc.
2637///
2638/// pure-specifier:
2639/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002640///
Richard Smith7a614d82011-06-11 17:19:42 +00002641/// brace-or-equal-initializer:
2642/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002643/// braced-init-list
2644///
Richard Smith7a614d82011-06-11 17:19:42 +00002645/// initializer-clause:
2646/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002647/// braced-init-list
2648///
Richard Smithb3104392013-11-09 04:52:51 +00002649/// defaulted/deleted function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002650/// '=' 'default'
2651/// '=' 'delete'
2652///
2653/// Prior to C++0x, the assignment-expression in an initializer-clause must
2654/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002655ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002656 SourceLocation &EqualLoc) {
2657 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2658 && "Data member initializer not starting with '=' or '{'");
2659
Douglas Gregor552e2992012-02-21 02:22:07 +00002660 EnterExpressionEvaluationContext Context(Actions,
2661 Sema::PotentiallyEvaluated,
2662 D);
Stephen Hines651f13c2014-04-23 16:59:28 -07002663 if (TryConsumeToken(tok::equal, EqualLoc)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002664 if (Tok.is(tok::kw_delete)) {
2665 // In principle, an initializer of '= delete p;' is legal, but it will
2666 // never type-check. It's better to diagnose it as an ill-formed expression
2667 // than as an ill-formed deleted non-function member.
2668 // An initializer of '= delete p, foo' will never be parsed, because
2669 // a top-level comma always ends the initializer expression.
2670 const Token &Next = NextToken();
2671 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
Stephen Hines651f13c2014-04-23 16:59:28 -07002672 Next.is(tok::eof)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002673 if (IsFunction)
2674 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2675 << 1 /* delete */;
2676 else
2677 Diag(ConsumeToken(), diag::err_deleted_non_function);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002678 return ExprError();
Richard Smith7a614d82011-06-11 17:19:42 +00002679 }
2680 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002681 if (IsFunction)
2682 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2683 << 0 /* default */;
2684 else
2685 Diag(ConsumeToken(), diag::err_default_special_members);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002686 return ExprError();
Richard Smith7a614d82011-06-11 17:19:42 +00002687 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002688 }
2689 if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) {
2690 Diag(Tok, diag::err_ms_property_initializer) << PD;
2691 return ExprError();
Sebastian Redl33deb352012-02-22 10:50:08 +00002692 }
2693 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002694}
2695
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002696void Parser::SkipCXXMemberSpecification(SourceLocation RecordLoc,
2697 SourceLocation AttrFixitLoc,
2698 unsigned TagType, Decl *TagDecl) {
2699 // Skip the optional 'final' keyword.
2700 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
2701 assert(isCXX11FinalKeyword() && "not a class definition");
2702 ConsumeToken();
2703
2704 // Diagnose any C++11 attributes after 'final' keyword.
2705 // We deliberately discard these attributes.
2706 ParsedAttributesWithRange Attrs(AttrFactory);
2707 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
2708
2709 // This can only happen if we had malformed misplaced attributes;
2710 // we only get called if there is a colon or left-brace after the
2711 // attributes.
2712 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_brace))
2713 return;
2714 }
2715
2716 // Skip the base clauses. This requires actually parsing them, because
2717 // otherwise we can't be sure where they end (a left brace may appear
2718 // within a template argument).
2719 if (Tok.is(tok::colon)) {
2720 // Enter the scope of the class so that we can correctly parse its bases.
2721 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
2722 ParsingClassDefinition ParsingDef(*this, TagDecl, /*NonNestedClass*/ true,
2723 TagType == DeclSpec::TST_interface);
2724 Actions.ActOnTagStartSkippedDefinition(getCurScope(), TagDecl);
2725
2726 // Parse the bases but don't attach them to the class.
2727 ParseBaseClause(nullptr);
2728
2729 Actions.ActOnTagFinishSkippedDefinition();
2730
2731 if (!Tok.is(tok::l_brace)) {
2732 Diag(PP.getLocForEndOfToken(PrevTokLocation),
2733 diag::err_expected_lbrace_after_base_specifiers);
2734 return;
2735 }
2736 }
2737
2738 // Skip the body.
2739 assert(Tok.is(tok::l_brace));
2740 BalancedDelimiterTracker T(*this, tok::l_brace);
2741 T.consumeOpen();
2742 T.skipToEnd();
2743}
2744
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002745/// ParseCXXMemberSpecification - Parse the class definition.
2746///
2747/// member-specification:
2748/// member-declaration member-specification[opt]
2749/// access-specifier ':' member-specification[opt]
2750///
Joao Matos17d35c32012-08-31 22:18:20 +00002751void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han07fc1ba2013-01-07 16:57:11 +00002752 SourceLocation AttrFixitLoc,
Richard Smith05321402013-02-19 23:47:15 +00002753 ParsedAttributesWithRange &Attrs,
Joao Matos17d35c32012-08-31 22:18:20 +00002754 unsigned TagType, Decl *TagDecl) {
2755 assert((TagType == DeclSpec::TST_struct ||
2756 TagType == DeclSpec::TST_interface ||
2757 TagType == DeclSpec::TST_union ||
2758 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2759
John McCallf312b1e2010-08-26 23:41:50 +00002760 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2761 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002762
Douglas Gregor26997fd2010-01-16 20:52:59 +00002763 // Determine whether this is a non-nested class. Note that local
2764 // classes are *not* considered to be nested classes.
2765 bool NonNestedClass = true;
2766 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002767 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002768 if (S->isClassScope()) {
2769 // We're inside a class scope, so this is a nested class.
2770 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002771
2772 // The Microsoft extension __interface does not permit nested classes.
2773 if (getCurrentClass().IsInterface) {
2774 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2775 << /*ErrorType=*/6
2776 << (isa<NamedDecl>(TagDecl)
2777 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
Stephen Hines651f13c2014-04-23 16:59:28 -07002778 : "(anonymous)");
John McCalle402e722012-09-25 07:32:39 +00002779 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002780 break;
2781 }
2782
2783 if ((S->getFlags() & Scope::FnScope)) {
2784 // If we're in a function or function template declared in the
2785 // body of a class, then this is a local class rather than a
2786 // nested class.
2787 const Scope *Parent = S->getParent();
2788 if (Parent->isTemplateParamScope())
2789 Parent = Parent->getParent();
2790 if (Parent->isClassScope())
2791 break;
2792 }
2793 }
2794 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002795
2796 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002797 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002798
Douglas Gregor6569d682009-05-27 23:11:45 +00002799 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002800 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2801 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002802
Douglas Gregorddc29e12009-02-06 22:42:48 +00002803 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002804 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002805
Anders Carlssonb184a182011-03-25 14:46:08 +00002806 SourceLocation FinalLoc;
David Majnemer7121bdb2013-10-18 00:33:31 +00002807 bool IsFinalSpelledSealed = false;
Anders Carlssonb184a182011-03-25 14:46:08 +00002808
2809 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002810 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemer7121bdb2013-10-18 00:33:31 +00002811 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2812 assert((Specifier == VirtSpecifiers::VS_Final ||
2813 Specifier == VirtSpecifiers::VS_Sealed) &&
2814 "not a class definition");
Richard Smith8b11b5e2011-10-15 04:21:46 +00002815 FinalLoc = ConsumeToken();
David Majnemer7121bdb2013-10-18 00:33:31 +00002816 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonb184a182011-03-25 14:46:08 +00002817
David Majnemer7121bdb2013-10-18 00:33:31 +00002818 if (TagType == DeclSpec::TST_interface)
John McCalle402e722012-09-25 07:32:39 +00002819 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemer7121bdb2013-10-18 00:33:31 +00002820 << VirtSpecifiers::getSpecifierName(Specifier);
2821 else if (Specifier == VirtSpecifiers::VS_Final)
2822 Diag(FinalLoc, getLangOpts().CPlusPlus11
2823 ? diag::warn_cxx98_compat_override_control_keyword
2824 : diag::ext_override_control_keyword)
2825 << VirtSpecifiers::getSpecifierName(Specifier);
2826 else if (Specifier == VirtSpecifiers::VS_Sealed)
2827 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han2e397132012-11-26 22:54:45 +00002828
Michael Han07fc1ba2013-01-07 16:57:11 +00002829 // Parse any C++11 attributes after 'final' keyword.
2830 // These attributes are not allowed to appear here,
2831 // and the only possible place for them to appertain
2832 // to the class would be between class-key and class-name.
Richard Smith05321402013-02-19 23:47:15 +00002833 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002834
2835 // ParseClassSpecifier() does only a superficial check for attributes before
2836 // deciding to call this method. For example, for
2837 // `class C final alignas ([l) {` it will decide that this looks like a
2838 // misplaced attribute since it sees `alignas '(' ')'`. But the actual
2839 // attribute parsing code will try to parse the '[' as a constexpr lambda
2840 // and consume enough tokens that the alignas parsing code will eat the
2841 // opening '{'. So bail out if the next token isn't one we expect.
2842 if (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)) {
2843 if (TagDecl)
2844 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
2845 return;
2846 }
Anders Carlssonb184a182011-03-25 14:46:08 +00002847 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002848
John McCallbd0dfa52009-12-19 21:48:58 +00002849 if (Tok.is(tok::colon)) {
2850 ParseBaseClause(TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002851 if (!Tok.is(tok::l_brace)) {
Stephen Hines176edba2014-12-01 14:53:08 -08002852 bool SuggestFixIt = false;
2853 SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation);
2854 if (Tok.isAtStartOfLine()) {
2855 switch (Tok.getKind()) {
2856 case tok::kw_private:
2857 case tok::kw_protected:
2858 case tok::kw_public:
2859 SuggestFixIt = NextToken().getKind() == tok::colon;
2860 break;
2861 case tok::kw_static_assert:
2862 case tok::r_brace:
2863 case tok::kw_using:
2864 // base-clause can have simple-template-id; 'template' can't be there
2865 case tok::kw_template:
2866 SuggestFixIt = true;
2867 break;
2868 case tok::identifier:
2869 SuggestFixIt = isConstructorDeclarator(true);
2870 break;
2871 default:
2872 SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
2873 break;
2874 }
2875 }
2876 DiagnosticBuilder LBraceDiag =
2877 Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers);
2878 if (SuggestFixIt) {
2879 LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {");
2880 // Try recovering from missing { after base-clause.
2881 PP.EnterToken(Tok);
2882 Tok.setKind(tok::l_brace);
2883 } else {
2884 if (TagDecl)
2885 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
2886 return;
2887 }
John McCallbd0dfa52009-12-19 21:48:58 +00002888 }
2889 }
2890
2891 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002892 BalancedDelimiterTracker T(*this, tok::l_brace);
2893 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002894
John McCall42a4f662010-05-28 08:11:17 +00002895 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002896 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemer7121bdb2013-10-18 00:33:31 +00002897 IsFinalSpelledSealed,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002898 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002899
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002900 // C++ 11p3: Members of a class defined with the keyword class are private
2901 // by default. Members of a class defined with the keywords struct or union
2902 // are public by default.
2903 AccessSpecifier CurAS;
2904 if (TagType == DeclSpec::TST_class)
2905 CurAS = AS_private;
2906 else
2907 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002908 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002909
Douglas Gregor07976d22010-06-21 22:31:09 +00002910 if (TagDecl) {
2911 // While we still have something to read, read the member-declarations.
Stephen Hines651f13c2014-04-23 16:59:28 -07002912 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Douglas Gregor07976d22010-06-21 22:31:09 +00002913 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002914
David Blaikie4e4d0842012-03-11 07:00:24 +00002915 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002916 Tok.is(tok::kw___if_not_exists))) {
2917 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2918 continue;
2919 }
2920
Douglas Gregor07976d22010-06-21 22:31:09 +00002921 // Check for extraneous top-level semicolon.
2922 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002923 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002924 continue;
2925 }
2926
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002927 if (Tok.is(tok::annot_pragma_vis)) {
2928 HandlePragmaVisibility();
2929 continue;
2930 }
2931
2932 if (Tok.is(tok::annot_pragma_pack)) {
2933 HandlePragmaPack();
2934 continue;
2935 }
2936
Argyrios Kyrtzidisf4deaef2012-10-12 17:39:59 +00002937 if (Tok.is(tok::annot_pragma_align)) {
2938 HandlePragmaAlign();
2939 continue;
2940 }
2941
Alexey Bataevc6400582013-03-22 06:34:35 +00002942 if (Tok.is(tok::annot_pragma_openmp)) {
2943 ParseOpenMPDeclarativeDirective();
2944 continue;
2945 }
2946
Stephen Hines651f13c2014-04-23 16:59:28 -07002947 if (Tok.is(tok::annot_pragma_ms_pointers_to_members)) {
2948 HandlePragmaMSPointersToMembers();
2949 continue;
2950 }
2951
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002952 if (Tok.is(tok::annot_pragma_ms_pragma)) {
2953 HandlePragmaMSPragma();
2954 continue;
2955 }
2956
Richard Smithb3104392013-11-09 04:52:51 +00002957 // If we see a namespace here, a close brace was missing somewhere.
2958 if (Tok.is(tok::kw_namespace)) {
Richard Smith7faf81f2013-11-15 23:00:02 +00002959 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
Richard Smithb3104392013-11-09 04:52:51 +00002960 break;
2961 }
2962
Douglas Gregor07976d22010-06-21 22:31:09 +00002963 AccessSpecifier AS = getAccessSpecifierIfPresent();
2964 if (AS != AS_none) {
2965 // Current token is a C++ access specifier.
2966 CurAS = AS;
2967 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002968 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002969 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002970 AccessAttrs.clear();
2971 MaybeParseGNUAttributes(AccessAttrs);
2972
David Blaikie13f8daf2011-10-13 06:08:43 +00002973 SourceLocation EndLoc;
Stephen Hines651f13c2014-04-23 16:59:28 -07002974 if (TryConsumeToken(tok::colon, EndLoc)) {
2975 } else if (TryConsumeToken(tok::semi, EndLoc)) {
2976 Diag(EndLoc, diag::err_expected)
2977 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
David Blaikie13f8daf2011-10-13 06:08:43 +00002978 } else {
2979 EndLoc = ASLoc.getLocWithOffset(TokLength);
Stephen Hines651f13c2014-04-23 16:59:28 -07002980 Diag(EndLoc, diag::err_expected)
2981 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
David Blaikie13f8daf2011-10-13 06:08:43 +00002982 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002983
John McCalle402e722012-09-25 07:32:39 +00002984 // The Microsoft extension __interface does not permit non-public
2985 // access specifiers.
2986 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2987 Diag(ASLoc, diag::err_access_specifier_interface)
2988 << (CurAS == AS_protected);
2989 }
2990
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002991 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2992 AccessAttrs.getList())) {
2993 // found another attribute than only annotations
2994 AccessAttrs.clear();
2995 }
2996
Douglas Gregor07976d22010-06-21 22:31:09 +00002997 continue;
2998 }
2999
Douglas Gregor07976d22010-06-21 22:31:09 +00003000 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003001 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00003002 }
3003
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003004 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00003005 } else {
Alexey Bataev8fe24752013-11-18 08:17:37 +00003006 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00003007 }
Mike Stump1eb44332009-09-09 15:08:12 +00003008
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00003009 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003010 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003011 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00003012
John McCall42a4f662010-05-28 08:11:17 +00003013 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00003014 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003015 T.getOpenLocation(),
3016 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003017 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00003018
Douglas Gregor74e2fc32012-04-16 18:27:27 +00003019 // C++11 [class.mem]p2:
3020 // Within the class member-specification, the class is regarded as complete
Stephen Hines176edba2014-12-01 14:53:08 -08003021 // within function bodies, default arguments, exception-specifications, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00003022 // brace-or-equal-initializers for non-static data members (including such
3023 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00003024 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00003025 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00003026 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00003027 // declarations and the lexed inline method definitions, along with any
3028 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00003029 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00003030 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00003031 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00003032
3033 // We've finished with all pending member declarations.
3034 Actions.ActOnFinishCXXMemberDecls();
3035
Richard Smith7a614d82011-06-11 17:19:42 +00003036 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00003037 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00003038 PrevTokLocation = SavedPrevTokLocation;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003039
3040 // We've finished parsing everything, including default argument
3041 // initializers.
3042 Actions.ActOnFinishCXXMemberDefaultArgs(TagDecl);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00003043 }
3044
John McCall42a4f662010-05-28 08:11:17 +00003045 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003046 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3047 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00003048
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00003049 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00003050 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00003051 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00003052}
Douglas Gregor7ad83902008-11-05 04:29:56 +00003053
Richard Smith7faf81f2013-11-15 23:00:02 +00003054void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithb3104392013-11-09 04:52:51 +00003055 assert(Tok.is(tok::kw_namespace));
3056
3057 // FIXME: Suggest where the close brace should have gone by looking
3058 // at indentation changes within the definition body.
Richard Smith7faf81f2013-11-15 23:00:02 +00003059 Diag(D->getLocation(),
3060 diag::err_missing_end_of_definition) << D;
Richard Smithb3104392013-11-09 04:52:51 +00003061 Diag(Tok.getLocation(),
Richard Smith7faf81f2013-11-15 23:00:02 +00003062 diag::note_missing_end_of_definition_before) << D;
Richard Smithb3104392013-11-09 04:52:51 +00003063
3064 // Push '};' onto the token stream to recover.
3065 PP.EnterToken(Tok);
3066
3067 Tok.startToken();
3068 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
3069 Tok.setKind(tok::semi);
3070 PP.EnterToken(Tok);
3071
3072 Tok.setKind(tok::r_brace);
3073}
3074
Douglas Gregor7ad83902008-11-05 04:29:56 +00003075/// ParseConstructorInitializer - Parse a C++ constructor initializer,
3076/// which explicitly initializes the members or base classes of a
3077/// class (C++ [class.base.init]). For example, the three initializers
3078/// after the ':' in the Derived constructor below:
3079///
3080/// @code
3081/// class Base { };
3082/// class Derived : Base {
3083/// int x;
3084/// float f;
3085/// public:
3086/// Derived(float f) : Base(), x(17), f(f) { }
3087/// };
3088/// @endcode
3089///
Mike Stump1eb44332009-09-09 15:08:12 +00003090/// [C++] ctor-initializer:
3091/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00003092///
Mike Stump1eb44332009-09-09 15:08:12 +00003093/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00003094/// mem-initializer ...[opt]
3095/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00003096void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003097 assert(Tok.is(tok::colon) &&
3098 "Constructor initializer always starts with ':'");
Douglas Gregor7ad83902008-11-05 04:29:56 +00003099
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07003100 // Poison the SEH identifiers so they are flagged as illegal in constructor
3101 // initializers.
John Wiegley28bbe4b2011-04-28 01:08:34 +00003102 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00003103 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003104
Chris Lattner5f9e2722011-07-23 10:55:15 +00003105 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003106 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003107
Douglas Gregor7ad83902008-11-05 04:29:56 +00003108 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00003109 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko572cf582013-06-23 22:58:02 +00003110 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
3111 MemInitializers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003112 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00003113 } else {
3114 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
3115 if (!MemInit.isInvalid())
3116 MemInitializers.push_back(MemInit.get());
3117 else
3118 AnyErrors = true;
3119 }
3120
Douglas Gregor7ad83902008-11-05 04:29:56 +00003121 if (Tok.is(tok::comma))
3122 ConsumeToken();
3123 else if (Tok.is(tok::l_brace))
3124 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00003125 // If the next token looks like a base or member initializer, assume that
3126 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00003127 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
3128 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3129 Diag(Loc, diag::err_ctor_init_missing_comma)
3130 << FixItHint::CreateInsertion(Loc, ", ");
3131 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00003132 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Stephen Hines651f13c2014-04-23 16:59:28 -07003133 Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
3134 << tok::comma;
Alexey Bataev8fe24752013-11-18 08:17:37 +00003135 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor7ad83902008-11-05 04:29:56 +00003136 break;
3137 }
3138 } while (true);
3139
David Blaikie93c86172013-01-17 05:26:25 +00003140 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00003141 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00003142}
3143
3144/// ParseMemInitializer - Parse a C++ member initializer, which is
3145/// part of a constructor initializer that explicitly initializes one
3146/// member or base class (C++ [class.base.init]). See
3147/// ParseConstructorInitializer for an example.
3148///
3149/// [C++] mem-initializer:
3150/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00003151/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00003152///
Douglas Gregor7ad83902008-11-05 04:29:56 +00003153/// [C++] mem-initializer-id:
3154/// '::'[opt] nested-name-specifier[opt] class-name
3155/// identifier
Stephen Hines176edba2014-12-01 14:53:08 -08003156MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00003157 // parse '::'[opt] nested-name-specifier[opt]
3158 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003159 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00003160 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00003161 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00003162 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00003163 if (TemplateId->Kind == TNK_Type_template ||
3164 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00003165 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00003166 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00003167 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00003168 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00003169 }
David Blaikief2116622012-01-24 06:03:59 +00003170 // Uses of decltype will already have been converted to annot_decltype by
3171 // ParseOptionalCXXScopeSpecifier at this point.
3172 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
3173 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00003174 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00003175 return true;
3176 }
Mike Stump1eb44332009-09-09 15:08:12 +00003177
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003178 IdentifierInfo *II = nullptr;
David Blaikief2116622012-01-24 06:03:59 +00003179 DeclSpec DS(AttrFactory);
3180 SourceLocation IdLoc = Tok.getLocation();
3181 if (Tok.is(tok::annot_decltype)) {
3182 // Get the decltype expression, if there is one.
3183 ParseDecltypeSpecifier(DS);
3184 } else {
3185 if (Tok.is(tok::identifier))
3186 // Get the identifier. This may be a member name or a class name,
3187 // but we'll let the semantic analysis determine which it is.
3188 II = Tok.getIdentifierInfo();
3189 ConsumeToken();
3190 }
3191
Douglas Gregor7ad83902008-11-05 04:29:56 +00003192
3193 // Parse the '('.
Richard Smith80ad52f2013-01-02 11:42:31 +00003194 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00003195 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3196
Sebastian Redl6df65482011-09-24 17:48:25 +00003197 ExprResult InitList = ParseBraceInitializer();
3198 if (InitList.isInvalid())
3199 return true;
3200
3201 SourceLocation EllipsisLoc;
Stephen Hines651f13c2014-04-23 16:59:28 -07003202 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redl6df65482011-09-24 17:48:25 +00003203
3204 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00003205 TemplateTypeTy, DS, IdLoc,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003206 InitList.get(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00003207 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003208 BalancedDelimiterTracker T(*this, tok::l_paren);
3209 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00003210
Sebastian Redldbef1bb2011-06-05 12:23:16 +00003211 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00003212 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00003213 CommaLocsTy CommaLocs;
3214 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00003215 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00003216 return true;
3217 }
3218
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003219 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00003220
3221 SourceLocation EllipsisLoc;
Stephen Hines651f13c2014-04-23 16:59:28 -07003222 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00003223
3224 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00003225 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenkoa36bbac2013-05-09 23:51:52 +00003226 T.getOpenLocation(), ArgExprs,
3227 T.getCloseLocation(), EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00003228 }
3229
Stephen Hines651f13c2014-04-23 16:59:28 -07003230 if (getLangOpts().CPlusPlus11)
3231 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
3232 else
3233 return Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregor7ad83902008-11-05 04:29:56 +00003234}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003235
Sebastian Redl7acafd02011-03-05 14:45:16 +00003236/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003237///
Douglas Gregora4745612008-12-01 18:00:20 +00003238/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00003239/// dynamic-exception-specification
3240/// noexcept-specification
3241///
3242/// noexcept-specification:
3243/// 'noexcept'
3244/// 'noexcept' '(' constant-expression ')'
3245ExceptionSpecificationType
Stephen Hines176edba2014-12-01 14:53:08 -08003246Parser::tryParseExceptionSpecification(bool Delayed,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00003247 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003248 SmallVectorImpl<ParsedType> &DynamicExceptions,
3249 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Stephen Hines176edba2014-12-01 14:53:08 -08003250 ExprResult &NoexceptExpr,
3251 CachedTokens *&ExceptionSpecTokens) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00003252 ExceptionSpecificationType Result = EST_None;
Stephen Hines176edba2014-12-01 14:53:08 -08003253 ExceptionSpecTokens = 0;
3254
3255 // Handle delayed parsing of exception-specifications.
3256 if (Delayed) {
3257 if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept))
3258 return EST_None;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003259
Stephen Hines176edba2014-12-01 14:53:08 -08003260 // Consume and cache the starting token.
3261 bool IsNoexcept = Tok.is(tok::kw_noexcept);
3262 Token StartTok = Tok;
3263 SpecificationRange = SourceRange(ConsumeToken());
3264
3265 // Check for a '('.
3266 if (!Tok.is(tok::l_paren)) {
3267 // If this is a bare 'noexcept', we're done.
3268 if (IsNoexcept) {
3269 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3270 NoexceptExpr = 0;
3271 return EST_BasicNoexcept;
3272 }
3273
3274 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3275 return EST_DynamicNone;
3276 }
3277
3278 // Cache the tokens for the exception-specification.
3279 ExceptionSpecTokens = new CachedTokens;
3280 ExceptionSpecTokens->push_back(StartTok); // 'throw' or 'noexcept'
3281 ExceptionSpecTokens->push_back(Tok); // '('
3282 SpecificationRange.setEnd(ConsumeParen()); // '('
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003283
3284 ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens,
3285 /*StopAtSemi=*/true,
3286 /*ConsumeFinalToken=*/true);
Stephen Hines176edba2014-12-01 14:53:08 -08003287 SpecificationRange.setEnd(Tok.getLocation());
Stephen Hines176edba2014-12-01 14:53:08 -08003288 return EST_Unparsed;
3289 }
3290
Sebastian Redl7acafd02011-03-05 14:45:16 +00003291 // See if there's a dynamic specification.
3292 if (Tok.is(tok::kw_throw)) {
3293 Result = ParseDynamicExceptionSpecification(SpecificationRange,
3294 DynamicExceptions,
3295 DynamicExceptionRanges);
3296 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
3297 "Produced different number of exception types and ranges.");
3298 }
3299
3300 // If there's no noexcept specification, we're done.
3301 if (Tok.isNot(tok::kw_noexcept))
3302 return Result;
3303
Richard Smith841804b2011-10-17 23:06:20 +00003304 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3305
Sebastian Redl7acafd02011-03-05 14:45:16 +00003306 // If we already had a dynamic specification, parse the noexcept for,
3307 // recovery, but emit a diagnostic and don't store the results.
3308 SourceRange NoexceptRange;
3309 ExceptionSpecificationType NoexceptType = EST_None;
3310
3311 SourceLocation KeywordLoc = ConsumeToken();
3312 if (Tok.is(tok::l_paren)) {
3313 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003314 BalancedDelimiterTracker T(*this, tok::l_paren);
3315 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00003316 NoexceptType = EST_ComputedNoexcept;
3317 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00003318 // The argument must be contextually convertible to bool. We use
3319 // ActOnBooleanCondition for this purpose.
3320 if (!NoexceptExpr.isInvalid())
3321 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
3322 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003323 T.consumeClose();
3324 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00003325 } else {
3326 // There is no argument.
3327 NoexceptType = EST_BasicNoexcept;
3328 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
3329 }
3330
3331 if (Result == EST_None) {
3332 SpecificationRange = NoexceptRange;
3333 Result = NoexceptType;
3334
3335 // If there's a dynamic specification after a noexcept specification,
3336 // parse that and ignore the results.
3337 if (Tok.is(tok::kw_throw)) {
3338 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3339 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
3340 DynamicExceptionRanges);
3341 }
3342 } else {
3343 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3344 }
3345
3346 return Result;
3347}
3348
Richard Smith79f4bb72013-06-13 02:02:51 +00003349static void diagnoseDynamicExceptionSpecification(
3350 Parser &P, const SourceRange &Range, bool IsNoexcept) {
3351 if (P.getLangOpts().CPlusPlus11) {
3352 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
3353 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
3354 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
3355 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
3356 }
3357}
3358
Sebastian Redl7acafd02011-03-05 14:45:16 +00003359/// ParseDynamicExceptionSpecification - Parse a C++
3360/// dynamic-exception-specification (C++ [except.spec]).
3361///
3362/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00003363/// 'throw' '(' type-id-list [opt] ')'
3364/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00003365///
Douglas Gregora4745612008-12-01 18:00:20 +00003366/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00003367/// type-id ... [opt]
3368/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003369///
Sebastian Redl7acafd02011-03-05 14:45:16 +00003370ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
3371 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003372 SmallVectorImpl<ParsedType> &Exceptions,
3373 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003374 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00003375
Sebastian Redl7acafd02011-03-05 14:45:16 +00003376 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003377 BalancedDelimiterTracker T(*this, tok::l_paren);
3378 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00003379 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3380 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00003381 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003382 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003383
Douglas Gregora4745612008-12-01 18:00:20 +00003384 // Parse throw(...), a Microsoft extension that means "this function
3385 // can throw anything".
3386 if (Tok.is(tok::ellipsis)) {
3387 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00003388 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00003389 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003390 T.consumeClose();
3391 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith79f4bb72013-06-13 02:02:51 +00003392 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003393 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00003394 }
3395
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003396 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00003397 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003398 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00003399 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00003400
Douglas Gregora04426c2010-12-20 23:57:46 +00003401 if (Tok.is(tok::ellipsis)) {
3402 // C++0x [temp.variadic]p5:
3403 // - In a dynamic-exception-specification (15.4); the pattern is a
3404 // type-id.
3405 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00003406 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00003407 if (!Res.isInvalid())
3408 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3409 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00003410
Sebastian Redlef65f062009-05-29 18:02:33 +00003411 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00003412 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00003413 Ranges.push_back(Range);
3414 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003415
3416 if (!TryConsumeToken(tok::comma))
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003417 break;
3418 }
3419
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003420 T.consumeClose();
3421 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith79f4bb72013-06-13 02:02:51 +00003422 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3423 Exceptions.empty());
Sebastian Redl60618fa2011-03-12 11:50:43 +00003424 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003425}
Douglas Gregor6569d682009-05-27 23:11:45 +00003426
Douglas Gregordab60ad2010-10-01 18:44:50 +00003427/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3428/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00003429TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00003430 assert(Tok.is(tok::arrow) && "expected arrow");
3431
3432 ConsumeToken();
3433
Richard Smith7796eb52012-03-12 08:56:40 +00003434 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00003435}
3436
Douglas Gregor6569d682009-05-27 23:11:45 +00003437/// \brief We have just started parsing the definition of a new class,
3438/// so push that class onto our stack of classes that is currently
3439/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00003440Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00003441Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3442 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00003443 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00003444 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00003445 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00003446 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00003447}
3448
3449/// \brief Deallocate the given parsed class and all of its nested
3450/// classes.
3451void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00003452 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3453 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00003454 delete Class;
3455}
3456
3457/// \brief Pop the top class of the stack of classes that are
3458/// currently being parsed.
3459///
3460/// This routine should be called when we have finished parsing the
3461/// definition of a class, but have not yet popped the Scope
3462/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00003463void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00003464 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00003465
John McCalleee1d542011-02-14 07:13:47 +00003466 Actions.PopParsingClass(state);
3467
Douglas Gregor6569d682009-05-27 23:11:45 +00003468 ParsingClass *Victim = ClassStack.top();
3469 ClassStack.pop();
3470 if (Victim->TopLevelClass) {
3471 // Deallocate all of the nested classes of this class,
3472 // recursively: we don't need to keep any of this information.
3473 DeallocateParsedClasses(Victim);
3474 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003475 }
Douglas Gregor6569d682009-05-27 23:11:45 +00003476 assert(!ClassStack.empty() && "Missing top-level class?");
3477
Douglas Gregord54eb442010-10-12 16:25:54 +00003478 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00003479 // The victim is a nested class, but we will not need to perform
3480 // any processing after the definition of this class since it has
3481 // no members whose handling was delayed. Therefore, we can just
3482 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00003483 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00003484 return;
3485 }
3486
3487 // This nested class has some members that will need to be processed
3488 // after the top-level class is completely defined. Therefore, add
3489 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003490 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00003491 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00003492 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00003493}
Sean Huntbbd37c62009-11-21 08:43:09 +00003494
Richard Smithc56298d2012-04-10 03:25:07 +00003495/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3496///
3497/// \return the parsed identifier on success, and 0 if the next token is not an
3498/// attribute-token.
3499///
3500/// C++11 [dcl.attr.grammar]p3:
3501/// If a keyword or an alternative token that satisfies the syntactic
3502/// requirements of an identifier is contained in an attribute-token,
3503/// it is considered an identifier.
3504IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3505 switch (Tok.getKind()) {
3506 default:
3507 // Identifiers and keywords have identifier info attached.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003508 if (!Tok.isAnnotation()) {
3509 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3510 Loc = ConsumeToken();
3511 return II;
3512 }
Richard Smithc56298d2012-04-10 03:25:07 +00003513 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003514 return nullptr;
Richard Smithc56298d2012-04-10 03:25:07 +00003515
3516 case tok::ampamp: // 'and'
3517 case tok::pipe: // 'bitor'
3518 case tok::pipepipe: // 'or'
3519 case tok::caret: // 'xor'
3520 case tok::tilde: // 'compl'
3521 case tok::amp: // 'bitand'
3522 case tok::ampequal: // 'and_eq'
3523 case tok::pipeequal: // 'or_eq'
3524 case tok::caretequal: // 'xor_eq'
3525 case tok::exclaim: // 'not'
3526 case tok::exclaimequal: // 'not_eq'
3527 // Alternative tokens do not have identifier info, but their spelling
3528 // starts with an alphabetical character.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003529 SmallString<8> SpellingBuf;
Richard Smithc56298d2012-04-10 03:25:07 +00003530 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rose3f6f51e2013-02-08 22:30:41 +00003531 if (isLetter(Spelling[0])) {
Richard Smithc56298d2012-04-10 03:25:07 +00003532 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00003533 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00003534 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003535 return nullptr;
Richard Smithc56298d2012-04-10 03:25:07 +00003536 }
3537}
3538
Michael Han6880f492012-10-03 01:56:22 +00003539static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3540 IdentifierInfo *ScopeName) {
3541 switch (AttributeList::getKind(AttrName, ScopeName,
3542 AttributeList::AS_CXX11)) {
3543 case AttributeList::AT_CarriesDependency:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003544 case AttributeList::AT_Deprecated:
Michael Han6880f492012-10-03 01:56:22 +00003545 case AttributeList::AT_FallThrough:
Richard Smithcd8ab512013-01-17 01:30:42 +00003546 case AttributeList::AT_CXX11NoReturn: {
Michael Han6880f492012-10-03 01:56:22 +00003547 return true;
3548 }
3549
3550 default:
3551 return false;
3552 }
3553}
3554
Stephen Hines651f13c2014-04-23 16:59:28 -07003555/// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause.
3556///
3557/// [C++11] attribute-argument-clause:
3558/// '(' balanced-token-seq ')'
3559///
3560/// [C++11] balanced-token-seq:
3561/// balanced-token
3562/// balanced-token-seq balanced-token
3563///
3564/// [C++11] balanced-token:
3565/// '(' balanced-token-seq ')'
3566/// '[' balanced-token-seq ']'
3567/// '{' balanced-token-seq '}'
3568/// any token but '(', ')', '[', ']', '{', or '}'
3569bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName,
3570 SourceLocation AttrNameLoc,
3571 ParsedAttributes &Attrs,
3572 SourceLocation *EndLoc,
3573 IdentifierInfo *ScopeName,
3574 SourceLocation ScopeLoc) {
3575 assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003576 SourceLocation LParenLoc = Tok.getLocation();
Stephen Hines651f13c2014-04-23 16:59:28 -07003577
3578 // If the attribute isn't known, we will not attempt to parse any
3579 // arguments.
3580 if (!hasAttribute(AttrSyntax::CXX, ScopeName, AttrName,
3581 getTargetInfo().getTriple(), getLangOpts())) {
3582 // Eat the left paren, then skip to the ending right paren.
3583 ConsumeParen();
3584 SkipUntil(tok::r_paren);
3585 return false;
3586 }
3587
3588 if (ScopeName && ScopeName->getName() == "gnu")
3589 // GNU-scoped attributes have some special cases to handle GNU-specific
3590 // behaviors.
3591 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003592 ScopeLoc, AttributeList::AS_CXX11, nullptr);
3593 else {
3594 unsigned NumArgs =
3595 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
3596 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3597
3598 const AttributeList *Attr = Attrs.getList();
3599 if (Attr && IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) {
3600 // If the attribute is a standard or built-in attribute and we are
3601 // parsing an argument list, we need to determine whether this attribute
3602 // was allowed to have an argument list (such as [[deprecated]]), and how
3603 // many arguments were parsed (so we can diagnose on [[deprecated()]]).
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003604 if (Attr->getMaxArgs() && !NumArgs) {
3605 // The attribute was allowed to have arguments, but none were provided
3606 // even though the attribute parsed successfully. This is an error.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003607 Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003608 } else if (!Attr->getMaxArgs()) {
3609 // The attribute parsed successfully, but was not allowed to have any
3610 // arguments. It doesn't matter whether any were provided -- the
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003611 // presence of the argument list (even if empty) is diagnosed.
3612 Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003613 << AttrName
3614 << FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc));
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003615 }
3616 }
3617 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003618 return true;
3619}
3620
3621/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier.
Sean Huntbbd37c62009-11-21 08:43:09 +00003622///
Richard Smith6ee326a2012-04-10 01:32:12 +00003623/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00003624/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003625/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00003626///
Richard Smith6ee326a2012-04-10 01:32:12 +00003627/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00003628/// attribute[opt]
3629/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00003630/// attribute '...'
3631/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00003632///
Richard Smith6ee326a2012-04-10 01:32:12 +00003633/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00003634/// attribute-token attribute-argument-clause[opt]
3635///
Richard Smith6ee326a2012-04-10 01:32:12 +00003636/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003637/// identifier
3638/// attribute-scoped-token
3639///
Richard Smith6ee326a2012-04-10 01:32:12 +00003640/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003641/// attribute-namespace '::' identifier
3642///
Richard Smith6ee326a2012-04-10 01:32:12 +00003643/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00003644/// identifier
Richard Smithc56298d2012-04-10 03:25:07 +00003645void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003646 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003647 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00003648 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003649 ParseAlignmentSpecifier(attrs, endLoc);
3650 return;
3651 }
3652
Sean Huntbbd37c62009-11-21 08:43:09 +00003653 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00003654 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00003655
Richard Smith41be6732011-10-14 20:48:27 +00003656 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3657
Sean Huntbbd37c62009-11-21 08:43:09 +00003658 ConsumeBracket();
3659 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003660
Richard Smithcd8ab512013-01-17 01:30:42 +00003661 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3662
Richard Smithc56298d2012-04-10 03:25:07 +00003663 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003664 // attribute not present
Stephen Hines651f13c2014-04-23 16:59:28 -07003665 if (TryConsumeToken(tok::comma))
Sean Huntbbd37c62009-11-21 08:43:09 +00003666 continue;
Sean Huntbbd37c62009-11-21 08:43:09 +00003667
Richard Smithc56298d2012-04-10 03:25:07 +00003668 SourceLocation ScopeLoc, AttrLoc;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003669 IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr;
Richard Smithc56298d2012-04-10 03:25:07 +00003670
3671 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3672 if (!AttrName)
3673 // Break out to the "expected ']'" diagnostic.
3674 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003675
Sean Huntbbd37c62009-11-21 08:43:09 +00003676 // scoped attribute
Stephen Hines651f13c2014-04-23 16:59:28 -07003677 if (TryConsumeToken(tok::coloncolon)) {
Richard Smithc56298d2012-04-10 03:25:07 +00003678 ScopeName = AttrName;
3679 ScopeLoc = AttrLoc;
3680
3681 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3682 if (!AttrName) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003683 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexey Bataev8fe24752013-11-18 08:17:37 +00003684 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Sean Huntbbd37c62009-11-21 08:43:09 +00003685 continue;
3686 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003687 }
3688
Stephen Hines651f13c2014-04-23 16:59:28 -07003689 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00003690 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00003691
Richard Smithcd8ab512013-01-17 01:30:42 +00003692 if (StandardAttr &&
3693 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3694 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
Stephen Hines651f13c2014-04-23 16:59:28 -07003695 << AttrName << SourceRange(SeenAttrs[AttrName]);
Richard Smithcd8ab512013-01-17 01:30:42 +00003696
Michael Han6880f492012-10-03 01:56:22 +00003697 // Parse attribute arguments
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003698 if (Tok.is(tok::l_paren))
Stephen Hines651f13c2014-04-23 16:59:28 -07003699 AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3700 ScopeName, ScopeLoc);
Michael Han6880f492012-10-03 01:56:22 +00003701
3702 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003703 attrs.addNew(AttrName,
3704 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3705 AttrLoc),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003706 ScopeName, ScopeLoc, nullptr, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003707
Stephen Hines651f13c2014-04-23 16:59:28 -07003708 if (TryConsumeToken(tok::ellipsis))
Michael Han6880f492012-10-03 01:56:22 +00003709 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3710 << AttrName->getName();
Sean Huntbbd37c62009-11-21 08:43:09 +00003711 }
3712
Stephen Hines651f13c2014-04-23 16:59:28 -07003713 if (ExpectAndConsume(tok::r_square))
Alexey Bataev8fe24752013-11-18 08:17:37 +00003714 SkipUntil(tok::r_square);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003715 if (endLoc)
3716 *endLoc = Tok.getLocation();
Stephen Hines651f13c2014-04-23 16:59:28 -07003717 if (ExpectAndConsume(tok::r_square))
Alexey Bataev8fe24752013-11-18 08:17:37 +00003718 SkipUntil(tok::r_square);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003719}
Sean Huntbbd37c62009-11-21 08:43:09 +00003720
Sean Hunt2edf0a22012-06-23 05:07:58 +00003721/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003722///
3723/// attribute-specifier-seq:
3724/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003725void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003726 SourceLocation *endLoc) {
Richard Smith672edb02013-02-22 09:15:49 +00003727 assert(getLangOpts().CPlusPlus11);
3728
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003729 SourceLocation StartLoc = Tok.getLocation(), Loc;
3730 if (!endLoc)
3731 endLoc = &Loc;
3732
Douglas Gregor8828ee72011-10-07 20:35:25 +00003733 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003734 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003735 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003736
3737 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003738}
3739
Richard Smith5eed7e02013-10-15 01:34:54 +00003740void Parser::DiagnoseAndSkipCXX11Attributes() {
Richard Smith5eed7e02013-10-15 01:34:54 +00003741 // Start and end location of an attribute or an attribute list.
3742 SourceLocation StartLoc = Tok.getLocation();
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003743 SourceLocation EndLoc = SkipCXX11Attributes();
3744
3745 if (EndLoc.isValid()) {
3746 SourceRange Range(StartLoc, EndLoc);
3747 Diag(StartLoc, diag::err_attributes_not_allowed)
3748 << Range;
3749 }
3750}
3751
3752SourceLocation Parser::SkipCXX11Attributes() {
Richard Smith5eed7e02013-10-15 01:34:54 +00003753 SourceLocation EndLoc;
3754
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003755 if (!isCXX11AttributeSpecifier())
3756 return EndLoc;
3757
Richard Smith5eed7e02013-10-15 01:34:54 +00003758 do {
3759 if (Tok.is(tok::l_square)) {
3760 BalancedDelimiterTracker T(*this, tok::l_square);
3761 T.consumeOpen();
3762 T.skipToEnd();
3763 EndLoc = T.getCloseLocation();
3764 } else {
3765 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3766 ConsumeToken();
3767 BalancedDelimiterTracker T(*this, tok::l_paren);
3768 if (!T.consumeOpen())
3769 T.skipToEnd();
3770 EndLoc = T.getCloseLocation();
3771 }
3772 } while (isCXX11AttributeSpecifier());
3773
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003774 return EndLoc;
Richard Smith5eed7e02013-10-15 01:34:54 +00003775}
3776
Francois Pichet334d47e2010-10-11 12:59:39 +00003777/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3778///
3779/// [MS] ms-attribute:
3780/// '[' token-seq ']'
3781///
3782/// [MS] ms-attribute-seq:
3783/// ms-attribute[opt]
3784/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003785void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3786 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003787 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3788
3789 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003790 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003791 ConsumeBracket();
Alexey Bataev8fe24752013-11-18 08:17:37 +00003792 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
John McCall7f040a92010-12-24 02:08:15 +00003793 if (endLoc) *endLoc = Tok.getLocation();
Stephen Hines651f13c2014-04-23 16:59:28 -07003794 ExpectAndConsume(tok::r_square);
Francois Pichet334d47e2010-10-11 12:59:39 +00003795 }
3796}
Francois Pichet563a6452011-05-25 10:19:49 +00003797
3798void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3799 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003800 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003801 if (ParseMicrosoftIfExistsCondition(Result))
3802 return;
3803
Douglas Gregor3896fc52011-10-24 22:31:10 +00003804 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3805 if (Braces.consumeOpen()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003806 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet563a6452011-05-25 10:19:49 +00003807 return;
3808 }
Francois Pichet563a6452011-05-25 10:19:49 +00003809
Douglas Gregor3896fc52011-10-24 22:31:10 +00003810 switch (Result.Behavior) {
3811 case IEB_Parse:
3812 // Parse the declarations below.
3813 break;
3814
3815 case IEB_Dependent:
3816 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3817 << Result.IsIfExists;
3818 // Fall through to skip.
3819
3820 case IEB_Skip:
3821 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003822 return;
3823 }
3824
Stephen Hines651f13c2014-04-23 16:59:28 -07003825 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003826 // __if_exists, __if_not_exists can nest.
3827 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3828 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3829 continue;
3830 }
3831
3832 // Check for extraneous top-level semicolon.
3833 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003834 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003835 continue;
3836 }
3837
3838 AccessSpecifier AS = getAccessSpecifierIfPresent();
3839 if (AS != AS_none) {
3840 // Current token is a C++ access specifier.
3841 CurAS = AS;
3842 SourceLocation ASLoc = Tok.getLocation();
3843 ConsumeToken();
3844 if (Tok.is(tok::colon))
3845 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3846 else
Stephen Hines651f13c2014-04-23 16:59:28 -07003847 Diag(Tok, diag::err_expected) << tok::colon;
Francois Pichet563a6452011-05-25 10:19:49 +00003848 ConsumeToken();
3849 continue;
3850 }
3851
3852 // Parse all the comma separated declarators.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003853 ParseCXXClassMemberDeclaration(CurAS, nullptr);
Francois Pichet563a6452011-05-25 10:19:49 +00003854 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003855
3856 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003857}