blob: 1a0d262279f2c4db178b14eafc9488d2e029193f [file] [log] [blame]
Chris Lattnera5235172007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera5235172007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor423984d2008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Erik Verbruggen888d52a2014-01-15 09:15:43 +000016#include "clang/AST/ASTContext.h"
Chandler Carruth757fcd62014-03-04 10:05:20 +000017#include "clang/AST/DeclTemplate.h"
Aaron Ballmanb8e20392014-03-31 17:32:39 +000018#include "clang/Basic/Attributes.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000019#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Basic/OperatorKinds.h"
Chandler Carruth0d9593d2015-01-14 11:29:14 +000021#include "clang/Basic/TargetInfo.h"
Chris Lattner60f36222009-01-29 05:15:15 +000022#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000023#include "clang/Sema/DeclSpec.h"
John McCall8b0666c2010-08-20 18:27:03 +000024#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000025#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "clang/Sema/Scope.h"
John McCalldb632ac2012-09-25 07:32:39 +000027#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000028#include "llvm/ADT/SmallString.h"
Chris Lattnera5235172007-08-25 06:57:03 +000029using namespace clang;
30
31/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redl67667942010-08-27 23:12:46 +000032/// may either be a top level namespace or a block-level namespace alias. If
33/// there was an inline keyword, it has already been parsed.
Chris Lattnera5235172007-08-25 06:57:03 +000034///
35/// namespace-definition: [C++ 7.3: basic.namespace]
36/// named-namespace-definition
37/// unnamed-namespace-definition
38///
39/// unnamed-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000040/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000041///
42/// named-namespace-definition:
43/// original-namespace-definition
44/// extension-namespace-definition
45///
46/// original-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000047/// 'inline'[opt] 'namespace' identifier attributes[opt]
48/// '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000049///
50/// extension-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000051/// 'inline'[opt] 'namespace' original-namespace-name
52/// '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000053///
Chris Lattnera5235172007-08-25 06:57:03 +000054/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
55/// 'namespace' identifier '=' qualified-namespace-specifier ';'
56///
John McCall48871652010-08-21 09:40:31 +000057Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redl67667942010-08-27 23:12:46 +000058 SourceLocation &DeclEnd,
59 SourceLocation InlineLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000060 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000061 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian4bf82622011-08-22 17:59:19 +000062 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000063
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000064 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000065 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000066 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +000067 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000068 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000069
Chris Lattnera5235172007-08-25 06:57:03 +000070 SourceLocation IdentLoc;
Craig Topper161e4db2014-05-21 06:02:52 +000071 IdentifierInfo *Ident = nullptr;
Richard Trieu61384cb2011-05-26 20:11:09 +000072 std::vector<SourceLocation> ExtraIdentLoc;
73 std::vector<IdentifierInfo*> ExtraIdent;
74 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000075
Aaron Ballman730476b2014-11-08 15:33:35 +000076 ParsedAttributesWithRange attrs(AttrFactory);
77 SourceLocation attrLoc;
78 if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) {
79 if (!getLangOpts().CPlusPlus1z)
Aaron Ballmanc0ae7df2014-11-08 17:07:15 +000080 Diag(Tok.getLocation(), diag::warn_cxx14_compat_attribute)
81 << 0 /*namespace*/;
Aaron Ballman730476b2014-11-08 15:33:35 +000082 attrLoc = Tok.getLocation();
83 ParseCXX11Attributes(attrs);
84 }
Mike Stump11289f42009-09-09 15:08:12 +000085
Chris Lattner76c72282007-10-09 17:33:22 +000086 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000087 Ident = Tok.getIdentifierInfo();
88 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieu61384cb2011-05-26 20:11:09 +000089 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
90 ExtraNamespaceLoc.push_back(ConsumeToken());
91 ExtraIdent.push_back(Tok.getIdentifierInfo());
92 ExtraIdentLoc.push_back(ConsumeToken());
93 }
Chris Lattnera5235172007-08-25 06:57:03 +000094 }
Mike Stump11289f42009-09-09 15:08:12 +000095
Aaron Ballmanc0ae7df2014-11-08 17:07:15 +000096 // A nested namespace definition cannot have attributes.
97 if (!ExtraNamespaceLoc.empty() && attrLoc.isValid())
98 Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute);
99
Chris Lattnera5235172007-08-25 06:57:03 +0000100 // Read label attributes, if present.
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000101 if (Tok.is(tok::kw___attribute)) {
Aaron Ballman730476b2014-11-08 15:33:35 +0000102 attrLoc = Tok.getLocation();
John McCall53fa7142010-12-24 02:08:15 +0000103 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000104 }
Mike Stump11289f42009-09-09 15:08:12 +0000105
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000106 if (Tok.is(tok::equal)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000107 if (!Ident) {
Alp Tokerec543272013-12-24 09:48:30 +0000108 Diag(Tok, diag::err_expected) << tok::identifier;
Nico Weber729f1e22012-10-27 23:44:27 +0000109 // Skip to end of the definition and eat the ';'.
110 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000111 return nullptr;
Nico Weber729f1e22012-10-27 23:44:27 +0000112 }
Aaron Ballman730476b2014-11-08 15:33:35 +0000113 if (attrLoc.isValid())
114 Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +0000115 if (InlineLoc.isValid())
116 Diag(InlineLoc, diag::err_inline_namespace_alias)
117 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000118 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000119 }
Mike Stump11289f42009-09-09 15:08:12 +0000120
Richard Trieu61384cb2011-05-26 20:11:09 +0000121
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000122 BalancedDelimiterTracker T(*this, tok::l_brace);
123 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000124 if (Ident)
125 Diag(Tok, diag::err_expected) << tok::l_brace;
126 else
127 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Craig Topper161e4db2014-05-21 06:02:52 +0000128 return nullptr;
Chris Lattnera5235172007-08-25 06:57:03 +0000129 }
Mike Stump11289f42009-09-09 15:08:12 +0000130
Douglas Gregor0be31a22010-07-02 17:43:08 +0000131 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
132 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
133 getCurScope()->getFnParent()) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000134 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000135 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +0000136 return nullptr;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000137 }
138
Richard Smith13307f52014-11-08 05:37:34 +0000139 if (ExtraIdent.empty()) {
140 // Normal namespace definition, not a nested-namespace-definition.
141 } else if (InlineLoc.isValid()) {
142 Diag(InlineLoc, diag::err_inline_nested_namespace_definition);
143 } else if (getLangOpts().CPlusPlus1z) {
144 Diag(ExtraNamespaceLoc[0],
145 diag::warn_cxx14_compat_nested_namespace_definition);
146 } else {
Richard Trieu61384cb2011-05-26 20:11:09 +0000147 TentativeParsingAction TPA(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000148 SkipUntil(tok::r_brace, StopBeforeMatch);
Richard Trieu61384cb2011-05-26 20:11:09 +0000149 Token rBraceToken = Tok;
150 TPA.Revert();
151
152 if (!rBraceToken.is(tok::r_brace)) {
Richard Smith13307f52014-11-08 05:37:34 +0000153 Diag(ExtraNamespaceLoc[0], diag::ext_nested_namespace_definition)
Richard Trieu61384cb2011-05-26 20:11:09 +0000154 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
155 } else {
Benjamin Kramerf546f412011-05-26 21:32:30 +0000156 std::string NamespaceFix;
Richard Trieu61384cb2011-05-26 20:11:09 +0000157 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
158 E = ExtraIdent.end(); I != E; ++I) {
159 NamespaceFix += " { namespace ";
160 NamespaceFix += (*I)->getName();
161 }
Benjamin Kramerf546f412011-05-26 21:32:30 +0000162
Richard Trieu61384cb2011-05-26 20:11:09 +0000163 std::string RBraces;
Benjamin Kramerf546f412011-05-26 21:32:30 +0000164 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieu61384cb2011-05-26 20:11:09 +0000165 RBraces += "} ";
Benjamin Kramerf546f412011-05-26 21:32:30 +0000166
Richard Smith13307f52014-11-08 05:37:34 +0000167 Diag(ExtraNamespaceLoc[0], diag::ext_nested_namespace_definition)
Richard Trieu61384cb2011-05-26 20:11:09 +0000168 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
169 ExtraIdentLoc.back()),
170 NamespaceFix)
171 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
172 }
173 }
174
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000175 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith5d164bc2011-10-15 05:09:34 +0000176 if (InlineLoc.isValid())
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000177 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000178 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000179
Chris Lattner4de55aa2009-03-29 14:02:43 +0000180 // Enter a scope for the namespace.
181 ParseScope NamespaceScope(this, Scope::DeclScope);
182
John McCall48871652010-08-21 09:40:31 +0000183 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000184 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000185 IdentLoc, Ident, T.getOpenLocation(),
186 attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000187
John McCallfaf5fb42010-08-26 23:41:50 +0000188 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
189 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000190
Richard Trieu61384cb2011-05-26 20:11:09 +0000191 // Parse the contents of the namespace. This includes parsing recovery on
192 // any improperly nested namespaces.
193 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000194 InlineLoc, attrs, T);
Mike Stump11289f42009-09-09 15:08:12 +0000195
Chris Lattner4de55aa2009-03-29 14:02:43 +0000196 // Leave the namespace scope.
197 NamespaceScope.Exit();
198
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000199 DeclEnd = T.getCloseLocation();
200 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000201
202 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000203}
Chris Lattner38376f12008-01-12 07:05:38 +0000204
Richard Trieu61384cb2011-05-26 20:11:09 +0000205/// ParseInnerNamespace - Parse the contents of a namespace.
Richard Smith13307f52014-11-08 05:37:34 +0000206void Parser::ParseInnerNamespace(std::vector<SourceLocation> &IdentLoc,
207 std::vector<IdentifierInfo *> &Ident,
208 std::vector<SourceLocation> &NamespaceLoc,
209 unsigned int index, SourceLocation &InlineLoc,
210 ParsedAttributes &attrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000211 BalancedDelimiterTracker &Tracker) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000212 if (index == Ident.size()) {
Richard Smith34f30512013-11-23 04:06:09 +0000213 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000214 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000215 MaybeParseCXX11Attributes(attrs);
Richard Trieu61384cb2011-05-26 20:11:09 +0000216 MaybeParseMicrosoftAttributes(attrs);
217 ParseExternalDeclaration(attrs);
218 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000219
220 // The caller is what called check -- we are simply calling
221 // the close for it.
222 Tracker.consumeClose();
Richard Trieu61384cb2011-05-26 20:11:09 +0000223
224 return;
225 }
226
Richard Smith13307f52014-11-08 05:37:34 +0000227 // Handle a nested namespace definition.
228 // FIXME: Preserve the source information through to the AST rather than
229 // desugaring it here.
Richard Trieu61384cb2011-05-26 20:11:09 +0000230 ParseScope NamespaceScope(this, Scope::DeclScope);
231 Decl *NamespcDecl =
232 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
233 NamespaceLoc[index], IdentLoc[index],
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000234 Ident[index], Tracker.getOpenLocation(),
235 attrs.getList());
Richard Trieu61384cb2011-05-26 20:11:09 +0000236
237 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000238 attrs, Tracker);
Richard Trieu61384cb2011-05-26 20:11:09 +0000239
240 NamespaceScope.Exit();
241
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000242 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieu61384cb2011-05-26 20:11:09 +0000243}
244
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000245/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
246/// alias definition.
247///
John McCall48871652010-08-21 09:40:31 +0000248Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000249 SourceLocation AliasLoc,
250 IdentifierInfo *Alias,
251 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000252 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000253
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000254 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000255
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000256 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000257 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000258 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000259 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000260 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000261
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000262 CXXScopeSpec SS;
263 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000264 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000265
266 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
267 Diag(Tok, diag::err_expected_namespace_name);
268 // Skip to end of the definition and eat the ';'.
269 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000270 return nullptr;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000271 }
272
273 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000274 IdentifierInfo *Ident = Tok.getIdentifierInfo();
275 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000276
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000277 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000278 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000279 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
280 SkipUntil(tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000281
Douglas Gregor0be31a22010-07-02 17:43:08 +0000282 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000283 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000284}
285
Chris Lattner38376f12008-01-12 07:05:38 +0000286/// ParseLinkage - We know that the current token is a string_literal
287/// and just before that, that extern was seen.
288///
289/// linkage-specification: [C++ 7.5p2: dcl.link]
290/// 'extern' string-literal '{' declaration-seq[opt] '}'
291/// 'extern' string-literal declaration
292///
Chris Lattner8ea64422010-11-09 20:15:55 +0000293Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Richard Smith4ee696d2014-02-17 23:25:27 +0000294 assert(isTokenStringLiteral() && "Not a string literal!");
295 ExprResult Lang = ParseStringLiteralExpression(false);
Chris Lattner38376f12008-01-12 07:05:38 +0000296
Douglas Gregor07665a62009-01-05 19:45:36 +0000297 ParseScope LinkageScope(this, Scope::DeclScope);
Richard Smith4ee696d2014-02-17 23:25:27 +0000298 Decl *LinkageSpec =
299 Lang.isInvalid()
Craig Topper161e4db2014-05-21 06:02:52 +0000300 ? nullptr
Richard Smith4ee696d2014-02-17 23:25:27 +0000301 : Actions.ActOnStartLinkageSpecification(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000302 getCurScope(), DS.getSourceRange().getBegin(), Lang.get(),
Richard Smith4ee696d2014-02-17 23:25:27 +0000303 Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
Douglas Gregor07665a62009-01-05 19:45:36 +0000304
John McCall084e83d2011-03-24 11:26:52 +0000305 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000306 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000307 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000308
Douglas Gregor07665a62009-01-05 19:45:36 +0000309 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara4d423992011-05-01 16:25:54 +0000310 // Reset the source range in DS, as the leading "extern"
311 // does not really belong to the inner declaration ...
312 DS.SetRangeStart(SourceLocation());
313 DS.SetRangeEnd(SourceLocation());
314 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000315 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000316 ParseExternalDeclaration(attrs, &DS);
Richard Smith4ee696d2014-02-17 23:25:27 +0000317 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
318 getCurScope(), LinkageSpec, SourceLocation())
Craig Topper161e4db2014-05-21 06:02:52 +0000319 : nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000320 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000321
Douglas Gregorb65a9132010-02-07 08:38:28 +0000322 DS.abort();
323
John McCall53fa7142010-12-24 02:08:15 +0000324 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000325
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000326 BalancedDelimiterTracker T(*this, tok::l_brace);
327 T.consumeOpen();
Richard Smith77944862014-03-02 05:58:18 +0000328
329 unsigned NestedModules = 0;
330 while (true) {
331 switch (Tok.getKind()) {
332 case tok::annot_module_begin:
333 ++NestedModules;
334 ParseTopLevelDecl();
335 continue;
336
337 case tok::annot_module_end:
338 if (!NestedModules)
339 break;
340 --NestedModules;
341 ParseTopLevelDecl();
342 continue;
343
344 case tok::annot_module_include:
345 ParseTopLevelDecl();
346 continue;
347
348 case tok::eof:
349 break;
350
351 case tok::r_brace:
352 if (!NestedModules)
353 break;
354 // Fall through.
355 default:
356 ParsedAttributesWithRange attrs(AttrFactory);
357 MaybeParseCXX11Attributes(attrs);
358 MaybeParseMicrosoftAttributes(attrs);
359 ParseExternalDeclaration(attrs);
360 continue;
361 }
362
363 break;
Chris Lattner38376f12008-01-12 07:05:38 +0000364 }
365
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000366 T.consumeClose();
Richard Smith4ee696d2014-02-17 23:25:27 +0000367 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
368 getCurScope(), LinkageSpec, T.getCloseLocation())
Craig Topper161e4db2014-05-21 06:02:52 +0000369 : nullptr;
Chris Lattner38376f12008-01-12 07:05:38 +0000370}
Douglas Gregor556877c2008-04-13 21:30:24 +0000371
Douglas Gregord7c4d982008-12-30 03:27:21 +0000372/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
373/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000374Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000375 const ParsedTemplateInfo &TemplateInfo,
376 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000377 ParsedAttributesWithRange &attrs,
378 Decl **OwnedType) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000379 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000380 ObjCDeclContextSwitch ObjCDC(*this);
381
Douglas Gregord7c4d982008-12-30 03:27:21 +0000382 // Eat 'using'.
383 SourceLocation UsingLoc = ConsumeToken();
384
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000385 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000386 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000387 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000388 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000389 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000390
John McCall9b72f892010-11-10 02:40:36 +0000391 // 'using namespace' means this is a using-directive.
392 if (Tok.is(tok::kw_namespace)) {
393 // Template parameters are always an error here.
394 if (TemplateInfo.Kind) {
395 SourceRange R = TemplateInfo.getSourceRange();
396 Diag(UsingLoc, diag::err_templated_using_directive)
397 << R << FixItHint::CreateRemoval(R);
398 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000399
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000400 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000401 }
402
Richard Smithdda56e42011-04-15 14:24:37 +0000403 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000404
405 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000406 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000407
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000408 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000409 AS_none, OwnedType);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000410}
411
412/// ParseUsingDirective - Parse C++ using-directive, assumes
413/// that current token is 'namespace' and 'using' was already parsed.
414///
415/// using-directive: [C++ 7.3.p4: namespace.udir]
416/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
417/// namespace-name ;
418/// [GNU] using-directive:
419/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
420/// namespace-name attributes[opt] ;
421///
John McCall48871652010-08-21 09:40:31 +0000422Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000423 SourceLocation UsingLoc,
424 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000425 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000426 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
427
428 // Eat 'namespace'.
429 SourceLocation NamespcLoc = ConsumeToken();
430
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000431 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000432 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000433 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000434 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000435 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000436
Douglas Gregord7c4d982008-12-30 03:27:21 +0000437 CXXScopeSpec SS;
438 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000439 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000440
Craig Topper161e4db2014-05-21 06:02:52 +0000441 IdentifierInfo *NamespcName = nullptr;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000442 SourceLocation IdentLoc = SourceLocation();
443
444 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000445 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000446 Diag(Tok, diag::err_expected_namespace_name);
447 // If there was invalid namespace name, skip to end of decl, and eat ';'.
448 SkipUntil(tok::semi);
449 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Craig Topper161e4db2014-05-21 06:02:52 +0000450 return nullptr;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000451 }
Mike Stump11289f42009-09-09 15:08:12 +0000452
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000453 // Parse identifier.
454 NamespcName = Tok.getIdentifierInfo();
455 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000456
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000457 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000458 bool GNUAttr = false;
459 if (Tok.is(tok::kw___attribute)) {
460 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000461 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000462 }
Mike Stump11289f42009-09-09 15:08:12 +0000463
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000464 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000465 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000466 if (ExpectAndConsume(tok::semi,
467 GNUAttr ? diag::err_expected_semi_after_attribute_list
468 : diag::err_expected_semi_after_namespace_name))
469 SkipUntil(tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000470
Douglas Gregor0be31a22010-07-02 17:43:08 +0000471 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000472 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000473}
474
Richard Smithdda56e42011-04-15 14:24:37 +0000475/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
476/// Assumes that 'using' was already seen.
Douglas Gregord7c4d982008-12-30 03:27:21 +0000477///
478/// using-declaration: [C++ 7.3.p3: namespace.udecl]
479/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000480/// unqualified-id
481/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000482///
Richard Smith810ad3e2013-01-29 10:02:16 +0000483/// alias-declaration: C++11 [dcl.dcl]p1
484/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smithdda56e42011-04-15 14:24:37 +0000485///
John McCall48871652010-08-21 09:40:31 +0000486Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000487 const ParsedTemplateInfo &TemplateInfo,
488 SourceLocation UsingLoc,
489 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000490 AccessSpecifier AS,
491 Decl **OwnedType) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000492 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000493 SourceLocation TypenameLoc;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000494 bool HasTypenameKeyword = false;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000495
Richard Smithc2c8bb82013-10-15 01:34:54 +0000496 // Check for misplaced attributes before the identifier in an
497 // alias-declaration.
498 ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
499 MaybeParseCXX11Attributes(MisplacedAttrs);
Douglas Gregorfec52632009-06-20 00:51:54 +0000500
501 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000502 // FIXME: This is wrong; we should parse this as a typename-specifier.
Alp Toker97650562014-01-10 11:19:30 +0000503 if (TryConsumeToken(tok::kw_typename, TypenameLoc))
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000504 HasTypenameKeyword = true;
Douglas Gregorfec52632009-06-20 00:51:54 +0000505
Nikola Smiljanic67860242014-09-26 00:28:20 +0000506 if (Tok.is(tok::kw___super)) {
507 Diag(Tok.getLocation(), diag::err_super_in_using_declaration);
508 SkipUntil(tok::semi);
509 return nullptr;
510 }
511
Douglas Gregorfec52632009-06-20 00:51:54 +0000512 // Parse nested-name-specifier.
Craig Topper161e4db2014-05-21 06:02:52 +0000513 IdentifierInfo *LastII = nullptr;
Richard Smith7447af42013-03-26 01:15:19 +0000514 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
Craig Topper161e4db2014-05-21 06:02:52 +0000515 /*MayBePseudoDtor=*/nullptr,
516 /*IsTypename=*/false,
Richard Smith7447af42013-03-26 01:15:19 +0000517 /*LastII=*/&LastII);
Douglas Gregorfec52632009-06-20 00:51:54 +0000518
Douglas Gregorfec52632009-06-20 00:51:54 +0000519 // Check nested-name specifier.
520 if (SS.isInvalid()) {
521 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000522 return nullptr;
Douglas Gregorfec52632009-06-20 00:51:54 +0000523 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000524
Richard Smith7447af42013-03-26 01:15:19 +0000525 SourceLocation TemplateKWLoc;
526 UnqualifiedId Name;
527
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000528 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000529 // destructor names and allow the action module to diagnose any semantic
530 // errors.
Richard Smith7447af42013-03-26 01:15:19 +0000531 //
532 // C++11 [class.qual]p2:
533 // [...] in a using-declaration that is a member-declaration, if the name
534 // specified after the nested-name-specifier is the same as the identifier
535 // or the simple-template-id's template-name in the last component of the
536 // nested-name-specifier, the name is [...] considered to name the
537 // constructor.
538 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
539 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
540 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
541 !SS.getScopeRep()->getAsNamespace() &&
542 !SS.getScopeRep()->getAsNamespaceAlias()) {
543 SourceLocation IdLoc = ConsumeToken();
544 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
545 Name.setConstructorName(Type, IdLoc, IdLoc);
Richard Smith88fe69c2015-07-06 01:45:27 +0000546 } else if (ParseUnqualifiedId(
547 SS, /*EnteringContext=*/false,
548 /*AllowDestructorName=*/true,
Richard Smithc7ae3e02015-07-21 00:23:34 +0000549 /*AllowConstructorName=*/!(Tok.is(tok::identifier) &&
550 NextToken().is(tok::equal)),
Richard Smith88fe69c2015-07-06 01:45:27 +0000551 ParsedType(), TemplateKWLoc, Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000552 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000553 return nullptr;
Douglas Gregorfec52632009-06-20 00:51:54 +0000554 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000555
Richard Smithc2c8bb82013-10-15 01:34:54 +0000556 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith37a45dd2013-10-24 01:21:09 +0000557 MaybeParseGNUAttributes(Attrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000558 MaybeParseCXX11Attributes(Attrs);
Richard Smithdda56e42011-04-15 14:24:37 +0000559
560 // Maybe this is an alias-declaration.
Richard Smithdda56e42011-04-15 14:24:37 +0000561 TypeResult TypeAlias;
Richard Smithc2c8bb82013-10-15 01:34:54 +0000562 bool IsAliasDecl = Tok.is(tok::equal);
David Majnemerf9bde282015-03-11 06:45:39 +0000563 Decl *DeclFromDeclSpec = nullptr;
Richard Smithdda56e42011-04-15 14:24:37 +0000564 if (IsAliasDecl) {
Richard Smithc2c8bb82013-10-15 01:34:54 +0000565 // If we had any misplaced attributes from earlier, this is where they
566 // should have been written.
567 if (MisplacedAttrs.Range.isValid()) {
568 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
569 << FixItHint::CreateInsertionFromRange(
570 Tok.getLocation(),
571 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
572 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
573 Attrs.takeAllFrom(MisplacedAttrs);
574 }
575
Richard Smithdda56e42011-04-15 14:24:37 +0000576 ConsumeToken();
577
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000578 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000579 diag::warn_cxx98_compat_alias_declaration :
580 diag::ext_alias_declaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000581
Richard Smith3f1b5d02011-05-05 21:57:07 +0000582 // Type alias templates cannot be specialized.
583 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000584 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
585 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000586 SpecKind = 0;
587 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
588 SpecKind = 1;
589 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
590 SpecKind = 2;
591 if (SpecKind != -1) {
592 SourceRange Range;
593 if (SpecKind == 0)
594 Range = SourceRange(Name.TemplateId->LAngleLoc,
595 Name.TemplateId->RAngleLoc);
596 else
597 Range = TemplateInfo.getSourceRange();
598 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
599 << SpecKind << Range;
600 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000601 return nullptr;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000602 }
603
Richard Smithdda56e42011-04-15 14:24:37 +0000604 // Name must be an identifier.
605 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
606 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
607 // No removal fixit: can't recover from this.
608 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000609 return nullptr;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000610 } else if (HasTypenameKeyword)
Richard Smithdda56e42011-04-15 14:24:37 +0000611 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
612 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
613 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
614 else if (SS.isNotEmpty())
615 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
616 << FixItHint::CreateRemoval(SS.getRange());
617
David Majnemerf9bde282015-03-11 06:45:39 +0000618 TypeAlias = ParseTypeName(nullptr, TemplateInfo.Kind
619 ? Declarator::AliasTemplateContext
620 : Declarator::AliasDeclContext,
621 AS, &DeclFromDeclSpec, &Attrs);
622 if (OwnedType)
623 *OwnedType = DeclFromDeclSpec;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000624 } else {
625 // C++11 attributes are not allowed on a using-declaration, but GNU ones
626 // are.
Richard Smithc2c8bb82013-10-15 01:34:54 +0000627 ProhibitAttributes(MisplacedAttrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000628 ProhibitAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000629
Richard Smithdda56e42011-04-15 14:24:37 +0000630 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith54ecd982013-02-20 19:22:51 +0000631 MaybeParseGNUAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000632 }
Mike Stump11289f42009-09-09 15:08:12 +0000633
Douglas Gregorfec52632009-06-20 00:51:54 +0000634 // Eat ';'.
635 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000636 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
637 !Attrs.empty() ? "attributes list"
638 : IsAliasDecl ? "alias declaration"
639 : "using declaration"))
640 SkipUntil(tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000641
John McCall9b72f892010-11-10 02:40:36 +0000642 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000643 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000644 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000645 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-11-10 02:40:36 +0000646 SourceRange R = TemplateInfo.getSourceRange();
647 Diag(UsingLoc, diag::err_templated_using_declaration)
648 << R << FixItHint::CreateRemoval(R);
649
650 // Unfortunately, we have to bail out instead of recovering by
651 // ignoring the parameters, just in case the nested name specifier
652 // depends on the parameters.
Craig Topper161e4db2014-05-21 06:02:52 +0000653 return nullptr;
John McCall9b72f892010-11-10 02:40:36 +0000654 }
655
Douglas Gregor882a61a2011-09-26 14:30:28 +0000656 // "typename" keyword is allowed for identifiers only,
657 // because it may be a type definition.
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000658 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor882a61a2011-09-26 14:30:28 +0000659 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
660 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000661 // Proceed parsing, but reset the HasTypenameKeyword flag.
662 HasTypenameKeyword = false;
Douglas Gregor882a61a2011-09-26 14:30:28 +0000663 }
664
Richard Smith3f1b5d02011-05-05 21:57:07 +0000665 if (IsAliasDecl) {
666 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000667 MultiTemplateParamsArg TemplateParamsArg(
Craig Topper161e4db2014-05-21 06:02:52 +0000668 TemplateParams ? TemplateParams->data() : nullptr,
Richard Smith3f1b5d02011-05-05 21:57:07 +0000669 TemplateParams ? TemplateParams->size() : 0);
670 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith54ecd982013-02-20 19:22:51 +0000671 UsingLoc, Name, Attrs.getList(),
David Majnemerf9bde282015-03-11 06:45:39 +0000672 TypeAlias, DeclFromDeclSpec);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000673 }
Richard Smithdda56e42011-04-15 14:24:37 +0000674
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000675 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
676 /* HasUsingKeyword */ true, UsingLoc,
677 SS, Name, Attrs.getList(),
678 HasTypenameKeyword, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000679}
680
Benjamin Kramere56f3932011-12-23 17:00:35 +0000681/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000682///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000683/// [C++0x] static_assert-declaration:
684/// static_assert ( constant-expression , string-literal ) ;
685///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000686/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000687/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000688///
John McCall48871652010-08-21 09:40:31 +0000689Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000690 assert(Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert) &&
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000691 "Not a static_assert declaration");
692
David Blaikiebbafb8a2012-03-11 07:00:24 +0000693 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000694 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000695 if (Tok.is(tok::kw_static_assert))
696 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000697
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000698 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000699
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000700 BalancedDelimiterTracker T(*this, tok::l_paren);
701 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000702 Diag(Tok, diag::err_expected) << tok::l_paren;
Richard Smith76965712012-09-13 19:12:50 +0000703 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000704 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000705 }
Mike Stump11289f42009-09-09 15:08:12 +0000706
John McCalldadc5752010-08-24 06:29:42 +0000707 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000708 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000709 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000710 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000711 }
Mike Stump11289f42009-09-09 15:08:12 +0000712
Richard Smith085a64f2014-06-20 19:57:12 +0000713 ExprResult AssertMessage;
714 if (Tok.is(tok::r_paren)) {
715 Diag(Tok, getLangOpts().CPlusPlus1z
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000716 ? diag::warn_cxx14_compat_static_assert_no_message
Richard Smith085a64f2014-06-20 19:57:12 +0000717 : diag::ext_static_assert_no_message)
718 << (getLangOpts().CPlusPlus1z
719 ? FixItHint()
720 : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\""));
721 } else {
722 if (ExpectAndConsume(tok::comma)) {
723 SkipUntil(tok::semi);
724 return nullptr;
725 }
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000726
Richard Smith085a64f2014-06-20 19:57:12 +0000727 if (!isTokenStringLiteral()) {
728 Diag(Tok, diag::err_expected_string_literal)
729 << /*Source='static_assert'*/1;
730 SkipMalformedDecl();
731 return nullptr;
732 }
Mike Stump11289f42009-09-09 15:08:12 +0000733
Richard Smith085a64f2014-06-20 19:57:12 +0000734 AssertMessage = ParseStringLiteralExpression();
735 if (AssertMessage.isInvalid()) {
736 SkipMalformedDecl();
737 return nullptr;
738 }
Richard Smithd67aea22012-03-06 03:21:47 +0000739 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000740
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000741 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000742
Chris Lattner49836b42009-04-02 04:16:50 +0000743 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000744 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000745
John McCallb268a282010-08-23 23:25:46 +0000746 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000747 AssertExpr.get(),
748 AssertMessage.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000749 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000750}
751
Richard Smith74aeef52013-04-26 16:15:35 +0000752/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000753///
754/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000755/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000756///
David Blaikie15a430a2011-12-04 05:04:18 +0000757SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000758 assert(Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)
David Blaikie15a430a2011-12-04 05:04:18 +0000759 && "Not a decltype specifier");
760
David Blaikie15a430a2011-12-04 05:04:18 +0000761 ExprResult Result;
762 SourceLocation StartLoc = Tok.getLocation();
763 SourceLocation EndLoc;
764
765 if (Tok.is(tok::annot_decltype)) {
766 Result = getExprAnnotation(Tok);
767 EndLoc = Tok.getAnnotationEndLoc();
768 ConsumeToken();
769 if (Result.isInvalid()) {
770 DS.SetTypeSpecError();
771 return EndLoc;
772 }
773 } else {
Richard Smith324df552012-02-24 22:30:04 +0000774 if (Tok.getIdentifierInfo()->isStr("decltype"))
775 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000776
David Blaikie15a430a2011-12-04 05:04:18 +0000777 ConsumeToken();
778
779 BalancedDelimiterTracker T(*this, tok::l_paren);
780 if (T.expectAndConsume(diag::err_expected_lparen_after,
781 "decltype", tok::r_paren)) {
782 DS.SetTypeSpecError();
783 return T.getOpenLocation() == Tok.getLocation() ?
784 StartLoc : T.getOpenLocation();
785 }
786
Richard Smith74aeef52013-04-26 16:15:35 +0000787 // Check for C++1y 'decltype(auto)'.
788 if (Tok.is(tok::kw_auto)) {
789 // No need to disambiguate here: an expression can't start with 'auto',
790 // because the typename-specifier in a function-style cast operation can't
791 // be 'auto'.
792 Diag(Tok.getLocation(),
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000793 getLangOpts().CPlusPlus14
Richard Smith74aeef52013-04-26 16:15:35 +0000794 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
795 : diag::ext_decltype_auto_type_specifier);
796 ConsumeToken();
797 } else {
798 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000799
Richard Smith74aeef52013-04-26 16:15:35 +0000800 // C++11 [dcl.type.simple]p4:
801 // The operand of the decltype specifier is an unevaluated operand.
802 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
Craig Topper161e4db2014-05-21 06:02:52 +0000803 nullptr,/*IsDecltype=*/true);
Kaelyn Takata5cc85352015-04-10 19:16:46 +0000804 Result =
805 Actions.CorrectDelayedTyposInExpr(ParseExpression(), [](Expr *E) {
806 return E->hasPlaceholderType() ? ExprError() : E;
807 });
Richard Smith74aeef52013-04-26 16:15:35 +0000808 if (Result.isInvalid()) {
809 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000810 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smith74aeef52013-04-26 16:15:35 +0000811 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000812 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000813 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
814 // Backtrack to get the location of the last token before the semi.
815 PP.RevertCachedTokens(2);
816 ConsumeToken(); // the semi.
817 EndLoc = ConsumeAnyToken();
818 assert(Tok.is(tok::semi));
819 } else {
820 EndLoc = Tok.getLocation();
821 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000822 }
Richard Smith74aeef52013-04-26 16:15:35 +0000823 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000824 }
Richard Smith74aeef52013-04-26 16:15:35 +0000825
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000826 Result = Actions.ActOnDecltypeExpression(Result.get());
David Blaikie15a430a2011-12-04 05:04:18 +0000827 }
828
829 // Match the ')'
830 T.consumeClose();
831 if (T.getCloseLocation().isInvalid()) {
832 DS.SetTypeSpecError();
833 // FIXME: this should return the location of the last token
834 // that was consumed (by "consumeClose()")
835 return T.getCloseLocation();
836 }
837
Richard Smithfd555f62012-02-22 02:04:18 +0000838 if (Result.isInvalid()) {
839 DS.SetTypeSpecError();
840 return T.getCloseLocation();
841 }
842
David Blaikie15a430a2011-12-04 05:04:18 +0000843 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000844 }
Richard Smith74aeef52013-04-26 16:15:35 +0000845 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000846
Craig Topper161e4db2014-05-21 06:02:52 +0000847 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +0000848 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000849 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Anders Carlsson74948d02009-06-24 17:47:40 +0000850 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000851 if (Result.get()
852 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000853 DiagID, Result.get(), Policy)
Richard Smith74aeef52013-04-26 16:15:35 +0000854 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000855 DiagID, Policy)) {
John McCall49bfce42009-08-03 20:12:06 +0000856 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000857 DS.SetTypeSpecError();
858 }
859 return EndLoc;
860}
861
862void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
863 SourceLocation StartLoc,
864 SourceLocation EndLoc) {
865 // make sure we have a token we can turn into an annotation token
866 if (PP.isBacktrackEnabled())
867 PP.RevertCachedTokens(1);
868 else
869 PP.EnterToken(Tok);
870
871 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000872 setExprAnnotation(Tok,
873 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
874 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
875 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000876 Tok.setAnnotationEndLoc(EndLoc);
877 Tok.setLocation(StartLoc);
878 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000879}
880
Alexis Hunt4a257072011-05-19 05:37:45 +0000881void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
882 assert(Tok.is(tok::kw___underlying_type) &&
883 "Not an underlying type specifier");
884
885 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000886 BalancedDelimiterTracker T(*this, tok::l_paren);
887 if (T.expectAndConsume(diag::err_expected_lparen_after,
888 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +0000889 return;
890 }
891
892 TypeResult Result = ParseTypeName();
893 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000894 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt4a257072011-05-19 05:37:45 +0000895 return;
896 }
897
898 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000899 T.consumeClose();
900 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000901 return;
902
Craig Topper161e4db2014-05-21 06:02:52 +0000903 const char *PrevSpec = nullptr;
Alexis Hunt4a257072011-05-19 05:37:45 +0000904 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000905 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000906 DiagID, Result.get(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000907 Actions.getASTContext().getPrintingPolicy()))
Alexis Hunt4a257072011-05-19 05:37:45 +0000908 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanellaa90af722013-07-06 18:54:58 +0000909 DS.setTypeofParensRange(T.getRange());
Alexis Hunt4a257072011-05-19 05:37:45 +0000910}
911
David Blaikie00ee7a082011-10-25 15:01:20 +0000912/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
913/// class name or decltype-specifier. Note that we only check that the result
914/// names a type; semantic analysis will need to verify that the type names a
915/// class. The result is either a type or null, depending on whether a type
916/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +0000917///
Richard Smith4c96e992013-02-19 23:47:15 +0000918/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000919/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +0000920/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000921/// nested-name-specifier[opt] class-name
922/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +0000923/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +0000924/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000925/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000926///
Richard Smith4c96e992013-02-19 23:47:15 +0000927/// In C++98, instead of base-type-specifier, we have:
928///
929/// ::[opt] nested-name-specifier[opt] class-name
Craig Topper9ad7e262014-10-31 06:57:07 +0000930TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
931 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +0000932 // Ignore attempts to use typename
933 if (Tok.is(tok::kw_typename)) {
934 Diag(Tok, diag::err_expected_class_name_not_template)
935 << FixItHint::CreateRemoval(Tok.getLocation());
936 ConsumeToken();
937 }
938
David Blaikieafa155f2011-10-25 18:17:58 +0000939 // Parse optional nested-name-specifier
940 CXXScopeSpec SS;
941 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
942
943 BaseLoc = Tok.getLocation();
944
David Blaikie1cd50022011-10-25 17:10:12 +0000945 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +0000946 // tok == kw_decltype is just error recovery, it can only happen when SS
947 // isn't empty
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000948 if (Tok.isOneOf(tok::kw_decltype, tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +0000949 if (SS.isNotEmpty())
950 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
951 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +0000952 // Fake up a Declarator to use with ActOnTypeName.
953 DeclSpec DS(AttrFactory);
954
David Blaikie7491e732011-12-08 04:53:15 +0000955 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +0000956
957 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
958 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
959 }
960
Douglas Gregord54dfb82009-02-25 23:52:28 +0000961 // Check whether we have a template-id that names a type.
962 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000963 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +0000964 if (TemplateId->Kind == TNK_Type_template ||
965 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000966 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000967
968 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000969 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000970 EndLocation = Tok.getAnnotationEndLoc();
971 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000972
973 if (Type)
974 return Type;
975 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000976 }
977
978 // Fall through to produce an error below.
979 }
980
Douglas Gregor831c93f2008-11-05 20:51:48 +0000981 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000982 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000983 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000984 }
985
Douglas Gregor18473f32010-01-12 21:28:44 +0000986 IdentifierInfo *Id = Tok.getIdentifierInfo();
987 SourceLocation IdLoc = ConsumeToken();
988
989 if (Tok.is(tok::less)) {
990 // It looks the user intended to write a template-id here, but the
991 // template-name was wrong. Try to fix that.
992 TemplateNameKind TNK = TNK_Type_template;
993 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000994 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000995 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000996 Diag(IdLoc, diag::err_unknown_template_name)
997 << Id;
998 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000999
Serge Pavlovb716b3c2013-08-10 05:54:47 +00001000 if (!Template) {
1001 TemplateArgList TemplateArgs;
1002 SourceLocation LAngleLoc, RAngleLoc;
1003 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
1004 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor18473f32010-01-12 21:28:44 +00001005 return true;
Serge Pavlovb716b3c2013-08-10 05:54:47 +00001006 }
Douglas Gregor18473f32010-01-12 21:28:44 +00001007
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001008 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +00001009 UnqualifiedId TemplateName;
1010 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001011
Douglas Gregor18473f32010-01-12 21:28:44 +00001012 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +00001013 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
1014 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +00001015 return true;
1016 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +00001017 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001018
Douglas Gregor18473f32010-01-12 21:28:44 +00001019 // If we didn't end up with a typename token, there's nothing more we
1020 // can do.
1021 if (Tok.isNot(tok::annot_typename))
1022 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001023
Douglas Gregor18473f32010-01-12 21:28:44 +00001024 // Retrieve the type from the annotation token, consume that token, and
1025 // return.
1026 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +00001027 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +00001028 ConsumeToken();
1029 return Type;
1030 }
1031
Douglas Gregor831c93f2008-11-05 20:51:48 +00001032 // We have an identifier; check whether it is actually a type.
Craig Topper161e4db2014-05-21 06:02:52 +00001033 IdentifierInfo *CorrectedII = nullptr;
Douglas Gregore7c20652011-03-02 00:47:37 +00001034 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +00001035 false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00001036 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +00001037 /*NonTrivialTypeSourceInfo=*/true,
1038 &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001039 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001040 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001041 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +00001042 }
1043
1044 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +00001045 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001046
1047 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +00001048 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +00001049 DS.SetRangeStart(IdLoc);
1050 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +00001051 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001052
Craig Topper161e4db2014-05-21 06:02:52 +00001053 const char *PrevSpec = nullptr;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001054 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001055 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
1056 Actions.getASTContext().getPrintingPolicy());
Nick Lewycky19b9f952010-07-26 16:56:01 +00001057
1058 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1059 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001060}
1061
John McCall8d32c052012-05-22 21:28:12 +00001062void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001063 while (Tok.isOneOf(tok::kw___single_inheritance,
1064 tok::kw___multiple_inheritance,
1065 tok::kw___virtual_inheritance)) {
John McCall8d32c052012-05-22 21:28:12 +00001066 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1067 SourceLocation AttrNameLoc = ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001068 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Aaron Ballman8edb5c22013-12-18 23:44:18 +00001069 AttributeList::AS_Keyword);
John McCall8d32c052012-05-22 21:28:12 +00001070 }
1071}
1072
Richard Smith369b9f92012-06-25 21:37:02 +00001073/// Determine whether the following tokens are valid after a type-specifier
1074/// which could be a standalone declaration. This will conservatively return
1075/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +00001076bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +00001077 // This switch enumerates the valid "follow" set for type-specifiers.
1078 switch (Tok.getKind()) {
1079 default: break;
1080 case tok::semi: // struct foo {...} ;
1081 case tok::star: // struct foo {...} * P;
1082 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +00001083 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +00001084 case tok::identifier: // struct foo {...} V ;
1085 case tok::r_paren: //(struct foo {...} ) {4}
1086 case tok::annot_cxxscope: // struct foo {...} a:: b;
1087 case tok::annot_typename: // struct foo {...} a ::b;
1088 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1089 case tok::l_paren: // struct foo {...} ( x);
1090 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001091 case tok::kw_operator: // struct foo operator ++() {...}
Alp Tokerd3f79c52013-11-24 20:24:54 +00001092 case tok::kw___declspec: // struct foo {...} __declspec(...)
Richard Smith843f18f2014-08-13 02:13:15 +00001093 case tok::l_square: // void f(struct f [ 3])
1094 case tok::ellipsis: // void f(struct f ... [Ns])
Abramo Bagnara152eb392014-08-16 08:29:27 +00001095 // FIXME: we should emit semantic diagnostic when declaration
1096 // attribute is in type attribute position.
1097 case tok::kw___attribute: // struct foo __attribute__((used)) x;
Richard Smith369b9f92012-06-25 21:37:02 +00001098 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001099 case tok::colon:
1100 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smith369b9f92012-06-25 21:37:02 +00001101 // Type qualifiers
1102 case tok::kw_const: // struct foo {...} const x;
1103 case tok::kw_volatile: // struct foo {...} volatile x;
1104 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith843f18f2014-08-13 02:13:15 +00001105 case tok::kw__Atomic: // struct foo {...} _Atomic x;
Nico Rieck3e1ee832014-12-04 23:30:25 +00001106 case tok::kw___unaligned: // struct foo {...} __unaligned *x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001107 // Function specifiers
1108 // Note, no 'explicit'. An explicit function must be either a conversion
1109 // operator or a constructor. Either way, it can't have a return type.
1110 case tok::kw_inline: // struct foo inline f();
1111 case tok::kw_virtual: // struct foo virtual f();
1112 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001113 // Storage-class specifiers
1114 case tok::kw_static: // struct foo {...} static x;
1115 case tok::kw_extern: // struct foo {...} extern x;
1116 case tok::kw_typedef: // struct foo {...} typedef x;
1117 case tok::kw_register: // struct foo {...} register x;
1118 case tok::kw_auto: // struct foo {...} auto x;
1119 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001120 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001121 case tok::kw_constexpr: // struct foo {...} constexpr x;
1122 // As shown above, type qualifiers and storage class specifiers absolutely
1123 // can occur after class specifiers according to the grammar. However,
1124 // almost no one actually writes code like this. If we see one of these,
1125 // it is much more likely that someone missed a semi colon and the
1126 // type/storage class specifier we're seeing is part of the *next*
1127 // intended declaration, as in:
1128 //
1129 // struct foo { ... }
1130 // typedef int X;
1131 //
1132 // We'd really like to emit a missing semicolon error instead of emitting
1133 // an error on the 'int' saying that you can't have two type specifiers in
1134 // the same declaration of X. Because of this, we look ahead past this
1135 // token to see if it's a type specifier. If so, we know the code is
1136 // otherwise invalid, so we can produce the expected semi error.
1137 if (!isKnownToBeTypeSpecifier(NextToken()))
1138 return true;
1139 break;
1140 case tok::r_brace: // struct bar { struct foo {...} }
1141 // Missing ';' at end of struct is accepted as an extension in C mode.
1142 if (!getLangOpts().CPlusPlus)
1143 return true;
1144 break;
Richard Smith52c5b872013-01-29 04:13:32 +00001145 case tok::greater:
1146 // template<class T = class X>
1147 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001148 }
1149 return false;
1150}
1151
Douglas Gregor556877c2008-04-13 21:30:24 +00001152/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1153/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1154/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001155/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001156///
1157/// class-specifier: [C++ class]
1158/// class-head '{' member-specification[opt] '}'
1159/// class-head '{' member-specification[opt] '}' attributes[opt]
1160/// class-head:
1161/// class-key identifier[opt] base-clause[opt]
1162/// class-key nested-name-specifier identifier base-clause[opt]
1163/// class-key nested-name-specifier[opt] simple-template-id
1164/// base-clause[opt]
1165/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001166/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001167/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001168/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001169/// simple-template-id base-clause[opt]
1170/// class-key:
1171/// 'class'
1172/// 'struct'
1173/// 'union'
1174///
1175/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001176/// class-key ::[opt] nested-name-specifier[opt] identifier
1177/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1178/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001179///
1180/// Note that the C++ class-specifier and elaborated-type-specifier,
1181/// together, subsume the C99 struct-or-union-specifier:
1182///
1183/// struct-or-union-specifier: [C99 6.7.2.1]
1184/// struct-or-union identifier[opt] '{' struct-contents '}'
1185/// struct-or-union identifier
1186/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1187/// '}' attributes[opt]
1188/// [GNU] struct-or-union attributes[opt] identifier
1189/// struct-or-union:
1190/// 'struct'
1191/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001192void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1193 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001194 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001195 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001196 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001197 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001198 DeclSpec::TST TagType;
1199 if (TagTokKind == tok::kw_struct)
1200 TagType = DeclSpec::TST_struct;
1201 else if (TagTokKind == tok::kw___interface)
1202 TagType = DeclSpec::TST_interface;
1203 else if (TagTokKind == tok::kw_class)
1204 TagType = DeclSpec::TST_class;
1205 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001206 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1207 TagType = DeclSpec::TST_union;
1208 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001209
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001210 if (Tok.is(tok::code_completion)) {
1211 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001212 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001213 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001214 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001215
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001216 // C++03 [temp.explicit] 14.7.2/8:
1217 // The usual access checking rules do not apply to names used to specify
1218 // explicit instantiations.
1219 //
1220 // As an extension we do not perform access checking on the names used to
1221 // specify explicit specializations either. This is important to allow
1222 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001223 //
1224 // Note that we don't suppress if this turns out to be an elaborated
1225 // type specifier.
1226 bool shouldDelayDiagsInTag =
1227 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1228 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1229 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001230
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001231 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001232 // If attributes exist after tag, parse them.
Richard Smith37a45dd2013-10-24 01:21:09 +00001233 MaybeParseGNUAttributes(attrs);
Aaron Ballman068aa512015-05-20 20:58:33 +00001234 MaybeParseMicrosoftDeclSpecs(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001235
John McCall8d32c052012-05-22 21:28:12 +00001236 // Parse inheritance specifiers.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001237 if (Tok.isOneOf(tok::kw___single_inheritance,
1238 tok::kw___multiple_inheritance,
1239 tok::kw___virtual_inheritance))
Richard Smith37a45dd2013-10-24 01:21:09 +00001240 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCall8d32c052012-05-22 21:28:12 +00001241
Alexis Hunt96d5c762009-11-21 08:43:09 +00001242 // If C++0x attributes exist here, parse them.
1243 // FIXME: Are we consistent with the ordering of parsing of different
1244 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001245 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001246
Michael Han309af292013-01-07 16:57:11 +00001247 // Source location used by FIXIT to insert misplaced
1248 // C++11 attributes
1249 SourceLocation AttrFixitLoc = Tok.getLocation();
1250
Nico Weber7c3c5be2014-09-23 04:09:56 +00001251 if (TagType == DeclSpec::TST_struct &&
David Majnemer86330af2014-12-29 02:14:26 +00001252 Tok.isNot(tok::identifier) &&
1253 !Tok.isAnnotation() &&
Nico Weber7c3c5be2014-09-23 04:09:56 +00001254 Tok.getIdentifierInfo() &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001255 Tok.isOneOf(tok::kw___is_abstract,
1256 tok::kw___is_arithmetic,
1257 tok::kw___is_array,
1258 tok::kw___is_base_of,
1259 tok::kw___is_class,
1260 tok::kw___is_complete_type,
1261 tok::kw___is_compound,
1262 tok::kw___is_const,
1263 tok::kw___is_constructible,
1264 tok::kw___is_convertible,
1265 tok::kw___is_convertible_to,
1266 tok::kw___is_destructible,
1267 tok::kw___is_empty,
1268 tok::kw___is_enum,
1269 tok::kw___is_floating_point,
1270 tok::kw___is_final,
1271 tok::kw___is_function,
1272 tok::kw___is_fundamental,
1273 tok::kw___is_integral,
1274 tok::kw___is_interface_class,
1275 tok::kw___is_literal,
1276 tok::kw___is_lvalue_expr,
1277 tok::kw___is_lvalue_reference,
1278 tok::kw___is_member_function_pointer,
1279 tok::kw___is_member_object_pointer,
1280 tok::kw___is_member_pointer,
1281 tok::kw___is_nothrow_assignable,
1282 tok::kw___is_nothrow_constructible,
1283 tok::kw___is_nothrow_destructible,
1284 tok::kw___is_object,
1285 tok::kw___is_pod,
1286 tok::kw___is_pointer,
1287 tok::kw___is_polymorphic,
1288 tok::kw___is_reference,
1289 tok::kw___is_rvalue_expr,
1290 tok::kw___is_rvalue_reference,
1291 tok::kw___is_same,
1292 tok::kw___is_scalar,
1293 tok::kw___is_sealed,
1294 tok::kw___is_signed,
1295 tok::kw___is_standard_layout,
1296 tok::kw___is_trivial,
1297 tok::kw___is_trivially_assignable,
1298 tok::kw___is_trivially_constructible,
1299 tok::kw___is_trivially_copyable,
1300 tok::kw___is_union,
1301 tok::kw___is_unsigned,
1302 tok::kw___is_void,
1303 tok::kw___is_volatile))
Nico Weber7c3c5be2014-09-23 04:09:56 +00001304 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
1305 // name of struct templates, but some are keywords in GCC >= 4.3
1306 // and Clang. Therefore, when we see the token sequence "struct
1307 // X", make X into a normal identifier rather than a keyword, to
1308 // allow libstdc++ 4.2 and libc++ to work properly.
1309 TryKeywordIdentFallback(true);
Mike Stump11289f42009-09-09 15:08:12 +00001310
David Majnemer51fd8a02015-07-22 23:46:18 +00001311 struct PreserveAtomicIdentifierInfoRAII {
1312 PreserveAtomicIdentifierInfoRAII(Token &Tok, bool Enabled)
1313 : AtomicII(nullptr) {
1314 if (!Enabled)
1315 return;
1316 assert(Tok.is(tok::kw__Atomic));
1317 AtomicII = Tok.getIdentifierInfo();
1318 AtomicII->revertTokenIDToIdentifier();
1319 Tok.setKind(tok::identifier);
1320 }
1321 ~PreserveAtomicIdentifierInfoRAII() {
1322 if (!AtomicII)
1323 return;
1324 AtomicII->revertIdentifierToTokenID(tok::kw__Atomic);
1325 }
1326 IdentifierInfo *AtomicII;
1327 };
1328
1329 // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
1330 // implementation for VS2013 uses _Atomic as an identifier for one of the
1331 // classes in <atomic>. When we are parsing 'struct _Atomic', don't consider
1332 // '_Atomic' to be a keyword. We are careful to undo this so that clang can
1333 // use '_Atomic' in its own header files.
1334 bool ShouldChangeAtomicToIdentifier = getLangOpts().MSVCCompat &&
1335 Tok.is(tok::kw__Atomic) &&
1336 TagType == DeclSpec::TST_struct;
1337 PreserveAtomicIdentifierInfoRAII AtomicTokenGuard(
1338 Tok, ShouldChangeAtomicToIdentifier);
1339
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001340 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001341 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001342 if (getLangOpts().CPlusPlus) {
Serge Pavlov458ea762014-07-16 05:16:52 +00001343 // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it
1344 // is a base-specifier-list.
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001345 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001346
Nico Webercfaa4cd2015-02-15 07:26:13 +00001347 CXXScopeSpec Spec;
1348 bool HasValidSpec = true;
1349 if (ParseOptionalCXXScopeSpecifier(Spec, ParsedType(), EnteringContext)) {
John McCall413021a2010-07-30 06:26:29 +00001350 DS.SetTypeSpecError();
Nico Webercfaa4cd2015-02-15 07:26:13 +00001351 HasValidSpec = false;
1352 }
1353 if (Spec.isSet())
1354 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id)) {
Alp Tokerec543272013-12-24 09:48:30 +00001355 Diag(Tok, diag::err_expected) << tok::identifier;
Nico Webercfaa4cd2015-02-15 07:26:13 +00001356 HasValidSpec = false;
1357 }
1358 if (HasValidSpec)
1359 SS = Spec;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001360 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001361
Douglas Gregor916462b2009-10-30 21:46:58 +00001362 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1363
Douglas Gregor67a65642009-02-17 23:15:12 +00001364 // Parse the (optional) class name or simple-template-id.
Craig Topper161e4db2014-05-21 06:02:52 +00001365 IdentifierInfo *Name = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001366 SourceLocation NameLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001367 TemplateIdAnnotation *TemplateId = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001368 if (Tok.is(tok::identifier)) {
1369 Name = Tok.getIdentifierInfo();
1370 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001371
David Blaikiebbafb8a2012-03-11 07:00:24 +00001372 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001373 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001374 // Eat the template argument list and try to continue parsing this as
1375 // a class (or template thereof).
1376 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001377 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +00001378 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +00001379 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001380 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001381 // We couldn't parse the template argument list at all, so don't
1382 // try to give any location information for the list.
1383 LAngleLoc = RAngleLoc = SourceLocation();
1384 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001385
Douglas Gregor916462b2009-10-30 21:46:58 +00001386 Diag(NameLoc, diag::err_explicit_spec_non_template)
Alp Toker01d65e12014-01-06 12:54:41 +00001387 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1388 << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
Joao Matose9a3ed42012-08-31 22:18:20 +00001389
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001390 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001391 // we've removed its template argument list.
1392 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1393 if (TemplateParams && TemplateParams->size() > 1) {
1394 TemplateParams->pop_back();
1395 } else {
Craig Topper161e4db2014-05-21 06:02:52 +00001396 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001397 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001398 = ParsedTemplateInfo::NonTemplate;
1399 }
1400 } else if (TemplateInfo.Kind
1401 == ParsedTemplateInfo::ExplicitInstantiation) {
1402 // Pretend this is just a forward declaration.
Craig Topper161e4db2014-05-21 06:02:52 +00001403 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001404 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001405 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001406 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001407 = SourceLocation();
1408 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1409 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001410 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001411 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001412 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001413 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001414 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001415
Douglas Gregore7c20652011-03-02 00:47:37 +00001416 if (TemplateId->Kind != TNK_Type_template &&
1417 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001418 // The template-name in the simple-template-id refers to
1419 // something other than a class template. Give an appropriate
1420 // error message and skip to the ';'.
1421 SourceRange Range(NameLoc);
1422 if (SS.isNotEmpty())
1423 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001424
Richard Smith72bfbd82013-12-04 00:28:23 +00001425 // FIXME: Name may be null here.
Douglas Gregor7f741122009-02-25 19:37:18 +00001426 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001427 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001428
Douglas Gregor7f741122009-02-25 19:37:18 +00001429 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001430 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor7f741122009-02-25 19:37:18 +00001431 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001432 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001433 }
1434
Richard Smithbfdb1082012-03-12 08:56:40 +00001435 // There are four options here.
1436 // - If we are in a trailing return type, this is always just a reference,
1437 // and we must not try to parse a definition. For instance,
1438 // [] () -> struct S { };
1439 // does not define a type.
1440 // - If we have 'struct foo {...', 'struct foo :...',
1441 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1442 // - If we have 'struct foo;', then this is either a forward declaration
1443 // or a friend declaration, which have to be treated differently.
1444 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001445 //
1446 // We also detect these erroneous cases to provide better diagnostic for
1447 // C++11 attributes parsing.
1448 // - attributes follow class name:
1449 // struct foo [[]] {};
1450 // - attributes appear before or after 'final':
1451 // struct foo [[]] final [[]] {};
1452 //
Richard Smithc5b05522012-03-12 07:56:15 +00001453 // However, in type-specifier-seq's, things look like declarations but are
1454 // just references, e.g.
1455 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001456 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001457 // &T::operator struct s;
Richard Smith649c7b062014-01-08 00:56:48 +00001458 // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
Michael Han9407e502012-11-26 22:54:45 +00001459
1460 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001461 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001462
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001463 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
John McCallfaf5fb42010-08-26 23:41:50 +00001464 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001465 if (DSC == DSC_trailing)
1466 TUK = Sema::TUK_Reference;
1467 else if (Tok.is(tok::l_brace) ||
1468 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001469 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001470 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001471 if (DS.isFriendSpecified()) {
1472 // C++ [class.friend]p2:
1473 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001474 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001475 << SourceRange(DS.getFriendSpecLoc());
1476
1477 // Skip everything up to the semicolon, so that this looks like a proper
1478 // friend class (or template thereof) declaration.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001479 SkipUntil(tok::semi, StopBeforeMatch);
John McCallfaf5fb42010-08-26 23:41:50 +00001480 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001481 } else {
1482 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001483 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001484 }
Richard Smith434516c2013-02-22 06:46:23 +00001485 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1486 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001487 // We can't tell if this is a definition or reference
1488 // until we skipped the 'final' and C++11 attribute specifiers.
1489 TentativeParsingAction PA(*this);
1490
1491 // Skip the 'final' keyword.
1492 ConsumeToken();
1493
1494 // Skip C++11 attribute specifiers.
1495 while (true) {
1496 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1497 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001498 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001499 break;
Richard Smith434516c2013-02-22 06:46:23 +00001500 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001501 ConsumeToken();
1502 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001503 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001504 break;
1505 } else {
1506 break;
1507 }
1508 }
1509
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001510 if (Tok.isOneOf(tok::l_brace, tok::colon))
Michael Han9407e502012-11-26 22:54:45 +00001511 TUK = Sema::TUK_Definition;
1512 else
1513 TUK = Sema::TUK_Reference;
1514
1515 PA.Revert();
Richard Smith649c7b062014-01-08 00:56:48 +00001516 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00001517 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001518 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001519 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001520 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001521 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Joao Matose9a3ed42012-08-31 22:18:20 +00001522 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00001523 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001524 DeclSpec::getSpecifierName(TagType, PPol));
Joao Matose9a3ed42012-08-31 22:18:20 +00001525 PP.EnterToken(Tok);
1526 Tok.setKind(tok::semi);
1527 }
Richard Smith369b9f92012-06-25 21:37:02 +00001528 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001529 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001530
Michael Han9407e502012-11-26 22:54:45 +00001531 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1532 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001533 if (TUK != Sema::TUK_Reference) {
1534 // If this is not a reference, then the only possible
1535 // valid place for C++11 attributes to appear here
1536 // is between class-key and class-name. If there are
1537 // any attributes after class-name, we try a fixit to move
1538 // them to the right place.
1539 SourceRange AttrRange = Attributes.Range;
1540 if (AttrRange.isValid()) {
1541 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1542 << AttrRange
1543 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1544 CharSourceRange(AttrRange, true))
1545 << FixItHint::CreateRemoval(AttrRange);
1546
1547 // Recover by adding misplaced attributes to the attribute list
1548 // of the class so they can be applied on the class later.
1549 attrs.takeAllFrom(Attributes);
1550 }
1551 }
Michael Han9407e502012-11-26 22:54:45 +00001552
John McCall6347b682012-05-07 06:16:58 +00001553 // If this is an elaborated type specifier, and we delayed
1554 // diagnostics before, just merge them into the current pool.
1555 if (shouldDelayDiagsInTag) {
1556 diagsFromTag.done();
1557 if (TUK == Sema::TUK_Reference)
1558 diagsFromTag.redelay();
1559 }
1560
John McCall413021a2010-07-30 06:26:29 +00001561 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001562 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001563 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1564 // We have a declaration or reference to an anonymous class.
1565 Diag(StartLoc, diag::err_anon_type_definition)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001566 << DeclSpec::getSpecifierName(TagType, Policy);
John McCall413021a2010-07-30 06:26:29 +00001567 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001568
David Majnemer3252fd02013-12-05 01:36:53 +00001569 // If we are parsing a definition and stop at a base-clause, continue on
1570 // until the semicolon. Continuing from the comma will just trick us into
1571 // thinking we are seeing a variable declaration.
1572 if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1573 SkipUntil(tok::semi, StopBeforeMatch);
1574 else
1575 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor556877c2008-04-13 21:30:24 +00001576 return;
1577 }
1578
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001579 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001580 DeclResult TagOrTempResult = true; // invalid
1581 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001582
Douglas Gregord6ab8742009-05-28 23:31:59 +00001583 bool Owned = false;
Richard Smithd9ba2242015-05-07 03:54:19 +00001584 Sema::SkipBodyInfo SkipBody;
John McCall06f6fe8d2009-09-04 01:14:41 +00001585 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001586 // Explicit specialization, class template partial specialization,
1587 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001588 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001589 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001590 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001591 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001592 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001593 ProhibitAttributes(attrs);
1594
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001595 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001596 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001597 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001598 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001599 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001600 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001601 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001602 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001603 TemplateId->TemplateNameLoc,
1604 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001605 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001606 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001607 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001608
1609 // Friend template-ids are treated as references unless
1610 // they have template headers, in which case they're ill-formed
1611 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1612 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001613 } else if (TUK == Sema::TUK_Reference ||
1614 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001615 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001616 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001617 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001618 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001619 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001620 TemplateId->Template,
1621 TemplateId->TemplateNameLoc,
1622 TemplateId->LAngleLoc,
1623 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001624 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001625 } else {
1626 // This is an explicit specialization or a class template
1627 // partial specialization.
1628 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001629 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1630 // This looks like an explicit instantiation, because we have
1631 // something like
1632 //
1633 // template class Foo<X>
1634 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001635 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001636 // meant to be an explicit specialization, but the user forgot
1637 // the '<>' after 'template'.
Richard Smith003c5e12013-11-08 19:03:29 +00001638 // It this is friend declaration however, since it cannot have a
1639 // template header, it is most likely that the user meant to
1640 // remove the 'template' keyword.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001641 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith003c5e12013-11-08 19:03:29 +00001642 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001643
Richard Smith003c5e12013-11-08 19:03:29 +00001644 if (TUK == Sema::TUK_Friend) {
1645 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
Craig Topper161e4db2014-05-21 06:02:52 +00001646 TemplateParams = nullptr;
Richard Smith003c5e12013-11-08 19:03:29 +00001647 } else {
1648 SourceLocation LAngleLoc =
1649 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1650 Diag(TemplateId->TemplateNameLoc,
1651 diag::err_explicit_instantiation_with_definition)
1652 << SourceRange(TemplateInfo.TemplateLoc)
1653 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1654
1655 // Create a fake template parameter list that contains only
1656 // "template<>", so that we treat this construct as a class
1657 // template specialization.
1658 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
Craig Topper161e4db2014-05-21 06:02:52 +00001659 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, nullptr,
1660 0, LAngleLoc));
Richard Smith003c5e12013-11-08 19:03:29 +00001661 TemplateParams = &FakedParamLists;
1662 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001663 }
1664
1665 // Build the class template specialization.
Richard Smith4b55a9c2014-04-17 03:29:33 +00001666 TagOrTempResult = Actions.ActOnClassTemplateSpecialization(
1667 getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(),
1668 *TemplateId, attrs.getList(),
Craig Topper161e4db2014-05-21 06:02:52 +00001669 MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0]
1670 : nullptr,
Richard Smithc7e6ff02015-05-18 20:36:47 +00001671 TemplateParams ? TemplateParams->size() : 0),
1672 &SkipBody);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001673 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001674 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001675 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001676 // Explicit instantiation of a member of a class template
1677 // specialization, e.g.,
1678 //
1679 // template struct Outer<int>::Inner;
1680 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001681 ProhibitAttributes(attrs);
1682
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001683 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001684 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001685 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001686 TemplateInfo.TemplateLoc,
1687 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001688 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001689 } else if (TUK == Sema::TUK_Friend &&
1690 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001691 ProhibitAttributes(attrs);
1692
John McCallace48cd2010-10-19 01:40:49 +00001693 TagOrTempResult =
1694 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1695 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001696 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001697 MultiTemplateParamsArg(
Craig Topper161e4db2014-05-21 06:02:52 +00001698 TemplateParams? &(*TemplateParams)[0]
1699 : nullptr,
John McCallace48cd2010-10-19 01:40:49 +00001700 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001701 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001702 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1703 ProhibitAttributes(attrs);
Richard Smith003c5e12013-11-08 19:03:29 +00001704
Larisse Voufo725de3e2013-06-21 00:08:46 +00001705 if (TUK == Sema::TUK_Definition &&
1706 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1707 // If the declarator-id is not a template-id, issue a diagnostic and
1708 // recover by ignoring the 'template' keyword.
1709 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1710 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Craig Topper161e4db2014-05-21 06:02:52 +00001711 TemplateParams = nullptr;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001712 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001713
John McCall7f41d982009-09-11 04:59:25 +00001714 bool IsDependent = false;
1715
John McCall32723e92010-10-19 18:40:57 +00001716 // Don't pass down template parameter lists if this is just a tag
1717 // reference. For example, we don't need the template parameters here:
1718 // template <class T> class A *makeA(T t);
1719 MultiTemplateParamsArg TParams;
1720 if (TUK != Sema::TUK_Reference && TemplateParams)
1721 TParams =
1722 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1723
David Majnemer936b4112015-04-19 07:53:29 +00001724 handleDeclspecAlignBeforeClassKey(attrs, DS, TUK);
1725
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001726 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001727 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001728 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001729 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001730 TParams, Owned, IsDependent,
1731 SourceLocation(), false,
Richard Smith649c7b062014-01-08 00:56:48 +00001732 clang::TypeResult(),
Richard Smith65ebb4a2015-03-26 04:09:53 +00001733 DSC == DSC_type_specifier,
1734 &SkipBody);
John McCall7f41d982009-09-11 04:59:25 +00001735
1736 // If ActOnTag said the type was dependent, try again with the
1737 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001738 if (IsDependent) {
1739 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001740 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001741 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001742 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001743 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001744
Douglas Gregor556877c2008-04-13 21:30:24 +00001745 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001746 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001747 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001748 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001749 isCXX11FinalKeyword());
Richard Smithd9ba2242015-05-07 03:54:19 +00001750 if (SkipBody.ShouldSkip)
Richard Smith65ebb4a2015-03-26 04:09:53 +00001751 SkipCXXMemberSpecification(StartLoc, AttrFixitLoc, TagType,
1752 TagOrTempResult.get());
1753 else if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001754 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1755 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001756 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001757 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001758 }
1759
Craig Topper161e4db2014-05-21 06:02:52 +00001760 const char *PrevSpec = nullptr;
John McCallba7bf592010-08-24 05:47:05 +00001761 unsigned DiagID;
1762 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001763 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001764 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1765 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001766 PrevSpec, DiagID, TypeResult.get(), Policy);
John McCall7f41d982009-09-11 04:59:25 +00001767 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001768 Result = DS.SetTypeSpecType(TagType, StartLoc,
1769 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001770 PrevSpec, DiagID, TagOrTempResult.get(), Owned,
1771 Policy);
John McCall7f41d982009-09-11 04:59:25 +00001772 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001773 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001774 return;
1775 }
Mike Stump11289f42009-09-09 15:08:12 +00001776
John McCallba7bf592010-08-24 05:47:05 +00001777 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001778 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001779
Chris Lattnercf251412010-02-02 01:23:29 +00001780 // At this point, we've successfully parsed a class-specifier in 'definition'
1781 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1782 // going to look at what comes after it to improve error recovery. If an
1783 // impossible token occurs next, we assume that the programmer forgot a ; at
1784 // the end of the declaration and recover that way.
1785 //
Richard Smith369b9f92012-06-25 21:37:02 +00001786 // Also enforce C++ [temp]p3:
1787 // In a template-declaration which defines a class, no declarator
1788 // is permitted.
Richard Smith843f18f2014-08-13 02:13:15 +00001789 //
1790 // After a type-specifier, we don't expect a semicolon. This only happens in
1791 // C, since definitions are not permitted in this context in C++.
Joao Matose9a3ed42012-08-31 22:18:20 +00001792 if (TUK == Sema::TUK_Definition &&
Richard Smith843f18f2014-08-13 02:13:15 +00001793 (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) &&
Joao Matose9a3ed42012-08-31 22:18:20 +00001794 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001795 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001796 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Alp Toker383d2c42014-01-01 03:08:43 +00001797 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001798 DeclSpec::getSpecifierName(TagType, PPol));
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001799 // Push this token back into the preprocessor and change our current token
1800 // to ';' so that the rest of the code recovers as though there were an
1801 // ';' after the definition.
1802 PP.EnterToken(Tok);
1803 Tok.setKind(tok::semi);
1804 }
Chris Lattnercf251412010-02-02 01:23:29 +00001805 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001806}
1807
Mike Stump11289f42009-09-09 15:08:12 +00001808/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001809///
1810/// base-clause : [C++ class.derived]
1811/// ':' base-specifier-list
1812/// base-specifier-list:
1813/// base-specifier '...'[opt]
1814/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001815void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001816 assert(Tok.is(tok::colon) && "Not a base clause");
1817 ConsumeToken();
1818
Douglas Gregor29a92472008-10-22 17:49:05 +00001819 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001820 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001821
Douglas Gregor556877c2008-04-13 21:30:24 +00001822 while (true) {
1823 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001824 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001825 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001826 // Skip the rest of this base specifier, up until the comma or
1827 // opening brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001828 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor29a92472008-10-22 17:49:05 +00001829 } else {
1830 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001831 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001832 }
1833
1834 // If the next token is a comma, consume it and keep reading
1835 // base-specifiers.
Alp Toker97650562014-01-10 11:19:30 +00001836 if (!TryConsumeToken(tok::comma))
1837 break;
Douglas Gregor556877c2008-04-13 21:30:24 +00001838 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001839
1840 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001841 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001842}
1843
1844/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1845/// one entry in the base class list of a class specifier, for example:
1846/// class foo : public bar, virtual private baz {
1847/// 'public bar' and 'virtual private baz' are each base-specifiers.
1848///
1849/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001850/// attribute-specifier-seq[opt] base-type-specifier
1851/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1852/// base-type-specifier
1853/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1854/// base-type-specifier
Craig Topper9ad7e262014-10-31 06:57:07 +00001855BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001856 bool IsVirtual = false;
1857 SourceLocation StartLoc = Tok.getLocation();
1858
Richard Smith4c96e992013-02-19 23:47:15 +00001859 ParsedAttributesWithRange Attributes(AttrFactory);
1860 MaybeParseCXX11Attributes(Attributes);
1861
Douglas Gregor556877c2008-04-13 21:30:24 +00001862 // Parse the 'virtual' keyword.
Alp Toker97650562014-01-10 11:19:30 +00001863 if (TryConsumeToken(tok::kw_virtual))
Douglas Gregor556877c2008-04-13 21:30:24 +00001864 IsVirtual = true;
Douglas Gregor556877c2008-04-13 21:30:24 +00001865
Richard Smith4c96e992013-02-19 23:47:15 +00001866 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1867
Douglas Gregor556877c2008-04-13 21:30:24 +00001868 // Parse an (optional) access specifier.
1869 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001870 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001871 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001872
Richard Smith4c96e992013-02-19 23:47:15 +00001873 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1874
Douglas Gregor556877c2008-04-13 21:30:24 +00001875 // Parse the 'virtual' keyword (again!), in case it came after the
1876 // access specifier.
1877 if (Tok.is(tok::kw_virtual)) {
1878 SourceLocation VirtualLoc = ConsumeToken();
1879 if (IsVirtual) {
1880 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001881 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001882 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001883 }
1884
1885 IsVirtual = true;
1886 }
1887
Richard Smith4c96e992013-02-19 23:47:15 +00001888 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1889
Douglas Gregor831c93f2008-11-05 20:51:48 +00001890 // Parse the class-name.
David Majnemer51fd8a02015-07-22 23:46:18 +00001891
1892 // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
1893 // implementation for VS2013 uses _Atomic as an identifier for one of the
1894 // classes in <atomic>. Treat '_Atomic' to be an identifier when we are
1895 // parsing the class-name for a base specifier.
1896 if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) &&
1897 NextToken().is(tok::less))
1898 Tok.setKind(tok::identifier);
1899
Douglas Gregord54dfb82009-02-25 23:52:28 +00001900 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001901 SourceLocation BaseLoc;
1902 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001903 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001904 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001905
Douglas Gregor752a5952011-01-03 22:36:02 +00001906 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1907 // actually part of the base-specifier-list grammar productions, but we
1908 // parse it here for convenience.
1909 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00001910 TryConsumeToken(tok::ellipsis, EllipsisLoc);
1911
Mike Stump11289f42009-09-09 15:08:12 +00001912 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001913 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001914
Douglas Gregor556877c2008-04-13 21:30:24 +00001915 // Notify semantic analysis that we have parsed a complete
1916 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00001917 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1918 Access, BaseType.get(), BaseLoc,
1919 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001920}
1921
1922/// getAccessSpecifierIfPresent - Determine whether the next token is
1923/// a C++ access-specifier.
1924///
1925/// access-specifier: [C++ class.derived]
1926/// 'private'
1927/// 'protected'
1928/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001929AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001930 switch (Tok.getKind()) {
1931 default: return AS_none;
1932 case tok::kw_private: return AS_private;
1933 case tok::kw_protected: return AS_protected;
1934 case tok::kw_public: return AS_public;
1935 }
1936}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001937
Douglas Gregor433e0532012-04-16 18:27:27 +00001938/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith0b3a4622014-11-13 20:01:57 +00001939/// delayed, e.g., default arguments or an exception-specification, create a
1940/// late-parsed method declaration record to handle the parsing at the end of
1941/// the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00001942void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1943 Decl *ThisDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001944 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001945 = DeclaratorInfo.getFunctionTypeInfo();
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001946 // If there was a late-parsed exception-specification, we'll need a
1947 // late parse
1948 bool NeedLateParse = FTI.getExceptionSpecType() == EST_Unparsed;
Douglas Gregor433e0532012-04-16 18:27:27 +00001949
Nathan Sidwell5bb231c2015-02-19 14:03:22 +00001950 if (!NeedLateParse) {
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001951 // Look ahead to see if there are any default args
Nathan Sidwell5bb231c2015-02-19 14:03:22 +00001952 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
1953 auto Param = cast<ParmVarDecl>(FTI.Params[ParamIdx].Param);
1954 if (Param->hasUnparsedDefaultArg()) {
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001955 NeedLateParse = true;
1956 break;
1957 }
Nathan Sidwell5bb231c2015-02-19 14:03:22 +00001958 }
1959 }
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001960
1961 if (NeedLateParse) {
Richard Smith0b3a4622014-11-13 20:01:57 +00001962 // Push this method onto the stack of late-parsed method
1963 // declarations.
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001964 auto LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
Richard Smith0b3a4622014-11-13 20:01:57 +00001965 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
1966 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
1967
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001968 // Stash the exception-specification tokens in the late-pased method.
Richard Smith0b3a4622014-11-13 20:01:57 +00001969 LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens;
1970 FTI.ExceptionSpecTokens = 0;
1971
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001972 // Push tokens for each parameter. Those that do not have
1973 // defaults will be NULL.
Richard Smith0b3a4622014-11-13 20:01:57 +00001974 LateMethod->DefaultArgs.reserve(FTI.NumParams);
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001975 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx)
Alp Tokerc5350722014-02-26 22:27:52 +00001976 LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
Nathan Sidwelld5b9a1d2015-01-25 00:25:44 +00001977 FTI.Params[ParamIdx].Param, FTI.Params[ParamIdx].DefaultArgTokens));
Eli Friedman3af2a772009-07-22 21:45:50 +00001978 }
1979}
1980
Richard Smith89645bc2013-01-02 12:01:23 +00001981/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001982/// virt-specifier.
1983///
1984/// virt-specifier:
1985/// override
1986/// final
Richard Smith89645bc2013-01-02 12:01:23 +00001987VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001988 if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001989 return VirtSpecifiers::VS_None;
1990
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001991 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001992
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001993 // Initialize the contextual keywords.
1994 if (!Ident_final) {
1995 Ident_final = &PP.getIdentifierTable().get("final");
1996 if (getLangOpts().MicrosoftExt)
1997 Ident_sealed = &PP.getIdentifierTable().get("sealed");
1998 Ident_override = &PP.getIdentifierTable().get("override");
Anders Carlsson56104902011-01-17 03:05:47 +00001999 }
2000
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002001 if (II == Ident_override)
2002 return VirtSpecifiers::VS_Override;
2003
2004 if (II == Ident_sealed)
2005 return VirtSpecifiers::VS_Sealed;
2006
2007 if (II == Ident_final)
2008 return VirtSpecifiers::VS_Final;
2009
Anders Carlsson56104902011-01-17 03:05:47 +00002010 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002011}
2012
Richard Smith89645bc2013-01-02 12:01:23 +00002013/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002014///
2015/// virt-specifier-seq:
2016/// virt-specifier
2017/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00002018void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
Richard Smith3d1a94c2014-08-12 00:22:39 +00002019 bool IsInterface,
2020 SourceLocation FriendLoc) {
Anders Carlsson56104902011-01-17 03:05:47 +00002021 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00002022 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00002023 if (Specifier == VirtSpecifiers::VS_None)
2024 return;
2025
Richard Smith3d1a94c2014-08-12 00:22:39 +00002026 if (FriendLoc.isValid()) {
2027 Diag(Tok.getLocation(), diag::err_friend_decl_spec)
2028 << VirtSpecifiers::getSpecifierName(Specifier)
2029 << FixItHint::CreateRemoval(Tok.getLocation())
2030 << SourceRange(FriendLoc, FriendLoc);
2031 ConsumeToken();
2032 continue;
2033 }
2034
Anders Carlsson56104902011-01-17 03:05:47 +00002035 // C++ [class.mem]p8:
2036 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Craig Topper161e4db2014-05-21 06:02:52 +00002037 const char *PrevSpec = nullptr;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00002038 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00002039 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
2040 << PrevSpec
2041 << FixItHint::CreateRemoval(Tok.getLocation());
2042
David Majnemera5433082013-10-18 00:33:31 +00002043 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
2044 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalldb632ac2012-09-25 07:32:39 +00002045 Diag(Tok.getLocation(), diag::err_override_control_interface)
2046 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemera5433082013-10-18 00:33:31 +00002047 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
2048 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalldb632ac2012-09-25 07:32:39 +00002049 } else {
David Majnemera5433082013-10-18 00:33:31 +00002050 Diag(Tok.getLocation(),
2051 getLangOpts().CPlusPlus11
2052 ? diag::warn_cxx98_compat_override_control_keyword
2053 : diag::ext_override_control_keyword)
2054 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalldb632ac2012-09-25 07:32:39 +00002055 }
Anders Carlsson56104902011-01-17 03:05:47 +00002056 ConsumeToken();
2057 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002058}
2059
Richard Smith89645bc2013-01-02 12:01:23 +00002060/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002061/// 'final' or Microsoft 'sealed' contextual keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00002062bool Parser::isCXX11FinalKeyword() const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00002063 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
2064 return Specifier == VirtSpecifiers::VS_Final ||
2065 Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002066}
2067
Richard Smith72553fc2014-01-23 23:53:27 +00002068/// \brief Parse a C++ member-declarator up to, but not including, the optional
2069/// brace-or-equal-initializer or pure-specifier.
Nico Weberd89e6f72015-01-16 19:34:13 +00002070bool Parser::ParseCXXMemberDeclaratorBeforeInitializer(
Richard Smith72553fc2014-01-23 23:53:27 +00002071 Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
2072 LateParsedAttrList &LateParsedAttrs) {
2073 // member-declarator:
2074 // declarator pure-specifier[opt]
2075 // declarator brace-or-equal-initializer[opt]
2076 // identifier[opt] ':' constant-expression
Serge Pavlov458ea762014-07-16 05:16:52 +00002077 if (Tok.isNot(tok::colon))
Richard Smith72553fc2014-01-23 23:53:27 +00002078 ParseDeclarator(DeclaratorInfo);
Richard Smith3d1a94c2014-08-12 00:22:39 +00002079 else
2080 DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation());
Richard Smith72553fc2014-01-23 23:53:27 +00002081
2082 if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002083 assert(DeclaratorInfo.isPastIdentifier() &&
2084 "don't know where identifier would go yet?");
Richard Smith72553fc2014-01-23 23:53:27 +00002085 BitfieldSize = ParseConstantExpression();
2086 if (BitfieldSize.isInvalid())
2087 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002088 } else {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002089 ParseOptionalCXX11VirtSpecifierSeq(
2090 VS, getCurrentClass().IsInterface,
2091 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002092 if (!VS.isUnset())
2093 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
2094 }
Richard Smith72553fc2014-01-23 23:53:27 +00002095
2096 // If a simple-asm-expr is present, parse it.
2097 if (Tok.is(tok::kw_asm)) {
2098 SourceLocation Loc;
2099 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
2100 if (AsmLabel.isInvalid())
2101 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2102
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002103 DeclaratorInfo.setAsmLabel(AsmLabel.get());
Richard Smith72553fc2014-01-23 23:53:27 +00002104 DeclaratorInfo.SetRangeEnd(Loc);
2105 }
2106
2107 // If attributes exist after the declarator, but before an '{', parse them.
2108 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Richard Smith4b5a9492014-01-24 22:34:35 +00002109
2110 // For compatibility with code written to older Clang, also accept a
2111 // virt-specifier *after* the GNU attributes.
Aaron Ballman5d153e32014-08-04 17:03:51 +00002112 if (BitfieldSize.isUnset() && VS.isUnset()) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002113 ParseOptionalCXX11VirtSpecifierSeq(
2114 VS, getCurrentClass().IsInterface,
2115 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Aaron Ballman5d153e32014-08-04 17:03:51 +00002116 if (!VS.isUnset()) {
2117 // If we saw any GNU-style attributes that are known to GCC followed by a
2118 // virt-specifier, issue a GCC-compat warning.
2119 const AttributeList *Attr = DeclaratorInfo.getAttributes();
2120 while (Attr) {
2121 if (Attr->isKnownToGCC() && !Attr->isCXX11Attribute())
2122 Diag(Attr->getLoc(), diag::warn_gcc_attribute_location);
2123 Attr = Attr->getNext();
2124 }
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002125 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
Aaron Ballman5d153e32014-08-04 17:03:51 +00002126 }
2127 }
Nico Weberd89e6f72015-01-16 19:34:13 +00002128
2129 // If this has neither a name nor a bit width, something has gone seriously
2130 // wrong. Skip until the semi-colon or }.
2131 if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
2132 // If so, skip until the semi-colon or a }.
2133 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2134 return true;
2135 }
2136 return false;
Richard Smith72553fc2014-01-23 23:53:27 +00002137}
2138
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002139/// \brief Look for declaration specifiers possibly occurring after C++11
2140/// virt-specifier-seq and diagnose them.
2141void Parser::MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(
2142 Declarator &D,
2143 VirtSpecifiers &VS) {
2144 DeclSpec DS(AttrFactory);
2145
2146 // GNU-style and C++11 attributes are not allowed here, but they will be
2147 // handled by the caller. Diagnose everything else.
2148 ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed, false);
2149 D.ExtendWithDeclSpec(DS);
2150
2151 if (D.isFunctionDeclarator()) {
Ehsan Akhgaric07d1e22015-03-25 00:53:33 +00002152 auto &Function = D.getFunctionTypeInfo();
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002153 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2154 auto DeclSpecCheck = [&] (DeclSpec::TQ TypeQual,
2155 const char *FixItName,
2156 SourceLocation SpecLoc,
2157 unsigned* QualifierLoc) {
2158 FixItHint Insertion;
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002159 if (DS.getTypeQualifiers() & TypeQual) {
2160 if (!(Function.TypeQuals & TypeQual)) {
2161 std::string Name(FixItName);
2162 Name += " ";
2163 Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name.c_str());
2164 Function.TypeQuals |= TypeQual;
2165 *QualifierLoc = SpecLoc.getRawEncoding();
2166 }
2167 Diag(SpecLoc, diag::err_declspec_after_virtspec)
2168 << FixItName
2169 << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
2170 << FixItHint::CreateRemoval(SpecLoc)
2171 << Insertion;
2172 }
2173 };
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002174 DeclSpecCheck(DeclSpec::TQ_const, "const", DS.getConstSpecLoc(),
2175 &Function.ConstQualifierLoc);
2176 DeclSpecCheck(DeclSpec::TQ_volatile, "volatile", DS.getVolatileSpecLoc(),
2177 &Function.VolatileQualifierLoc);
2178 DeclSpecCheck(DeclSpec::TQ_restrict, "restrict", DS.getRestrictSpecLoc(),
2179 &Function.RestrictQualifierLoc);
2180 }
Ehsan Akhgaric07d1e22015-03-25 00:53:33 +00002181
2182 // Parse ref-qualifiers.
2183 bool RefQualifierIsLValueRef = true;
2184 SourceLocation RefQualifierLoc;
2185 if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) {
2186 const char *Name = (RefQualifierIsLValueRef ? "& " : "&& ");
2187 FixItHint Insertion = FixItHint::CreateInsertion(VS.getFirstLocation(), Name);
2188 Function.RefQualifierIsLValueRef = RefQualifierIsLValueRef;
2189 Function.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
2190
2191 Diag(RefQualifierLoc, diag::err_declspec_after_virtspec)
2192 << (RefQualifierIsLValueRef ? "&" : "&&")
2193 << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
2194 << FixItHint::CreateRemoval(RefQualifierLoc)
2195 << Insertion;
2196 D.SetRangeEnd(RefQualifierLoc);
2197 }
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00002198 }
2199}
2200
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002201/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
2202///
2203/// member-declaration:
2204/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
2205/// function-definition ';'[opt]
2206/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
2207/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00002208/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002209/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002210/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002211///
2212/// member-declarator-list:
2213/// member-declarator
2214/// member-declarator-list ',' member-declarator
2215///
2216/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002217/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002218/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00002219/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002220/// identifier[opt] ':' constant-expression
2221///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002222/// virt-specifier-seq:
2223/// virt-specifier
2224/// virt-specifier-seq virt-specifier
2225///
2226/// virt-specifier:
2227/// override
2228/// final
David Majnemera5433082013-10-18 00:33:31 +00002229/// [MS] sealed
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002230///
Sebastian Redl42e92c42009-04-12 17:16:29 +00002231/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002232/// '= 0'
2233///
2234/// constant-initializer:
2235/// '=' constant-expression
2236///
Alexey Bataev05c25d62015-07-31 08:42:25 +00002237Parser::DeclGroupPtrTy
2238Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
2239 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00002240 const ParsedTemplateInfo &TemplateInfo,
2241 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00002242 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002243 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00002244 Diag(Tok, diag::err_at_defs_cxx);
2245 else
2246 Diag(Tok, diag::err_at_in_class);
Richard Smithda35e962013-11-09 04:52:51 +00002247
Douglas Gregor23c84762011-04-14 17:21:19 +00002248 ConsumeToken();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002249 SkipUntil(tok::r_brace, StopAtSemi);
Alexey Bataev05c25d62015-07-31 08:42:25 +00002250 return DeclGroupPtrTy();
Douglas Gregor23c84762011-04-14 17:21:19 +00002251 }
Richard Smithda35e962013-11-09 04:52:51 +00002252
Serge Pavlov458ea762014-07-16 05:16:52 +00002253 // Turn on colon protection early, while parsing declspec, although there is
2254 // nothing to protect there. It prevents from false errors if error recovery
2255 // incorrectly determines where the declspec ends, as in the example:
2256 // struct A { enum class B { C }; };
2257 // const int C = 4;
2258 // struct D { A::B : C; };
2259 ColonProtectionRAIIObject X(*this);
2260
John McCalla0097262009-12-11 02:10:03 +00002261 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00002262 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00002263 if (!TemplateInfo.Kind &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002264 Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw___super)) {
Richard Smith45855df2012-05-09 08:23:23 +00002265 if (TryAnnotateCXXScopeToken())
2266 MalformedTypeSpec = true;
2267
2268 bool isAccessDecl;
2269 if (Tok.isNot(tok::annot_cxxscope))
2270 isAccessDecl = false;
2271 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00002272 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
2273 else
2274 isAccessDecl = NextToken().is(tok::kw_operator);
2275
2276 if (isAccessDecl) {
2277 // Collect the scope specifier token we annotated earlier.
2278 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002279 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2280 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00002281
Nico Weberef03e702014-09-10 00:59:37 +00002282 if (SS.isInvalid()) {
2283 SkipUntil(tok::semi);
Alexey Bataev05c25d62015-07-31 08:42:25 +00002284 return DeclGroupPtrTy();
Nico Weberef03e702014-09-10 00:59:37 +00002285 }
2286
John McCalla0097262009-12-11 02:10:03 +00002287 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00002288 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00002289 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +00002290 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
2291 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00002292 SkipUntil(tok::semi);
Alexey Bataev05c25d62015-07-31 08:42:25 +00002293 return DeclGroupPtrTy();
John McCalla0097262009-12-11 02:10:03 +00002294 }
2295
2296 // TODO: recover from mistakenly-qualified operator declarations.
Alp Toker383d2c42014-01-01 03:08:43 +00002297 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2298 "access declaration")) {
2299 SkipUntil(tok::semi);
Alexey Bataev05c25d62015-07-31 08:42:25 +00002300 return DeclGroupPtrTy();
Alp Toker383d2c42014-01-01 03:08:43 +00002301 }
John McCalla0097262009-12-11 02:10:03 +00002302
Alexey Bataev05c25d62015-07-31 08:42:25 +00002303 return DeclGroupPtrTy::make(DeclGroupRef(Actions.ActOnUsingDeclaration(
2304 getCurScope(), AS,
2305 /* HasUsingKeyword */ false, SourceLocation(), SS, Name,
2306 /* AttrList */ nullptr,
2307 /* HasTypenameKeyword */ false, SourceLocation())));
John McCalla0097262009-12-11 02:10:03 +00002308 }
2309 }
2310
Aaron Ballmane7c544d2014-08-04 20:28:35 +00002311 // static_assert-declaration. A templated static_assert declaration is
2312 // diagnosed in Parser::ParseSingleDeclarationAfterTemplate.
2313 if (!TemplateInfo.Kind &&
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002314 Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert)) {
Chris Lattner49836b42009-04-02 04:16:50 +00002315 SourceLocation DeclEnd;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002316 return DeclGroupPtrTy::make(
2317 DeclGroupRef(ParseStaticAssertDeclaration(DeclEnd)));
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002318 }
Mike Stump11289f42009-09-09 15:08:12 +00002319
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002320 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00002321 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00002322 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00002323 SourceLocation DeclEnd;
Alexey Bataev05c25d62015-07-31 08:42:25 +00002324 return DeclGroupPtrTy::make(
2325 DeclGroupRef(ParseDeclarationStartingWithTemplate(
2326 Declarator::MemberContext, DeclEnd, AS, AccessAttrs)));
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002327 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002328
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002329 // Handle: member-declaration ::= '__extension__' member-declaration
2330 if (Tok.is(tok::kw___extension__)) {
2331 // __extension__ silences extension warnings in the subexpression.
2332 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2333 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002334 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2335 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002336 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002337
John McCall084e83d2011-03-24 11:26:52 +00002338 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002339 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002340 // Optional C++11 attribute-specifier
2341 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002342 // We need to keep these attributes for future diagnostic
2343 // before they are taken over by declaration specifier.
2344 FnAttrs.addAll(attrs.getList());
2345 FnAttrs.Range = attrs.Range;
2346
John McCall53fa7142010-12-24 02:08:15 +00002347 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002348
Douglas Gregorfec52632009-06-20 00:51:54 +00002349 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002350 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002351
Douglas Gregorfec52632009-06-20 00:51:54 +00002352 // Eat 'using'.
2353 SourceLocation UsingLoc = ConsumeToken();
2354
2355 if (Tok.is(tok::kw_namespace)) {
2356 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002357 SkipUntil(tok::semi, StopBeforeMatch);
Alexey Bataev05c25d62015-07-31 08:42:25 +00002358 return DeclGroupPtrTy();
Douglas Gregorfec52632009-06-20 00:51:54 +00002359 }
Alexey Bataev05c25d62015-07-31 08:42:25 +00002360 SourceLocation DeclEnd;
2361 // Otherwise, it must be a using-declaration or an alias-declaration.
2362 return DeclGroupPtrTy::make(DeclGroupRef(ParseUsingDeclaration(
2363 Declarator::MemberContext, TemplateInfo, UsingLoc, DeclEnd, AS)));
Douglas Gregorfec52632009-06-20 00:51:54 +00002364 }
2365
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002366 // Hold late-parsed attributes so we can attach a Decl to them later.
2367 LateParsedAttrList CommonLateParsedAttrs;
2368
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002369 // decl-specifier-seq:
2370 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002371 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002372 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002373 if (MalformedTypeSpec)
2374 DS.SetTypeSpecError();
Richard Smith72553fc2014-01-23 23:53:27 +00002375
Serge Pavlov458ea762014-07-16 05:16:52 +00002376 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2377 &CommonLateParsedAttrs);
2378
2379 // Turn off colon protection that was set for declspec.
2380 X.restore();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002381
Richard Smith404dfb42013-11-19 22:47:36 +00002382 // If we had a free-standing type definition with a missing semicolon, we
2383 // may get this far before the problem becomes obvious.
2384 if (DS.hasTagDefinition() &&
2385 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2386 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2387 &CommonLateParsedAttrs))
Alexey Bataev05c25d62015-07-31 08:42:25 +00002388 return DeclGroupPtrTy();
Richard Smith404dfb42013-11-19 22:47:36 +00002389
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002390 MultiTemplateParamsArg TemplateParams(
Craig Topper161e4db2014-05-21 06:02:52 +00002391 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data()
2392 : nullptr,
John McCall11083da2009-09-16 22:47:08 +00002393 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2394
Alp Toker35d87032013-12-30 23:29:50 +00002395 if (TryConsumeToken(tok::semi)) {
Michael Handdc016d2012-11-28 23:17:40 +00002396 if (DS.isFriendSpecified())
2397 ProhibitAttributes(FnAttrs);
2398
John McCall48871652010-08-21 09:40:31 +00002399 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00002400 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00002401 DS.complete(TheDecl);
Alexey Bataev05c25d62015-07-31 08:42:25 +00002402 return DeclGroupPtrTy::make(DeclGroupRef(TheDecl));
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002403 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002404
John McCall28a6aea2009-11-04 02:18:39 +00002405 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002406 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002407
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002408 // Hold late-parsed attributes so we can attach a Decl to them later.
2409 LateParsedAttrList LateParsedAttrs;
2410
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002411 SourceLocation EqualLoc;
Richard Smith9ba0fec2015-06-30 01:28:56 +00002412 SourceLocation PureSpecLoc;
2413
Yaron Keren180c1672015-06-30 07:35:19 +00002414 auto TryConsumePureSpecifier = [&] (bool AllowDefinition) {
Richard Smith9ba0fec2015-06-30 01:28:56 +00002415 if (Tok.isNot(tok::equal))
2416 return false;
2417
2418 auto &Zero = NextToken();
2419 SmallString<8> Buffer;
2420 if (Zero.isNot(tok::numeric_constant) || Zero.getLength() != 1 ||
2421 PP.getSpelling(Zero, Buffer) != "0")
2422 return false;
2423
2424 auto &After = GetLookAheadToken(2);
2425 if (!After.isOneOf(tok::semi, tok::comma) &&
2426 !(AllowDefinition &&
2427 After.isOneOf(tok::l_brace, tok::colon, tok::kw_try)))
2428 return false;
2429
2430 EqualLoc = ConsumeToken();
2431 PureSpecLoc = ConsumeToken();
2432 return true;
2433 };
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002434
Richard Smith72553fc2014-01-23 23:53:27 +00002435 SmallVector<Decl *, 8> DeclsInGroup;
2436 ExprResult BitfieldSize;
2437 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002438
Richard Smith72553fc2014-01-23 23:53:27 +00002439 // Parse the first declarator.
Nico Weberd89e6f72015-01-16 19:34:13 +00002440 if (ParseCXXMemberDeclaratorBeforeInitializer(
2441 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) {
Richard Smith72553fc2014-01-23 23:53:27 +00002442 TryConsumeToken(tok::semi);
Alexey Bataev05c25d62015-07-31 08:42:25 +00002443 return DeclGroupPtrTy();
Richard Smith72553fc2014-01-23 23:53:27 +00002444 }
John Thompson5bc5cbe2009-11-25 22:58:06 +00002445
Richard Smith72553fc2014-01-23 23:53:27 +00002446 // Check for a member function definition.
Richard Smith4b5a9492014-01-24 22:34:35 +00002447 if (BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002448 // MSVC permits pure specifier on inline functions defined at class scope.
Francois Pichet3abc9b82011-05-11 02:14:46 +00002449 // Hence check for =0 before checking for function definition.
Richard Smith9ba0fec2015-06-30 01:28:56 +00002450 if (getLangOpts().MicrosoftExt && DeclaratorInfo.isDeclarationOfFunction())
2451 TryConsumePureSpecifier(/*AllowDefinition*/ true);
Francois Pichet3abc9b82011-05-11 02:14:46 +00002452
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002453 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002454 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002455 //
2456 // In C++11, a non-function declarator followed by an open brace is a
2457 // braced-init-list for an in-class member initialization, not an
2458 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002459 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002460 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002461 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002462 if (Tok.isOneOf(tok::l_brace, tok::colon, tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002463 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002464 } else if (Tok.is(tok::equal)) {
2465 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002466 if (KW.is(tok::kw_default))
2467 DefinitionKind = FDK_Defaulted;
2468 else if (KW.is(tok::kw_delete))
2469 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002470 }
2471 }
Eli Bendersky41842222015-03-23 23:49:41 +00002472 DeclaratorInfo.setFunctionDefinitionKind(DefinitionKind);
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002473
Michael Handdc016d2012-11-28 23:17:40 +00002474 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2475 // to a friend declaration, that declaration shall be a definition.
2476 if (DeclaratorInfo.isFunctionDeclarator() &&
2477 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2478 // Diagnose attributes that appear before decl specifier:
2479 // [[]] friend int foo();
2480 ProhibitAttributes(FnAttrs);
2481 }
2482
Nico Webera7f137d2015-01-16 19:35:01 +00002483 if (DefinitionKind != FDK_Declaration) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002484 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002485 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002486 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002487 SkipUntil(tok::r_brace);
Michael Handdc016d2012-11-28 23:17:40 +00002488
Douglas Gregor8a4db832011-01-19 16:41:58 +00002489 // Consume the optional ';'
Alp Toker35d87032013-12-30 23:29:50 +00002490 TryConsumeToken(tok::semi);
2491
Alexey Bataev05c25d62015-07-31 08:42:25 +00002492 return DeclGroupPtrTy();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002493 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002494
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002495 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002496 Diag(DeclaratorInfo.getIdentifierLoc(),
2497 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002498
Richard Smith2603b092012-11-15 22:54:20 +00002499 // Recover by treating the 'typedef' as spurious.
2500 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002501 }
2502
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002503 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002504 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Richard Smith9ba0fec2015-06-30 01:28:56 +00002505 VS, PureSpecLoc);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002506
David Majnemer23252a32013-08-01 04:22:55 +00002507 if (FunDecl) {
2508 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2509 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2510 }
2511 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2512 LateParsedAttrs[i]->addDecl(FunDecl);
2513 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002514 }
2515 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002516
2517 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002518 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002519 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002520
Alexey Bataev05c25d62015-07-31 08:42:25 +00002521 return DeclGroupPtrTy::make(DeclGroupRef(FunDecl));
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002522 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002523 }
2524
2525 // member-declarator-list:
2526 // member-declarator
2527 // member-declarator-list ',' member-declarator
2528
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002529 while (1) {
Richard Smith2b013182012-06-10 03:12:00 +00002530 InClassInitStyle HasInClassInit = ICIS_NoInit;
Richard Smith9ba0fec2015-06-30 01:28:56 +00002531 bool HasStaticInitializer = false;
2532 if (Tok.isOneOf(tok::equal, tok::l_brace) && PureSpecLoc.isInvalid()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002533 if (BitfieldSize.get()) {
2534 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002535 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith9ba0fec2015-06-30 01:28:56 +00002536 } else if (DeclaratorInfo.isDeclarationOfFunction()) {
2537 // It's a pure-specifier.
2538 if (!TryConsumePureSpecifier(/*AllowFunctionDefinition*/ false))
2539 // Parse it as an expression so that Sema can diagnose it.
2540 HasStaticInitializer = true;
2541 } else if (DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2542 DeclSpec::SCS_static &&
2543 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2544 DeclSpec::SCS_typedef &&
2545 !DS.isFriendSpecified()) {
2546 // It's a default member initializer.
2547 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002548 } else {
Richard Smith9ba0fec2015-06-30 01:28:56 +00002549 HasStaticInitializer = true;
Richard Smith938f40b2011-06-11 17:19:42 +00002550 }
2551 }
2552
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002553 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002554 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002555 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002556
Craig Topper161e4db2014-05-21 06:02:52 +00002557 NamedDecl *ThisDecl = nullptr;
John McCall07e91c02009-08-06 02:15:43 +00002558 if (DS.isFriendSpecified()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002559 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
Michael Handdc016d2012-11-28 23:17:40 +00002560 // to a friend declaration, that declaration shall be a definition.
2561 //
Richard Smith72553fc2014-01-23 23:53:27 +00002562 // Diagnose attributes that appear in a friend member function declarator:
2563 // friend int foo [[]] ();
Michael Handdc016d2012-11-28 23:17:40 +00002564 SmallVector<SourceRange, 4> Ranges;
2565 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
Richard Smith72553fc2014-01-23 23:53:27 +00002566 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
2567 E = Ranges.end(); I != E; ++I)
2568 Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
Michael Handdc016d2012-11-28 23:17:40 +00002569
Douglas Gregor0be31a22010-07-02 17:43:08 +00002570 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002571 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002572 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002573 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002574 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002575 TemplateParams,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002576 BitfieldSize.get(),
Richard Smith2b013182012-06-10 03:12:00 +00002577 VS, HasInClassInit);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002578
2579 if (VarTemplateDecl *VT =
Craig Topper161e4db2014-05-21 06:02:52 +00002580 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr)
Larisse Voufo39a1e502013-08-06 01:03:05 +00002581 // Re-direct this decl to refer to the templated decl so that we can
2582 // initialize it.
2583 ThisDecl = VT->getTemplatedDecl();
2584
David Majnemer23252a32013-08-01 04:22:55 +00002585 if (ThisDecl && AccessAttrs)
Richard Smithf8a75c32013-08-29 00:47:48 +00002586 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00002587 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002588
Richard Smith9ba0fec2015-06-30 01:28:56 +00002589 // Error recovery might have converted a non-static member into a static
2590 // member.
David Blaikie35506f82013-01-30 01:22:18 +00002591 if (HasInClassInit != ICIS_NoInit &&
Richard Smith9ba0fec2015-06-30 01:28:56 +00002592 DeclaratorInfo.getDeclSpec().getStorageClassSpec() ==
2593 DeclSpec::SCS_static) {
2594 HasInClassInit = ICIS_NoInit;
2595 HasStaticInitializer = true;
2596 }
2597
2598 if (ThisDecl && PureSpecLoc.isValid())
2599 Actions.ActOnPureSpecifier(ThisDecl, PureSpecLoc);
2600
2601 // Handle the initializer.
2602 if (HasInClassInit != ICIS_NoInit) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002603 // The initializer was deferred; parse it and cache the tokens.
David Majnemer23252a32013-08-01 04:22:55 +00002604 Diag(Tok, getLangOpts().CPlusPlus11
2605 ? diag::warn_cxx98_compat_nonstatic_member_init
2606 : diag::ext_nonstatic_member_init);
Richard Smith5d164bc2011-10-15 05:09:34 +00002607
Richard Smith938f40b2011-06-11 17:19:42 +00002608 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002609 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2610 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002611 //
2612 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002613 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002614 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002615 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemer23252a32013-08-01 04:22:55 +00002616
2617 // Avoid later warnings about a class member of incomplete type.
David Blaikiecdd91db2012-02-14 09:00:46 +00002618 if (ThisDecl)
David Blaikiecdd91db2012-02-14 09:00:46 +00002619 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002620 } else
2621 ParseCXXNonStaticMemberInitializer(ThisDecl);
Richard Smith9ba0fec2015-06-30 01:28:56 +00002622 } else if (HasStaticInitializer) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002623 // Normal initializer.
Richard Smith9ba0fec2015-06-30 01:28:56 +00002624 ExprResult Init = ParseCXXMemberInitializer(
2625 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
David Majnemer23252a32013-08-01 04:22:55 +00002626
Douglas Gregor728d00b2011-10-10 14:49:18 +00002627 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002628 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002629 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002630 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smith74aeef52013-04-26 16:15:35 +00002631 DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002632 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor728d00b2011-10-10 14:49:18 +00002633 // No initializer.
Richard Smith74aeef52013-04-26 16:15:35 +00002634 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002635
Douglas Gregor728d00b2011-10-10 14:49:18 +00002636 if (ThisDecl) {
David Majnemer23252a32013-08-01 04:22:55 +00002637 if (!ThisDecl->isInvalidDecl()) {
2638 // Set the Decl for any late parsed attributes
2639 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2640 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2641
2642 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2643 LateParsedAttrs[i]->addDecl(ThisDecl);
2644 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002645 Actions.FinalizeDeclaration(ThisDecl);
2646 DeclsInGroup.push_back(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002647
2648 if (DeclaratorInfo.isFunctionDeclarator() &&
2649 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2650 DeclSpec::SCS_typedef)
2651 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002652 }
David Majnemer23252a32013-08-01 04:22:55 +00002653 LateParsedAttrs.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002654
2655 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002656
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002657 // If we don't have a comma, it is either the end of the list (a ';')
2658 // or an error, bail out.
Alp Toker094e5212014-01-05 03:27:11 +00002659 SourceLocation CommaLoc;
2660 if (!TryConsumeToken(tok::comma, CommaLoc))
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002661 break;
Mike Stump11289f42009-09-09 15:08:12 +00002662
Richard Smithc8a79032012-01-09 22:31:44 +00002663 if (Tok.isAtStartOfLine() &&
2664 !MightBeDeclarator(Declarator::MemberContext)) {
2665 // This comma was followed by a line-break and something which can't be
2666 // the start of a declarator. The comma was probably a typo for a
2667 // semicolon.
2668 Diag(CommaLoc, diag::err_expected_semi_declaration)
2669 << FixItHint::CreateReplacement(CommaLoc, ";");
2670 ExpectSemi = false;
2671 break;
2672 }
Mike Stump11289f42009-09-09 15:08:12 +00002673
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002674 // Parse the next declarator.
2675 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002676 VS.clear();
Nico Weberf56c85b2015-01-17 02:26:40 +00002677 BitfieldSize = ExprResult(/*Invalid=*/false);
Richard Smith9ba0fec2015-06-30 01:28:56 +00002678 EqualLoc = PureSpecLoc = SourceLocation();
Richard Smith8d06f422012-01-12 23:53:29 +00002679 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002680
Richard Smith72553fc2014-01-23 23:53:27 +00002681 // GNU attributes are allowed before the second and subsequent declarator.
John McCall53fa7142010-12-24 02:08:15 +00002682 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002683
Nico Weberd89e6f72015-01-16 19:34:13 +00002684 if (ParseCXXMemberDeclaratorBeforeInitializer(
2685 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs))
2686 break;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002687 }
2688
Richard Smithc8a79032012-01-09 22:31:44 +00002689 if (ExpectSemi &&
2690 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002691 // Skip to end of block or statement.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002692 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002693 // If we stopped at a ';', eat it.
Alp Toker35d87032013-12-30 23:29:50 +00002694 TryConsumeToken(tok::semi);
Alexey Bataev05c25d62015-07-31 08:42:25 +00002695 return DeclGroupPtrTy();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002696 }
2697
Alexey Bataev05c25d62015-07-31 08:42:25 +00002698 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002699}
2700
Richard Smith9ba0fec2015-06-30 01:28:56 +00002701/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer.
2702/// Also detect and reject any attempted defaulted/deleted function definition.
2703/// The location of the '=', if any, will be placed in EqualLoc.
Richard Smith938f40b2011-06-11 17:19:42 +00002704///
Richard Smith9ba0fec2015-06-30 01:28:56 +00002705/// This does not check for a pure-specifier; that's handled elsewhere.
Sebastian Redleef474c2012-02-22 10:50:08 +00002706///
Richard Smith938f40b2011-06-11 17:19:42 +00002707/// brace-or-equal-initializer:
2708/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002709/// braced-init-list
2710///
Richard Smith938f40b2011-06-11 17:19:42 +00002711/// initializer-clause:
2712/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002713/// braced-init-list
2714///
Richard Smithda35e962013-11-09 04:52:51 +00002715/// defaulted/deleted function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002716/// '=' 'default'
2717/// '=' 'delete'
2718///
2719/// Prior to C++0x, the assignment-expression in an initializer-clause must
2720/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002721ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002722 SourceLocation &EqualLoc) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002723 assert(Tok.isOneOf(tok::equal, tok::l_brace)
Richard Smith938f40b2011-06-11 17:19:42 +00002724 && "Data member initializer not starting with '=' or '{'");
2725
Douglas Gregor926410d2012-02-21 02:22:07 +00002726 EnterExpressionEvaluationContext Context(Actions,
2727 Sema::PotentiallyEvaluated,
2728 D);
Alp Toker094e5212014-01-05 03:27:11 +00002729 if (TryConsumeToken(tok::equal, EqualLoc)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002730 if (Tok.is(tok::kw_delete)) {
2731 // In principle, an initializer of '= delete p;' is legal, but it will
2732 // never type-check. It's better to diagnose it as an ill-formed expression
2733 // than as an ill-formed deleted non-function member.
2734 // An initializer of '= delete p, foo' will never be parsed, because
2735 // a top-level comma always ends the initializer expression.
2736 const Token &Next = NextToken();
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00002737 if (IsFunction || Next.isOneOf(tok::semi, tok::comma, tok::eof)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002738 if (IsFunction)
2739 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2740 << 1 /* delete */;
2741 else
2742 Diag(ConsumeToken(), diag::err_deleted_non_function);
Richard Smithedcb26e2014-06-11 00:49:52 +00002743 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002744 }
2745 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002746 if (IsFunction)
2747 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2748 << 0 /* default */;
2749 else
2750 Diag(ConsumeToken(), diag::err_default_special_members);
Richard Smithedcb26e2014-06-11 00:49:52 +00002751 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002752 }
David Majnemer87ff66c2014-12-13 11:34:16 +00002753 }
2754 if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) {
2755 Diag(Tok, diag::err_ms_property_initializer) << PD;
2756 return ExprError();
Sebastian Redleef474c2012-02-22 10:50:08 +00002757 }
2758 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002759}
2760
Richard Smith65ebb4a2015-03-26 04:09:53 +00002761void Parser::SkipCXXMemberSpecification(SourceLocation RecordLoc,
2762 SourceLocation AttrFixitLoc,
2763 unsigned TagType, Decl *TagDecl) {
2764 // Skip the optional 'final' keyword.
2765 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
2766 assert(isCXX11FinalKeyword() && "not a class definition");
2767 ConsumeToken();
2768
2769 // Diagnose any C++11 attributes after 'final' keyword.
2770 // We deliberately discard these attributes.
2771 ParsedAttributesWithRange Attrs(AttrFactory);
2772 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
2773
2774 // This can only happen if we had malformed misplaced attributes;
2775 // we only get called if there is a colon or left-brace after the
2776 // attributes.
2777 if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_brace))
2778 return;
2779 }
2780
2781 // Skip the base clauses. This requires actually parsing them, because
2782 // otherwise we can't be sure where they end (a left brace may appear
2783 // within a template argument).
2784 if (Tok.is(tok::colon)) {
2785 // Enter the scope of the class so that we can correctly parse its bases.
2786 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
2787 ParsingClassDefinition ParsingDef(*this, TagDecl, /*NonNestedClass*/ true,
2788 TagType == DeclSpec::TST_interface);
Richard Smith0f192e82015-06-11 22:48:25 +00002789 auto OldContext =
2790 Actions.ActOnTagStartSkippedDefinition(getCurScope(), TagDecl);
Richard Smith65ebb4a2015-03-26 04:09:53 +00002791
2792 // Parse the bases but don't attach them to the class.
2793 ParseBaseClause(nullptr);
2794
Richard Smith0f192e82015-06-11 22:48:25 +00002795 Actions.ActOnTagFinishSkippedDefinition(OldContext);
Richard Smith65ebb4a2015-03-26 04:09:53 +00002796
2797 if (!Tok.is(tok::l_brace)) {
2798 Diag(PP.getLocForEndOfToken(PrevTokLocation),
2799 diag::err_expected_lbrace_after_base_specifiers);
2800 return;
2801 }
2802 }
2803
2804 // Skip the body.
2805 assert(Tok.is(tok::l_brace));
2806 BalancedDelimiterTracker T(*this, tok::l_brace);
2807 T.consumeOpen();
2808 T.skipToEnd();
Richard Smith04c6c1f2015-07-01 18:56:50 +00002809
2810 // Parse and discard any trailing attributes.
2811 ParsedAttributes Attrs(AttrFactory);
2812 if (Tok.is(tok::kw___attribute))
2813 MaybeParseGNUAttributes(Attrs);
Richard Smith65ebb4a2015-03-26 04:09:53 +00002814}
2815
Alexey Bataev05c25d62015-07-31 08:42:25 +00002816Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclarationWithPragmas(
2817 AccessSpecifier &AS, ParsedAttributesWithRange &AccessAttrs,
2818 DeclSpec::TST TagType, Decl *TagDecl) {
2819 if (getLangOpts().MicrosoftExt &&
2820 Tok.isOneOf(tok::kw___if_exists, tok::kw___if_not_exists)) {
2821 ParseMicrosoftIfExistsClassDeclaration(TagType, AS);
2822 return DeclGroupPtrTy();
2823 }
2824
2825 // Check for extraneous top-level semicolon.
2826 if (Tok.is(tok::semi)) {
2827 ConsumeExtraSemi(InsideStruct, TagType);
2828 return DeclGroupPtrTy();
2829 }
2830
2831 if (Tok.is(tok::annot_pragma_vis)) {
2832 HandlePragmaVisibility();
2833 return DeclGroupPtrTy();
2834 }
2835
2836 if (Tok.is(tok::annot_pragma_pack)) {
2837 HandlePragmaPack();
2838 return DeclGroupPtrTy();
2839 }
2840
2841 if (Tok.is(tok::annot_pragma_align)) {
2842 HandlePragmaAlign();
2843 return DeclGroupPtrTy();
2844 }
2845
2846 if (Tok.is(tok::annot_pragma_ms_pointers_to_members)) {
2847 HandlePragmaMSPointersToMembers();
2848 return DeclGroupPtrTy();
2849 }
2850
2851 if (Tok.is(tok::annot_pragma_ms_pragma)) {
2852 HandlePragmaMSPragma();
2853 return DeclGroupPtrTy();
2854 }
2855
2856 // If we see a namespace here, a close brace was missing somewhere.
2857 if (Tok.is(tok::kw_namespace)) {
2858 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
2859 return DeclGroupPtrTy();
2860 }
2861
2862 AccessSpecifier NewAS = getAccessSpecifierIfPresent();
2863 if (NewAS != AS_none) {
2864 // Current token is a C++ access specifier.
2865 AS = NewAS;
2866 SourceLocation ASLoc = Tok.getLocation();
2867 unsigned TokLength = Tok.getLength();
2868 ConsumeToken();
2869 AccessAttrs.clear();
2870 MaybeParseGNUAttributes(AccessAttrs);
2871
2872 SourceLocation EndLoc;
2873 if (TryConsumeToken(tok::colon, EndLoc)) {
2874 } else if (TryConsumeToken(tok::semi, EndLoc)) {
2875 Diag(EndLoc, diag::err_expected)
2876 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
2877 } else {
2878 EndLoc = ASLoc.getLocWithOffset(TokLength);
2879 Diag(EndLoc, diag::err_expected)
2880 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
2881 }
2882
2883 // The Microsoft extension __interface does not permit non-public
2884 // access specifiers.
2885 if (TagType == DeclSpec::TST_interface && AS != AS_public) {
2886 Diag(ASLoc, diag::err_access_specifier_interface) << (AS == AS_protected);
2887 }
2888
2889 if (Actions.ActOnAccessSpecifier(NewAS, ASLoc, EndLoc,
2890 AccessAttrs.getList())) {
2891 // found another attribute than only annotations
2892 AccessAttrs.clear();
2893 }
2894
2895 return DeclGroupPtrTy();
2896 }
2897
2898 if (Tok.is(tok::annot_pragma_openmp))
2899 return ParseOpenMPDeclarativeDirective();
2900
2901 // Parse all the comma separated declarators.
2902 return ParseCXXClassMemberDeclaration(AS, AccessAttrs.getList());
2903}
2904
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002905/// ParseCXXMemberSpecification - Parse the class definition.
2906///
2907/// member-specification:
2908/// member-declaration member-specification[opt]
2909/// access-specifier ':' member-specification[opt]
2910///
Joao Matose9a3ed42012-08-31 22:18:20 +00002911void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002912 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00002913 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002914 unsigned TagType, Decl *TagDecl) {
2915 assert((TagType == DeclSpec::TST_struct ||
2916 TagType == DeclSpec::TST_interface ||
2917 TagType == DeclSpec::TST_union ||
2918 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2919
John McCallfaf5fb42010-08-26 23:41:50 +00002920 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2921 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00002922
Douglas Gregoredf8f392010-01-16 20:52:59 +00002923 // Determine whether this is a non-nested class. Note that local
2924 // classes are *not* considered to be nested classes.
2925 bool NonNestedClass = true;
2926 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002927 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002928 if (S->isClassScope()) {
2929 // We're inside a class scope, so this is a nested class.
2930 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00002931
2932 // The Microsoft extension __interface does not permit nested classes.
2933 if (getCurrentClass().IsInterface) {
2934 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2935 << /*ErrorType=*/6
2936 << (isa<NamedDecl>(TagDecl)
2937 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
David Blaikieabe1a392014-04-02 05:58:29 +00002938 : "(anonymous)");
John McCalldb632ac2012-09-25 07:32:39 +00002939 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00002940 break;
2941 }
2942
Serge Pavlovd9c0bcf2015-07-14 10:02:10 +00002943 if ((S->getFlags() & Scope::FnScope))
2944 // If we're in a function or function template then this is a local
2945 // class rather than a nested class.
2946 break;
Douglas Gregoredf8f392010-01-16 20:52:59 +00002947 }
2948 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002949
2950 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00002951 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002952
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002953 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00002954 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2955 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002956
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002957 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002958 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002959
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002960 SourceLocation FinalLoc;
David Majnemera5433082013-10-18 00:33:31 +00002961 bool IsFinalSpelledSealed = false;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002962
2963 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002964 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemera5433082013-10-18 00:33:31 +00002965 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2966 assert((Specifier == VirtSpecifiers::VS_Final ||
2967 Specifier == VirtSpecifiers::VS_Sealed) &&
2968 "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00002969 FinalLoc = ConsumeToken();
David Majnemera5433082013-10-18 00:33:31 +00002970 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002971
David Majnemera5433082013-10-18 00:33:31 +00002972 if (TagType == DeclSpec::TST_interface)
John McCalldb632ac2012-09-25 07:32:39 +00002973 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemera5433082013-10-18 00:33:31 +00002974 << VirtSpecifiers::getSpecifierName(Specifier);
2975 else if (Specifier == VirtSpecifiers::VS_Final)
2976 Diag(FinalLoc, getLangOpts().CPlusPlus11
2977 ? diag::warn_cxx98_compat_override_control_keyword
2978 : diag::ext_override_control_keyword)
2979 << VirtSpecifiers::getSpecifierName(Specifier);
2980 else if (Specifier == VirtSpecifiers::VS_Sealed)
2981 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han9407e502012-11-26 22:54:45 +00002982
Michael Han309af292013-01-07 16:57:11 +00002983 // Parse any C++11 attributes after 'final' keyword.
2984 // These attributes are not allowed to appear here,
2985 // and the only possible place for them to appertain
2986 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00002987 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Nico Weber4b4be842014-12-29 06:56:50 +00002988
2989 // ParseClassSpecifier() does only a superficial check for attributes before
2990 // deciding to call this method. For example, for
2991 // `class C final alignas ([l) {` it will decide that this looks like a
2992 // misplaced attribute since it sees `alignas '(' ')'`. But the actual
2993 // attribute parsing code will try to parse the '[' as a constexpr lambda
2994 // and consume enough tokens that the alignas parsing code will eat the
2995 // opening '{'. So bail out if the next token isn't one we expect.
Nico Weber36de3a22014-12-29 21:56:22 +00002996 if (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)) {
2997 if (TagDecl)
2998 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
Nico Weber4b4be842014-12-29 06:56:50 +00002999 return;
Nico Weber36de3a22014-12-29 21:56:22 +00003000 }
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00003001 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00003002
John McCall2d814c32009-12-19 21:48:58 +00003003 if (Tok.is(tok::colon)) {
3004 ParseBaseClause(TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00003005 if (!Tok.is(tok::l_brace)) {
Ismail Pazarbasi129c44c2014-09-25 21:13:02 +00003006 bool SuggestFixIt = false;
3007 SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation);
3008 if (Tok.isAtStartOfLine()) {
3009 switch (Tok.getKind()) {
3010 case tok::kw_private:
3011 case tok::kw_protected:
3012 case tok::kw_public:
3013 SuggestFixIt = NextToken().getKind() == tok::colon;
3014 break;
3015 case tok::kw_static_assert:
3016 case tok::r_brace:
3017 case tok::kw_using:
3018 // base-clause can have simple-template-id; 'template' can't be there
3019 case tok::kw_template:
3020 SuggestFixIt = true;
3021 break;
3022 case tok::identifier:
3023 SuggestFixIt = isConstructorDeclarator(true);
3024 break;
3025 default:
3026 SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
3027 break;
3028 }
3029 }
3030 DiagnosticBuilder LBraceDiag =
3031 Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers);
3032 if (SuggestFixIt) {
3033 LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {");
3034 // Try recovering from missing { after base-clause.
3035 PP.EnterToken(Tok);
3036 Tok.setKind(tok::l_brace);
3037 } else {
3038 if (TagDecl)
3039 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
3040 return;
3041 }
John McCall2d814c32009-12-19 21:48:58 +00003042 }
3043 }
3044
3045 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003046 BalancedDelimiterTracker T(*this, tok::l_brace);
3047 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00003048
John McCall08bede42010-05-28 08:11:17 +00003049 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00003050 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemera5433082013-10-18 00:33:31 +00003051 IsFinalSpelledSealed,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003052 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00003053
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003054 // C++ 11p3: Members of a class defined with the keyword class are private
3055 // by default. Members of a class defined with the keywords struct or union
3056 // are public by default.
3057 AccessSpecifier CurAS;
3058 if (TagType == DeclSpec::TST_class)
3059 CurAS = AS_private;
3060 else
3061 CurAS = AS_public;
Alexey Bataev05c25d62015-07-31 08:42:25 +00003062 ParsedAttributesWithRange AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003063
Douglas Gregor9377c822010-06-21 22:31:09 +00003064 if (TagDecl) {
3065 // While we still have something to read, read the member-declarations.
Alexey Bataev05c25d62015-07-31 08:42:25 +00003066 while (Tok.isNot(tok::r_brace) && !isEofOrEom())
Douglas Gregor9377c822010-06-21 22:31:09 +00003067 // Each iteration of this loop reads one member-declaration.
Alexey Bataev05c25d62015-07-31 08:42:25 +00003068 ParseCXXClassMemberDeclarationWithPragmas(
3069 CurAS, AccessAttrs, static_cast<DeclSpec::TST>(TagType), TagDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003070
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003071 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00003072 } else {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003073 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003074 }
Mike Stump11289f42009-09-09 15:08:12 +00003075
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003076 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003077 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003078 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003079
John McCall08bede42010-05-28 08:11:17 +00003080 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00003081 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003082 T.getOpenLocation(),
3083 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003084 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003085
Douglas Gregor433e0532012-04-16 18:27:27 +00003086 // C++11 [class.mem]p2:
3087 // Within the class member-specification, the class is regarded as complete
Richard Smith0b3a4622014-11-13 20:01:57 +00003088 // within function bodies, default arguments, exception-specifications, and
Douglas Gregor433e0532012-04-16 18:27:27 +00003089 // brace-or-equal-initializers for non-static data members (including such
3090 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00003091 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003092 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00003093 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00003094 // declarations and the lexed inline method definitions, along with any
3095 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00003096 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00003097 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003098 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00003099
3100 // We've finished with all pending member declarations.
3101 Actions.ActOnFinishCXXMemberDecls();
3102
Richard Smith938f40b2011-06-11 17:19:42 +00003103 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003104 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00003105 PrevTokLocation = SavedPrevTokLocation;
Reid Klecknerbba3cb92015-03-17 19:00:50 +00003106
3107 // We've finished parsing everything, including default argument
3108 // initializers.
Hans Wennborg99000c22015-08-15 01:18:16 +00003109 Actions.ActOnFinishCXXNonNestedClass(TagDecl);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003110 }
3111
John McCall08bede42010-05-28 08:11:17 +00003112 if (TagDecl)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003113 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3114 T.getCloseLocation());
John McCall2ff380a2010-03-17 00:38:33 +00003115
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003116 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003117 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00003118 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00003119}
Douglas Gregore8381c02008-11-05 04:29:56 +00003120
Richard Smith2ac43ad2013-11-15 23:00:02 +00003121void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithda35e962013-11-09 04:52:51 +00003122 assert(Tok.is(tok::kw_namespace));
3123
3124 // FIXME: Suggest where the close brace should have gone by looking
3125 // at indentation changes within the definition body.
Richard Smith2ac43ad2013-11-15 23:00:02 +00003126 Diag(D->getLocation(),
3127 diag::err_missing_end_of_definition) << D;
Richard Smithda35e962013-11-09 04:52:51 +00003128 Diag(Tok.getLocation(),
Richard Smith2ac43ad2013-11-15 23:00:02 +00003129 diag::note_missing_end_of_definition_before) << D;
Richard Smithda35e962013-11-09 04:52:51 +00003130
3131 // Push '};' onto the token stream to recover.
3132 PP.EnterToken(Tok);
3133
3134 Tok.startToken();
3135 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
3136 Tok.setKind(tok::semi);
3137 PP.EnterToken(Tok);
3138
3139 Tok.setKind(tok::r_brace);
3140}
3141
Douglas Gregore8381c02008-11-05 04:29:56 +00003142/// ParseConstructorInitializer - Parse a C++ constructor initializer,
3143/// which explicitly initializes the members or base classes of a
3144/// class (C++ [class.base.init]). For example, the three initializers
3145/// after the ':' in the Derived constructor below:
3146///
3147/// @code
3148/// class Base { };
3149/// class Derived : Base {
3150/// int x;
3151/// float f;
3152/// public:
3153/// Derived(float f) : Base(), x(17), f(f) { }
3154/// };
3155/// @endcode
3156///
Mike Stump11289f42009-09-09 15:08:12 +00003157/// [C++] ctor-initializer:
3158/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00003159///
Mike Stump11289f42009-09-09 15:08:12 +00003160/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00003161/// mem-initializer ...[opt]
3162/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00003163void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Nico Weber3b00fdc2015-03-07 19:52:39 +00003164 assert(Tok.is(tok::colon) &&
3165 "Constructor initializer always starts with ':'");
Douglas Gregore8381c02008-11-05 04:29:56 +00003166
Nico Weber3b00fdc2015-03-07 19:52:39 +00003167 // Poison the SEH identifiers so they are flagged as illegal in constructor
3168 // initializers.
John Wiegley1c0675e2011-04-28 01:08:34 +00003169 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00003170 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003171
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003172 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00003173 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003174
Douglas Gregore8381c02008-11-05 04:29:56 +00003175 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00003176 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00003177 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
3178 MemInitializers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003179 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00003180 } else {
3181 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
3182 if (!MemInit.isInvalid())
3183 MemInitializers.push_back(MemInit.get());
3184 else
3185 AnyErrors = true;
3186 }
3187
Douglas Gregore8381c02008-11-05 04:29:56 +00003188 if (Tok.is(tok::comma))
3189 ConsumeToken();
3190 else if (Tok.is(tok::l_brace))
3191 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00003192 // If the next token looks like a base or member initializer, assume that
3193 // we're just missing a comma.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003194 else if (Tok.isOneOf(tok::identifier, tok::coloncolon)) {
Douglas Gregorce66d022010-09-07 14:51:08 +00003195 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3196 Diag(Loc, diag::err_ctor_init_missing_comma)
3197 << FixItHint::CreateInsertion(Loc, ", ");
3198 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00003199 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alp Tokerec543272013-12-24 09:48:30 +00003200 Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
3201 << tok::comma;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003202 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregore8381c02008-11-05 04:29:56 +00003203 break;
3204 }
3205 } while (true);
3206
David Blaikie3fc2f912013-01-17 05:26:25 +00003207 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00003208 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00003209}
3210
3211/// ParseMemInitializer - Parse a C++ member initializer, which is
3212/// part of a constructor initializer that explicitly initializes one
3213/// member or base class (C++ [class.base.init]). See
3214/// ParseConstructorInitializer for an example.
3215///
3216/// [C++] mem-initializer:
3217/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00003218/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00003219///
Douglas Gregore8381c02008-11-05 04:29:56 +00003220/// [C++] mem-initializer-id:
3221/// '::'[opt] nested-name-specifier[opt] class-name
3222/// identifier
Craig Topper9ad7e262014-10-31 06:57:07 +00003223MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00003224 // parse '::'[opt] nested-name-specifier[opt]
3225 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00003226 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00003227 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003228 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00003229 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00003230 if (TemplateId->Kind == TNK_Type_template ||
3231 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00003232 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003233 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00003234 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003235 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00003236 }
David Blaikie186a8892012-01-24 06:03:59 +00003237 // Uses of decltype will already have been converted to annot_decltype by
3238 // ParseOptionalCXXScopeSpecifier at this point.
3239 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
3240 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00003241 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00003242 return true;
3243 }
Mike Stump11289f42009-09-09 15:08:12 +00003244
Craig Topper161e4db2014-05-21 06:02:52 +00003245 IdentifierInfo *II = nullptr;
David Blaikie186a8892012-01-24 06:03:59 +00003246 DeclSpec DS(AttrFactory);
3247 SourceLocation IdLoc = Tok.getLocation();
3248 if (Tok.is(tok::annot_decltype)) {
3249 // Get the decltype expression, if there is one.
3250 ParseDecltypeSpecifier(DS);
3251 } else {
3252 if (Tok.is(tok::identifier))
3253 // Get the identifier. This may be a member name or a class name,
3254 // but we'll let the semantic analysis determine which it is.
3255 II = Tok.getIdentifierInfo();
3256 ConsumeToken();
3257 }
3258
Douglas Gregore8381c02008-11-05 04:29:56 +00003259
3260 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003261 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003262 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3263
Sebastian Redla74948d2011-09-24 17:48:25 +00003264 ExprResult InitList = ParseBraceInitializer();
3265 if (InitList.isInvalid())
3266 return true;
3267
3268 SourceLocation EllipsisLoc;
Alp Toker094e5212014-01-05 03:27:11 +00003269 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redla74948d2011-09-24 17:48:25 +00003270
3271 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00003272 TemplateTypeTy, DS, IdLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003273 InitList.get(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00003274 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003275 BalancedDelimiterTracker T(*this, tok::l_paren);
3276 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00003277
Sebastian Redl3da34892011-06-05 12:23:16 +00003278 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00003279 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00003280 CommaLocsTy CommaLocs;
3281 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003282 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00003283 return true;
3284 }
3285
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003286 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00003287
3288 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00003289 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00003290
3291 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00003292 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00003293 T.getOpenLocation(), ArgExprs,
3294 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00003295 }
3296
Alp Tokerec543272013-12-24 09:48:30 +00003297 if (getLangOpts().CPlusPlus11)
3298 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
3299 else
3300 return Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregore8381c02008-11-05 04:29:56 +00003301}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003302
Sebastian Redl965b0e32011-03-05 14:45:16 +00003303/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003304///
Douglas Gregor356513d2008-12-01 18:00:20 +00003305/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00003306/// dynamic-exception-specification
3307/// noexcept-specification
3308///
3309/// noexcept-specification:
3310/// 'noexcept'
3311/// 'noexcept' '(' constant-expression ')'
3312ExceptionSpecificationType
Richard Smith0b3a4622014-11-13 20:01:57 +00003313Parser::tryParseExceptionSpecification(bool Delayed,
Douglas Gregor433e0532012-04-16 18:27:27 +00003314 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003315 SmallVectorImpl<ParsedType> &DynamicExceptions,
3316 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith0b3a4622014-11-13 20:01:57 +00003317 ExprResult &NoexceptExpr,
3318 CachedTokens *&ExceptionSpecTokens) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003319 ExceptionSpecificationType Result = EST_None;
Richard Smith0b3a4622014-11-13 20:01:57 +00003320 ExceptionSpecTokens = 0;
3321
3322 // Handle delayed parsing of exception-specifications.
3323 if (Delayed) {
3324 if (Tok.isNot(tok::kw_throw) && Tok.isNot(tok::kw_noexcept))
3325 return EST_None;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003326
Richard Smith0b3a4622014-11-13 20:01:57 +00003327 // Consume and cache the starting token.
3328 bool IsNoexcept = Tok.is(tok::kw_noexcept);
3329 Token StartTok = Tok;
3330 SpecificationRange = SourceRange(ConsumeToken());
3331
3332 // Check for a '('.
3333 if (!Tok.is(tok::l_paren)) {
3334 // If this is a bare 'noexcept', we're done.
3335 if (IsNoexcept) {
3336 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3337 NoexceptExpr = 0;
3338 return EST_BasicNoexcept;
3339 }
3340
3341 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3342 return EST_DynamicNone;
3343 }
3344
3345 // Cache the tokens for the exception-specification.
3346 ExceptionSpecTokens = new CachedTokens;
3347 ExceptionSpecTokens->push_back(StartTok); // 'throw' or 'noexcept'
3348 ExceptionSpecTokens->push_back(Tok); // '('
3349 SpecificationRange.setEnd(ConsumeParen()); // '('
Richard Smithb1c217e2015-01-13 02:24:58 +00003350
3351 ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens,
3352 /*StopAtSemi=*/true,
3353 /*ConsumeFinalToken=*/true);
Richard Smith0b3a4622014-11-13 20:01:57 +00003354 SpecificationRange.setEnd(Tok.getLocation());
Richard Smith0b3a4622014-11-13 20:01:57 +00003355 return EST_Unparsed;
3356 }
3357
Sebastian Redl965b0e32011-03-05 14:45:16 +00003358 // See if there's a dynamic specification.
3359 if (Tok.is(tok::kw_throw)) {
3360 Result = ParseDynamicExceptionSpecification(SpecificationRange,
3361 DynamicExceptions,
3362 DynamicExceptionRanges);
3363 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
3364 "Produced different number of exception types and ranges.");
3365 }
3366
3367 // If there's no noexcept specification, we're done.
3368 if (Tok.isNot(tok::kw_noexcept))
3369 return Result;
3370
Richard Smithb15c11c2011-10-17 23:06:20 +00003371 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3372
Sebastian Redl965b0e32011-03-05 14:45:16 +00003373 // If we already had a dynamic specification, parse the noexcept for,
3374 // recovery, but emit a diagnostic and don't store the results.
3375 SourceRange NoexceptRange;
3376 ExceptionSpecificationType NoexceptType = EST_None;
3377
3378 SourceLocation KeywordLoc = ConsumeToken();
3379 if (Tok.is(tok::l_paren)) {
3380 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003381 BalancedDelimiterTracker T(*this, tok::l_paren);
3382 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003383 NoexceptType = EST_ComputedNoexcept;
3384 NoexceptExpr = ParseConstantExpression();
Serge Pavlov3739f5e72015-06-29 17:50:19 +00003385 T.consumeClose();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003386 // The argument must be contextually convertible to bool. We use
3387 // ActOnBooleanCondition for this purpose.
Serge Pavlov3739f5e72015-06-29 17:50:19 +00003388 if (!NoexceptExpr.isInvalid()) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003389 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
3390 NoexceptExpr.get());
Serge Pavlov3739f5e72015-06-29 17:50:19 +00003391 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
3392 } else {
3393 NoexceptType = EST_None;
3394 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003395 } else {
3396 // There is no argument.
3397 NoexceptType = EST_BasicNoexcept;
3398 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
3399 }
3400
3401 if (Result == EST_None) {
3402 SpecificationRange = NoexceptRange;
3403 Result = NoexceptType;
3404
3405 // If there's a dynamic specification after a noexcept specification,
3406 // parse that and ignore the results.
3407 if (Tok.is(tok::kw_throw)) {
3408 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3409 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
3410 DynamicExceptionRanges);
3411 }
3412 } else {
3413 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3414 }
3415
3416 return Result;
3417}
3418
Richard Smith8ca78a12013-06-13 02:02:51 +00003419static void diagnoseDynamicExceptionSpecification(
3420 Parser &P, const SourceRange &Range, bool IsNoexcept) {
3421 if (P.getLangOpts().CPlusPlus11) {
3422 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
3423 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
3424 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
3425 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
3426 }
3427}
3428
Sebastian Redl965b0e32011-03-05 14:45:16 +00003429/// ParseDynamicExceptionSpecification - Parse a C++
3430/// dynamic-exception-specification (C++ [except.spec]).
3431///
3432/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00003433/// 'throw' '(' type-id-list [opt] ')'
3434/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00003435///
Douglas Gregor356513d2008-12-01 18:00:20 +00003436/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00003437/// type-id ... [opt]
3438/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003439///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003440ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
3441 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003442 SmallVectorImpl<ParsedType> &Exceptions,
3443 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003444 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00003445
Sebastian Redl965b0e32011-03-05 14:45:16 +00003446 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003447 BalancedDelimiterTracker T(*this, tok::l_paren);
3448 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003449 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3450 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003451 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003452 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003453
Douglas Gregor356513d2008-12-01 18:00:20 +00003454 // Parse throw(...), a Microsoft extension that means "this function
3455 // can throw anything".
3456 if (Tok.is(tok::ellipsis)) {
3457 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003458 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00003459 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003460 T.consumeClose();
3461 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003462 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003463 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00003464 }
3465
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003466 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00003467 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003468 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00003469 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00003470
Douglas Gregor830837d2010-12-20 23:57:46 +00003471 if (Tok.is(tok::ellipsis)) {
3472 // C++0x [temp.variadic]p5:
3473 // - In a dynamic-exception-specification (15.4); the pattern is a
3474 // type-id.
3475 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003476 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00003477 if (!Res.isInvalid())
3478 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3479 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003480
Sebastian Redld6434562009-05-29 18:02:33 +00003481 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003482 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00003483 Ranges.push_back(Range);
3484 }
Alp Toker97650562014-01-10 11:19:30 +00003485
3486 if (!TryConsumeToken(tok::comma))
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003487 break;
3488 }
3489
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003490 T.consumeClose();
3491 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003492 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3493 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003494 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003495}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003496
Douglas Gregor7fb25412010-10-01 18:44:50 +00003497/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3498/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003499TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00003500 assert(Tok.is(tok::arrow) && "expected arrow");
3501
3502 ConsumeToken();
3503
Richard Smithbfdb1082012-03-12 08:56:40 +00003504 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003505}
3506
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003507/// \brief We have just started parsing the definition of a new class,
3508/// so push that class onto our stack of classes that is currently
3509/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003510Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003511Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3512 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003513 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003514 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003515 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003516 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003517}
3518
3519/// \brief Deallocate the given parsed class and all of its nested
3520/// classes.
3521void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003522 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3523 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003524 delete Class;
3525}
3526
3527/// \brief Pop the top class of the stack of classes that are
3528/// currently being parsed.
3529///
3530/// This routine should be called when we have finished parsing the
3531/// definition of a class, but have not yet popped the Scope
3532/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003533void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003534 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003535
John McCallc1465822011-02-14 07:13:47 +00003536 Actions.PopParsingClass(state);
3537
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003538 ParsingClass *Victim = ClassStack.top();
3539 ClassStack.pop();
3540 if (Victim->TopLevelClass) {
3541 // Deallocate all of the nested classes of this class,
3542 // recursively: we don't need to keep any of this information.
3543 DeallocateParsedClasses(Victim);
3544 return;
Mike Stump11289f42009-09-09 15:08:12 +00003545 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003546 assert(!ClassStack.empty() && "Missing top-level class?");
3547
Douglas Gregorefc46952010-10-12 16:25:54 +00003548 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003549 // The victim is a nested class, but we will not need to perform
3550 // any processing after the definition of this class since it has
3551 // no members whose handling was delayed. Therefore, we can just
3552 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003553 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003554 return;
3555 }
3556
3557 // This nested class has some members that will need to be processed
3558 // after the top-level class is completely defined. Therefore, add
3559 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003560 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003561 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003562 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003563}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003564
Richard Smith3dff2512012-04-10 03:25:07 +00003565/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3566///
3567/// \return the parsed identifier on success, and 0 if the next token is not an
3568/// attribute-token.
3569///
3570/// C++11 [dcl.attr.grammar]p3:
3571/// If a keyword or an alternative token that satisfies the syntactic
3572/// requirements of an identifier is contained in an attribute-token,
3573/// it is considered an identifier.
3574IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3575 switch (Tok.getKind()) {
3576 default:
3577 // Identifiers and keywords have identifier info attached.
David Majnemerd5271992015-01-09 18:09:39 +00003578 if (!Tok.isAnnotation()) {
3579 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3580 Loc = ConsumeToken();
3581 return II;
3582 }
Richard Smith3dff2512012-04-10 03:25:07 +00003583 }
Craig Topper161e4db2014-05-21 06:02:52 +00003584 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003585
3586 case tok::ampamp: // 'and'
3587 case tok::pipe: // 'bitor'
3588 case tok::pipepipe: // 'or'
3589 case tok::caret: // 'xor'
3590 case tok::tilde: // 'compl'
3591 case tok::amp: // 'bitand'
3592 case tok::ampequal: // 'and_eq'
3593 case tok::pipeequal: // 'or_eq'
3594 case tok::caretequal: // 'xor_eq'
3595 case tok::exclaim: // 'not'
3596 case tok::exclaimequal: // 'not_eq'
3597 // Alternative tokens do not have identifier info, but their spelling
3598 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003599 SmallString<8> SpellingBuf;
Benjamin Kramer60be5632015-03-29 19:25:07 +00003600 SourceLocation SpellingLoc =
3601 PP.getSourceManager().getSpellingLoc(Tok.getLocation());
3602 StringRef Spelling = PP.getSpelling(SpellingLoc, SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003603 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003604 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003605 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003606 }
Craig Topper161e4db2014-05-21 06:02:52 +00003607 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003608 }
3609}
3610
Michael Han23214e52012-10-03 01:56:22 +00003611static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3612 IdentifierInfo *ScopeName) {
3613 switch (AttributeList::getKind(AttrName, ScopeName,
3614 AttributeList::AS_CXX11)) {
3615 case AttributeList::AT_CarriesDependency:
Aaron Ballman35f94212014-04-14 16:03:22 +00003616 case AttributeList::AT_Deprecated:
Michael Han23214e52012-10-03 01:56:22 +00003617 case AttributeList::AT_FallThrough:
Richard Smith10876ef2013-01-17 01:30:42 +00003618 case AttributeList::AT_CXX11NoReturn: {
Michael Han23214e52012-10-03 01:56:22 +00003619 return true;
3620 }
3621
3622 default:
3623 return false;
3624 }
3625}
3626
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003627/// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause.
3628///
3629/// [C++11] attribute-argument-clause:
3630/// '(' balanced-token-seq ')'
3631///
3632/// [C++11] balanced-token-seq:
3633/// balanced-token
3634/// balanced-token-seq balanced-token
3635///
3636/// [C++11] balanced-token:
3637/// '(' balanced-token-seq ')'
3638/// '[' balanced-token-seq ']'
3639/// '{' balanced-token-seq '}'
3640/// any token but '(', ')', '[', ']', '{', or '}'
3641bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName,
3642 SourceLocation AttrNameLoc,
3643 ParsedAttributes &Attrs,
3644 SourceLocation *EndLoc,
3645 IdentifierInfo *ScopeName,
3646 SourceLocation ScopeLoc) {
3647 assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
Aaron Ballman35f94212014-04-14 16:03:22 +00003648 SourceLocation LParenLoc = Tok.getLocation();
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003649
3650 // If the attribute isn't known, we will not attempt to parse any
3651 // arguments.
3652 if (!hasAttribute(AttrSyntax::CXX, ScopeName, AttrName,
Bob Wilson7c730832015-07-20 22:57:31 +00003653 getTargetInfo(), getLangOpts())) {
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003654 // Eat the left paren, then skip to the ending right paren.
3655 ConsumeParen();
3656 SkipUntil(tok::r_paren);
3657 return false;
3658 }
3659
3660 if (ScopeName && ScopeName->getName() == "gnu")
3661 // GNU-scoped attributes have some special cases to handle GNU-specific
3662 // behaviors.
3663 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
Craig Topper161e4db2014-05-21 06:02:52 +00003664 ScopeLoc, AttributeList::AS_CXX11, nullptr);
Aaron Ballman35f94212014-04-14 16:03:22 +00003665 else {
3666 unsigned NumArgs =
3667 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
3668 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3669
3670 const AttributeList *Attr = Attrs.getList();
3671 if (Attr && IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) {
3672 // If the attribute is a standard or built-in attribute and we are
3673 // parsing an argument list, we need to determine whether this attribute
3674 // was allowed to have an argument list (such as [[deprecated]]), and how
3675 // many arguments were parsed (so we can diagnose on [[deprecated()]]).
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003676 if (Attr->getMaxArgs() && !NumArgs) {
3677 // The attribute was allowed to have arguments, but none were provided
3678 // even though the attribute parsed successfully. This is an error.
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003679 Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003680 } else if (!Attr->getMaxArgs()) {
3681 // The attribute parsed successfully, but was not allowed to have any
3682 // arguments. It doesn't matter whether any were provided -- the
Aaron Ballman35f94212014-04-14 16:03:22 +00003683 // presence of the argument list (even if empty) is diagnosed.
3684 Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
Aaron Ballman9b7cee62014-12-19 18:37:22 +00003685 << AttrName
3686 << FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc));
Aaron Ballman35f94212014-04-14 16:03:22 +00003687 }
3688 }
3689 }
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003690 return true;
3691}
3692
3693/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003694///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003695/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003696/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003697/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003698///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003699/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003700/// attribute[opt]
3701/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003702/// attribute '...'
3703/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003704///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003705/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003706/// attribute-token attribute-argument-clause[opt]
3707///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003708/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003709/// identifier
3710/// attribute-scoped-token
3711///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003712/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003713/// attribute-namespace '::' identifier
3714///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003715/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003716/// identifier
Richard Smith3dff2512012-04-10 03:25:07 +00003717void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003718 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003719 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003720 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003721 ParseAlignmentSpecifier(attrs, endLoc);
3722 return;
3723 }
3724
Alexis Hunt96d5c762009-11-21 08:43:09 +00003725 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003726 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003727
Richard Smithf679b5b2011-10-14 20:48:27 +00003728 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3729
Alexis Hunt96d5c762009-11-21 08:43:09 +00003730 ConsumeBracket();
3731 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003732
Richard Smith10876ef2013-01-17 01:30:42 +00003733 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3734
Richard Smith3dff2512012-04-10 03:25:07 +00003735 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003736 // attribute not present
Alp Toker97650562014-01-10 11:19:30 +00003737 if (TryConsumeToken(tok::comma))
Alexis Hunt96d5c762009-11-21 08:43:09 +00003738 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003739
Richard Smith3dff2512012-04-10 03:25:07 +00003740 SourceLocation ScopeLoc, AttrLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00003741 IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003742
3743 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3744 if (!AttrName)
3745 // Break out to the "expected ']'" diagnostic.
3746 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003747
Alexis Hunt96d5c762009-11-21 08:43:09 +00003748 // scoped attribute
Alp Toker97650562014-01-10 11:19:30 +00003749 if (TryConsumeToken(tok::coloncolon)) {
Richard Smith3dff2512012-04-10 03:25:07 +00003750 ScopeName = AttrName;
3751 ScopeLoc = AttrLoc;
3752
3753 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3754 if (!AttrName) {
Alp Tokerec543272013-12-24 09:48:30 +00003755 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003756 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003757 continue;
3758 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003759 }
3760
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003761 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003762 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003763
Richard Smith10876ef2013-01-17 01:30:42 +00003764 if (StandardAttr &&
3765 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3766 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003767 << AttrName << SourceRange(SeenAttrs[AttrName]);
Richard Smith10876ef2013-01-17 01:30:42 +00003768
Michael Han23214e52012-10-03 01:56:22 +00003769 // Parse attribute arguments
Aaron Ballman35f94212014-04-14 16:03:22 +00003770 if (Tok.is(tok::l_paren))
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003771 AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3772 ScopeName, ScopeLoc);
Michael Han23214e52012-10-03 01:56:22 +00003773
3774 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003775 attrs.addNew(AttrName,
3776 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3777 AttrLoc),
Craig Topper161e4db2014-05-21 06:02:52 +00003778 ScopeName, ScopeLoc, nullptr, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003779
Alp Toker97650562014-01-10 11:19:30 +00003780 if (TryConsumeToken(tok::ellipsis))
Michael Han23214e52012-10-03 01:56:22 +00003781 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3782 << AttrName->getName();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003783 }
3784
Alp Toker383d2c42014-01-01 03:08:43 +00003785 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003786 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003787 if (endLoc)
3788 *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003789 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003790 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003791}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003792
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003793/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003794///
3795/// attribute-specifier-seq:
3796/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003797void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003798 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003799 assert(getLangOpts().CPlusPlus11);
3800
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003801 SourceLocation StartLoc = Tok.getLocation(), Loc;
3802 if (!endLoc)
3803 endLoc = &Loc;
3804
Douglas Gregor6f981002011-10-07 20:35:25 +00003805 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003806 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003807 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003808
3809 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003810}
3811
Richard Smithc2c8bb82013-10-15 01:34:54 +00003812void Parser::DiagnoseAndSkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00003813 // Start and end location of an attribute or an attribute list.
3814 SourceLocation StartLoc = Tok.getLocation();
Richard Smith955bf012014-06-19 11:42:00 +00003815 SourceLocation EndLoc = SkipCXX11Attributes();
3816
3817 if (EndLoc.isValid()) {
3818 SourceRange Range(StartLoc, EndLoc);
3819 Diag(StartLoc, diag::err_attributes_not_allowed)
3820 << Range;
3821 }
3822}
3823
3824SourceLocation Parser::SkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00003825 SourceLocation EndLoc;
3826
Richard Smith955bf012014-06-19 11:42:00 +00003827 if (!isCXX11AttributeSpecifier())
3828 return EndLoc;
3829
Richard Smithc2c8bb82013-10-15 01:34:54 +00003830 do {
3831 if (Tok.is(tok::l_square)) {
3832 BalancedDelimiterTracker T(*this, tok::l_square);
3833 T.consumeOpen();
3834 T.skipToEnd();
3835 EndLoc = T.getCloseLocation();
3836 } else {
3837 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3838 ConsumeToken();
3839 BalancedDelimiterTracker T(*this, tok::l_paren);
3840 if (!T.consumeOpen())
3841 T.skipToEnd();
3842 EndLoc = T.getCloseLocation();
3843 }
3844 } while (isCXX11AttributeSpecifier());
3845
Richard Smith955bf012014-06-19 11:42:00 +00003846 return EndLoc;
Richard Smithc2c8bb82013-10-15 01:34:54 +00003847}
3848
David Majnemere4752e752015-07-08 05:55:00 +00003849/// ParseMicrosoftAttributes - Parse Microsoft attributes [Attr]
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003850///
3851/// [MS] ms-attribute:
3852/// '[' token-seq ']'
3853///
3854/// [MS] ms-attribute-seq:
3855/// ms-attribute[opt]
3856/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00003857void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3858 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003859 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3860
Saleem Abdulrasool425efcf2015-06-15 20:57:04 +00003861 do {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003862 // FIXME: If this is actually a C++11 attribute, parse it as one.
Saleem Abdulrasool425efcf2015-06-15 20:57:04 +00003863 BalancedDelimiterTracker T(*this, tok::l_square);
3864 T.consumeOpen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003865 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
Saleem Abdulrasool425efcf2015-06-15 20:57:04 +00003866 T.consumeClose();
3867 if (endLoc)
3868 *endLoc = T.getCloseLocation();
3869 } while (Tok.is(tok::l_square));
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003870}
Francois Pichet8f981d52011-05-25 10:19:49 +00003871
3872void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3873 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00003874 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00003875 if (ParseMicrosoftIfExistsCondition(Result))
3876 return;
3877
Douglas Gregor43edb322011-10-24 22:31:10 +00003878 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3879 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00003880 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet8f981d52011-05-25 10:19:49 +00003881 return;
3882 }
Francois Pichet8f981d52011-05-25 10:19:49 +00003883
Douglas Gregor43edb322011-10-24 22:31:10 +00003884 switch (Result.Behavior) {
3885 case IEB_Parse:
3886 // Parse the declarations below.
3887 break;
3888
3889 case IEB_Dependent:
3890 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3891 << Result.IsIfExists;
3892 // Fall through to skip.
3893
3894 case IEB_Skip:
3895 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00003896 return;
3897 }
3898
Richard Smith34f30512013-11-23 04:06:09 +00003899 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003900 // __if_exists, __if_not_exists can nest.
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00003901 if (Tok.isOneOf(tok::kw___if_exists, tok::kw___if_not_exists)) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003902 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3903 continue;
3904 }
3905
3906 // Check for extraneous top-level semicolon.
3907 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003908 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00003909 continue;
3910 }
3911
3912 AccessSpecifier AS = getAccessSpecifierIfPresent();
3913 if (AS != AS_none) {
3914 // Current token is a C++ access specifier.
3915 CurAS = AS;
3916 SourceLocation ASLoc = Tok.getLocation();
3917 ConsumeToken();
3918 if (Tok.is(tok::colon))
3919 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3920 else
Alp Toker35d87032013-12-30 23:29:50 +00003921 Diag(Tok, diag::err_expected) << tok::colon;
Francois Pichet8f981d52011-05-25 10:19:49 +00003922 ConsumeToken();
3923 continue;
3924 }
3925
3926 // Parse all the comma separated declarators.
Craig Topper161e4db2014-05-21 06:02:52 +00003927 ParseCXXClassMemberDeclaration(CurAS, nullptr);
Francois Pichet8f981d52011-05-25 10:19:49 +00003928 }
Douglas Gregor43edb322011-10-24 22:31:10 +00003929
3930 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00003931}