blob: 223046c90d92850d2585d0fd7b0ecae86c2f1c66 [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"
Aaron Ballmanb8e20392014-03-31 17:32:39 +000020#include "clang/Basic/TargetInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Basic/OperatorKinds.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
76 Token attrTok;
Mike Stump11289f42009-09-09 15:08:12 +000077
Chris Lattner76c72282007-10-09 17:33:22 +000078 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000079 Ident = Tok.getIdentifierInfo();
80 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieu61384cb2011-05-26 20:11:09 +000081 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
82 ExtraNamespaceLoc.push_back(ConsumeToken());
83 ExtraIdent.push_back(Tok.getIdentifierInfo());
84 ExtraIdentLoc.push_back(ConsumeToken());
85 }
Chris Lattnera5235172007-08-25 06:57:03 +000086 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattnera5235172007-08-25 06:57:03 +000088 // Read label attributes, if present.
John McCall084e83d2011-03-24 11:26:52 +000089 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000090 if (Tok.is(tok::kw___attribute)) {
91 attrTok = Tok;
John McCall53fa7142010-12-24 02:08:15 +000092 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000093 }
Mike Stump11289f42009-09-09 15:08:12 +000094
Douglas Gregor6b6bba42009-06-17 19:49:00 +000095 if (Tok.is(tok::equal)) {
Craig Topper161e4db2014-05-21 06:02:52 +000096 if (!Ident) {
Alp Tokerec543272013-12-24 09:48:30 +000097 Diag(Tok, diag::err_expected) << tok::identifier;
Nico Weber729f1e22012-10-27 23:44:27 +000098 // Skip to end of the definition and eat the ';'.
99 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000100 return nullptr;
Nico Weber729f1e22012-10-27 23:44:27 +0000101 }
John McCall53fa7142010-12-24 02:08:15 +0000102 if (!attrs.empty())
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000103 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +0000104 if (InlineLoc.isValid())
105 Diag(InlineLoc, diag::err_inline_namespace_alias)
106 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000107 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000108 }
Mike Stump11289f42009-09-09 15:08:12 +0000109
Richard Trieu61384cb2011-05-26 20:11:09 +0000110
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000111 BalancedDelimiterTracker T(*this, tok::l_brace);
112 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000113 if (Ident)
114 Diag(Tok, diag::err_expected) << tok::l_brace;
115 else
116 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Craig Topper161e4db2014-05-21 06:02:52 +0000117 return nullptr;
Chris Lattnera5235172007-08-25 06:57:03 +0000118 }
Mike Stump11289f42009-09-09 15:08:12 +0000119
Douglas Gregor0be31a22010-07-02 17:43:08 +0000120 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
121 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
122 getCurScope()->getFnParent()) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000123 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000124 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +0000125 return nullptr;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000126 }
127
Richard Smith13307f52014-11-08 05:37:34 +0000128 if (ExtraIdent.empty()) {
129 // Normal namespace definition, not a nested-namespace-definition.
130 } else if (InlineLoc.isValid()) {
131 Diag(InlineLoc, diag::err_inline_nested_namespace_definition);
132 } else if (getLangOpts().CPlusPlus1z) {
133 Diag(ExtraNamespaceLoc[0],
134 diag::warn_cxx14_compat_nested_namespace_definition);
135 } else {
Richard Trieu61384cb2011-05-26 20:11:09 +0000136 TentativeParsingAction TPA(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000137 SkipUntil(tok::r_brace, StopBeforeMatch);
Richard Trieu61384cb2011-05-26 20:11:09 +0000138 Token rBraceToken = Tok;
139 TPA.Revert();
140
141 if (!rBraceToken.is(tok::r_brace)) {
Richard Smith13307f52014-11-08 05:37:34 +0000142 Diag(ExtraNamespaceLoc[0], diag::ext_nested_namespace_definition)
Richard Trieu61384cb2011-05-26 20:11:09 +0000143 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
144 } else {
Benjamin Kramerf546f412011-05-26 21:32:30 +0000145 std::string NamespaceFix;
Richard Trieu61384cb2011-05-26 20:11:09 +0000146 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
147 E = ExtraIdent.end(); I != E; ++I) {
148 NamespaceFix += " { namespace ";
149 NamespaceFix += (*I)->getName();
150 }
Benjamin Kramerf546f412011-05-26 21:32:30 +0000151
Richard Trieu61384cb2011-05-26 20:11:09 +0000152 std::string RBraces;
Benjamin Kramerf546f412011-05-26 21:32:30 +0000153 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieu61384cb2011-05-26 20:11:09 +0000154 RBraces += "} ";
Benjamin Kramerf546f412011-05-26 21:32:30 +0000155
Richard Smith13307f52014-11-08 05:37:34 +0000156 Diag(ExtraNamespaceLoc[0], diag::ext_nested_namespace_definition)
Richard Trieu61384cb2011-05-26 20:11:09 +0000157 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
158 ExtraIdentLoc.back()),
159 NamespaceFix)
160 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
161 }
162 }
163
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000164 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith5d164bc2011-10-15 05:09:34 +0000165 if (InlineLoc.isValid())
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000166 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000167 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000168
Chris Lattner4de55aa2009-03-29 14:02:43 +0000169 // Enter a scope for the namespace.
170 ParseScope NamespaceScope(this, Scope::DeclScope);
171
John McCall48871652010-08-21 09:40:31 +0000172 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000173 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000174 IdentLoc, Ident, T.getOpenLocation(),
175 attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000176
John McCallfaf5fb42010-08-26 23:41:50 +0000177 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
178 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000179
Richard Trieu61384cb2011-05-26 20:11:09 +0000180 // Parse the contents of the namespace. This includes parsing recovery on
181 // any improperly nested namespaces.
182 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000183 InlineLoc, attrs, T);
Mike Stump11289f42009-09-09 15:08:12 +0000184
Chris Lattner4de55aa2009-03-29 14:02:43 +0000185 // Leave the namespace scope.
186 NamespaceScope.Exit();
187
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000188 DeclEnd = T.getCloseLocation();
189 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000190
191 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000192}
Chris Lattner38376f12008-01-12 07:05:38 +0000193
Richard Trieu61384cb2011-05-26 20:11:09 +0000194/// ParseInnerNamespace - Parse the contents of a namespace.
Richard Smith13307f52014-11-08 05:37:34 +0000195void Parser::ParseInnerNamespace(std::vector<SourceLocation> &IdentLoc,
196 std::vector<IdentifierInfo *> &Ident,
197 std::vector<SourceLocation> &NamespaceLoc,
198 unsigned int index, SourceLocation &InlineLoc,
199 ParsedAttributes &attrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000200 BalancedDelimiterTracker &Tracker) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000201 if (index == Ident.size()) {
Richard Smith34f30512013-11-23 04:06:09 +0000202 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000203 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000204 MaybeParseCXX11Attributes(attrs);
Richard Trieu61384cb2011-05-26 20:11:09 +0000205 MaybeParseMicrosoftAttributes(attrs);
206 ParseExternalDeclaration(attrs);
207 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000208
209 // The caller is what called check -- we are simply calling
210 // the close for it.
211 Tracker.consumeClose();
Richard Trieu61384cb2011-05-26 20:11:09 +0000212
213 return;
214 }
215
Richard Smith13307f52014-11-08 05:37:34 +0000216 // Handle a nested namespace definition.
217 // FIXME: Preserve the source information through to the AST rather than
218 // desugaring it here.
Richard Trieu61384cb2011-05-26 20:11:09 +0000219 ParseScope NamespaceScope(this, Scope::DeclScope);
220 Decl *NamespcDecl =
221 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
222 NamespaceLoc[index], IdentLoc[index],
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000223 Ident[index], Tracker.getOpenLocation(),
224 attrs.getList());
Richard Trieu61384cb2011-05-26 20:11:09 +0000225
226 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000227 attrs, Tracker);
Richard Trieu61384cb2011-05-26 20:11:09 +0000228
229 NamespaceScope.Exit();
230
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000231 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieu61384cb2011-05-26 20:11:09 +0000232}
233
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000234/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
235/// alias definition.
236///
John McCall48871652010-08-21 09:40:31 +0000237Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000238 SourceLocation AliasLoc,
239 IdentifierInfo *Alias,
240 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000241 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000242
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000243 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000244
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000245 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000246 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000247 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000248 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000249 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000250
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000251 CXXScopeSpec SS;
252 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000253 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000254
255 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
256 Diag(Tok, diag::err_expected_namespace_name);
257 // Skip to end of the definition and eat the ';'.
258 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000259 return nullptr;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000260 }
261
262 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000263 IdentifierInfo *Ident = Tok.getIdentifierInfo();
264 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000265
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000266 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000267 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000268 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
269 SkipUntil(tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000270
Douglas Gregor0be31a22010-07-02 17:43:08 +0000271 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000272 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000273}
274
Chris Lattner38376f12008-01-12 07:05:38 +0000275/// ParseLinkage - We know that the current token is a string_literal
276/// and just before that, that extern was seen.
277///
278/// linkage-specification: [C++ 7.5p2: dcl.link]
279/// 'extern' string-literal '{' declaration-seq[opt] '}'
280/// 'extern' string-literal declaration
281///
Chris Lattner8ea64422010-11-09 20:15:55 +0000282Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Richard Smith4ee696d2014-02-17 23:25:27 +0000283 assert(isTokenStringLiteral() && "Not a string literal!");
284 ExprResult Lang = ParseStringLiteralExpression(false);
Chris Lattner38376f12008-01-12 07:05:38 +0000285
Douglas Gregor07665a62009-01-05 19:45:36 +0000286 ParseScope LinkageScope(this, Scope::DeclScope);
Richard Smith4ee696d2014-02-17 23:25:27 +0000287 Decl *LinkageSpec =
288 Lang.isInvalid()
Craig Topper161e4db2014-05-21 06:02:52 +0000289 ? nullptr
Richard Smith4ee696d2014-02-17 23:25:27 +0000290 : Actions.ActOnStartLinkageSpecification(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000291 getCurScope(), DS.getSourceRange().getBegin(), Lang.get(),
Richard Smith4ee696d2014-02-17 23:25:27 +0000292 Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
Douglas Gregor07665a62009-01-05 19:45:36 +0000293
John McCall084e83d2011-03-24 11:26:52 +0000294 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000295 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000296 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000297
Douglas Gregor07665a62009-01-05 19:45:36 +0000298 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara4d423992011-05-01 16:25:54 +0000299 // Reset the source range in DS, as the leading "extern"
300 // does not really belong to the inner declaration ...
301 DS.SetRangeStart(SourceLocation());
302 DS.SetRangeEnd(SourceLocation());
303 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000304 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000305 ParseExternalDeclaration(attrs, &DS);
Richard Smith4ee696d2014-02-17 23:25:27 +0000306 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
307 getCurScope(), LinkageSpec, SourceLocation())
Craig Topper161e4db2014-05-21 06:02:52 +0000308 : nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000309 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000310
Douglas Gregorb65a9132010-02-07 08:38:28 +0000311 DS.abort();
312
John McCall53fa7142010-12-24 02:08:15 +0000313 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000314
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000315 BalancedDelimiterTracker T(*this, tok::l_brace);
316 T.consumeOpen();
Richard Smith77944862014-03-02 05:58:18 +0000317
318 unsigned NestedModules = 0;
319 while (true) {
320 switch (Tok.getKind()) {
321 case tok::annot_module_begin:
322 ++NestedModules;
323 ParseTopLevelDecl();
324 continue;
325
326 case tok::annot_module_end:
327 if (!NestedModules)
328 break;
329 --NestedModules;
330 ParseTopLevelDecl();
331 continue;
332
333 case tok::annot_module_include:
334 ParseTopLevelDecl();
335 continue;
336
337 case tok::eof:
338 break;
339
340 case tok::r_brace:
341 if (!NestedModules)
342 break;
343 // Fall through.
344 default:
345 ParsedAttributesWithRange attrs(AttrFactory);
346 MaybeParseCXX11Attributes(attrs);
347 MaybeParseMicrosoftAttributes(attrs);
348 ParseExternalDeclaration(attrs);
349 continue;
350 }
351
352 break;
Chris Lattner38376f12008-01-12 07:05:38 +0000353 }
354
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000355 T.consumeClose();
Richard Smith4ee696d2014-02-17 23:25:27 +0000356 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
357 getCurScope(), LinkageSpec, T.getCloseLocation())
Craig Topper161e4db2014-05-21 06:02:52 +0000358 : nullptr;
Chris Lattner38376f12008-01-12 07:05:38 +0000359}
Douglas Gregor556877c2008-04-13 21:30:24 +0000360
Douglas Gregord7c4d982008-12-30 03:27:21 +0000361/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
362/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000363Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000364 const ParsedTemplateInfo &TemplateInfo,
365 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000366 ParsedAttributesWithRange &attrs,
367 Decl **OwnedType) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000368 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000369 ObjCDeclContextSwitch ObjCDC(*this);
370
Douglas Gregord7c4d982008-12-30 03:27:21 +0000371 // Eat 'using'.
372 SourceLocation UsingLoc = ConsumeToken();
373
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000374 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000375 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000376 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000377 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000378 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000379
John McCall9b72f892010-11-10 02:40:36 +0000380 // 'using namespace' means this is a using-directive.
381 if (Tok.is(tok::kw_namespace)) {
382 // Template parameters are always an error here.
383 if (TemplateInfo.Kind) {
384 SourceRange R = TemplateInfo.getSourceRange();
385 Diag(UsingLoc, diag::err_templated_using_directive)
386 << R << FixItHint::CreateRemoval(R);
387 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000388
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000389 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000390 }
391
Richard Smithdda56e42011-04-15 14:24:37 +0000392 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000393
394 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000395 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000396
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000397 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000398 AS_none, OwnedType);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000399}
400
401/// ParseUsingDirective - Parse C++ using-directive, assumes
402/// that current token is 'namespace' and 'using' was already parsed.
403///
404/// using-directive: [C++ 7.3.p4: namespace.udir]
405/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
406/// namespace-name ;
407/// [GNU] using-directive:
408/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
409/// namespace-name attributes[opt] ;
410///
John McCall48871652010-08-21 09:40:31 +0000411Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000412 SourceLocation UsingLoc,
413 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000414 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000415 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
416
417 // Eat 'namespace'.
418 SourceLocation NamespcLoc = ConsumeToken();
419
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000420 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000421 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000422 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000423 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000424 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000425
Douglas Gregord7c4d982008-12-30 03:27:21 +0000426 CXXScopeSpec SS;
427 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000428 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000429
Craig Topper161e4db2014-05-21 06:02:52 +0000430 IdentifierInfo *NamespcName = nullptr;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000431 SourceLocation IdentLoc = SourceLocation();
432
433 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000434 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000435 Diag(Tok, diag::err_expected_namespace_name);
436 // If there was invalid namespace name, skip to end of decl, and eat ';'.
437 SkipUntil(tok::semi);
438 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Craig Topper161e4db2014-05-21 06:02:52 +0000439 return nullptr;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000440 }
Mike Stump11289f42009-09-09 15:08:12 +0000441
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000442 // Parse identifier.
443 NamespcName = Tok.getIdentifierInfo();
444 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000445
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000446 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000447 bool GNUAttr = false;
448 if (Tok.is(tok::kw___attribute)) {
449 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000450 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000451 }
Mike Stump11289f42009-09-09 15:08:12 +0000452
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000453 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000454 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000455 if (ExpectAndConsume(tok::semi,
456 GNUAttr ? diag::err_expected_semi_after_attribute_list
457 : diag::err_expected_semi_after_namespace_name))
458 SkipUntil(tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000459
Douglas Gregor0be31a22010-07-02 17:43:08 +0000460 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000461 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000462}
463
Richard Smithdda56e42011-04-15 14:24:37 +0000464/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
465/// Assumes that 'using' was already seen.
Douglas Gregord7c4d982008-12-30 03:27:21 +0000466///
467/// using-declaration: [C++ 7.3.p3: namespace.udecl]
468/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000469/// unqualified-id
470/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000471///
Richard Smith810ad3e2013-01-29 10:02:16 +0000472/// alias-declaration: C++11 [dcl.dcl]p1
473/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smithdda56e42011-04-15 14:24:37 +0000474///
John McCall48871652010-08-21 09:40:31 +0000475Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000476 const ParsedTemplateInfo &TemplateInfo,
477 SourceLocation UsingLoc,
478 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000479 AccessSpecifier AS,
480 Decl **OwnedType) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000481 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000482 SourceLocation TypenameLoc;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000483 bool HasTypenameKeyword = false;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000484
Richard Smithc2c8bb82013-10-15 01:34:54 +0000485 // Check for misplaced attributes before the identifier in an
486 // alias-declaration.
487 ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
488 MaybeParseCXX11Attributes(MisplacedAttrs);
Douglas Gregorfec52632009-06-20 00:51:54 +0000489
490 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000491 // FIXME: This is wrong; we should parse this as a typename-specifier.
Alp Toker97650562014-01-10 11:19:30 +0000492 if (TryConsumeToken(tok::kw_typename, TypenameLoc))
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000493 HasTypenameKeyword = true;
Douglas Gregorfec52632009-06-20 00:51:54 +0000494
Nikola Smiljanic67860242014-09-26 00:28:20 +0000495 if (Tok.is(tok::kw___super)) {
496 Diag(Tok.getLocation(), diag::err_super_in_using_declaration);
497 SkipUntil(tok::semi);
498 return nullptr;
499 }
500
Douglas Gregorfec52632009-06-20 00:51:54 +0000501 // Parse nested-name-specifier.
Craig Topper161e4db2014-05-21 06:02:52 +0000502 IdentifierInfo *LastII = nullptr;
Richard Smith7447af42013-03-26 01:15:19 +0000503 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
Craig Topper161e4db2014-05-21 06:02:52 +0000504 /*MayBePseudoDtor=*/nullptr,
505 /*IsTypename=*/false,
Richard Smith7447af42013-03-26 01:15:19 +0000506 /*LastII=*/&LastII);
Douglas Gregorfec52632009-06-20 00:51:54 +0000507
Douglas Gregorfec52632009-06-20 00:51:54 +0000508 // Check nested-name specifier.
509 if (SS.isInvalid()) {
510 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000511 return nullptr;
Douglas Gregorfec52632009-06-20 00:51:54 +0000512 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000513
Richard Smith7447af42013-03-26 01:15:19 +0000514 SourceLocation TemplateKWLoc;
515 UnqualifiedId Name;
516
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000517 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000518 // destructor names and allow the action module to diagnose any semantic
519 // errors.
Richard Smith7447af42013-03-26 01:15:19 +0000520 //
521 // C++11 [class.qual]p2:
522 // [...] in a using-declaration that is a member-declaration, if the name
523 // specified after the nested-name-specifier is the same as the identifier
524 // or the simple-template-id's template-name in the last component of the
525 // nested-name-specifier, the name is [...] considered to name the
526 // constructor.
527 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
528 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
529 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
530 !SS.getScopeRep()->getAsNamespace() &&
531 !SS.getScopeRep()->getAsNamespaceAlias()) {
532 SourceLocation IdLoc = ConsumeToken();
533 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
534 Name.setConstructorName(Type, IdLoc, IdLoc);
535 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
536 /*AllowDestructorName=*/ true,
537 /*AllowConstructorName=*/ true, ParsedType(),
538 TemplateKWLoc, Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000539 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000540 return nullptr;
Douglas Gregorfec52632009-06-20 00:51:54 +0000541 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000542
Richard Smithc2c8bb82013-10-15 01:34:54 +0000543 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith37a45dd2013-10-24 01:21:09 +0000544 MaybeParseGNUAttributes(Attrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000545 MaybeParseCXX11Attributes(Attrs);
Richard Smithdda56e42011-04-15 14:24:37 +0000546
547 // Maybe this is an alias-declaration.
Richard Smithdda56e42011-04-15 14:24:37 +0000548 TypeResult TypeAlias;
Richard Smithc2c8bb82013-10-15 01:34:54 +0000549 bool IsAliasDecl = Tok.is(tok::equal);
Richard Smithdda56e42011-04-15 14:24:37 +0000550 if (IsAliasDecl) {
Richard Smithc2c8bb82013-10-15 01:34:54 +0000551 // If we had any misplaced attributes from earlier, this is where they
552 // should have been written.
553 if (MisplacedAttrs.Range.isValid()) {
554 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
555 << FixItHint::CreateInsertionFromRange(
556 Tok.getLocation(),
557 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
558 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
559 Attrs.takeAllFrom(MisplacedAttrs);
560 }
561
Richard Smithdda56e42011-04-15 14:24:37 +0000562 ConsumeToken();
563
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000564 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000565 diag::warn_cxx98_compat_alias_declaration :
566 diag::ext_alias_declaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000567
Richard Smith3f1b5d02011-05-05 21:57:07 +0000568 // Type alias templates cannot be specialized.
569 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000570 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
571 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000572 SpecKind = 0;
573 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
574 SpecKind = 1;
575 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
576 SpecKind = 2;
577 if (SpecKind != -1) {
578 SourceRange Range;
579 if (SpecKind == 0)
580 Range = SourceRange(Name.TemplateId->LAngleLoc,
581 Name.TemplateId->RAngleLoc);
582 else
583 Range = TemplateInfo.getSourceRange();
584 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
585 << SpecKind << Range;
586 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000587 return nullptr;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000588 }
589
Richard Smithdda56e42011-04-15 14:24:37 +0000590 // Name must be an identifier.
591 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
592 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
593 // No removal fixit: can't recover from this.
594 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000595 return nullptr;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000596 } else if (HasTypenameKeyword)
Richard Smithdda56e42011-04-15 14:24:37 +0000597 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
598 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
599 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
600 else if (SS.isNotEmpty())
601 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
602 << FixItHint::CreateRemoval(SS.getRange());
603
Craig Topper161e4db2014-05-21 06:02:52 +0000604 TypeAlias = ParseTypeName(nullptr, TemplateInfo.Kind ?
Richard Smith3f1b5d02011-05-05 21:57:07 +0000605 Declarator::AliasTemplateContext :
Richard Smith54ecd982013-02-20 19:22:51 +0000606 Declarator::AliasDeclContext, AS, OwnedType,
607 &Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000608 } else {
609 // C++11 attributes are not allowed on a using-declaration, but GNU ones
610 // are.
Richard Smithc2c8bb82013-10-15 01:34:54 +0000611 ProhibitAttributes(MisplacedAttrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000612 ProhibitAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000613
Richard Smithdda56e42011-04-15 14:24:37 +0000614 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith54ecd982013-02-20 19:22:51 +0000615 MaybeParseGNUAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000616 }
Mike Stump11289f42009-09-09 15:08:12 +0000617
Douglas Gregorfec52632009-06-20 00:51:54 +0000618 // Eat ';'.
619 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000620 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
621 !Attrs.empty() ? "attributes list"
622 : IsAliasDecl ? "alias declaration"
623 : "using declaration"))
624 SkipUntil(tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000625
John McCall9b72f892010-11-10 02:40:36 +0000626 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000627 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000628 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000629 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-11-10 02:40:36 +0000630 SourceRange R = TemplateInfo.getSourceRange();
631 Diag(UsingLoc, diag::err_templated_using_declaration)
632 << R << FixItHint::CreateRemoval(R);
633
634 // Unfortunately, we have to bail out instead of recovering by
635 // ignoring the parameters, just in case the nested name specifier
636 // depends on the parameters.
Craig Topper161e4db2014-05-21 06:02:52 +0000637 return nullptr;
John McCall9b72f892010-11-10 02:40:36 +0000638 }
639
Douglas Gregor882a61a2011-09-26 14:30:28 +0000640 // "typename" keyword is allowed for identifiers only,
641 // because it may be a type definition.
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000642 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor882a61a2011-09-26 14:30:28 +0000643 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
644 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000645 // Proceed parsing, but reset the HasTypenameKeyword flag.
646 HasTypenameKeyword = false;
Douglas Gregor882a61a2011-09-26 14:30:28 +0000647 }
648
Richard Smith3f1b5d02011-05-05 21:57:07 +0000649 if (IsAliasDecl) {
650 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000651 MultiTemplateParamsArg TemplateParamsArg(
Craig Topper161e4db2014-05-21 06:02:52 +0000652 TemplateParams ? TemplateParams->data() : nullptr,
Richard Smith3f1b5d02011-05-05 21:57:07 +0000653 TemplateParams ? TemplateParams->size() : 0);
654 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith54ecd982013-02-20 19:22:51 +0000655 UsingLoc, Name, Attrs.getList(),
656 TypeAlias);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000657 }
Richard Smithdda56e42011-04-15 14:24:37 +0000658
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000659 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
660 /* HasUsingKeyword */ true, UsingLoc,
661 SS, Name, Attrs.getList(),
662 HasTypenameKeyword, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000663}
664
Benjamin Kramere56f3932011-12-23 17:00:35 +0000665/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000666///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000667/// [C++0x] static_assert-declaration:
668/// static_assert ( constant-expression , string-literal ) ;
669///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000670/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000671/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000672///
John McCall48871652010-08-21 09:40:31 +0000673Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000674 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
675 "Not a static_assert declaration");
676
David Blaikiebbafb8a2012-03-11 07:00:24 +0000677 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000678 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000679 if (Tok.is(tok::kw_static_assert))
680 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000681
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000682 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000683
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000684 BalancedDelimiterTracker T(*this, tok::l_paren);
685 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000686 Diag(Tok, diag::err_expected) << tok::l_paren;
Richard Smith76965712012-09-13 19:12:50 +0000687 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000688 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000689 }
Mike Stump11289f42009-09-09 15:08:12 +0000690
John McCalldadc5752010-08-24 06:29:42 +0000691 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000692 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000693 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000694 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000695 }
Mike Stump11289f42009-09-09 15:08:12 +0000696
Richard Smith085a64f2014-06-20 19:57:12 +0000697 ExprResult AssertMessage;
698 if (Tok.is(tok::r_paren)) {
699 Diag(Tok, getLangOpts().CPlusPlus1z
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000700 ? diag::warn_cxx14_compat_static_assert_no_message
Richard Smith085a64f2014-06-20 19:57:12 +0000701 : diag::ext_static_assert_no_message)
702 << (getLangOpts().CPlusPlus1z
703 ? FixItHint()
704 : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\""));
705 } else {
706 if (ExpectAndConsume(tok::comma)) {
707 SkipUntil(tok::semi);
708 return nullptr;
709 }
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000710
Richard Smith085a64f2014-06-20 19:57:12 +0000711 if (!isTokenStringLiteral()) {
712 Diag(Tok, diag::err_expected_string_literal)
713 << /*Source='static_assert'*/1;
714 SkipMalformedDecl();
715 return nullptr;
716 }
Mike Stump11289f42009-09-09 15:08:12 +0000717
Richard Smith085a64f2014-06-20 19:57:12 +0000718 AssertMessage = ParseStringLiteralExpression();
719 if (AssertMessage.isInvalid()) {
720 SkipMalformedDecl();
721 return nullptr;
722 }
Richard Smithd67aea22012-03-06 03:21:47 +0000723 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000724
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000725 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000726
Chris Lattner49836b42009-04-02 04:16:50 +0000727 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000728 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000729
John McCallb268a282010-08-23 23:25:46 +0000730 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000731 AssertExpr.get(),
732 AssertMessage.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000733 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000734}
735
Richard Smith74aeef52013-04-26 16:15:35 +0000736/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000737///
738/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000739/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000740///
David Blaikie15a430a2011-12-04 05:04:18 +0000741SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
742 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
743 && "Not a decltype specifier");
744
David Blaikie15a430a2011-12-04 05:04:18 +0000745 ExprResult Result;
746 SourceLocation StartLoc = Tok.getLocation();
747 SourceLocation EndLoc;
748
749 if (Tok.is(tok::annot_decltype)) {
750 Result = getExprAnnotation(Tok);
751 EndLoc = Tok.getAnnotationEndLoc();
752 ConsumeToken();
753 if (Result.isInvalid()) {
754 DS.SetTypeSpecError();
755 return EndLoc;
756 }
757 } else {
Richard Smith324df552012-02-24 22:30:04 +0000758 if (Tok.getIdentifierInfo()->isStr("decltype"))
759 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000760
David Blaikie15a430a2011-12-04 05:04:18 +0000761 ConsumeToken();
762
763 BalancedDelimiterTracker T(*this, tok::l_paren);
764 if (T.expectAndConsume(diag::err_expected_lparen_after,
765 "decltype", tok::r_paren)) {
766 DS.SetTypeSpecError();
767 return T.getOpenLocation() == Tok.getLocation() ?
768 StartLoc : T.getOpenLocation();
769 }
770
Richard Smith74aeef52013-04-26 16:15:35 +0000771 // Check for C++1y 'decltype(auto)'.
772 if (Tok.is(tok::kw_auto)) {
773 // No need to disambiguate here: an expression can't start with 'auto',
774 // because the typename-specifier in a function-style cast operation can't
775 // be 'auto'.
776 Diag(Tok.getLocation(),
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000777 getLangOpts().CPlusPlus14
Richard Smith74aeef52013-04-26 16:15:35 +0000778 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
779 : diag::ext_decltype_auto_type_specifier);
780 ConsumeToken();
781 } else {
782 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000783
Richard Smith74aeef52013-04-26 16:15:35 +0000784 // C++11 [dcl.type.simple]p4:
785 // The operand of the decltype specifier is an unevaluated operand.
786 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
Craig Topper161e4db2014-05-21 06:02:52 +0000787 nullptr,/*IsDecltype=*/true);
Richard Smith74aeef52013-04-26 16:15:35 +0000788 Result = ParseExpression();
789 if (Result.isInvalid()) {
790 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000791 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smith74aeef52013-04-26 16:15:35 +0000792 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000793 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000794 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
795 // Backtrack to get the location of the last token before the semi.
796 PP.RevertCachedTokens(2);
797 ConsumeToken(); // the semi.
798 EndLoc = ConsumeAnyToken();
799 assert(Tok.is(tok::semi));
800 } else {
801 EndLoc = Tok.getLocation();
802 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000803 }
Richard Smith74aeef52013-04-26 16:15:35 +0000804 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000805 }
Richard Smith74aeef52013-04-26 16:15:35 +0000806
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000807 Result = Actions.ActOnDecltypeExpression(Result.get());
David Blaikie15a430a2011-12-04 05:04:18 +0000808 }
809
810 // Match the ')'
811 T.consumeClose();
812 if (T.getCloseLocation().isInvalid()) {
813 DS.SetTypeSpecError();
814 // FIXME: this should return the location of the last token
815 // that was consumed (by "consumeClose()")
816 return T.getCloseLocation();
817 }
818
Richard Smithfd555f62012-02-22 02:04:18 +0000819 if (Result.isInvalid()) {
820 DS.SetTypeSpecError();
821 return T.getCloseLocation();
822 }
823
David Blaikie15a430a2011-12-04 05:04:18 +0000824 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000825 }
Richard Smith74aeef52013-04-26 16:15:35 +0000826 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000827
Craig Topper161e4db2014-05-21 06:02:52 +0000828 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +0000829 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000830 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Anders Carlsson74948d02009-06-24 17:47:40 +0000831 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000832 if (Result.get()
833 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000834 DiagID, Result.get(), Policy)
Richard Smith74aeef52013-04-26 16:15:35 +0000835 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000836 DiagID, Policy)) {
John McCall49bfce42009-08-03 20:12:06 +0000837 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000838 DS.SetTypeSpecError();
839 }
840 return EndLoc;
841}
842
843void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
844 SourceLocation StartLoc,
845 SourceLocation EndLoc) {
846 // make sure we have a token we can turn into an annotation token
847 if (PP.isBacktrackEnabled())
848 PP.RevertCachedTokens(1);
849 else
850 PP.EnterToken(Tok);
851
852 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000853 setExprAnnotation(Tok,
854 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
855 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
856 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000857 Tok.setAnnotationEndLoc(EndLoc);
858 Tok.setLocation(StartLoc);
859 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000860}
861
Alexis Hunt4a257072011-05-19 05:37:45 +0000862void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
863 assert(Tok.is(tok::kw___underlying_type) &&
864 "Not an underlying type specifier");
865
866 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000867 BalancedDelimiterTracker T(*this, tok::l_paren);
868 if (T.expectAndConsume(diag::err_expected_lparen_after,
869 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +0000870 return;
871 }
872
873 TypeResult Result = ParseTypeName();
874 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000875 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt4a257072011-05-19 05:37:45 +0000876 return;
877 }
878
879 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000880 T.consumeClose();
881 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000882 return;
883
Craig Topper161e4db2014-05-21 06:02:52 +0000884 const char *PrevSpec = nullptr;
Alexis Hunt4a257072011-05-19 05:37:45 +0000885 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000886 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000887 DiagID, Result.get(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000888 Actions.getASTContext().getPrintingPolicy()))
Alexis Hunt4a257072011-05-19 05:37:45 +0000889 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanellaa90af722013-07-06 18:54:58 +0000890 DS.setTypeofParensRange(T.getRange());
Alexis Hunt4a257072011-05-19 05:37:45 +0000891}
892
David Blaikie00ee7a082011-10-25 15:01:20 +0000893/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
894/// class name or decltype-specifier. Note that we only check that the result
895/// names a type; semantic analysis will need to verify that the type names a
896/// class. The result is either a type or null, depending on whether a type
897/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +0000898///
Richard Smith4c96e992013-02-19 23:47:15 +0000899/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000900/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +0000901/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000902/// nested-name-specifier[opt] class-name
903/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +0000904/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +0000905/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000906/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000907///
Richard Smith4c96e992013-02-19 23:47:15 +0000908/// In C++98, instead of base-type-specifier, we have:
909///
910/// ::[opt] nested-name-specifier[opt] class-name
Craig Topper9ad7e262014-10-31 06:57:07 +0000911TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
912 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +0000913 // Ignore attempts to use typename
914 if (Tok.is(tok::kw_typename)) {
915 Diag(Tok, diag::err_expected_class_name_not_template)
916 << FixItHint::CreateRemoval(Tok.getLocation());
917 ConsumeToken();
918 }
919
David Blaikieafa155f2011-10-25 18:17:58 +0000920 // Parse optional nested-name-specifier
921 CXXScopeSpec SS;
922 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
923
924 BaseLoc = Tok.getLocation();
925
David Blaikie1cd50022011-10-25 17:10:12 +0000926 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +0000927 // tok == kw_decltype is just error recovery, it can only happen when SS
928 // isn't empty
929 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +0000930 if (SS.isNotEmpty())
931 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
932 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +0000933 // Fake up a Declarator to use with ActOnTypeName.
934 DeclSpec DS(AttrFactory);
935
David Blaikie7491e732011-12-08 04:53:15 +0000936 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +0000937
938 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
939 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
940 }
941
Douglas Gregord54dfb82009-02-25 23:52:28 +0000942 // Check whether we have a template-id that names a type.
943 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000944 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +0000945 if (TemplateId->Kind == TNK_Type_template ||
946 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000947 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000948
949 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000950 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000951 EndLocation = Tok.getAnnotationEndLoc();
952 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000953
954 if (Type)
955 return Type;
956 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000957 }
958
959 // Fall through to produce an error below.
960 }
961
Douglas Gregor831c93f2008-11-05 20:51:48 +0000962 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000963 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000964 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000965 }
966
Douglas Gregor18473f32010-01-12 21:28:44 +0000967 IdentifierInfo *Id = Tok.getIdentifierInfo();
968 SourceLocation IdLoc = ConsumeToken();
969
970 if (Tok.is(tok::less)) {
971 // It looks the user intended to write a template-id here, but the
972 // template-name was wrong. Try to fix that.
973 TemplateNameKind TNK = TNK_Type_template;
974 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000975 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000976 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000977 Diag(IdLoc, diag::err_unknown_template_name)
978 << Id;
979 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000980
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000981 if (!Template) {
982 TemplateArgList TemplateArgs;
983 SourceLocation LAngleLoc, RAngleLoc;
984 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
985 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor18473f32010-01-12 21:28:44 +0000986 return true;
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000987 }
Douglas Gregor18473f32010-01-12 21:28:44 +0000988
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000989 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000990 UnqualifiedId TemplateName;
991 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000992
Douglas Gregor18473f32010-01-12 21:28:44 +0000993 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +0000994 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
995 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +0000996 return true;
997 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +0000998 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000999
Douglas Gregor18473f32010-01-12 21:28:44 +00001000 // If we didn't end up with a typename token, there's nothing more we
1001 // can do.
1002 if (Tok.isNot(tok::annot_typename))
1003 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001004
Douglas Gregor18473f32010-01-12 21:28:44 +00001005 // Retrieve the type from the annotation token, consume that token, and
1006 // return.
1007 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +00001008 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +00001009 ConsumeToken();
1010 return Type;
1011 }
1012
Douglas Gregor831c93f2008-11-05 20:51:48 +00001013 // We have an identifier; check whether it is actually a type.
Craig Topper161e4db2014-05-21 06:02:52 +00001014 IdentifierInfo *CorrectedII = nullptr;
Douglas Gregore7c20652011-03-02 00:47:37 +00001015 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +00001016 false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00001017 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +00001018 /*NonTrivialTypeSourceInfo=*/true,
1019 &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001020 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001021 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001022 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +00001023 }
1024
1025 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +00001026 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001027
1028 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +00001029 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +00001030 DS.SetRangeStart(IdLoc);
1031 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +00001032 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001033
Craig Topper161e4db2014-05-21 06:02:52 +00001034 const char *PrevSpec = nullptr;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001035 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001036 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
1037 Actions.getASTContext().getPrintingPolicy());
Nick Lewycky19b9f952010-07-26 16:56:01 +00001038
1039 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1040 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001041}
1042
John McCall8d32c052012-05-22 21:28:12 +00001043void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
1044 while (Tok.is(tok::kw___single_inheritance) ||
1045 Tok.is(tok::kw___multiple_inheritance) ||
1046 Tok.is(tok::kw___virtual_inheritance)) {
1047 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1048 SourceLocation AttrNameLoc = ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001049 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Aaron Ballman8edb5c22013-12-18 23:44:18 +00001050 AttributeList::AS_Keyword);
John McCall8d32c052012-05-22 21:28:12 +00001051 }
1052}
1053
Richard Smith369b9f92012-06-25 21:37:02 +00001054/// Determine whether the following tokens are valid after a type-specifier
1055/// which could be a standalone declaration. This will conservatively return
1056/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +00001057bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +00001058 // This switch enumerates the valid "follow" set for type-specifiers.
1059 switch (Tok.getKind()) {
1060 default: break;
1061 case tok::semi: // struct foo {...} ;
1062 case tok::star: // struct foo {...} * P;
1063 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +00001064 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +00001065 case tok::identifier: // struct foo {...} V ;
1066 case tok::r_paren: //(struct foo {...} ) {4}
1067 case tok::annot_cxxscope: // struct foo {...} a:: b;
1068 case tok::annot_typename: // struct foo {...} a ::b;
1069 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1070 case tok::l_paren: // struct foo {...} ( x);
1071 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001072 case tok::kw_operator: // struct foo operator ++() {...}
Alp Tokerd3f79c52013-11-24 20:24:54 +00001073 case tok::kw___declspec: // struct foo {...} __declspec(...)
Richard Smith843f18f2014-08-13 02:13:15 +00001074 case tok::l_square: // void f(struct f [ 3])
1075 case tok::ellipsis: // void f(struct f ... [Ns])
Abramo Bagnara152eb392014-08-16 08:29:27 +00001076 // FIXME: we should emit semantic diagnostic when declaration
1077 // attribute is in type attribute position.
1078 case tok::kw___attribute: // struct foo __attribute__((used)) x;
Richard Smith369b9f92012-06-25 21:37:02 +00001079 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001080 case tok::colon:
1081 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smith369b9f92012-06-25 21:37:02 +00001082 // Type qualifiers
1083 case tok::kw_const: // struct foo {...} const x;
1084 case tok::kw_volatile: // struct foo {...} volatile x;
1085 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith843f18f2014-08-13 02:13:15 +00001086 case tok::kw__Atomic: // struct foo {...} _Atomic x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001087 // Function specifiers
1088 // Note, no 'explicit'. An explicit function must be either a conversion
1089 // operator or a constructor. Either way, it can't have a return type.
1090 case tok::kw_inline: // struct foo inline f();
1091 case tok::kw_virtual: // struct foo virtual f();
1092 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001093 // Storage-class specifiers
1094 case tok::kw_static: // struct foo {...} static x;
1095 case tok::kw_extern: // struct foo {...} extern x;
1096 case tok::kw_typedef: // struct foo {...} typedef x;
1097 case tok::kw_register: // struct foo {...} register x;
1098 case tok::kw_auto: // struct foo {...} auto x;
1099 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001100 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001101 case tok::kw_constexpr: // struct foo {...} constexpr x;
1102 // As shown above, type qualifiers and storage class specifiers absolutely
1103 // can occur after class specifiers according to the grammar. However,
1104 // almost no one actually writes code like this. If we see one of these,
1105 // it is much more likely that someone missed a semi colon and the
1106 // type/storage class specifier we're seeing is part of the *next*
1107 // intended declaration, as in:
1108 //
1109 // struct foo { ... }
1110 // typedef int X;
1111 //
1112 // We'd really like to emit a missing semicolon error instead of emitting
1113 // an error on the 'int' saying that you can't have two type specifiers in
1114 // the same declaration of X. Because of this, we look ahead past this
1115 // token to see if it's a type specifier. If so, we know the code is
1116 // otherwise invalid, so we can produce the expected semi error.
1117 if (!isKnownToBeTypeSpecifier(NextToken()))
1118 return true;
1119 break;
1120 case tok::r_brace: // struct bar { struct foo {...} }
1121 // Missing ';' at end of struct is accepted as an extension in C mode.
1122 if (!getLangOpts().CPlusPlus)
1123 return true;
1124 break;
Richard Smith52c5b872013-01-29 04:13:32 +00001125 case tok::greater:
1126 // template<class T = class X>
1127 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001128 }
1129 return false;
1130}
1131
Douglas Gregor556877c2008-04-13 21:30:24 +00001132/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1133/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1134/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001135/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001136///
1137/// class-specifier: [C++ class]
1138/// class-head '{' member-specification[opt] '}'
1139/// class-head '{' member-specification[opt] '}' attributes[opt]
1140/// class-head:
1141/// class-key identifier[opt] base-clause[opt]
1142/// class-key nested-name-specifier identifier base-clause[opt]
1143/// class-key nested-name-specifier[opt] simple-template-id
1144/// base-clause[opt]
1145/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001146/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001147/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001148/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001149/// simple-template-id base-clause[opt]
1150/// class-key:
1151/// 'class'
1152/// 'struct'
1153/// 'union'
1154///
1155/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001156/// class-key ::[opt] nested-name-specifier[opt] identifier
1157/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1158/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001159///
1160/// Note that the C++ class-specifier and elaborated-type-specifier,
1161/// together, subsume the C99 struct-or-union-specifier:
1162///
1163/// struct-or-union-specifier: [C99 6.7.2.1]
1164/// struct-or-union identifier[opt] '{' struct-contents '}'
1165/// struct-or-union identifier
1166/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1167/// '}' attributes[opt]
1168/// [GNU] struct-or-union attributes[opt] identifier
1169/// struct-or-union:
1170/// 'struct'
1171/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001172void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1173 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001174 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001175 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001176 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001177 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001178 DeclSpec::TST TagType;
1179 if (TagTokKind == tok::kw_struct)
1180 TagType = DeclSpec::TST_struct;
1181 else if (TagTokKind == tok::kw___interface)
1182 TagType = DeclSpec::TST_interface;
1183 else if (TagTokKind == tok::kw_class)
1184 TagType = DeclSpec::TST_class;
1185 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001186 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1187 TagType = DeclSpec::TST_union;
1188 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001189
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001190 if (Tok.is(tok::code_completion)) {
1191 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001192 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001193 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001194 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001195
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001196 // C++03 [temp.explicit] 14.7.2/8:
1197 // The usual access checking rules do not apply to names used to specify
1198 // explicit instantiations.
1199 //
1200 // As an extension we do not perform access checking on the names used to
1201 // specify explicit specializations either. This is important to allow
1202 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001203 //
1204 // Note that we don't suppress if this turns out to be an elaborated
1205 // type specifier.
1206 bool shouldDelayDiagsInTag =
1207 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1208 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1209 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001210
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001211 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001212 // If attributes exist after tag, parse them.
Richard Smith37a45dd2013-10-24 01:21:09 +00001213 MaybeParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +00001214
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001215 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +00001216 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +00001217 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001218
John McCall8d32c052012-05-22 21:28:12 +00001219 // Parse inheritance specifiers.
1220 if (Tok.is(tok::kw___single_inheritance) ||
1221 Tok.is(tok::kw___multiple_inheritance) ||
1222 Tok.is(tok::kw___virtual_inheritance))
Richard Smith37a45dd2013-10-24 01:21:09 +00001223 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCall8d32c052012-05-22 21:28:12 +00001224
Alexis Hunt96d5c762009-11-21 08:43:09 +00001225 // If C++0x attributes exist here, parse them.
1226 // FIXME: Are we consistent with the ordering of parsing of different
1227 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001228 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001229
Michael Han309af292013-01-07 16:57:11 +00001230 // Source location used by FIXIT to insert misplaced
1231 // C++11 attributes
1232 SourceLocation AttrFixitLoc = Tok.getLocation();
1233
Nico Weber7c3c5be2014-09-23 04:09:56 +00001234 if (TagType == DeclSpec::TST_struct &&
1235 !Tok.is(tok::identifier) &&
1236 Tok.getIdentifierInfo() &&
Nico Weberb10c9202014-09-24 03:28:54 +00001237 (Tok.is(tok::kw___is_abstract) ||
1238 Tok.is(tok::kw___is_arithmetic) ||
1239 Tok.is(tok::kw___is_array) ||
1240 Tok.is(tok::kw___is_base_of) ||
1241 Tok.is(tok::kw___is_class) ||
1242 Tok.is(tok::kw___is_complete_type) ||
1243 Tok.is(tok::kw___is_compound) ||
1244 Tok.is(tok::kw___is_const) ||
1245 Tok.is(tok::kw___is_constructible) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001246 Tok.is(tok::kw___is_convertible) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001247 Tok.is(tok::kw___is_convertible_to) ||
1248 Tok.is(tok::kw___is_destructible) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001249 Tok.is(tok::kw___is_empty) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001250 Tok.is(tok::kw___is_enum) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001251 Tok.is(tok::kw___is_floating_point) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001252 Tok.is(tok::kw___is_final) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001253 Tok.is(tok::kw___is_function) ||
1254 Tok.is(tok::kw___is_fundamental) ||
1255 Tok.is(tok::kw___is_integral) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001256 Tok.is(tok::kw___is_interface_class) ||
1257 Tok.is(tok::kw___is_literal) ||
1258 Tok.is(tok::kw___is_lvalue_expr) ||
1259 Tok.is(tok::kw___is_lvalue_reference) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001260 Tok.is(tok::kw___is_member_function_pointer) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001261 Tok.is(tok::kw___is_member_object_pointer) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001262 Tok.is(tok::kw___is_member_pointer) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001263 Tok.is(tok::kw___is_nothrow_assignable) ||
1264 Tok.is(tok::kw___is_nothrow_constructible) ||
1265 Tok.is(tok::kw___is_nothrow_destructible) ||
1266 Tok.is(tok::kw___is_object) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001267 Tok.is(tok::kw___is_pod) ||
1268 Tok.is(tok::kw___is_pointer) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001269 Tok.is(tok::kw___is_polymorphic) ||
1270 Tok.is(tok::kw___is_reference) ||
1271 Tok.is(tok::kw___is_rvalue_expr) ||
1272 Tok.is(tok::kw___is_rvalue_reference) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001273 Tok.is(tok::kw___is_same) ||
1274 Tok.is(tok::kw___is_scalar) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001275 Tok.is(tok::kw___is_sealed) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001276 Tok.is(tok::kw___is_signed) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001277 Tok.is(tok::kw___is_standard_layout) ||
1278 Tok.is(tok::kw___is_trivial) ||
1279 Tok.is(tok::kw___is_trivially_assignable) ||
1280 Tok.is(tok::kw___is_trivially_constructible) ||
1281 Tok.is(tok::kw___is_trivially_copyable) ||
1282 Tok.is(tok::kw___is_union) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001283 Tok.is(tok::kw___is_unsigned) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001284 Tok.is(tok::kw___is_void) ||
1285 Tok.is(tok::kw___is_volatile)))
Nico Weber7c3c5be2014-09-23 04:09:56 +00001286 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
1287 // name of struct templates, but some are keywords in GCC >= 4.3
1288 // and Clang. Therefore, when we see the token sequence "struct
1289 // X", make X into a normal identifier rather than a keyword, to
1290 // allow libstdc++ 4.2 and libc++ to work properly.
1291 TryKeywordIdentFallback(true);
Mike Stump11289f42009-09-09 15:08:12 +00001292
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001293 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001294 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001295 if (getLangOpts().CPlusPlus) {
Serge Pavlov458ea762014-07-16 05:16:52 +00001296 // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it
1297 // is a base-specifier-list.
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001298 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001299
Douglas Gregordf593fb2011-11-07 17:33:42 +00001300 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall413021a2010-07-30 06:26:29 +00001301 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +00001302 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001303 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Alp Tokerec543272013-12-24 09:48:30 +00001304 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001305 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001306
Douglas Gregor916462b2009-10-30 21:46:58 +00001307 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1308
Douglas Gregor67a65642009-02-17 23:15:12 +00001309 // Parse the (optional) class name or simple-template-id.
Craig Topper161e4db2014-05-21 06:02:52 +00001310 IdentifierInfo *Name = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001311 SourceLocation NameLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001312 TemplateIdAnnotation *TemplateId = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001313 if (Tok.is(tok::identifier)) {
1314 Name = Tok.getIdentifierInfo();
1315 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001316
David Blaikiebbafb8a2012-03-11 07:00:24 +00001317 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001318 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001319 // Eat the template argument list and try to continue parsing this as
1320 // a class (or template thereof).
1321 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001322 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +00001323 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +00001324 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001325 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001326 // We couldn't parse the template argument list at all, so don't
1327 // try to give any location information for the list.
1328 LAngleLoc = RAngleLoc = SourceLocation();
1329 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001330
Douglas Gregor916462b2009-10-30 21:46:58 +00001331 Diag(NameLoc, diag::err_explicit_spec_non_template)
Alp Toker01d65e12014-01-06 12:54:41 +00001332 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1333 << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
Joao Matose9a3ed42012-08-31 22:18:20 +00001334
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001335 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001336 // we've removed its template argument list.
1337 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1338 if (TemplateParams && TemplateParams->size() > 1) {
1339 TemplateParams->pop_back();
1340 } else {
Craig Topper161e4db2014-05-21 06:02:52 +00001341 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001342 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001343 = ParsedTemplateInfo::NonTemplate;
1344 }
1345 } else if (TemplateInfo.Kind
1346 == ParsedTemplateInfo::ExplicitInstantiation) {
1347 // Pretend this is just a forward declaration.
Craig Topper161e4db2014-05-21 06:02:52 +00001348 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001349 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001350 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001351 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001352 = SourceLocation();
1353 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1354 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001355 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001356 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001357 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001358 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001359 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001360
Douglas Gregore7c20652011-03-02 00:47:37 +00001361 if (TemplateId->Kind != TNK_Type_template &&
1362 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001363 // The template-name in the simple-template-id refers to
1364 // something other than a class template. Give an appropriate
1365 // error message and skip to the ';'.
1366 SourceRange Range(NameLoc);
1367 if (SS.isNotEmpty())
1368 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001369
Richard Smith72bfbd82013-12-04 00:28:23 +00001370 // FIXME: Name may be null here.
Douglas Gregor7f741122009-02-25 19:37:18 +00001371 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001372 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001373
Douglas Gregor7f741122009-02-25 19:37:18 +00001374 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001375 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor7f741122009-02-25 19:37:18 +00001376 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001377 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001378 }
1379
Richard Smithbfdb1082012-03-12 08:56:40 +00001380 // There are four options here.
1381 // - If we are in a trailing return type, this is always just a reference,
1382 // and we must not try to parse a definition. For instance,
1383 // [] () -> struct S { };
1384 // does not define a type.
1385 // - If we have 'struct foo {...', 'struct foo :...',
1386 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1387 // - If we have 'struct foo;', then this is either a forward declaration
1388 // or a friend declaration, which have to be treated differently.
1389 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001390 //
1391 // We also detect these erroneous cases to provide better diagnostic for
1392 // C++11 attributes parsing.
1393 // - attributes follow class name:
1394 // struct foo [[]] {};
1395 // - attributes appear before or after 'final':
1396 // struct foo [[]] final [[]] {};
1397 //
Richard Smithc5b05522012-03-12 07:56:15 +00001398 // However, in type-specifier-seq's, things look like declarations but are
1399 // just references, e.g.
1400 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001401 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001402 // &T::operator struct s;
Richard Smith649c7b062014-01-08 00:56:48 +00001403 // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
Michael Han9407e502012-11-26 22:54:45 +00001404
1405 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001406 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001407
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001408 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
John McCallfaf5fb42010-08-26 23:41:50 +00001409 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001410 if (DSC == DSC_trailing)
1411 TUK = Sema::TUK_Reference;
1412 else if (Tok.is(tok::l_brace) ||
1413 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001414 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001415 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001416 if (DS.isFriendSpecified()) {
1417 // C++ [class.friend]p2:
1418 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001419 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001420 << SourceRange(DS.getFriendSpecLoc());
1421
1422 // Skip everything up to the semicolon, so that this looks like a proper
1423 // friend class (or template thereof) declaration.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001424 SkipUntil(tok::semi, StopBeforeMatch);
John McCallfaf5fb42010-08-26 23:41:50 +00001425 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001426 } else {
1427 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001428 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001429 }
Richard Smith434516c2013-02-22 06:46:23 +00001430 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1431 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001432 // We can't tell if this is a definition or reference
1433 // until we skipped the 'final' and C++11 attribute specifiers.
1434 TentativeParsingAction PA(*this);
1435
1436 // Skip the 'final' keyword.
1437 ConsumeToken();
1438
1439 // Skip C++11 attribute specifiers.
1440 while (true) {
1441 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1442 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001443 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001444 break;
Richard Smith434516c2013-02-22 06:46:23 +00001445 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001446 ConsumeToken();
1447 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001448 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001449 break;
1450 } else {
1451 break;
1452 }
1453 }
1454
1455 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1456 TUK = Sema::TUK_Definition;
1457 else
1458 TUK = Sema::TUK_Reference;
1459
1460 PA.Revert();
Richard Smith649c7b062014-01-08 00:56:48 +00001461 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00001462 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001463 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001464 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001465 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001466 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Joao Matose9a3ed42012-08-31 22:18:20 +00001467 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00001468 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001469 DeclSpec::getSpecifierName(TagType, PPol));
Joao Matose9a3ed42012-08-31 22:18:20 +00001470 PP.EnterToken(Tok);
1471 Tok.setKind(tok::semi);
1472 }
Richard Smith369b9f92012-06-25 21:37:02 +00001473 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001474 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001475
Michael Han9407e502012-11-26 22:54:45 +00001476 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1477 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001478 if (TUK != Sema::TUK_Reference) {
1479 // If this is not a reference, then the only possible
1480 // valid place for C++11 attributes to appear here
1481 // is between class-key and class-name. If there are
1482 // any attributes after class-name, we try a fixit to move
1483 // them to the right place.
1484 SourceRange AttrRange = Attributes.Range;
1485 if (AttrRange.isValid()) {
1486 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1487 << AttrRange
1488 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1489 CharSourceRange(AttrRange, true))
1490 << FixItHint::CreateRemoval(AttrRange);
1491
1492 // Recover by adding misplaced attributes to the attribute list
1493 // of the class so they can be applied on the class later.
1494 attrs.takeAllFrom(Attributes);
1495 }
1496 }
Michael Han9407e502012-11-26 22:54:45 +00001497
John McCall6347b682012-05-07 06:16:58 +00001498 // If this is an elaborated type specifier, and we delayed
1499 // diagnostics before, just merge them into the current pool.
1500 if (shouldDelayDiagsInTag) {
1501 diagsFromTag.done();
1502 if (TUK == Sema::TUK_Reference)
1503 diagsFromTag.redelay();
1504 }
1505
John McCall413021a2010-07-30 06:26:29 +00001506 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001507 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001508 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1509 // We have a declaration or reference to an anonymous class.
1510 Diag(StartLoc, diag::err_anon_type_definition)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001511 << DeclSpec::getSpecifierName(TagType, Policy);
John McCall413021a2010-07-30 06:26:29 +00001512 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001513
David Majnemer3252fd02013-12-05 01:36:53 +00001514 // If we are parsing a definition and stop at a base-clause, continue on
1515 // until the semicolon. Continuing from the comma will just trick us into
1516 // thinking we are seeing a variable declaration.
1517 if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1518 SkipUntil(tok::semi, StopBeforeMatch);
1519 else
1520 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor556877c2008-04-13 21:30:24 +00001521 return;
1522 }
1523
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001524 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001525 DeclResult TagOrTempResult = true; // invalid
1526 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001527
Douglas Gregord6ab8742009-05-28 23:31:59 +00001528 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +00001529 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001530 // Explicit specialization, class template partial specialization,
1531 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001532 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001533 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001534 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001535 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001536 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001537 ProhibitAttributes(attrs);
1538
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001539 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001540 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001541 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001542 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001543 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001544 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001545 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001546 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001547 TemplateId->TemplateNameLoc,
1548 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001549 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001550 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001551 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001552
1553 // Friend template-ids are treated as references unless
1554 // they have template headers, in which case they're ill-formed
1555 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1556 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001557 } else if (TUK == Sema::TUK_Reference ||
1558 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001559 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001560 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001561 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001562 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001563 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001564 TemplateId->Template,
1565 TemplateId->TemplateNameLoc,
1566 TemplateId->LAngleLoc,
1567 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001568 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001569 } else {
1570 // This is an explicit specialization or a class template
1571 // partial specialization.
1572 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001573 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1574 // This looks like an explicit instantiation, because we have
1575 // something like
1576 //
1577 // template class Foo<X>
1578 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001579 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001580 // meant to be an explicit specialization, but the user forgot
1581 // the '<>' after 'template'.
Richard Smith003c5e12013-11-08 19:03:29 +00001582 // It this is friend declaration however, since it cannot have a
1583 // template header, it is most likely that the user meant to
1584 // remove the 'template' keyword.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001585 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith003c5e12013-11-08 19:03:29 +00001586 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001587
Richard Smith003c5e12013-11-08 19:03:29 +00001588 if (TUK == Sema::TUK_Friend) {
1589 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
Craig Topper161e4db2014-05-21 06:02:52 +00001590 TemplateParams = nullptr;
Richard Smith003c5e12013-11-08 19:03:29 +00001591 } else {
1592 SourceLocation LAngleLoc =
1593 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1594 Diag(TemplateId->TemplateNameLoc,
1595 diag::err_explicit_instantiation_with_definition)
1596 << SourceRange(TemplateInfo.TemplateLoc)
1597 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1598
1599 // Create a fake template parameter list that contains only
1600 // "template<>", so that we treat this construct as a class
1601 // template specialization.
1602 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
Craig Topper161e4db2014-05-21 06:02:52 +00001603 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, nullptr,
1604 0, LAngleLoc));
Richard Smith003c5e12013-11-08 19:03:29 +00001605 TemplateParams = &FakedParamLists;
1606 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001607 }
1608
1609 // Build the class template specialization.
Richard Smith4b55a9c2014-04-17 03:29:33 +00001610 TagOrTempResult = Actions.ActOnClassTemplateSpecialization(
1611 getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(),
1612 *TemplateId, attrs.getList(),
Craig Topper161e4db2014-05-21 06:02:52 +00001613 MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0]
1614 : nullptr,
Richard Smith4b55a9c2014-04-17 03:29:33 +00001615 TemplateParams ? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001616 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001617 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001618 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001619 // Explicit instantiation of a member of a class template
1620 // specialization, e.g.,
1621 //
1622 // template struct Outer<int>::Inner;
1623 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001624 ProhibitAttributes(attrs);
1625
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001626 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001627 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001628 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001629 TemplateInfo.TemplateLoc,
1630 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001631 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001632 } else if (TUK == Sema::TUK_Friend &&
1633 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001634 ProhibitAttributes(attrs);
1635
John McCallace48cd2010-10-19 01:40:49 +00001636 TagOrTempResult =
1637 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1638 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001639 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001640 MultiTemplateParamsArg(
Craig Topper161e4db2014-05-21 06:02:52 +00001641 TemplateParams? &(*TemplateParams)[0]
1642 : nullptr,
John McCallace48cd2010-10-19 01:40:49 +00001643 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001644 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001645 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1646 ProhibitAttributes(attrs);
Richard Smith003c5e12013-11-08 19:03:29 +00001647
Larisse Voufo725de3e2013-06-21 00:08:46 +00001648 if (TUK == Sema::TUK_Definition &&
1649 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1650 // If the declarator-id is not a template-id, issue a diagnostic and
1651 // recover by ignoring the 'template' keyword.
1652 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1653 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Craig Topper161e4db2014-05-21 06:02:52 +00001654 TemplateParams = nullptr;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001655 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001656
John McCall7f41d982009-09-11 04:59:25 +00001657 bool IsDependent = false;
1658
John McCall32723e92010-10-19 18:40:57 +00001659 // Don't pass down template parameter lists if this is just a tag
1660 // reference. For example, we don't need the template parameters here:
1661 // template <class T> class A *makeA(T t);
1662 MultiTemplateParamsArg TParams;
1663 if (TUK != Sema::TUK_Reference && TemplateParams)
1664 TParams =
1665 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1666
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001667 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001668 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001669 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001670 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001671 TParams, Owned, IsDependent,
1672 SourceLocation(), false,
Richard Smith649c7b062014-01-08 00:56:48 +00001673 clang::TypeResult(),
1674 DSC == DSC_type_specifier);
John McCall7f41d982009-09-11 04:59:25 +00001675
1676 // If ActOnTag said the type was dependent, try again with the
1677 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001678 if (IsDependent) {
1679 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001680 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001681 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001682 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001683 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001684
Douglas Gregor556877c2008-04-13 21:30:24 +00001685 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001686 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001687 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001688 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001689 isCXX11FinalKeyword());
David Blaikiebbafb8a2012-03-11 07:00:24 +00001690 if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001691 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1692 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001693 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001694 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001695 }
1696
Craig Topper161e4db2014-05-21 06:02:52 +00001697 const char *PrevSpec = nullptr;
John McCallba7bf592010-08-24 05:47:05 +00001698 unsigned DiagID;
1699 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001700 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001701 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1702 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001703 PrevSpec, DiagID, TypeResult.get(), Policy);
John McCall7f41d982009-09-11 04:59:25 +00001704 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001705 Result = DS.SetTypeSpecType(TagType, StartLoc,
1706 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001707 PrevSpec, DiagID, TagOrTempResult.get(), Owned,
1708 Policy);
John McCall7f41d982009-09-11 04:59:25 +00001709 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001710 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001711 return;
1712 }
Mike Stump11289f42009-09-09 15:08:12 +00001713
John McCallba7bf592010-08-24 05:47:05 +00001714 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001715 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001716
Chris Lattnercf251412010-02-02 01:23:29 +00001717 // At this point, we've successfully parsed a class-specifier in 'definition'
1718 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1719 // going to look at what comes after it to improve error recovery. If an
1720 // impossible token occurs next, we assume that the programmer forgot a ; at
1721 // the end of the declaration and recover that way.
1722 //
Richard Smith369b9f92012-06-25 21:37:02 +00001723 // Also enforce C++ [temp]p3:
1724 // In a template-declaration which defines a class, no declarator
1725 // is permitted.
Richard Smith843f18f2014-08-13 02:13:15 +00001726 //
1727 // After a type-specifier, we don't expect a semicolon. This only happens in
1728 // C, since definitions are not permitted in this context in C++.
Joao Matose9a3ed42012-08-31 22:18:20 +00001729 if (TUK == Sema::TUK_Definition &&
Richard Smith843f18f2014-08-13 02:13:15 +00001730 (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) &&
Joao Matose9a3ed42012-08-31 22:18:20 +00001731 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001732 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001733 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Alp Toker383d2c42014-01-01 03:08:43 +00001734 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001735 DeclSpec::getSpecifierName(TagType, PPol));
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001736 // Push this token back into the preprocessor and change our current token
1737 // to ';' so that the rest of the code recovers as though there were an
1738 // ';' after the definition.
1739 PP.EnterToken(Tok);
1740 Tok.setKind(tok::semi);
1741 }
Chris Lattnercf251412010-02-02 01:23:29 +00001742 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001743}
1744
Mike Stump11289f42009-09-09 15:08:12 +00001745/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001746///
1747/// base-clause : [C++ class.derived]
1748/// ':' base-specifier-list
1749/// base-specifier-list:
1750/// base-specifier '...'[opt]
1751/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001752void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001753 assert(Tok.is(tok::colon) && "Not a base clause");
1754 ConsumeToken();
1755
Douglas Gregor29a92472008-10-22 17:49:05 +00001756 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001757 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001758
Douglas Gregor556877c2008-04-13 21:30:24 +00001759 while (true) {
1760 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001761 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001762 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001763 // Skip the rest of this base specifier, up until the comma or
1764 // opening brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001765 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor29a92472008-10-22 17:49:05 +00001766 } else {
1767 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001768 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001769 }
1770
1771 // If the next token is a comma, consume it and keep reading
1772 // base-specifiers.
Alp Toker97650562014-01-10 11:19:30 +00001773 if (!TryConsumeToken(tok::comma))
1774 break;
Douglas Gregor556877c2008-04-13 21:30:24 +00001775 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001776
1777 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001778 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001779}
1780
1781/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1782/// one entry in the base class list of a class specifier, for example:
1783/// class foo : public bar, virtual private baz {
1784/// 'public bar' and 'virtual private baz' are each base-specifiers.
1785///
1786/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001787/// attribute-specifier-seq[opt] base-type-specifier
1788/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1789/// base-type-specifier
1790/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1791/// base-type-specifier
Craig Topper9ad7e262014-10-31 06:57:07 +00001792BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001793 bool IsVirtual = false;
1794 SourceLocation StartLoc = Tok.getLocation();
1795
Richard Smith4c96e992013-02-19 23:47:15 +00001796 ParsedAttributesWithRange Attributes(AttrFactory);
1797 MaybeParseCXX11Attributes(Attributes);
1798
Douglas Gregor556877c2008-04-13 21:30:24 +00001799 // Parse the 'virtual' keyword.
Alp Toker97650562014-01-10 11:19:30 +00001800 if (TryConsumeToken(tok::kw_virtual))
Douglas Gregor556877c2008-04-13 21:30:24 +00001801 IsVirtual = true;
Douglas Gregor556877c2008-04-13 21:30:24 +00001802
Richard Smith4c96e992013-02-19 23:47:15 +00001803 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1804
Douglas Gregor556877c2008-04-13 21:30:24 +00001805 // Parse an (optional) access specifier.
1806 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001807 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001808 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001809
Richard Smith4c96e992013-02-19 23:47:15 +00001810 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1811
Douglas Gregor556877c2008-04-13 21:30:24 +00001812 // Parse the 'virtual' keyword (again!), in case it came after the
1813 // access specifier.
1814 if (Tok.is(tok::kw_virtual)) {
1815 SourceLocation VirtualLoc = ConsumeToken();
1816 if (IsVirtual) {
1817 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001818 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001819 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001820 }
1821
1822 IsVirtual = true;
1823 }
1824
Richard Smith4c96e992013-02-19 23:47:15 +00001825 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1826
Douglas Gregor831c93f2008-11-05 20:51:48 +00001827 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001828 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001829 SourceLocation BaseLoc;
1830 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001831 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001832 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001833
Douglas Gregor752a5952011-01-03 22:36:02 +00001834 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1835 // actually part of the base-specifier-list grammar productions, but we
1836 // parse it here for convenience.
1837 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00001838 TryConsumeToken(tok::ellipsis, EllipsisLoc);
1839
Mike Stump11289f42009-09-09 15:08:12 +00001840 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001841 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001842
Douglas Gregor556877c2008-04-13 21:30:24 +00001843 // Notify semantic analysis that we have parsed a complete
1844 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00001845 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1846 Access, BaseType.get(), BaseLoc,
1847 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001848}
1849
1850/// getAccessSpecifierIfPresent - Determine whether the next token is
1851/// a C++ access-specifier.
1852///
1853/// access-specifier: [C++ class.derived]
1854/// 'private'
1855/// 'protected'
1856/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001857AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001858 switch (Tok.getKind()) {
1859 default: return AS_none;
1860 case tok::kw_private: return AS_private;
1861 case tok::kw_protected: return AS_protected;
1862 case tok::kw_public: return AS_public;
1863 }
1864}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001865
Douglas Gregor433e0532012-04-16 18:27:27 +00001866/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith2331bbf2012-05-02 22:22:32 +00001867/// delayed, e.g., default arguments, create a late-parsed method declaration
1868/// record to handle the parsing at the end of the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00001869void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1870 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001871 // We just declared a member function. If this member function
Richard Smith2331bbf2012-05-02 22:22:32 +00001872 // has any default arguments, we'll need to parse them later.
Craig Topper161e4db2014-05-21 06:02:52 +00001873 LateParsedMethodDeclaration *LateMethod = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001874 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001875 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor433e0532012-04-16 18:27:27 +00001876
Alp Tokerc5350722014-02-26 22:27:52 +00001877 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
1878 if (LateMethod || FTI.Params[ParamIdx].DefaultArgTokens) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001879 if (!LateMethod) {
1880 // Push this method onto the stack of late-parsed method
1881 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001882 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1883 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001884 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001885
1886 // Add all of the parameters prior to this one (they don't
1887 // have default arguments).
Alp Tokerc5350722014-02-26 22:27:52 +00001888 LateMethod->DefaultArgs.reserve(FTI.NumParams);
Eli Friedman3af2a772009-07-22 21:45:50 +00001889 for (unsigned I = 0; I < ParamIdx; ++I)
1890 LateMethod->DefaultArgs.push_back(
Alp Tokerc5350722014-02-26 22:27:52 +00001891 LateParsedDefaultArgument(FTI.Params[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001892 }
1893
Douglas Gregor433e0532012-04-16 18:27:27 +00001894 // Add this parameter to the list of parameters (it may or may
Eli Friedman3af2a772009-07-22 21:45:50 +00001895 // not have a default argument).
Alp Tokerc5350722014-02-26 22:27:52 +00001896 LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
1897 FTI.Params[ParamIdx].Param, FTI.Params[ParamIdx].DefaultArgTokens));
Eli Friedman3af2a772009-07-22 21:45:50 +00001898 }
1899 }
1900}
1901
Richard Smith89645bc2013-01-02 12:01:23 +00001902/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001903/// virt-specifier.
1904///
1905/// virt-specifier:
1906/// override
1907/// final
Richard Smith89645bc2013-01-02 12:01:23 +00001908VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001909 if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001910 return VirtSpecifiers::VS_None;
1911
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001912 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001913
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001914 // Initialize the contextual keywords.
1915 if (!Ident_final) {
1916 Ident_final = &PP.getIdentifierTable().get("final");
1917 if (getLangOpts().MicrosoftExt)
1918 Ident_sealed = &PP.getIdentifierTable().get("sealed");
1919 Ident_override = &PP.getIdentifierTable().get("override");
Anders Carlsson56104902011-01-17 03:05:47 +00001920 }
1921
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001922 if (II == Ident_override)
1923 return VirtSpecifiers::VS_Override;
1924
1925 if (II == Ident_sealed)
1926 return VirtSpecifiers::VS_Sealed;
1927
1928 if (II == Ident_final)
1929 return VirtSpecifiers::VS_Final;
1930
Anders Carlsson56104902011-01-17 03:05:47 +00001931 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001932}
1933
Richard Smith89645bc2013-01-02 12:01:23 +00001934/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001935///
1936/// virt-specifier-seq:
1937/// virt-specifier
1938/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00001939void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
Richard Smith3d1a94c2014-08-12 00:22:39 +00001940 bool IsInterface,
1941 SourceLocation FriendLoc) {
Anders Carlsson56104902011-01-17 03:05:47 +00001942 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00001943 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001944 if (Specifier == VirtSpecifiers::VS_None)
1945 return;
1946
Richard Smith3d1a94c2014-08-12 00:22:39 +00001947 if (FriendLoc.isValid()) {
1948 Diag(Tok.getLocation(), diag::err_friend_decl_spec)
1949 << VirtSpecifiers::getSpecifierName(Specifier)
1950 << FixItHint::CreateRemoval(Tok.getLocation())
1951 << SourceRange(FriendLoc, FriendLoc);
1952 ConsumeToken();
1953 continue;
1954 }
1955
Anders Carlsson56104902011-01-17 03:05:47 +00001956 // C++ [class.mem]p8:
1957 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Craig Topper161e4db2014-05-21 06:02:52 +00001958 const char *PrevSpec = nullptr;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001959 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001960 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1961 << PrevSpec
1962 << FixItHint::CreateRemoval(Tok.getLocation());
1963
David Majnemera5433082013-10-18 00:33:31 +00001964 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
1965 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalldb632ac2012-09-25 07:32:39 +00001966 Diag(Tok.getLocation(), diag::err_override_control_interface)
1967 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemera5433082013-10-18 00:33:31 +00001968 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
1969 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalldb632ac2012-09-25 07:32:39 +00001970 } else {
David Majnemera5433082013-10-18 00:33:31 +00001971 Diag(Tok.getLocation(),
1972 getLangOpts().CPlusPlus11
1973 ? diag::warn_cxx98_compat_override_control_keyword
1974 : diag::ext_override_control_keyword)
1975 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalldb632ac2012-09-25 07:32:39 +00001976 }
Anders Carlsson56104902011-01-17 03:05:47 +00001977 ConsumeToken();
1978 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001979}
1980
Richard Smith89645bc2013-01-02 12:01:23 +00001981/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001982/// 'final' or Microsoft 'sealed' contextual keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00001983bool Parser::isCXX11FinalKeyword() const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001984 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
1985 return Specifier == VirtSpecifiers::VS_Final ||
1986 Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001987}
1988
Richard Smith72553fc2014-01-23 23:53:27 +00001989/// \brief Parse a C++ member-declarator up to, but not including, the optional
1990/// brace-or-equal-initializer or pure-specifier.
1991void Parser::ParseCXXMemberDeclaratorBeforeInitializer(
1992 Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
1993 LateParsedAttrList &LateParsedAttrs) {
1994 // member-declarator:
1995 // declarator pure-specifier[opt]
1996 // declarator brace-or-equal-initializer[opt]
1997 // identifier[opt] ':' constant-expression
Serge Pavlov458ea762014-07-16 05:16:52 +00001998 if (Tok.isNot(tok::colon))
Richard Smith72553fc2014-01-23 23:53:27 +00001999 ParseDeclarator(DeclaratorInfo);
Richard Smith3d1a94c2014-08-12 00:22:39 +00002000 else
2001 DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation());
Richard Smith72553fc2014-01-23 23:53:27 +00002002
2003 if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002004 assert(DeclaratorInfo.isPastIdentifier() &&
2005 "don't know where identifier would go yet?");
Richard Smith72553fc2014-01-23 23:53:27 +00002006 BitfieldSize = ParseConstantExpression();
2007 if (BitfieldSize.isInvalid())
2008 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2009 } else
Richard Smith3d1a94c2014-08-12 00:22:39 +00002010 ParseOptionalCXX11VirtSpecifierSeq(
2011 VS, getCurrentClass().IsInterface,
2012 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Richard Smith72553fc2014-01-23 23:53:27 +00002013
2014 // If a simple-asm-expr is present, parse it.
2015 if (Tok.is(tok::kw_asm)) {
2016 SourceLocation Loc;
2017 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
2018 if (AsmLabel.isInvalid())
2019 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2020
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002021 DeclaratorInfo.setAsmLabel(AsmLabel.get());
Richard Smith72553fc2014-01-23 23:53:27 +00002022 DeclaratorInfo.SetRangeEnd(Loc);
2023 }
2024
2025 // If attributes exist after the declarator, but before an '{', parse them.
2026 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Richard Smith4b5a9492014-01-24 22:34:35 +00002027
2028 // For compatibility with code written to older Clang, also accept a
2029 // virt-specifier *after* the GNU attributes.
Aaron Ballman5d153e32014-08-04 17:03:51 +00002030 if (BitfieldSize.isUnset() && VS.isUnset()) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002031 ParseOptionalCXX11VirtSpecifierSeq(
2032 VS, getCurrentClass().IsInterface,
2033 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Aaron Ballman5d153e32014-08-04 17:03:51 +00002034 if (!VS.isUnset()) {
2035 // If we saw any GNU-style attributes that are known to GCC followed by a
2036 // virt-specifier, issue a GCC-compat warning.
2037 const AttributeList *Attr = DeclaratorInfo.getAttributes();
2038 while (Attr) {
2039 if (Attr->isKnownToGCC() && !Attr->isCXX11Attribute())
2040 Diag(Attr->getLoc(), diag::warn_gcc_attribute_location);
2041 Attr = Attr->getNext();
2042 }
2043 }
2044 }
Richard Smith72553fc2014-01-23 23:53:27 +00002045}
2046
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002047/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
2048///
2049/// member-declaration:
2050/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
2051/// function-definition ';'[opt]
2052/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
2053/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00002054/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002055/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002056/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002057///
2058/// member-declarator-list:
2059/// member-declarator
2060/// member-declarator-list ',' member-declarator
2061///
2062/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002063/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002064/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00002065/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002066/// identifier[opt] ':' constant-expression
2067///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002068/// virt-specifier-seq:
2069/// virt-specifier
2070/// virt-specifier-seq virt-specifier
2071///
2072/// virt-specifier:
2073/// override
2074/// final
David Majnemera5433082013-10-18 00:33:31 +00002075/// [MS] sealed
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002076///
Sebastian Redl42e92c42009-04-12 17:16:29 +00002077/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002078/// '= 0'
2079///
2080/// constant-initializer:
2081/// '=' constant-expression
2082///
Douglas Gregor3447e762009-08-20 22:52:58 +00002083void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002084 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00002085 const ParsedTemplateInfo &TemplateInfo,
2086 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00002087 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002088 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00002089 Diag(Tok, diag::err_at_defs_cxx);
2090 else
2091 Diag(Tok, diag::err_at_in_class);
Richard Smithda35e962013-11-09 04:52:51 +00002092
Douglas Gregor23c84762011-04-14 17:21:19 +00002093 ConsumeToken();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002094 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor23c84762011-04-14 17:21:19 +00002095 return;
2096 }
Richard Smithda35e962013-11-09 04:52:51 +00002097
Serge Pavlov458ea762014-07-16 05:16:52 +00002098 // Turn on colon protection early, while parsing declspec, although there is
2099 // nothing to protect there. It prevents from false errors if error recovery
2100 // incorrectly determines where the declspec ends, as in the example:
2101 // struct A { enum class B { C }; };
2102 // const int C = 4;
2103 // struct D { A::B : C; };
2104 ColonProtectionRAIIObject X(*this);
2105
John McCalla0097262009-12-11 02:10:03 +00002106 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00002107 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00002108 if (!TemplateInfo.Kind &&
Nikola Smiljanic67860242014-09-26 00:28:20 +00002109 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2110 Tok.is(tok::kw___super))) {
Richard Smith45855df2012-05-09 08:23:23 +00002111 if (TryAnnotateCXXScopeToken())
2112 MalformedTypeSpec = true;
2113
2114 bool isAccessDecl;
2115 if (Tok.isNot(tok::annot_cxxscope))
2116 isAccessDecl = false;
2117 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00002118 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
2119 else
2120 isAccessDecl = NextToken().is(tok::kw_operator);
2121
2122 if (isAccessDecl) {
2123 // Collect the scope specifier token we annotated earlier.
2124 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002125 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2126 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00002127
Nico Weberef03e702014-09-10 00:59:37 +00002128 if (SS.isInvalid()) {
2129 SkipUntil(tok::semi);
2130 return;
2131 }
2132
John McCalla0097262009-12-11 02:10:03 +00002133 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00002134 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00002135 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +00002136 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
2137 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00002138 SkipUntil(tok::semi);
2139 return;
2140 }
2141
2142 // TODO: recover from mistakenly-qualified operator declarations.
Alp Toker383d2c42014-01-01 03:08:43 +00002143 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2144 "access declaration")) {
2145 SkipUntil(tok::semi);
John McCalla0097262009-12-11 02:10:03 +00002146 return;
Alp Toker383d2c42014-01-01 03:08:43 +00002147 }
John McCalla0097262009-12-11 02:10:03 +00002148
Douglas Gregor0be31a22010-07-02 17:43:08 +00002149 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002150 /* HasUsingKeyword */ false,
2151 SourceLocation(),
John McCalla0097262009-12-11 02:10:03 +00002152 SS, Name,
Craig Topper161e4db2014-05-21 06:02:52 +00002153 /* AttrList */ nullptr,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002154 /* HasTypenameKeyword */ false,
John McCalla0097262009-12-11 02:10:03 +00002155 SourceLocation());
2156 return;
2157 }
2158 }
2159
Aaron Ballmane7c544d2014-08-04 20:28:35 +00002160 // static_assert-declaration. A templated static_assert declaration is
2161 // diagnosed in Parser::ParseSingleDeclarationAfterTemplate.
2162 if (!TemplateInfo.Kind &&
2163 (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert))) {
Chris Lattner49836b42009-04-02 04:16:50 +00002164 SourceLocation DeclEnd;
2165 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002166 return;
2167 }
Mike Stump11289f42009-09-09 15:08:12 +00002168
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002169 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00002170 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00002171 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00002172 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00002173 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002174 AS, AccessAttrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002175 return;
2176 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002177
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002178 // Handle: member-declaration ::= '__extension__' member-declaration
2179 if (Tok.is(tok::kw___extension__)) {
2180 // __extension__ silences extension warnings in the subexpression.
2181 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2182 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002183 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2184 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002185 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002186
John McCall084e83d2011-03-24 11:26:52 +00002187 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002188 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002189 // Optional C++11 attribute-specifier
2190 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002191 // We need to keep these attributes for future diagnostic
2192 // before they are taken over by declaration specifier.
2193 FnAttrs.addAll(attrs.getList());
2194 FnAttrs.Range = attrs.Range;
2195
John McCall53fa7142010-12-24 02:08:15 +00002196 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002197
Douglas Gregorfec52632009-06-20 00:51:54 +00002198 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002199 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002200
Douglas Gregorfec52632009-06-20 00:51:54 +00002201 // Eat 'using'.
2202 SourceLocation UsingLoc = ConsumeToken();
2203
2204 if (Tok.is(tok::kw_namespace)) {
2205 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002206 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002207 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00002208 SourceLocation DeclEnd;
Richard Smith3f1b5d02011-05-05 21:57:07 +00002209 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +00002210 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2211 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00002212 }
2213 return;
2214 }
2215
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002216 // Hold late-parsed attributes so we can attach a Decl to them later.
2217 LateParsedAttrList CommonLateParsedAttrs;
2218
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002219 // decl-specifier-seq:
2220 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002221 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002222 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002223 if (MalformedTypeSpec)
2224 DS.SetTypeSpecError();
Richard Smith72553fc2014-01-23 23:53:27 +00002225
Serge Pavlov458ea762014-07-16 05:16:52 +00002226 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2227 &CommonLateParsedAttrs);
2228
2229 // Turn off colon protection that was set for declspec.
2230 X.restore();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002231
Richard Smith404dfb42013-11-19 22:47:36 +00002232 // If we had a free-standing type definition with a missing semicolon, we
2233 // may get this far before the problem becomes obvious.
2234 if (DS.hasTagDefinition() &&
2235 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2236 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2237 &CommonLateParsedAttrs))
2238 return;
2239
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002240 MultiTemplateParamsArg TemplateParams(
Craig Topper161e4db2014-05-21 06:02:52 +00002241 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data()
2242 : nullptr,
John McCall11083da2009-09-16 22:47:08 +00002243 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2244
Alp Toker35d87032013-12-30 23:29:50 +00002245 if (TryConsumeToken(tok::semi)) {
Michael Handdc016d2012-11-28 23:17:40 +00002246 if (DS.isFriendSpecified())
2247 ProhibitAttributes(FnAttrs);
2248
John McCall48871652010-08-21 09:40:31 +00002249 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00002250 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00002251 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00002252 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002253 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002254
John McCall28a6aea2009-11-04 02:18:39 +00002255 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002256 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002257
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002258 // Hold late-parsed attributes so we can attach a Decl to them later.
2259 LateParsedAttrList LateParsedAttrs;
2260
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002261 SourceLocation EqualLoc;
2262 bool HasInitializer = false;
2263 ExprResult Init;
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002264
Richard Smith72553fc2014-01-23 23:53:27 +00002265 SmallVector<Decl *, 8> DeclsInGroup;
2266 ExprResult BitfieldSize;
2267 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002268
Richard Smith72553fc2014-01-23 23:53:27 +00002269 // Parse the first declarator.
2270 ParseCXXMemberDeclaratorBeforeInitializer(DeclaratorInfo, VS, BitfieldSize,
2271 LateParsedAttrs);
Nico Weber24b2a822011-01-28 06:07:34 +00002272
Richard Smith72553fc2014-01-23 23:53:27 +00002273 // If this has neither a name nor a bit width, something has gone seriously
2274 // wrong. Skip until the semi-colon or }.
Richard Smith4b5a9492014-01-24 22:34:35 +00002275 if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002276 // If so, skip until the semi-colon or a }.
2277 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2278 TryConsumeToken(tok::semi);
2279 return;
2280 }
John Thompson5bc5cbe2009-11-25 22:58:06 +00002281
Richard Smith72553fc2014-01-23 23:53:27 +00002282 // Check for a member function definition.
Richard Smith4b5a9492014-01-24 22:34:35 +00002283 if (BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002284 // MSVC permits pure specifier on inline functions defined at class scope.
Francois Pichet3abc9b82011-05-11 02:14:46 +00002285 // Hence check for =0 before checking for function definition.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002286 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Richard Smith72553fc2014-01-23 23:53:27 +00002287 DeclaratorInfo.isFunctionDeclarator() &&
Francois Pichet3abc9b82011-05-11 02:14:46 +00002288 NextToken().is(tok::numeric_constant)) {
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002289 EqualLoc = ConsumeToken();
Francois Pichet3abc9b82011-05-11 02:14:46 +00002290 Init = ParseInitializer();
2291 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002292 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002293 else
2294 HasInitializer = true;
Francois Pichet3abc9b82011-05-11 02:14:46 +00002295 }
2296
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002297 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002298 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002299 //
2300 // In C++11, a non-function declarator followed by an open brace is a
2301 // braced-init-list for an in-class member initialization, not an
2302 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002303 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002304 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002305 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002306 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002307 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002308 } else if (Tok.is(tok::equal)) {
2309 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002310 if (KW.is(tok::kw_default))
2311 DefinitionKind = FDK_Defaulted;
2312 else if (KW.is(tok::kw_delete))
2313 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002314 }
2315 }
2316
Michael Handdc016d2012-11-28 23:17:40 +00002317 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2318 // to a friend declaration, that declaration shall be a definition.
2319 if (DeclaratorInfo.isFunctionDeclarator() &&
2320 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2321 // Diagnose attributes that appear before decl specifier:
2322 // [[]] friend int foo();
2323 ProhibitAttributes(FnAttrs);
2324 }
2325
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002326 if (DefinitionKind) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002327 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002328 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002329 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002330 SkipUntil(tok::r_brace);
Michael Handdc016d2012-11-28 23:17:40 +00002331
Douglas Gregor8a4db832011-01-19 16:41:58 +00002332 // Consume the optional ';'
Alp Toker35d87032013-12-30 23:29:50 +00002333 TryConsumeToken(tok::semi);
2334
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002335 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002336 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002337
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002338 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002339 Diag(DeclaratorInfo.getIdentifierLoc(),
2340 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002341
Richard Smith2603b092012-11-15 22:54:20 +00002342 // Recover by treating the 'typedef' as spurious.
2343 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002344 }
2345
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002346 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002347 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002348 VS, DefinitionKind, Init);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002349
David Majnemer23252a32013-08-01 04:22:55 +00002350 if (FunDecl) {
2351 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2352 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2353 }
2354 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2355 LateParsedAttrs[i]->addDecl(FunDecl);
2356 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002357 }
2358 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002359
2360 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002361 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002362 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002363
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002364 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002365 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002366 }
2367
2368 // member-declarator-list:
2369 // member-declarator
2370 // member-declarator-list ',' member-declarator
2371
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002372 while (1) {
Richard Smith2b013182012-06-10 03:12:00 +00002373 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002374 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith938f40b2011-06-11 17:19:42 +00002375 if (BitfieldSize.get()) {
2376 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002377 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith938f40b2011-06-11 17:19:42 +00002378 } else {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002379 HasInitializer = true;
Richard Smith2b013182012-06-10 03:12:00 +00002380 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2381 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smith2b013182012-06-10 03:12:00 +00002382 != DeclSpec::SCS_typedef)
2383 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002384 }
2385 }
2386
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002387 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002388 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002389 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002390
Craig Topper161e4db2014-05-21 06:02:52 +00002391 NamedDecl *ThisDecl = nullptr;
John McCall07e91c02009-08-06 02:15:43 +00002392 if (DS.isFriendSpecified()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002393 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
Michael Handdc016d2012-11-28 23:17:40 +00002394 // to a friend declaration, that declaration shall be a definition.
2395 //
Richard Smith72553fc2014-01-23 23:53:27 +00002396 // Diagnose attributes that appear in a friend member function declarator:
2397 // friend int foo [[]] ();
Michael Handdc016d2012-11-28 23:17:40 +00002398 SmallVector<SourceRange, 4> Ranges;
2399 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
Richard Smith72553fc2014-01-23 23:53:27 +00002400 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
2401 E = Ranges.end(); I != E; ++I)
2402 Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
Michael Handdc016d2012-11-28 23:17:40 +00002403
Douglas Gregor0be31a22010-07-02 17:43:08 +00002404 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002405 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002406 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002407 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002408 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002409 TemplateParams,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002410 BitfieldSize.get(),
Richard Smith2b013182012-06-10 03:12:00 +00002411 VS, HasInClassInit);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002412
2413 if (VarTemplateDecl *VT =
Craig Topper161e4db2014-05-21 06:02:52 +00002414 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr)
Larisse Voufo39a1e502013-08-06 01:03:05 +00002415 // Re-direct this decl to refer to the templated decl so that we can
2416 // initialize it.
2417 ThisDecl = VT->getTemplatedDecl();
2418
David Majnemer23252a32013-08-01 04:22:55 +00002419 if (ThisDecl && AccessAttrs)
Richard Smithf8a75c32013-08-29 00:47:48 +00002420 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00002421 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002422
Douglas Gregor728d00b2011-10-10 14:49:18 +00002423 // Handle the initializer.
David Blaikie35506f82013-01-30 01:22:18 +00002424 if (HasInClassInit != ICIS_NoInit &&
2425 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2426 DeclSpec::SCS_static) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002427 // The initializer was deferred; parse it and cache the tokens.
David Majnemer23252a32013-08-01 04:22:55 +00002428 Diag(Tok, getLangOpts().CPlusPlus11
2429 ? diag::warn_cxx98_compat_nonstatic_member_init
2430 : diag::ext_nonstatic_member_init);
Richard Smith5d164bc2011-10-15 05:09:34 +00002431
Richard Smith938f40b2011-06-11 17:19:42 +00002432 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002433 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2434 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002435 //
2436 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002437 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002438 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002439 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemer23252a32013-08-01 04:22:55 +00002440
2441 // Avoid later warnings about a class member of incomplete type.
David Blaikiecdd91db2012-02-14 09:00:46 +00002442 if (ThisDecl)
David Blaikiecdd91db2012-02-14 09:00:46 +00002443 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002444 } else
2445 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002446 } else if (HasInitializer) {
2447 // Normal initializer.
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002448 if (!Init.isUsable())
David Majnemer23252a32013-08-01 04:22:55 +00002449 Init = ParseCXXMemberInitializer(
2450 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2451
Douglas Gregor728d00b2011-10-10 14:49:18 +00002452 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002453 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002454 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002455 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smith74aeef52013-04-26 16:15:35 +00002456 DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002457 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor728d00b2011-10-10 14:49:18 +00002458 // No initializer.
Richard Smith74aeef52013-04-26 16:15:35 +00002459 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002460
Douglas Gregor728d00b2011-10-10 14:49:18 +00002461 if (ThisDecl) {
David Majnemer23252a32013-08-01 04:22:55 +00002462 if (!ThisDecl->isInvalidDecl()) {
2463 // Set the Decl for any late parsed attributes
2464 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2465 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2466
2467 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2468 LateParsedAttrs[i]->addDecl(ThisDecl);
2469 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002470 Actions.FinalizeDeclaration(ThisDecl);
2471 DeclsInGroup.push_back(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002472
2473 if (DeclaratorInfo.isFunctionDeclarator() &&
2474 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2475 DeclSpec::SCS_typedef)
2476 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002477 }
David Majnemer23252a32013-08-01 04:22:55 +00002478 LateParsedAttrs.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002479
2480 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002481
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002482 // If we don't have a comma, it is either the end of the list (a ';')
2483 // or an error, bail out.
Alp Toker094e5212014-01-05 03:27:11 +00002484 SourceLocation CommaLoc;
2485 if (!TryConsumeToken(tok::comma, CommaLoc))
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002486 break;
Mike Stump11289f42009-09-09 15:08:12 +00002487
Richard Smithc8a79032012-01-09 22:31:44 +00002488 if (Tok.isAtStartOfLine() &&
2489 !MightBeDeclarator(Declarator::MemberContext)) {
2490 // This comma was followed by a line-break and something which can't be
2491 // the start of a declarator. The comma was probably a typo for a
2492 // semicolon.
2493 Diag(CommaLoc, diag::err_expected_semi_declaration)
2494 << FixItHint::CreateReplacement(CommaLoc, ";");
2495 ExpectSemi = false;
2496 break;
2497 }
Mike Stump11289f42009-09-09 15:08:12 +00002498
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002499 // Parse the next declarator.
2500 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002501 VS.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002502 BitfieldSize = true;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002503 Init = true;
2504 HasInitializer = false;
Richard Smith8d06f422012-01-12 23:53:29 +00002505 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002506
Richard Smith72553fc2014-01-23 23:53:27 +00002507 // GNU attributes are allowed before the second and subsequent declarator.
John McCall53fa7142010-12-24 02:08:15 +00002508 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002509
Richard Smith72553fc2014-01-23 23:53:27 +00002510 ParseCXXMemberDeclaratorBeforeInitializer(DeclaratorInfo, VS, BitfieldSize,
2511 LateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002512 }
2513
Richard Smithc8a79032012-01-09 22:31:44 +00002514 if (ExpectSemi &&
2515 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002516 // Skip to end of block or statement.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002517 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002518 // If we stopped at a ';', eat it.
Alp Toker35d87032013-12-30 23:29:50 +00002519 TryConsumeToken(tok::semi);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002520 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002521 }
2522
Rafael Espindolaab417692013-07-09 12:05:01 +00002523 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002524}
2525
Richard Smith938f40b2011-06-11 17:19:42 +00002526/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2527/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2528/// function definition. The location of the '=', if any, will be placed in
2529/// EqualLoc.
2530///
2531/// pure-specifier:
2532/// '= 0'
Sebastian Redleef474c2012-02-22 10:50:08 +00002533///
Richard Smith938f40b2011-06-11 17:19:42 +00002534/// brace-or-equal-initializer:
2535/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002536/// braced-init-list
2537///
Richard Smith938f40b2011-06-11 17:19:42 +00002538/// initializer-clause:
2539/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002540/// braced-init-list
2541///
Richard Smithda35e962013-11-09 04:52:51 +00002542/// defaulted/deleted function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002543/// '=' 'default'
2544/// '=' 'delete'
2545///
2546/// Prior to C++0x, the assignment-expression in an initializer-clause must
2547/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002548ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002549 SourceLocation &EqualLoc) {
2550 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2551 && "Data member initializer not starting with '=' or '{'");
2552
Douglas Gregor926410d2012-02-21 02:22:07 +00002553 EnterExpressionEvaluationContext Context(Actions,
2554 Sema::PotentiallyEvaluated,
2555 D);
Alp Toker094e5212014-01-05 03:27:11 +00002556 if (TryConsumeToken(tok::equal, EqualLoc)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002557 if (Tok.is(tok::kw_delete)) {
2558 // In principle, an initializer of '= delete p;' is legal, but it will
2559 // never type-check. It's better to diagnose it as an ill-formed expression
2560 // than as an ill-formed deleted non-function member.
2561 // An initializer of '= delete p, foo' will never be parsed, because
2562 // a top-level comma always ends the initializer expression.
2563 const Token &Next = NextToken();
2564 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
Richard Smith34f30512013-11-23 04:06:09 +00002565 Next.is(tok::eof)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002566 if (IsFunction)
2567 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2568 << 1 /* delete */;
2569 else
2570 Diag(ConsumeToken(), diag::err_deleted_non_function);
Richard Smithedcb26e2014-06-11 00:49:52 +00002571 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002572 }
2573 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002574 if (IsFunction)
2575 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2576 << 0 /* default */;
2577 else
2578 Diag(ConsumeToken(), diag::err_default_special_members);
Richard Smithedcb26e2014-06-11 00:49:52 +00002579 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002580 }
2581
Sebastian Redleef474c2012-02-22 10:50:08 +00002582 }
2583 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002584}
2585
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002586/// ParseCXXMemberSpecification - Parse the class definition.
2587///
2588/// member-specification:
2589/// member-declaration member-specification[opt]
2590/// access-specifier ':' member-specification[opt]
2591///
Joao Matose9a3ed42012-08-31 22:18:20 +00002592void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002593 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00002594 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002595 unsigned TagType, Decl *TagDecl) {
2596 assert((TagType == DeclSpec::TST_struct ||
2597 TagType == DeclSpec::TST_interface ||
2598 TagType == DeclSpec::TST_union ||
2599 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2600
John McCallfaf5fb42010-08-26 23:41:50 +00002601 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2602 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00002603
Douglas Gregoredf8f392010-01-16 20:52:59 +00002604 // Determine whether this is a non-nested class. Note that local
2605 // classes are *not* considered to be nested classes.
2606 bool NonNestedClass = true;
2607 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002608 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002609 if (S->isClassScope()) {
2610 // We're inside a class scope, so this is a nested class.
2611 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00002612
2613 // The Microsoft extension __interface does not permit nested classes.
2614 if (getCurrentClass().IsInterface) {
2615 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2616 << /*ErrorType=*/6
2617 << (isa<NamedDecl>(TagDecl)
2618 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
David Blaikieabe1a392014-04-02 05:58:29 +00002619 : "(anonymous)");
John McCalldb632ac2012-09-25 07:32:39 +00002620 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00002621 break;
2622 }
2623
2624 if ((S->getFlags() & Scope::FnScope)) {
2625 // If we're in a function or function template declared in the
2626 // body of a class, then this is a local class rather than a
2627 // nested class.
2628 const Scope *Parent = S->getParent();
2629 if (Parent->isTemplateParamScope())
2630 Parent = Parent->getParent();
2631 if (Parent->isClassScope())
2632 break;
2633 }
2634 }
2635 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002636
2637 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00002638 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002639
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002640 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00002641 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2642 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002643
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002644 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002645 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002646
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002647 SourceLocation FinalLoc;
David Majnemera5433082013-10-18 00:33:31 +00002648 bool IsFinalSpelledSealed = false;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002649
2650 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002651 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemera5433082013-10-18 00:33:31 +00002652 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2653 assert((Specifier == VirtSpecifiers::VS_Final ||
2654 Specifier == VirtSpecifiers::VS_Sealed) &&
2655 "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00002656 FinalLoc = ConsumeToken();
David Majnemera5433082013-10-18 00:33:31 +00002657 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002658
David Majnemera5433082013-10-18 00:33:31 +00002659 if (TagType == DeclSpec::TST_interface)
John McCalldb632ac2012-09-25 07:32:39 +00002660 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemera5433082013-10-18 00:33:31 +00002661 << VirtSpecifiers::getSpecifierName(Specifier);
2662 else if (Specifier == VirtSpecifiers::VS_Final)
2663 Diag(FinalLoc, getLangOpts().CPlusPlus11
2664 ? diag::warn_cxx98_compat_override_control_keyword
2665 : diag::ext_override_control_keyword)
2666 << VirtSpecifiers::getSpecifierName(Specifier);
2667 else if (Specifier == VirtSpecifiers::VS_Sealed)
2668 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han9407e502012-11-26 22:54:45 +00002669
Michael Han309af292013-01-07 16:57:11 +00002670 // Parse any C++11 attributes after 'final' keyword.
2671 // These attributes are not allowed to appear here,
2672 // and the only possible place for them to appertain
2673 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00002674 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002675 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002676
John McCall2d814c32009-12-19 21:48:58 +00002677 if (Tok.is(tok::colon)) {
2678 ParseBaseClause(TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002679 if (!Tok.is(tok::l_brace)) {
Ismail Pazarbasi129c44c2014-09-25 21:13:02 +00002680 bool SuggestFixIt = false;
2681 SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation);
2682 if (Tok.isAtStartOfLine()) {
2683 switch (Tok.getKind()) {
2684 case tok::kw_private:
2685 case tok::kw_protected:
2686 case tok::kw_public:
2687 SuggestFixIt = NextToken().getKind() == tok::colon;
2688 break;
2689 case tok::kw_static_assert:
2690 case tok::r_brace:
2691 case tok::kw_using:
2692 // base-clause can have simple-template-id; 'template' can't be there
2693 case tok::kw_template:
2694 SuggestFixIt = true;
2695 break;
2696 case tok::identifier:
2697 SuggestFixIt = isConstructorDeclarator(true);
2698 break;
2699 default:
2700 SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
2701 break;
2702 }
2703 }
2704 DiagnosticBuilder LBraceDiag =
2705 Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers);
2706 if (SuggestFixIt) {
2707 LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {");
2708 // Try recovering from missing { after base-clause.
2709 PP.EnterToken(Tok);
2710 Tok.setKind(tok::l_brace);
2711 } else {
2712 if (TagDecl)
2713 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
2714 return;
2715 }
John McCall2d814c32009-12-19 21:48:58 +00002716 }
2717 }
2718
2719 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002720 BalancedDelimiterTracker T(*this, tok::l_brace);
2721 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00002722
John McCall08bede42010-05-28 08:11:17 +00002723 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00002724 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemera5433082013-10-18 00:33:31 +00002725 IsFinalSpelledSealed,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002726 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00002727
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002728 // C++ 11p3: Members of a class defined with the keyword class are private
2729 // by default. Members of a class defined with the keywords struct or union
2730 // are public by default.
2731 AccessSpecifier CurAS;
2732 if (TagType == DeclSpec::TST_class)
2733 CurAS = AS_private;
2734 else
2735 CurAS = AS_public;
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002736 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002737
Douglas Gregor9377c822010-06-21 22:31:09 +00002738 if (TagDecl) {
2739 // While we still have something to read, read the member-declarations.
Richard Smith34f30512013-11-23 04:06:09 +00002740 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Douglas Gregor9377c822010-06-21 22:31:09 +00002741 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002742
David Blaikiebbafb8a2012-03-11 07:00:24 +00002743 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet8f981d52011-05-25 10:19:49 +00002744 Tok.is(tok::kw___if_not_exists))) {
2745 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2746 continue;
2747 }
2748
Douglas Gregor9377c822010-06-21 22:31:09 +00002749 // Check for extraneous top-level semicolon.
2750 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002751 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor9377c822010-06-21 22:31:09 +00002752 continue;
2753 }
2754
Eli Friedmanec52f922012-02-23 23:47:16 +00002755 if (Tok.is(tok::annot_pragma_vis)) {
2756 HandlePragmaVisibility();
2757 continue;
2758 }
2759
2760 if (Tok.is(tok::annot_pragma_pack)) {
2761 HandlePragmaPack();
2762 continue;
2763 }
2764
Argyrios Kyrtzidis5c2021b2012-10-12 17:39:59 +00002765 if (Tok.is(tok::annot_pragma_align)) {
2766 HandlePragmaAlign();
2767 continue;
2768 }
2769
Alexey Bataeva769e072013-03-22 06:34:35 +00002770 if (Tok.is(tok::annot_pragma_openmp)) {
2771 ParseOpenMPDeclarativeDirective();
2772 continue;
2773 }
2774
David Majnemer4bb09802014-02-10 19:50:15 +00002775 if (Tok.is(tok::annot_pragma_ms_pointers_to_members)) {
2776 HandlePragmaMSPointersToMembers();
2777 continue;
2778 }
2779
Warren Huntc3b18962014-04-08 22:30:47 +00002780 if (Tok.is(tok::annot_pragma_ms_pragma)) {
2781 HandlePragmaMSPragma();
2782 continue;
2783 }
2784
Richard Smithda35e962013-11-09 04:52:51 +00002785 // If we see a namespace here, a close brace was missing somewhere.
2786 if (Tok.is(tok::kw_namespace)) {
Richard Smith2ac43ad2013-11-15 23:00:02 +00002787 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
Richard Smithda35e962013-11-09 04:52:51 +00002788 break;
2789 }
2790
Douglas Gregor9377c822010-06-21 22:31:09 +00002791 AccessSpecifier AS = getAccessSpecifierIfPresent();
2792 if (AS != AS_none) {
2793 // Current token is a C++ access specifier.
2794 CurAS = AS;
2795 SourceLocation ASLoc = Tok.getLocation();
David Blaikieeba32c22011-10-13 06:08:43 +00002796 unsigned TokLength = Tok.getLength();
Douglas Gregor9377c822010-06-21 22:31:09 +00002797 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002798 AccessAttrs.clear();
2799 MaybeParseGNUAttributes(AccessAttrs);
2800
David Blaikieeba32c22011-10-13 06:08:43 +00002801 SourceLocation EndLoc;
Alp Toker35d87032013-12-30 23:29:50 +00002802 if (TryConsumeToken(tok::colon, EndLoc)) {
2803 } else if (TryConsumeToken(tok::semi, EndLoc)) {
2804 Diag(EndLoc, diag::err_expected)
2805 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002806 } else {
2807 EndLoc = ASLoc.getLocWithOffset(TokLength);
Alp Toker35d87032013-12-30 23:29:50 +00002808 Diag(EndLoc, diag::err_expected)
2809 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002810 }
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002811
John McCalldb632ac2012-09-25 07:32:39 +00002812 // The Microsoft extension __interface does not permit non-public
2813 // access specifiers.
2814 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2815 Diag(ASLoc, diag::err_access_specifier_interface)
2816 << (CurAS == AS_protected);
2817 }
2818
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002819 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2820 AccessAttrs.getList())) {
2821 // found another attribute than only annotations
2822 AccessAttrs.clear();
2823 }
2824
Douglas Gregor9377c822010-06-21 22:31:09 +00002825 continue;
2826 }
2827
Douglas Gregor9377c822010-06-21 22:31:09 +00002828 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002829 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002830 }
2831
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002832 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00002833 } else {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002834 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002835 }
Mike Stump11289f42009-09-09 15:08:12 +00002836
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002837 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002838 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002839 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002840
John McCall08bede42010-05-28 08:11:17 +00002841 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002842 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002843 T.getOpenLocation(),
2844 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002845 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002846
Douglas Gregor433e0532012-04-16 18:27:27 +00002847 // C++11 [class.mem]p2:
2848 // Within the class member-specification, the class is regarded as complete
Richard Smith2331bbf2012-05-02 22:22:32 +00002849 // within function bodies, default arguments, and
Douglas Gregor433e0532012-04-16 18:27:27 +00002850 // brace-or-equal-initializers for non-static data members (including such
2851 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00002852 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002853 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00002854 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002855 // declarations and the lexed inline method definitions, along with any
2856 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00002857 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002858 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002859 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00002860
2861 // We've finished with all pending member declarations.
2862 Actions.ActOnFinishCXXMemberDecls();
2863
Richard Smith938f40b2011-06-11 17:19:42 +00002864 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002865 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00002866 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002867 }
2868
John McCall08bede42010-05-28 08:11:17 +00002869 if (TagDecl)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002870 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2871 T.getCloseLocation());
John McCall2ff380a2010-03-17 00:38:33 +00002872
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002873 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002874 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002875 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002876}
Douglas Gregore8381c02008-11-05 04:29:56 +00002877
Richard Smith2ac43ad2013-11-15 23:00:02 +00002878void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithda35e962013-11-09 04:52:51 +00002879 assert(Tok.is(tok::kw_namespace));
2880
2881 // FIXME: Suggest where the close brace should have gone by looking
2882 // at indentation changes within the definition body.
Richard Smith2ac43ad2013-11-15 23:00:02 +00002883 Diag(D->getLocation(),
2884 diag::err_missing_end_of_definition) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002885 Diag(Tok.getLocation(),
Richard Smith2ac43ad2013-11-15 23:00:02 +00002886 diag::note_missing_end_of_definition_before) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002887
2888 // Push '};' onto the token stream to recover.
2889 PP.EnterToken(Tok);
2890
2891 Tok.startToken();
2892 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
2893 Tok.setKind(tok::semi);
2894 PP.EnterToken(Tok);
2895
2896 Tok.setKind(tok::r_brace);
2897}
2898
Douglas Gregore8381c02008-11-05 04:29:56 +00002899/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2900/// which explicitly initializes the members or base classes of a
2901/// class (C++ [class.base.init]). For example, the three initializers
2902/// after the ':' in the Derived constructor below:
2903///
2904/// @code
2905/// class Base { };
2906/// class Derived : Base {
2907/// int x;
2908/// float f;
2909/// public:
2910/// Derived(float f) : Base(), x(17), f(f) { }
2911/// };
2912/// @endcode
2913///
Mike Stump11289f42009-09-09 15:08:12 +00002914/// [C++] ctor-initializer:
2915/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00002916///
Mike Stump11289f42009-09-09 15:08:12 +00002917/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00002918/// mem-initializer ...[opt]
2919/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00002920void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00002921 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2922
John Wiegley1c0675e2011-04-28 01:08:34 +00002923 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2924 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00002925 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002926
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002927 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002928 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002929
Douglas Gregore8381c02008-11-05 04:29:56 +00002930 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002931 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00002932 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2933 MemInitializers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002934 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002935 } else {
2936 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2937 if (!MemInit.isInvalid())
2938 MemInitializers.push_back(MemInit.get());
2939 else
2940 AnyErrors = true;
2941 }
2942
Douglas Gregore8381c02008-11-05 04:29:56 +00002943 if (Tok.is(tok::comma))
2944 ConsumeToken();
2945 else if (Tok.is(tok::l_brace))
2946 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00002947 // If the next token looks like a base or member initializer, assume that
2948 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00002949 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2950 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2951 Diag(Loc, diag::err_ctor_init_missing_comma)
2952 << FixItHint::CreateInsertion(Loc, ", ");
2953 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00002954 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alp Tokerec543272013-12-24 09:48:30 +00002955 Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
2956 << tok::comma;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002957 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregore8381c02008-11-05 04:29:56 +00002958 break;
2959 }
2960 } while (true);
2961
David Blaikie3fc2f912013-01-17 05:26:25 +00002962 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002963 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00002964}
2965
2966/// ParseMemInitializer - Parse a C++ member initializer, which is
2967/// part of a constructor initializer that explicitly initializes one
2968/// member or base class (C++ [class.base.init]). See
2969/// ParseConstructorInitializer for an example.
2970///
2971/// [C++] mem-initializer:
2972/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002973/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00002974///
Douglas Gregore8381c02008-11-05 04:29:56 +00002975/// [C++] mem-initializer-id:
2976/// '::'[opt] nested-name-specifier[opt] class-name
2977/// identifier
Craig Topper9ad7e262014-10-31 06:57:07 +00002978MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00002979 // parse '::'[opt] nested-name-specifier[opt]
2980 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002981 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00002982 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002983 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002984 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00002985 if (TemplateId->Kind == TNK_Type_template ||
2986 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00002987 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002988 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00002989 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002990 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002991 }
David Blaikie186a8892012-01-24 06:03:59 +00002992 // Uses of decltype will already have been converted to annot_decltype by
2993 // ParseOptionalCXXScopeSpecifier at this point.
2994 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2995 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002996 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00002997 return true;
2998 }
Mike Stump11289f42009-09-09 15:08:12 +00002999
Craig Topper161e4db2014-05-21 06:02:52 +00003000 IdentifierInfo *II = nullptr;
David Blaikie186a8892012-01-24 06:03:59 +00003001 DeclSpec DS(AttrFactory);
3002 SourceLocation IdLoc = Tok.getLocation();
3003 if (Tok.is(tok::annot_decltype)) {
3004 // Get the decltype expression, if there is one.
3005 ParseDecltypeSpecifier(DS);
3006 } else {
3007 if (Tok.is(tok::identifier))
3008 // Get the identifier. This may be a member name or a class name,
3009 // but we'll let the semantic analysis determine which it is.
3010 II = Tok.getIdentifierInfo();
3011 ConsumeToken();
3012 }
3013
Douglas Gregore8381c02008-11-05 04:29:56 +00003014
3015 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003016 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003017 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3018
Sebastian Redla74948d2011-09-24 17:48:25 +00003019 ExprResult InitList = ParseBraceInitializer();
3020 if (InitList.isInvalid())
3021 return true;
3022
3023 SourceLocation EllipsisLoc;
Alp Toker094e5212014-01-05 03:27:11 +00003024 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redla74948d2011-09-24 17:48:25 +00003025
3026 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00003027 TemplateTypeTy, DS, IdLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003028 InitList.get(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00003029 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003030 BalancedDelimiterTracker T(*this, tok::l_paren);
3031 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00003032
Sebastian Redl3da34892011-06-05 12:23:16 +00003033 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00003034 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00003035 CommaLocsTy CommaLocs;
3036 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003037 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00003038 return true;
3039 }
3040
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003041 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00003042
3043 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00003044 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00003045
3046 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00003047 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00003048 T.getOpenLocation(), ArgExprs,
3049 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00003050 }
3051
Alp Tokerec543272013-12-24 09:48:30 +00003052 if (getLangOpts().CPlusPlus11)
3053 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
3054 else
3055 return Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregore8381c02008-11-05 04:29:56 +00003056}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003057
Sebastian Redl965b0e32011-03-05 14:45:16 +00003058/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003059///
Douglas Gregor356513d2008-12-01 18:00:20 +00003060/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00003061/// dynamic-exception-specification
3062/// noexcept-specification
3063///
3064/// noexcept-specification:
3065/// 'noexcept'
3066/// 'noexcept' '(' constant-expression ')'
3067ExceptionSpecificationType
Richard Smith2331bbf2012-05-02 22:22:32 +00003068Parser::tryParseExceptionSpecification(
Douglas Gregor433e0532012-04-16 18:27:27 +00003069 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003070 SmallVectorImpl<ParsedType> &DynamicExceptions,
3071 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00003072 ExprResult &NoexceptExpr) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003073 ExceptionSpecificationType Result = EST_None;
3074
3075 // See if there's a dynamic specification.
3076 if (Tok.is(tok::kw_throw)) {
3077 Result = ParseDynamicExceptionSpecification(SpecificationRange,
3078 DynamicExceptions,
3079 DynamicExceptionRanges);
3080 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
3081 "Produced different number of exception types and ranges.");
3082 }
3083
3084 // If there's no noexcept specification, we're done.
3085 if (Tok.isNot(tok::kw_noexcept))
3086 return Result;
3087
Richard Smithb15c11c2011-10-17 23:06:20 +00003088 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3089
Sebastian Redl965b0e32011-03-05 14:45:16 +00003090 // If we already had a dynamic specification, parse the noexcept for,
3091 // recovery, but emit a diagnostic and don't store the results.
3092 SourceRange NoexceptRange;
3093 ExceptionSpecificationType NoexceptType = EST_None;
3094
3095 SourceLocation KeywordLoc = ConsumeToken();
3096 if (Tok.is(tok::l_paren)) {
3097 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003098 BalancedDelimiterTracker T(*this, tok::l_paren);
3099 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003100 NoexceptType = EST_ComputedNoexcept;
3101 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003102 // The argument must be contextually convertible to bool. We use
3103 // ActOnBooleanCondition for this purpose.
3104 if (!NoexceptExpr.isInvalid())
3105 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
3106 NoexceptExpr.get());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003107 T.consumeClose();
3108 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl965b0e32011-03-05 14:45:16 +00003109 } else {
3110 // There is no argument.
3111 NoexceptType = EST_BasicNoexcept;
3112 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
3113 }
3114
3115 if (Result == EST_None) {
3116 SpecificationRange = NoexceptRange;
3117 Result = NoexceptType;
3118
3119 // If there's a dynamic specification after a noexcept specification,
3120 // parse that and ignore the results.
3121 if (Tok.is(tok::kw_throw)) {
3122 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3123 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
3124 DynamicExceptionRanges);
3125 }
3126 } else {
3127 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3128 }
3129
3130 return Result;
3131}
3132
Richard Smith8ca78a12013-06-13 02:02:51 +00003133static void diagnoseDynamicExceptionSpecification(
3134 Parser &P, const SourceRange &Range, bool IsNoexcept) {
3135 if (P.getLangOpts().CPlusPlus11) {
3136 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
3137 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
3138 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
3139 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
3140 }
3141}
3142
Sebastian Redl965b0e32011-03-05 14:45:16 +00003143/// ParseDynamicExceptionSpecification - Parse a C++
3144/// dynamic-exception-specification (C++ [except.spec]).
3145///
3146/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00003147/// 'throw' '(' type-id-list [opt] ')'
3148/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00003149///
Douglas Gregor356513d2008-12-01 18:00:20 +00003150/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00003151/// type-id ... [opt]
3152/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003153///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003154ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
3155 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003156 SmallVectorImpl<ParsedType> &Exceptions,
3157 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003158 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00003159
Sebastian Redl965b0e32011-03-05 14:45:16 +00003160 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003161 BalancedDelimiterTracker T(*this, tok::l_paren);
3162 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003163 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3164 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003165 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003166 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003167
Douglas Gregor356513d2008-12-01 18:00:20 +00003168 // Parse throw(...), a Microsoft extension that means "this function
3169 // can throw anything".
3170 if (Tok.is(tok::ellipsis)) {
3171 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003172 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00003173 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003174 T.consumeClose();
3175 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003176 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003177 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00003178 }
3179
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003180 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00003181 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003182 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00003183 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00003184
Douglas Gregor830837d2010-12-20 23:57:46 +00003185 if (Tok.is(tok::ellipsis)) {
3186 // C++0x [temp.variadic]p5:
3187 // - In a dynamic-exception-specification (15.4); the pattern is a
3188 // type-id.
3189 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003190 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00003191 if (!Res.isInvalid())
3192 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3193 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003194
Sebastian Redld6434562009-05-29 18:02:33 +00003195 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003196 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00003197 Ranges.push_back(Range);
3198 }
Alp Toker97650562014-01-10 11:19:30 +00003199
3200 if (!TryConsumeToken(tok::comma))
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003201 break;
3202 }
3203
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003204 T.consumeClose();
3205 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003206 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3207 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003208 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003209}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003210
Douglas Gregor7fb25412010-10-01 18:44:50 +00003211/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3212/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003213TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00003214 assert(Tok.is(tok::arrow) && "expected arrow");
3215
3216 ConsumeToken();
3217
Richard Smithbfdb1082012-03-12 08:56:40 +00003218 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003219}
3220
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003221/// \brief We have just started parsing the definition of a new class,
3222/// so push that class onto our stack of classes that is currently
3223/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003224Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003225Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3226 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003227 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003228 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003229 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003230 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003231}
3232
3233/// \brief Deallocate the given parsed class and all of its nested
3234/// classes.
3235void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003236 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3237 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003238 delete Class;
3239}
3240
3241/// \brief Pop the top class of the stack of classes that are
3242/// currently being parsed.
3243///
3244/// This routine should be called when we have finished parsing the
3245/// definition of a class, but have not yet popped the Scope
3246/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003247void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003248 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003249
John McCallc1465822011-02-14 07:13:47 +00003250 Actions.PopParsingClass(state);
3251
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003252 ParsingClass *Victim = ClassStack.top();
3253 ClassStack.pop();
3254 if (Victim->TopLevelClass) {
3255 // Deallocate all of the nested classes of this class,
3256 // recursively: we don't need to keep any of this information.
3257 DeallocateParsedClasses(Victim);
3258 return;
Mike Stump11289f42009-09-09 15:08:12 +00003259 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003260 assert(!ClassStack.empty() && "Missing top-level class?");
3261
Douglas Gregorefc46952010-10-12 16:25:54 +00003262 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003263 // The victim is a nested class, but we will not need to perform
3264 // any processing after the definition of this class since it has
3265 // no members whose handling was delayed. Therefore, we can just
3266 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003267 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003268 return;
3269 }
3270
3271 // This nested class has some members that will need to be processed
3272 // after the top-level class is completely defined. Therefore, add
3273 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003274 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003275 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003276 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003277}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003278
Richard Smith3dff2512012-04-10 03:25:07 +00003279/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3280///
3281/// \return the parsed identifier on success, and 0 if the next token is not an
3282/// attribute-token.
3283///
3284/// C++11 [dcl.attr.grammar]p3:
3285/// If a keyword or an alternative token that satisfies the syntactic
3286/// requirements of an identifier is contained in an attribute-token,
3287/// it is considered an identifier.
3288IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3289 switch (Tok.getKind()) {
3290 default:
3291 // Identifiers and keywords have identifier info attached.
3292 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3293 Loc = ConsumeToken();
3294 return II;
3295 }
Craig Topper161e4db2014-05-21 06:02:52 +00003296 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003297
3298 case tok::ampamp: // 'and'
3299 case tok::pipe: // 'bitor'
3300 case tok::pipepipe: // 'or'
3301 case tok::caret: // 'xor'
3302 case tok::tilde: // 'compl'
3303 case tok::amp: // 'bitand'
3304 case tok::ampequal: // 'and_eq'
3305 case tok::pipeequal: // 'or_eq'
3306 case tok::caretequal: // 'xor_eq'
3307 case tok::exclaim: // 'not'
3308 case tok::exclaimequal: // 'not_eq'
3309 // Alternative tokens do not have identifier info, but their spelling
3310 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003311 SmallString<8> SpellingBuf;
Richard Smith3dff2512012-04-10 03:25:07 +00003312 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003313 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003314 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003315 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003316 }
Craig Topper161e4db2014-05-21 06:02:52 +00003317 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003318 }
3319}
3320
Michael Han23214e52012-10-03 01:56:22 +00003321static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3322 IdentifierInfo *ScopeName) {
3323 switch (AttributeList::getKind(AttrName, ScopeName,
3324 AttributeList::AS_CXX11)) {
3325 case AttributeList::AT_CarriesDependency:
Aaron Ballman35f94212014-04-14 16:03:22 +00003326 case AttributeList::AT_Deprecated:
Michael Han23214e52012-10-03 01:56:22 +00003327 case AttributeList::AT_FallThrough:
Richard Smith10876ef2013-01-17 01:30:42 +00003328 case AttributeList::AT_CXX11NoReturn: {
Michael Han23214e52012-10-03 01:56:22 +00003329 return true;
3330 }
3331
3332 default:
3333 return false;
3334 }
3335}
3336
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003337/// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause.
3338///
3339/// [C++11] attribute-argument-clause:
3340/// '(' balanced-token-seq ')'
3341///
3342/// [C++11] balanced-token-seq:
3343/// balanced-token
3344/// balanced-token-seq balanced-token
3345///
3346/// [C++11] balanced-token:
3347/// '(' balanced-token-seq ')'
3348/// '[' balanced-token-seq ']'
3349/// '{' balanced-token-seq '}'
3350/// any token but '(', ')', '[', ']', '{', or '}'
3351bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName,
3352 SourceLocation AttrNameLoc,
3353 ParsedAttributes &Attrs,
3354 SourceLocation *EndLoc,
3355 IdentifierInfo *ScopeName,
3356 SourceLocation ScopeLoc) {
3357 assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
Aaron Ballman35f94212014-04-14 16:03:22 +00003358 SourceLocation LParenLoc = Tok.getLocation();
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003359
3360 // If the attribute isn't known, we will not attempt to parse any
3361 // arguments.
3362 if (!hasAttribute(AttrSyntax::CXX, ScopeName, AttrName,
3363 getTargetInfo().getTriple(), getLangOpts())) {
3364 // Eat the left paren, then skip to the ending right paren.
3365 ConsumeParen();
3366 SkipUntil(tok::r_paren);
3367 return false;
3368 }
3369
3370 if (ScopeName && ScopeName->getName() == "gnu")
3371 // GNU-scoped attributes have some special cases to handle GNU-specific
3372 // behaviors.
3373 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
Craig Topper161e4db2014-05-21 06:02:52 +00003374 ScopeLoc, AttributeList::AS_CXX11, nullptr);
Aaron Ballman35f94212014-04-14 16:03:22 +00003375 else {
3376 unsigned NumArgs =
3377 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
3378 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3379
3380 const AttributeList *Attr = Attrs.getList();
3381 if (Attr && IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) {
3382 // If the attribute is a standard or built-in attribute and we are
3383 // parsing an argument list, we need to determine whether this attribute
3384 // was allowed to have an argument list (such as [[deprecated]]), and how
3385 // many arguments were parsed (so we can diagnose on [[deprecated()]]).
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003386 if (Attr->getMaxArgs() && !NumArgs) {
3387 // The attribute was allowed to have arguments, but none were provided
3388 // even though the attribute parsed successfully. This is an error.
3389 // FIXME: This is a good place for a fixit which removes the parens.
3390 Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
3391 return false;
3392 } else if (!Attr->getMaxArgs()) {
3393 // The attribute parsed successfully, but was not allowed to have any
3394 // arguments. It doesn't matter whether any were provided -- the
Aaron Ballman35f94212014-04-14 16:03:22 +00003395 // presence of the argument list (even if empty) is diagnosed.
3396 Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
3397 << AttrName;
3398 return false;
3399 }
3400 }
3401 }
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003402 return true;
3403}
3404
3405/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003406///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003407/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003408/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003409/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003410///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003411/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003412/// attribute[opt]
3413/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003414/// attribute '...'
3415/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003416///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003417/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003418/// attribute-token attribute-argument-clause[opt]
3419///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003420/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003421/// identifier
3422/// attribute-scoped-token
3423///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003424/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003425/// attribute-namespace '::' identifier
3426///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003427/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003428/// identifier
Richard Smith3dff2512012-04-10 03:25:07 +00003429void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003430 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003431 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003432 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003433 ParseAlignmentSpecifier(attrs, endLoc);
3434 return;
3435 }
3436
Alexis Hunt96d5c762009-11-21 08:43:09 +00003437 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003438 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003439
Richard Smithf679b5b2011-10-14 20:48:27 +00003440 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3441
Alexis Hunt96d5c762009-11-21 08:43:09 +00003442 ConsumeBracket();
3443 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003444
Richard Smith10876ef2013-01-17 01:30:42 +00003445 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3446
Richard Smith3dff2512012-04-10 03:25:07 +00003447 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003448 // attribute not present
Alp Toker97650562014-01-10 11:19:30 +00003449 if (TryConsumeToken(tok::comma))
Alexis Hunt96d5c762009-11-21 08:43:09 +00003450 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003451
Richard Smith3dff2512012-04-10 03:25:07 +00003452 SourceLocation ScopeLoc, AttrLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00003453 IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003454
3455 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3456 if (!AttrName)
3457 // Break out to the "expected ']'" diagnostic.
3458 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003459
Alexis Hunt96d5c762009-11-21 08:43:09 +00003460 // scoped attribute
Alp Toker97650562014-01-10 11:19:30 +00003461 if (TryConsumeToken(tok::coloncolon)) {
Richard Smith3dff2512012-04-10 03:25:07 +00003462 ScopeName = AttrName;
3463 ScopeLoc = AttrLoc;
3464
3465 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3466 if (!AttrName) {
Alp Tokerec543272013-12-24 09:48:30 +00003467 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003468 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003469 continue;
3470 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003471 }
3472
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003473 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003474 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003475
Richard Smith10876ef2013-01-17 01:30:42 +00003476 if (StandardAttr &&
3477 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3478 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003479 << AttrName << SourceRange(SeenAttrs[AttrName]);
Richard Smith10876ef2013-01-17 01:30:42 +00003480
Michael Han23214e52012-10-03 01:56:22 +00003481 // Parse attribute arguments
Aaron Ballman35f94212014-04-14 16:03:22 +00003482 if (Tok.is(tok::l_paren))
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003483 AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3484 ScopeName, ScopeLoc);
Michael Han23214e52012-10-03 01:56:22 +00003485
3486 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003487 attrs.addNew(AttrName,
3488 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3489 AttrLoc),
Craig Topper161e4db2014-05-21 06:02:52 +00003490 ScopeName, ScopeLoc, nullptr, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003491
Alp Toker97650562014-01-10 11:19:30 +00003492 if (TryConsumeToken(tok::ellipsis))
Michael Han23214e52012-10-03 01:56:22 +00003493 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3494 << AttrName->getName();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003495 }
3496
Alp Toker383d2c42014-01-01 03:08:43 +00003497 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003498 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003499 if (endLoc)
3500 *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003501 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003502 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003503}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003504
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003505/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003506///
3507/// attribute-specifier-seq:
3508/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003509void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003510 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003511 assert(getLangOpts().CPlusPlus11);
3512
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003513 SourceLocation StartLoc = Tok.getLocation(), Loc;
3514 if (!endLoc)
3515 endLoc = &Loc;
3516
Douglas Gregor6f981002011-10-07 20:35:25 +00003517 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003518 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003519 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003520
3521 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003522}
3523
Richard Smithc2c8bb82013-10-15 01:34:54 +00003524void Parser::DiagnoseAndSkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00003525 // Start and end location of an attribute or an attribute list.
3526 SourceLocation StartLoc = Tok.getLocation();
Richard Smith955bf012014-06-19 11:42:00 +00003527 SourceLocation EndLoc = SkipCXX11Attributes();
3528
3529 if (EndLoc.isValid()) {
3530 SourceRange Range(StartLoc, EndLoc);
3531 Diag(StartLoc, diag::err_attributes_not_allowed)
3532 << Range;
3533 }
3534}
3535
3536SourceLocation Parser::SkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00003537 SourceLocation EndLoc;
3538
Richard Smith955bf012014-06-19 11:42:00 +00003539 if (!isCXX11AttributeSpecifier())
3540 return EndLoc;
3541
Richard Smithc2c8bb82013-10-15 01:34:54 +00003542 do {
3543 if (Tok.is(tok::l_square)) {
3544 BalancedDelimiterTracker T(*this, tok::l_square);
3545 T.consumeOpen();
3546 T.skipToEnd();
3547 EndLoc = T.getCloseLocation();
3548 } else {
3549 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3550 ConsumeToken();
3551 BalancedDelimiterTracker T(*this, tok::l_paren);
3552 if (!T.consumeOpen())
3553 T.skipToEnd();
3554 EndLoc = T.getCloseLocation();
3555 }
3556 } while (isCXX11AttributeSpecifier());
3557
Richard Smith955bf012014-06-19 11:42:00 +00003558 return EndLoc;
Richard Smithc2c8bb82013-10-15 01:34:54 +00003559}
3560
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003561/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3562///
3563/// [MS] ms-attribute:
3564/// '[' token-seq ']'
3565///
3566/// [MS] ms-attribute-seq:
3567/// ms-attribute[opt]
3568/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00003569void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3570 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003571 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3572
3573 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003574 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003575 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003576 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
John McCall53fa7142010-12-24 02:08:15 +00003577 if (endLoc) *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003578 ExpectAndConsume(tok::r_square);
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003579 }
3580}
Francois Pichet8f981d52011-05-25 10:19:49 +00003581
3582void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3583 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00003584 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00003585 if (ParseMicrosoftIfExistsCondition(Result))
3586 return;
3587
Douglas Gregor43edb322011-10-24 22:31:10 +00003588 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3589 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00003590 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet8f981d52011-05-25 10:19:49 +00003591 return;
3592 }
Francois Pichet8f981d52011-05-25 10:19:49 +00003593
Douglas Gregor43edb322011-10-24 22:31:10 +00003594 switch (Result.Behavior) {
3595 case IEB_Parse:
3596 // Parse the declarations below.
3597 break;
3598
3599 case IEB_Dependent:
3600 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3601 << Result.IsIfExists;
3602 // Fall through to skip.
3603
3604 case IEB_Skip:
3605 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00003606 return;
3607 }
3608
Richard Smith34f30512013-11-23 04:06:09 +00003609 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003610 // __if_exists, __if_not_exists can nest.
3611 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3612 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3613 continue;
3614 }
3615
3616 // Check for extraneous top-level semicolon.
3617 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003618 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00003619 continue;
3620 }
3621
3622 AccessSpecifier AS = getAccessSpecifierIfPresent();
3623 if (AS != AS_none) {
3624 // Current token is a C++ access specifier.
3625 CurAS = AS;
3626 SourceLocation ASLoc = Tok.getLocation();
3627 ConsumeToken();
3628 if (Tok.is(tok::colon))
3629 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3630 else
Alp Toker35d87032013-12-30 23:29:50 +00003631 Diag(Tok, diag::err_expected) << tok::colon;
Francois Pichet8f981d52011-05-25 10:19:49 +00003632 ConsumeToken();
3633 continue;
3634 }
3635
3636 // Parse all the comma separated declarators.
Craig Topper161e4db2014-05-21 06:02:52 +00003637 ParseCXXClassMemberDeclaration(CurAS, nullptr);
Francois Pichet8f981d52011-05-25 10:19:49 +00003638 }
Douglas Gregor43edb322011-10-24 22:31:10 +00003639
3640 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00003641}