blob: e5cf84a74f97d03345be2c0ce1c1f0b2dee1dc8b [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()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000113 if (!ExtraIdent.empty()) {
114 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
115 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
116 }
Alp Tokerec543272013-12-24 09:48:30 +0000117
118 if (Ident)
119 Diag(Tok, diag::err_expected) << tok::l_brace;
120 else
121 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
122
Craig Topper161e4db2014-05-21 06:02:52 +0000123 return nullptr;
Chris Lattnera5235172007-08-25 06:57:03 +0000124 }
Mike Stump11289f42009-09-09 15:08:12 +0000125
Douglas Gregor0be31a22010-07-02 17:43:08 +0000126 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
127 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
128 getCurScope()->getFnParent()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000129 if (!ExtraIdent.empty()) {
130 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
131 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
132 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000133 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000134 SkipUntil(tok::r_brace);
Craig Topper161e4db2014-05-21 06:02:52 +0000135 return nullptr;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000136 }
137
Richard Trieu61384cb2011-05-26 20:11:09 +0000138 if (!ExtraIdent.empty()) {
139 TentativeParsingAction TPA(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000140 SkipUntil(tok::r_brace, StopBeforeMatch);
Richard Trieu61384cb2011-05-26 20:11:09 +0000141 Token rBraceToken = Tok;
142 TPA.Revert();
143
144 if (!rBraceToken.is(tok::r_brace)) {
145 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
146 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
147 } else {
Benjamin Kramerf546f412011-05-26 21:32:30 +0000148 std::string NamespaceFix;
Richard Trieu61384cb2011-05-26 20:11:09 +0000149 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
150 E = ExtraIdent.end(); I != E; ++I) {
151 NamespaceFix += " { namespace ";
152 NamespaceFix += (*I)->getName();
153 }
Benjamin Kramerf546f412011-05-26 21:32:30 +0000154
Richard Trieu61384cb2011-05-26 20:11:09 +0000155 std::string RBraces;
Benjamin Kramerf546f412011-05-26 21:32:30 +0000156 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieu61384cb2011-05-26 20:11:09 +0000157 RBraces += "} ";
Benjamin Kramerf546f412011-05-26 21:32:30 +0000158
Richard Trieu61384cb2011-05-26 20:11:09 +0000159 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
160 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
161 ExtraIdentLoc.back()),
162 NamespaceFix)
163 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
164 }
165 }
166
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000167 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith5d164bc2011-10-15 05:09:34 +0000168 if (InlineLoc.isValid())
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000169 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000170 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000171
Chris Lattner4de55aa2009-03-29 14:02:43 +0000172 // Enter a scope for the namespace.
173 ParseScope NamespaceScope(this, Scope::DeclScope);
174
John McCall48871652010-08-21 09:40:31 +0000175 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000176 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000177 IdentLoc, Ident, T.getOpenLocation(),
178 attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000179
John McCallfaf5fb42010-08-26 23:41:50 +0000180 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
181 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000182
Richard Trieu61384cb2011-05-26 20:11:09 +0000183 // Parse the contents of the namespace. This includes parsing recovery on
184 // any improperly nested namespaces.
185 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000186 InlineLoc, attrs, T);
Mike Stump11289f42009-09-09 15:08:12 +0000187
Chris Lattner4de55aa2009-03-29 14:02:43 +0000188 // Leave the namespace scope.
189 NamespaceScope.Exit();
190
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000191 DeclEnd = T.getCloseLocation();
192 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000193
194 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000195}
Chris Lattner38376f12008-01-12 07:05:38 +0000196
Richard Trieu61384cb2011-05-26 20:11:09 +0000197/// ParseInnerNamespace - Parse the contents of a namespace.
198void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
199 std::vector<IdentifierInfo*>& Ident,
200 std::vector<SourceLocation>& NamespaceLoc,
201 unsigned int index, SourceLocation& InlineLoc,
Richard Trieu61384cb2011-05-26 20:11:09 +0000202 ParsedAttributes& attrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000203 BalancedDelimiterTracker &Tracker) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000204 if (index == Ident.size()) {
Richard Smith34f30512013-11-23 04:06:09 +0000205 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000206 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000207 MaybeParseCXX11Attributes(attrs);
Richard Trieu61384cb2011-05-26 20:11:09 +0000208 MaybeParseMicrosoftAttributes(attrs);
209 ParseExternalDeclaration(attrs);
210 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000211
212 // The caller is what called check -- we are simply calling
213 // the close for it.
214 Tracker.consumeClose();
Richard Trieu61384cb2011-05-26 20:11:09 +0000215
216 return;
217 }
218
219 // Parse improperly nested namespaces.
220 ParseScope NamespaceScope(this, Scope::DeclScope);
221 Decl *NamespcDecl =
222 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
223 NamespaceLoc[index], IdentLoc[index],
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000224 Ident[index], Tracker.getOpenLocation(),
225 attrs.getList());
Richard Trieu61384cb2011-05-26 20:11:09 +0000226
227 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000228 attrs, Tracker);
Richard Trieu61384cb2011-05-26 20:11:09 +0000229
230 NamespaceScope.Exit();
231
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000232 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieu61384cb2011-05-26 20:11:09 +0000233}
234
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000235/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
236/// alias definition.
237///
John McCall48871652010-08-21 09:40:31 +0000238Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000239 SourceLocation AliasLoc,
240 IdentifierInfo *Alias,
241 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000242 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000243
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000244 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000245
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000246 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000247 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000248 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000249 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000250 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000251
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000252 CXXScopeSpec SS;
253 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000254 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000255
256 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
257 Diag(Tok, diag::err_expected_namespace_name);
258 // Skip to end of the definition and eat the ';'.
259 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000260 return nullptr;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000261 }
262
263 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000264 IdentifierInfo *Ident = Tok.getIdentifierInfo();
265 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000266
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000267 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000268 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000269 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
270 SkipUntil(tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000271
Douglas Gregor0be31a22010-07-02 17:43:08 +0000272 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000273 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000274}
275
Chris Lattner38376f12008-01-12 07:05:38 +0000276/// ParseLinkage - We know that the current token is a string_literal
277/// and just before that, that extern was seen.
278///
279/// linkage-specification: [C++ 7.5p2: dcl.link]
280/// 'extern' string-literal '{' declaration-seq[opt] '}'
281/// 'extern' string-literal declaration
282///
Chris Lattner8ea64422010-11-09 20:15:55 +0000283Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Richard Smith4ee696d2014-02-17 23:25:27 +0000284 assert(isTokenStringLiteral() && "Not a string literal!");
285 ExprResult Lang = ParseStringLiteralExpression(false);
Chris Lattner38376f12008-01-12 07:05:38 +0000286
Douglas Gregor07665a62009-01-05 19:45:36 +0000287 ParseScope LinkageScope(this, Scope::DeclScope);
Richard Smith4ee696d2014-02-17 23:25:27 +0000288 Decl *LinkageSpec =
289 Lang.isInvalid()
Craig Topper161e4db2014-05-21 06:02:52 +0000290 ? nullptr
Richard Smith4ee696d2014-02-17 23:25:27 +0000291 : Actions.ActOnStartLinkageSpecification(
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000292 getCurScope(), DS.getSourceRange().getBegin(), Lang.get(),
Richard Smith4ee696d2014-02-17 23:25:27 +0000293 Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
Douglas Gregor07665a62009-01-05 19:45:36 +0000294
John McCall084e83d2011-03-24 11:26:52 +0000295 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000296 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000297 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000298
Douglas Gregor07665a62009-01-05 19:45:36 +0000299 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara4d423992011-05-01 16:25:54 +0000300 // Reset the source range in DS, as the leading "extern"
301 // does not really belong to the inner declaration ...
302 DS.SetRangeStart(SourceLocation());
303 DS.SetRangeEnd(SourceLocation());
304 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000305 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000306 ParseExternalDeclaration(attrs, &DS);
Richard Smith4ee696d2014-02-17 23:25:27 +0000307 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
308 getCurScope(), LinkageSpec, SourceLocation())
Craig Topper161e4db2014-05-21 06:02:52 +0000309 : nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000310 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000311
Douglas Gregorb65a9132010-02-07 08:38:28 +0000312 DS.abort();
313
John McCall53fa7142010-12-24 02:08:15 +0000314 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000315
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000316 BalancedDelimiterTracker T(*this, tok::l_brace);
317 T.consumeOpen();
Richard Smith77944862014-03-02 05:58:18 +0000318
319 unsigned NestedModules = 0;
320 while (true) {
321 switch (Tok.getKind()) {
322 case tok::annot_module_begin:
323 ++NestedModules;
324 ParseTopLevelDecl();
325 continue;
326
327 case tok::annot_module_end:
328 if (!NestedModules)
329 break;
330 --NestedModules;
331 ParseTopLevelDecl();
332 continue;
333
334 case tok::annot_module_include:
335 ParseTopLevelDecl();
336 continue;
337
338 case tok::eof:
339 break;
340
341 case tok::r_brace:
342 if (!NestedModules)
343 break;
344 // Fall through.
345 default:
346 ParsedAttributesWithRange attrs(AttrFactory);
347 MaybeParseCXX11Attributes(attrs);
348 MaybeParseMicrosoftAttributes(attrs);
349 ParseExternalDeclaration(attrs);
350 continue;
351 }
352
353 break;
Chris Lattner38376f12008-01-12 07:05:38 +0000354 }
355
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000356 T.consumeClose();
Richard Smith4ee696d2014-02-17 23:25:27 +0000357 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
358 getCurScope(), LinkageSpec, T.getCloseLocation())
Craig Topper161e4db2014-05-21 06:02:52 +0000359 : nullptr;
Chris Lattner38376f12008-01-12 07:05:38 +0000360}
Douglas Gregor556877c2008-04-13 21:30:24 +0000361
Douglas Gregord7c4d982008-12-30 03:27:21 +0000362/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
363/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000364Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000365 const ParsedTemplateInfo &TemplateInfo,
366 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000367 ParsedAttributesWithRange &attrs,
368 Decl **OwnedType) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000369 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000370 ObjCDeclContextSwitch ObjCDC(*this);
371
Douglas Gregord7c4d982008-12-30 03:27:21 +0000372 // Eat 'using'.
373 SourceLocation UsingLoc = ConsumeToken();
374
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000375 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000376 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000377 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000378 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000379 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000380
John McCall9b72f892010-11-10 02:40:36 +0000381 // 'using namespace' means this is a using-directive.
382 if (Tok.is(tok::kw_namespace)) {
383 // Template parameters are always an error here.
384 if (TemplateInfo.Kind) {
385 SourceRange R = TemplateInfo.getSourceRange();
386 Diag(UsingLoc, diag::err_templated_using_directive)
387 << R << FixItHint::CreateRemoval(R);
388 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000389
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000390 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000391 }
392
Richard Smithdda56e42011-04-15 14:24:37 +0000393 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000394
395 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000396 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000397
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000398 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000399 AS_none, OwnedType);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000400}
401
402/// ParseUsingDirective - Parse C++ using-directive, assumes
403/// that current token is 'namespace' and 'using' was already parsed.
404///
405/// using-directive: [C++ 7.3.p4: namespace.udir]
406/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
407/// namespace-name ;
408/// [GNU] using-directive:
409/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
410/// namespace-name attributes[opt] ;
411///
John McCall48871652010-08-21 09:40:31 +0000412Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000413 SourceLocation UsingLoc,
414 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000415 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000416 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
417
418 // Eat 'namespace'.
419 SourceLocation NamespcLoc = ConsumeToken();
420
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000421 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000422 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000423 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +0000424 return nullptr;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000425 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000426
Douglas Gregord7c4d982008-12-30 03:27:21 +0000427 CXXScopeSpec SS;
428 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000429 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000430
Craig Topper161e4db2014-05-21 06:02:52 +0000431 IdentifierInfo *NamespcName = nullptr;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000432 SourceLocation IdentLoc = SourceLocation();
433
434 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000435 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000436 Diag(Tok, diag::err_expected_namespace_name);
437 // If there was invalid namespace name, skip to end of decl, and eat ';'.
438 SkipUntil(tok::semi);
439 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
Craig Topper161e4db2014-05-21 06:02:52 +0000440 return nullptr;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000441 }
Mike Stump11289f42009-09-09 15:08:12 +0000442
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000443 // Parse identifier.
444 NamespcName = Tok.getIdentifierInfo();
445 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000446
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000447 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000448 bool GNUAttr = false;
449 if (Tok.is(tok::kw___attribute)) {
450 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000451 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000452 }
Mike Stump11289f42009-09-09 15:08:12 +0000453
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000454 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000455 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000456 if (ExpectAndConsume(tok::semi,
457 GNUAttr ? diag::err_expected_semi_after_attribute_list
458 : diag::err_expected_semi_after_namespace_name))
459 SkipUntil(tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000460
Douglas Gregor0be31a22010-07-02 17:43:08 +0000461 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000462 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000463}
464
Richard Smithdda56e42011-04-15 14:24:37 +0000465/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
466/// Assumes that 'using' was already seen.
Douglas Gregord7c4d982008-12-30 03:27:21 +0000467///
468/// using-declaration: [C++ 7.3.p3: namespace.udecl]
469/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000470/// unqualified-id
471/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000472///
Richard Smith810ad3e2013-01-29 10:02:16 +0000473/// alias-declaration: C++11 [dcl.dcl]p1
474/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smithdda56e42011-04-15 14:24:37 +0000475///
John McCall48871652010-08-21 09:40:31 +0000476Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000477 const ParsedTemplateInfo &TemplateInfo,
478 SourceLocation UsingLoc,
479 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000480 AccessSpecifier AS,
481 Decl **OwnedType) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000482 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000483 SourceLocation TypenameLoc;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000484 bool HasTypenameKeyword = false;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000485
Richard Smithc2c8bb82013-10-15 01:34:54 +0000486 // Check for misplaced attributes before the identifier in an
487 // alias-declaration.
488 ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
489 MaybeParseCXX11Attributes(MisplacedAttrs);
Douglas Gregorfec52632009-06-20 00:51:54 +0000490
491 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000492 // FIXME: This is wrong; we should parse this as a typename-specifier.
Alp Toker97650562014-01-10 11:19:30 +0000493 if (TryConsumeToken(tok::kw_typename, TypenameLoc))
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000494 HasTypenameKeyword = true;
Douglas Gregorfec52632009-06-20 00:51:54 +0000495
Nikola Smiljanic67860242014-09-26 00:28:20 +0000496 if (Tok.is(tok::kw___super)) {
497 Diag(Tok.getLocation(), diag::err_super_in_using_declaration);
498 SkipUntil(tok::semi);
499 return nullptr;
500 }
501
Douglas Gregorfec52632009-06-20 00:51:54 +0000502 // Parse nested-name-specifier.
Craig Topper161e4db2014-05-21 06:02:52 +0000503 IdentifierInfo *LastII = nullptr;
Richard Smith7447af42013-03-26 01:15:19 +0000504 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
Craig Topper161e4db2014-05-21 06:02:52 +0000505 /*MayBePseudoDtor=*/nullptr,
506 /*IsTypename=*/false,
Richard Smith7447af42013-03-26 01:15:19 +0000507 /*LastII=*/&LastII);
Douglas Gregorfec52632009-06-20 00:51:54 +0000508
Douglas Gregorfec52632009-06-20 00:51:54 +0000509 // Check nested-name specifier.
510 if (SS.isInvalid()) {
511 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000512 return nullptr;
Douglas Gregorfec52632009-06-20 00:51:54 +0000513 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000514
Richard Smith7447af42013-03-26 01:15:19 +0000515 SourceLocation TemplateKWLoc;
516 UnqualifiedId Name;
517
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000518 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000519 // destructor names and allow the action module to diagnose any semantic
520 // errors.
Richard Smith7447af42013-03-26 01:15:19 +0000521 //
522 // C++11 [class.qual]p2:
523 // [...] in a using-declaration that is a member-declaration, if the name
524 // specified after the nested-name-specifier is the same as the identifier
525 // or the simple-template-id's template-name in the last component of the
526 // nested-name-specifier, the name is [...] considered to name the
527 // constructor.
528 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
529 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
530 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
531 !SS.getScopeRep()->getAsNamespace() &&
532 !SS.getScopeRep()->getAsNamespaceAlias()) {
533 SourceLocation IdLoc = ConsumeToken();
534 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
535 Name.setConstructorName(Type, IdLoc, IdLoc);
536 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
537 /*AllowDestructorName=*/ true,
538 /*AllowConstructorName=*/ true, ParsedType(),
539 TemplateKWLoc, Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000540 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000541 return nullptr;
Douglas Gregorfec52632009-06-20 00:51:54 +0000542 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000543
Richard Smithc2c8bb82013-10-15 01:34:54 +0000544 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith37a45dd2013-10-24 01:21:09 +0000545 MaybeParseGNUAttributes(Attrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000546 MaybeParseCXX11Attributes(Attrs);
Richard Smithdda56e42011-04-15 14:24:37 +0000547
548 // Maybe this is an alias-declaration.
Richard Smithdda56e42011-04-15 14:24:37 +0000549 TypeResult TypeAlias;
Richard Smithc2c8bb82013-10-15 01:34:54 +0000550 bool IsAliasDecl = Tok.is(tok::equal);
Richard Smithdda56e42011-04-15 14:24:37 +0000551 if (IsAliasDecl) {
Richard Smithc2c8bb82013-10-15 01:34:54 +0000552 // If we had any misplaced attributes from earlier, this is where they
553 // should have been written.
554 if (MisplacedAttrs.Range.isValid()) {
555 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
556 << FixItHint::CreateInsertionFromRange(
557 Tok.getLocation(),
558 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
559 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
560 Attrs.takeAllFrom(MisplacedAttrs);
561 }
562
Richard Smithdda56e42011-04-15 14:24:37 +0000563 ConsumeToken();
564
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000565 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000566 diag::warn_cxx98_compat_alias_declaration :
567 diag::ext_alias_declaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000568
Richard Smith3f1b5d02011-05-05 21:57:07 +0000569 // Type alias templates cannot be specialized.
570 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000571 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
572 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000573 SpecKind = 0;
574 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
575 SpecKind = 1;
576 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
577 SpecKind = 2;
578 if (SpecKind != -1) {
579 SourceRange Range;
580 if (SpecKind == 0)
581 Range = SourceRange(Name.TemplateId->LAngleLoc,
582 Name.TemplateId->RAngleLoc);
583 else
584 Range = TemplateInfo.getSourceRange();
585 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
586 << SpecKind << Range;
587 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000588 return nullptr;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000589 }
590
Richard Smithdda56e42011-04-15 14:24:37 +0000591 // Name must be an identifier.
592 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
593 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
594 // No removal fixit: can't recover from this.
595 SkipUntil(tok::semi);
Craig Topper161e4db2014-05-21 06:02:52 +0000596 return nullptr;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000597 } else if (HasTypenameKeyword)
Richard Smithdda56e42011-04-15 14:24:37 +0000598 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
599 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
600 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
601 else if (SS.isNotEmpty())
602 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
603 << FixItHint::CreateRemoval(SS.getRange());
604
Craig Topper161e4db2014-05-21 06:02:52 +0000605 TypeAlias = ParseTypeName(nullptr, TemplateInfo.Kind ?
Richard Smith3f1b5d02011-05-05 21:57:07 +0000606 Declarator::AliasTemplateContext :
Richard Smith54ecd982013-02-20 19:22:51 +0000607 Declarator::AliasDeclContext, AS, OwnedType,
608 &Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000609 } else {
610 // C++11 attributes are not allowed on a using-declaration, but GNU ones
611 // are.
Richard Smithc2c8bb82013-10-15 01:34:54 +0000612 ProhibitAttributes(MisplacedAttrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000613 ProhibitAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000614
Richard Smithdda56e42011-04-15 14:24:37 +0000615 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith54ecd982013-02-20 19:22:51 +0000616 MaybeParseGNUAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000617 }
Mike Stump11289f42009-09-09 15:08:12 +0000618
Douglas Gregorfec52632009-06-20 00:51:54 +0000619 // Eat ';'.
620 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000621 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
622 !Attrs.empty() ? "attributes list"
623 : IsAliasDecl ? "alias declaration"
624 : "using declaration"))
625 SkipUntil(tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000626
John McCall9b72f892010-11-10 02:40:36 +0000627 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000628 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000629 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000630 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-11-10 02:40:36 +0000631 SourceRange R = TemplateInfo.getSourceRange();
632 Diag(UsingLoc, diag::err_templated_using_declaration)
633 << R << FixItHint::CreateRemoval(R);
634
635 // Unfortunately, we have to bail out instead of recovering by
636 // ignoring the parameters, just in case the nested name specifier
637 // depends on the parameters.
Craig Topper161e4db2014-05-21 06:02:52 +0000638 return nullptr;
John McCall9b72f892010-11-10 02:40:36 +0000639 }
640
Douglas Gregor882a61a2011-09-26 14:30:28 +0000641 // "typename" keyword is allowed for identifiers only,
642 // because it may be a type definition.
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000643 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor882a61a2011-09-26 14:30:28 +0000644 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
645 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000646 // Proceed parsing, but reset the HasTypenameKeyword flag.
647 HasTypenameKeyword = false;
Douglas Gregor882a61a2011-09-26 14:30:28 +0000648 }
649
Richard Smith3f1b5d02011-05-05 21:57:07 +0000650 if (IsAliasDecl) {
651 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000652 MultiTemplateParamsArg TemplateParamsArg(
Craig Topper161e4db2014-05-21 06:02:52 +0000653 TemplateParams ? TemplateParams->data() : nullptr,
Richard Smith3f1b5d02011-05-05 21:57:07 +0000654 TemplateParams ? TemplateParams->size() : 0);
655 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith54ecd982013-02-20 19:22:51 +0000656 UsingLoc, Name, Attrs.getList(),
657 TypeAlias);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000658 }
Richard Smithdda56e42011-04-15 14:24:37 +0000659
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000660 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
661 /* HasUsingKeyword */ true, UsingLoc,
662 SS, Name, Attrs.getList(),
663 HasTypenameKeyword, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000664}
665
Benjamin Kramere56f3932011-12-23 17:00:35 +0000666/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000667///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000668/// [C++0x] static_assert-declaration:
669/// static_assert ( constant-expression , string-literal ) ;
670///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000671/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000672/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000673///
John McCall48871652010-08-21 09:40:31 +0000674Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000675 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
676 "Not a static_assert declaration");
677
David Blaikiebbafb8a2012-03-11 07:00:24 +0000678 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000679 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000680 if (Tok.is(tok::kw_static_assert))
681 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000682
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000683 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000684
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000685 BalancedDelimiterTracker T(*this, tok::l_paren);
686 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000687 Diag(Tok, diag::err_expected) << tok::l_paren;
Richard Smith76965712012-09-13 19:12:50 +0000688 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000689 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000690 }
Mike Stump11289f42009-09-09 15:08:12 +0000691
John McCalldadc5752010-08-24 06:29:42 +0000692 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000693 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000694 SkipMalformedDecl();
Craig Topper161e4db2014-05-21 06:02:52 +0000695 return nullptr;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000696 }
Mike Stump11289f42009-09-09 15:08:12 +0000697
Richard Smith085a64f2014-06-20 19:57:12 +0000698 ExprResult AssertMessage;
699 if (Tok.is(tok::r_paren)) {
700 Diag(Tok, getLangOpts().CPlusPlus1z
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000701 ? diag::warn_cxx14_compat_static_assert_no_message
Richard Smith085a64f2014-06-20 19:57:12 +0000702 : diag::ext_static_assert_no_message)
703 << (getLangOpts().CPlusPlus1z
704 ? FixItHint()
705 : FixItHint::CreateInsertion(Tok.getLocation(), ", \"\""));
706 } else {
707 if (ExpectAndConsume(tok::comma)) {
708 SkipUntil(tok::semi);
709 return nullptr;
710 }
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000711
Richard Smith085a64f2014-06-20 19:57:12 +0000712 if (!isTokenStringLiteral()) {
713 Diag(Tok, diag::err_expected_string_literal)
714 << /*Source='static_assert'*/1;
715 SkipMalformedDecl();
716 return nullptr;
717 }
Mike Stump11289f42009-09-09 15:08:12 +0000718
Richard Smith085a64f2014-06-20 19:57:12 +0000719 AssertMessage = ParseStringLiteralExpression();
720 if (AssertMessage.isInvalid()) {
721 SkipMalformedDecl();
722 return nullptr;
723 }
Richard Smithd67aea22012-03-06 03:21:47 +0000724 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000725
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000726 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000727
Chris Lattner49836b42009-04-02 04:16:50 +0000728 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000729 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000730
John McCallb268a282010-08-23 23:25:46 +0000731 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000732 AssertExpr.get(),
733 AssertMessage.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000734 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000735}
736
Richard Smith74aeef52013-04-26 16:15:35 +0000737/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000738///
739/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000740/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000741///
David Blaikie15a430a2011-12-04 05:04:18 +0000742SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
743 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
744 && "Not a decltype specifier");
745
David Blaikie15a430a2011-12-04 05:04:18 +0000746 ExprResult Result;
747 SourceLocation StartLoc = Tok.getLocation();
748 SourceLocation EndLoc;
749
750 if (Tok.is(tok::annot_decltype)) {
751 Result = getExprAnnotation(Tok);
752 EndLoc = Tok.getAnnotationEndLoc();
753 ConsumeToken();
754 if (Result.isInvalid()) {
755 DS.SetTypeSpecError();
756 return EndLoc;
757 }
758 } else {
Richard Smith324df552012-02-24 22:30:04 +0000759 if (Tok.getIdentifierInfo()->isStr("decltype"))
760 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000761
David Blaikie15a430a2011-12-04 05:04:18 +0000762 ConsumeToken();
763
764 BalancedDelimiterTracker T(*this, tok::l_paren);
765 if (T.expectAndConsume(diag::err_expected_lparen_after,
766 "decltype", tok::r_paren)) {
767 DS.SetTypeSpecError();
768 return T.getOpenLocation() == Tok.getLocation() ?
769 StartLoc : T.getOpenLocation();
770 }
771
Richard Smith74aeef52013-04-26 16:15:35 +0000772 // Check for C++1y 'decltype(auto)'.
773 if (Tok.is(tok::kw_auto)) {
774 // No need to disambiguate here: an expression can't start with 'auto',
775 // because the typename-specifier in a function-style cast operation can't
776 // be 'auto'.
777 Diag(Tok.getLocation(),
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000778 getLangOpts().CPlusPlus14
Richard Smith74aeef52013-04-26 16:15:35 +0000779 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
780 : diag::ext_decltype_auto_type_specifier);
781 ConsumeToken();
782 } else {
783 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000784
Richard Smith74aeef52013-04-26 16:15:35 +0000785 // C++11 [dcl.type.simple]p4:
786 // The operand of the decltype specifier is an unevaluated operand.
787 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
Craig Topper161e4db2014-05-21 06:02:52 +0000788 nullptr,/*IsDecltype=*/true);
Richard Smith74aeef52013-04-26 16:15:35 +0000789 Result = ParseExpression();
790 if (Result.isInvalid()) {
791 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000792 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smith74aeef52013-04-26 16:15:35 +0000793 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000794 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000795 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
796 // Backtrack to get the location of the last token before the semi.
797 PP.RevertCachedTokens(2);
798 ConsumeToken(); // the semi.
799 EndLoc = ConsumeAnyToken();
800 assert(Tok.is(tok::semi));
801 } else {
802 EndLoc = Tok.getLocation();
803 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000804 }
Richard Smith74aeef52013-04-26 16:15:35 +0000805 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000806 }
Richard Smith74aeef52013-04-26 16:15:35 +0000807
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000808 Result = Actions.ActOnDecltypeExpression(Result.get());
David Blaikie15a430a2011-12-04 05:04:18 +0000809 }
810
811 // Match the ')'
812 T.consumeClose();
813 if (T.getCloseLocation().isInvalid()) {
814 DS.SetTypeSpecError();
815 // FIXME: this should return the location of the last token
816 // that was consumed (by "consumeClose()")
817 return T.getCloseLocation();
818 }
819
Richard Smithfd555f62012-02-22 02:04:18 +0000820 if (Result.isInvalid()) {
821 DS.SetTypeSpecError();
822 return T.getCloseLocation();
823 }
824
David Blaikie15a430a2011-12-04 05:04:18 +0000825 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000826 }
Richard Smith74aeef52013-04-26 16:15:35 +0000827 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000828
Craig Topper161e4db2014-05-21 06:02:52 +0000829 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +0000830 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000831 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Anders Carlsson74948d02009-06-24 17:47:40 +0000832 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000833 if (Result.get()
834 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000835 DiagID, Result.get(), Policy)
Richard Smith74aeef52013-04-26 16:15:35 +0000836 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000837 DiagID, Policy)) {
John McCall49bfce42009-08-03 20:12:06 +0000838 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000839 DS.SetTypeSpecError();
840 }
841 return EndLoc;
842}
843
844void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
845 SourceLocation StartLoc,
846 SourceLocation EndLoc) {
847 // make sure we have a token we can turn into an annotation token
848 if (PP.isBacktrackEnabled())
849 PP.RevertCachedTokens(1);
850 else
851 PP.EnterToken(Tok);
852
853 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000854 setExprAnnotation(Tok,
855 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
856 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
857 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000858 Tok.setAnnotationEndLoc(EndLoc);
859 Tok.setLocation(StartLoc);
860 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000861}
862
Alexis Hunt4a257072011-05-19 05:37:45 +0000863void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
864 assert(Tok.is(tok::kw___underlying_type) &&
865 "Not an underlying type specifier");
866
867 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000868 BalancedDelimiterTracker T(*this, tok::l_paren);
869 if (T.expectAndConsume(diag::err_expected_lparen_after,
870 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +0000871 return;
872 }
873
874 TypeResult Result = ParseTypeName();
875 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000876 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt4a257072011-05-19 05:37:45 +0000877 return;
878 }
879
880 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000881 T.consumeClose();
882 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000883 return;
884
Craig Topper161e4db2014-05-21 06:02:52 +0000885 const char *PrevSpec = nullptr;
Alexis Hunt4a257072011-05-19 05:37:45 +0000886 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000887 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000888 DiagID, Result.get(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000889 Actions.getASTContext().getPrintingPolicy()))
Alexis Hunt4a257072011-05-19 05:37:45 +0000890 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanellaa90af722013-07-06 18:54:58 +0000891 DS.setTypeofParensRange(T.getRange());
Alexis Hunt4a257072011-05-19 05:37:45 +0000892}
893
David Blaikie00ee7a082011-10-25 15:01:20 +0000894/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
895/// class name or decltype-specifier. Note that we only check that the result
896/// names a type; semantic analysis will need to verify that the type names a
897/// class. The result is either a type or null, depending on whether a type
898/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +0000899///
Richard Smith4c96e992013-02-19 23:47:15 +0000900/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000901/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +0000902/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000903/// nested-name-specifier[opt] class-name
904/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +0000905/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +0000906/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000907/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000908///
Richard Smith4c96e992013-02-19 23:47:15 +0000909/// In C++98, instead of base-type-specifier, we have:
910///
911/// ::[opt] nested-name-specifier[opt] class-name
Craig Topper9ad7e262014-10-31 06:57:07 +0000912TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
913 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +0000914 // Ignore attempts to use typename
915 if (Tok.is(tok::kw_typename)) {
916 Diag(Tok, diag::err_expected_class_name_not_template)
917 << FixItHint::CreateRemoval(Tok.getLocation());
918 ConsumeToken();
919 }
920
David Blaikieafa155f2011-10-25 18:17:58 +0000921 // Parse optional nested-name-specifier
922 CXXScopeSpec SS;
923 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
924
925 BaseLoc = Tok.getLocation();
926
David Blaikie1cd50022011-10-25 17:10:12 +0000927 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +0000928 // tok == kw_decltype is just error recovery, it can only happen when SS
929 // isn't empty
930 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +0000931 if (SS.isNotEmpty())
932 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
933 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +0000934 // Fake up a Declarator to use with ActOnTypeName.
935 DeclSpec DS(AttrFactory);
936
David Blaikie7491e732011-12-08 04:53:15 +0000937 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +0000938
939 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
940 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
941 }
942
Douglas Gregord54dfb82009-02-25 23:52:28 +0000943 // Check whether we have a template-id that names a type.
944 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000945 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +0000946 if (TemplateId->Kind == TNK_Type_template ||
947 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000948 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000949
950 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000951 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000952 EndLocation = Tok.getAnnotationEndLoc();
953 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000954
955 if (Type)
956 return Type;
957 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000958 }
959
960 // Fall through to produce an error below.
961 }
962
Douglas Gregor831c93f2008-11-05 20:51:48 +0000963 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000964 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000965 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000966 }
967
Douglas Gregor18473f32010-01-12 21:28:44 +0000968 IdentifierInfo *Id = Tok.getIdentifierInfo();
969 SourceLocation IdLoc = ConsumeToken();
970
971 if (Tok.is(tok::less)) {
972 // It looks the user intended to write a template-id here, but the
973 // template-name was wrong. Try to fix that.
974 TemplateNameKind TNK = TNK_Type_template;
975 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000976 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000977 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000978 Diag(IdLoc, diag::err_unknown_template_name)
979 << Id;
980 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000981
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000982 if (!Template) {
983 TemplateArgList TemplateArgs;
984 SourceLocation LAngleLoc, RAngleLoc;
985 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
986 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor18473f32010-01-12 21:28:44 +0000987 return true;
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000988 }
Douglas Gregor18473f32010-01-12 21:28:44 +0000989
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000990 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000991 UnqualifiedId TemplateName;
992 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000993
Douglas Gregor18473f32010-01-12 21:28:44 +0000994 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +0000995 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
996 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +0000997 return true;
998 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +0000999 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001000
Douglas Gregor18473f32010-01-12 21:28:44 +00001001 // If we didn't end up with a typename token, there's nothing more we
1002 // can do.
1003 if (Tok.isNot(tok::annot_typename))
1004 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001005
Douglas Gregor18473f32010-01-12 21:28:44 +00001006 // Retrieve the type from the annotation token, consume that token, and
1007 // return.
1008 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +00001009 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +00001010 ConsumeToken();
1011 return Type;
1012 }
1013
Douglas Gregor831c93f2008-11-05 20:51:48 +00001014 // We have an identifier; check whether it is actually a type.
Craig Topper161e4db2014-05-21 06:02:52 +00001015 IdentifierInfo *CorrectedII = nullptr;
Douglas Gregore7c20652011-03-02 00:47:37 +00001016 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +00001017 false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00001018 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +00001019 /*NonTrivialTypeSourceInfo=*/true,
1020 &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001021 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001022 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001023 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +00001024 }
1025
1026 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +00001027 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001028
1029 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +00001030 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +00001031 DS.SetRangeStart(IdLoc);
1032 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +00001033 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001034
Craig Topper161e4db2014-05-21 06:02:52 +00001035 const char *PrevSpec = nullptr;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001036 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001037 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
1038 Actions.getASTContext().getPrintingPolicy());
Nick Lewycky19b9f952010-07-26 16:56:01 +00001039
1040 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1041 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001042}
1043
John McCall8d32c052012-05-22 21:28:12 +00001044void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
1045 while (Tok.is(tok::kw___single_inheritance) ||
1046 Tok.is(tok::kw___multiple_inheritance) ||
1047 Tok.is(tok::kw___virtual_inheritance)) {
1048 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1049 SourceLocation AttrNameLoc = ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +00001050 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Aaron Ballman8edb5c22013-12-18 23:44:18 +00001051 AttributeList::AS_Keyword);
John McCall8d32c052012-05-22 21:28:12 +00001052 }
1053}
1054
Richard Smith369b9f92012-06-25 21:37:02 +00001055/// Determine whether the following tokens are valid after a type-specifier
1056/// which could be a standalone declaration. This will conservatively return
1057/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +00001058bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +00001059 // This switch enumerates the valid "follow" set for type-specifiers.
1060 switch (Tok.getKind()) {
1061 default: break;
1062 case tok::semi: // struct foo {...} ;
1063 case tok::star: // struct foo {...} * P;
1064 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +00001065 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +00001066 case tok::identifier: // struct foo {...} V ;
1067 case tok::r_paren: //(struct foo {...} ) {4}
1068 case tok::annot_cxxscope: // struct foo {...} a:: b;
1069 case tok::annot_typename: // struct foo {...} a ::b;
1070 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1071 case tok::l_paren: // struct foo {...} ( x);
1072 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001073 case tok::kw_operator: // struct foo operator ++() {...}
Alp Tokerd3f79c52013-11-24 20:24:54 +00001074 case tok::kw___declspec: // struct foo {...} __declspec(...)
Richard Smith843f18f2014-08-13 02:13:15 +00001075 case tok::l_square: // void f(struct f [ 3])
1076 case tok::ellipsis: // void f(struct f ... [Ns])
Abramo Bagnara152eb392014-08-16 08:29:27 +00001077 // FIXME: we should emit semantic diagnostic when declaration
1078 // attribute is in type attribute position.
1079 case tok::kw___attribute: // struct foo __attribute__((used)) x;
Richard Smith369b9f92012-06-25 21:37:02 +00001080 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001081 case tok::colon:
1082 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smith369b9f92012-06-25 21:37:02 +00001083 // Type qualifiers
1084 case tok::kw_const: // struct foo {...} const x;
1085 case tok::kw_volatile: // struct foo {...} volatile x;
1086 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith843f18f2014-08-13 02:13:15 +00001087 case tok::kw__Atomic: // struct foo {...} _Atomic x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001088 // Function specifiers
1089 // Note, no 'explicit'. An explicit function must be either a conversion
1090 // operator or a constructor. Either way, it can't have a return type.
1091 case tok::kw_inline: // struct foo inline f();
1092 case tok::kw_virtual: // struct foo virtual f();
1093 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001094 // Storage-class specifiers
1095 case tok::kw_static: // struct foo {...} static x;
1096 case tok::kw_extern: // struct foo {...} extern x;
1097 case tok::kw_typedef: // struct foo {...} typedef x;
1098 case tok::kw_register: // struct foo {...} register x;
1099 case tok::kw_auto: // struct foo {...} auto x;
1100 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001101 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001102 case tok::kw_constexpr: // struct foo {...} constexpr x;
1103 // As shown above, type qualifiers and storage class specifiers absolutely
1104 // can occur after class specifiers according to the grammar. However,
1105 // almost no one actually writes code like this. If we see one of these,
1106 // it is much more likely that someone missed a semi colon and the
1107 // type/storage class specifier we're seeing is part of the *next*
1108 // intended declaration, as in:
1109 //
1110 // struct foo { ... }
1111 // typedef int X;
1112 //
1113 // We'd really like to emit a missing semicolon error instead of emitting
1114 // an error on the 'int' saying that you can't have two type specifiers in
1115 // the same declaration of X. Because of this, we look ahead past this
1116 // token to see if it's a type specifier. If so, we know the code is
1117 // otherwise invalid, so we can produce the expected semi error.
1118 if (!isKnownToBeTypeSpecifier(NextToken()))
1119 return true;
1120 break;
1121 case tok::r_brace: // struct bar { struct foo {...} }
1122 // Missing ';' at end of struct is accepted as an extension in C mode.
1123 if (!getLangOpts().CPlusPlus)
1124 return true;
1125 break;
Richard Smith52c5b872013-01-29 04:13:32 +00001126 case tok::greater:
1127 // template<class T = class X>
1128 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001129 }
1130 return false;
1131}
1132
Douglas Gregor556877c2008-04-13 21:30:24 +00001133/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1134/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1135/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001136/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001137///
1138/// class-specifier: [C++ class]
1139/// class-head '{' member-specification[opt] '}'
1140/// class-head '{' member-specification[opt] '}' attributes[opt]
1141/// class-head:
1142/// class-key identifier[opt] base-clause[opt]
1143/// class-key nested-name-specifier identifier base-clause[opt]
1144/// class-key nested-name-specifier[opt] simple-template-id
1145/// base-clause[opt]
1146/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001147/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001148/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001149/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001150/// simple-template-id base-clause[opt]
1151/// class-key:
1152/// 'class'
1153/// 'struct'
1154/// 'union'
1155///
1156/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001157/// class-key ::[opt] nested-name-specifier[opt] identifier
1158/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1159/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001160///
1161/// Note that the C++ class-specifier and elaborated-type-specifier,
1162/// together, subsume the C99 struct-or-union-specifier:
1163///
1164/// struct-or-union-specifier: [C99 6.7.2.1]
1165/// struct-or-union identifier[opt] '{' struct-contents '}'
1166/// struct-or-union identifier
1167/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1168/// '}' attributes[opt]
1169/// [GNU] struct-or-union attributes[opt] identifier
1170/// struct-or-union:
1171/// 'struct'
1172/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001173void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1174 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001175 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001176 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001177 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001178 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001179 DeclSpec::TST TagType;
1180 if (TagTokKind == tok::kw_struct)
1181 TagType = DeclSpec::TST_struct;
1182 else if (TagTokKind == tok::kw___interface)
1183 TagType = DeclSpec::TST_interface;
1184 else if (TagTokKind == tok::kw_class)
1185 TagType = DeclSpec::TST_class;
1186 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001187 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1188 TagType = DeclSpec::TST_union;
1189 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001190
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001191 if (Tok.is(tok::code_completion)) {
1192 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001193 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001194 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001195 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001196
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001197 // C++03 [temp.explicit] 14.7.2/8:
1198 // The usual access checking rules do not apply to names used to specify
1199 // explicit instantiations.
1200 //
1201 // As an extension we do not perform access checking on the names used to
1202 // specify explicit specializations either. This is important to allow
1203 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001204 //
1205 // Note that we don't suppress if this turns out to be an elaborated
1206 // type specifier.
1207 bool shouldDelayDiagsInTag =
1208 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1209 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1210 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001211
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001212 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001213 // If attributes exist after tag, parse them.
Richard Smith37a45dd2013-10-24 01:21:09 +00001214 MaybeParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +00001215
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001216 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +00001217 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +00001218 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001219
John McCall8d32c052012-05-22 21:28:12 +00001220 // Parse inheritance specifiers.
1221 if (Tok.is(tok::kw___single_inheritance) ||
1222 Tok.is(tok::kw___multiple_inheritance) ||
1223 Tok.is(tok::kw___virtual_inheritance))
Richard Smith37a45dd2013-10-24 01:21:09 +00001224 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCall8d32c052012-05-22 21:28:12 +00001225
Alexis Hunt96d5c762009-11-21 08:43:09 +00001226 // If C++0x attributes exist here, parse them.
1227 // FIXME: Are we consistent with the ordering of parsing of different
1228 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001229 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001230
Michael Han309af292013-01-07 16:57:11 +00001231 // Source location used by FIXIT to insert misplaced
1232 // C++11 attributes
1233 SourceLocation AttrFixitLoc = Tok.getLocation();
1234
Nico Weber7c3c5be2014-09-23 04:09:56 +00001235 if (TagType == DeclSpec::TST_struct &&
1236 !Tok.is(tok::identifier) &&
1237 Tok.getIdentifierInfo() &&
Nico Weberb10c9202014-09-24 03:28:54 +00001238 (Tok.is(tok::kw___is_abstract) ||
1239 Tok.is(tok::kw___is_arithmetic) ||
1240 Tok.is(tok::kw___is_array) ||
1241 Tok.is(tok::kw___is_base_of) ||
1242 Tok.is(tok::kw___is_class) ||
1243 Tok.is(tok::kw___is_complete_type) ||
1244 Tok.is(tok::kw___is_compound) ||
1245 Tok.is(tok::kw___is_const) ||
1246 Tok.is(tok::kw___is_constructible) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001247 Tok.is(tok::kw___is_convertible) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001248 Tok.is(tok::kw___is_convertible_to) ||
1249 Tok.is(tok::kw___is_destructible) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001250 Tok.is(tok::kw___is_empty) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001251 Tok.is(tok::kw___is_enum) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001252 Tok.is(tok::kw___is_floating_point) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001253 Tok.is(tok::kw___is_final) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001254 Tok.is(tok::kw___is_function) ||
1255 Tok.is(tok::kw___is_fundamental) ||
1256 Tok.is(tok::kw___is_integral) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001257 Tok.is(tok::kw___is_interface_class) ||
1258 Tok.is(tok::kw___is_literal) ||
1259 Tok.is(tok::kw___is_lvalue_expr) ||
1260 Tok.is(tok::kw___is_lvalue_reference) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001261 Tok.is(tok::kw___is_member_function_pointer) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001262 Tok.is(tok::kw___is_member_object_pointer) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001263 Tok.is(tok::kw___is_member_pointer) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001264 Tok.is(tok::kw___is_nothrow_assignable) ||
1265 Tok.is(tok::kw___is_nothrow_constructible) ||
1266 Tok.is(tok::kw___is_nothrow_destructible) ||
1267 Tok.is(tok::kw___is_object) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001268 Tok.is(tok::kw___is_pod) ||
1269 Tok.is(tok::kw___is_pointer) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001270 Tok.is(tok::kw___is_polymorphic) ||
1271 Tok.is(tok::kw___is_reference) ||
1272 Tok.is(tok::kw___is_rvalue_expr) ||
1273 Tok.is(tok::kw___is_rvalue_reference) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001274 Tok.is(tok::kw___is_same) ||
1275 Tok.is(tok::kw___is_scalar) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001276 Tok.is(tok::kw___is_sealed) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001277 Tok.is(tok::kw___is_signed) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001278 Tok.is(tok::kw___is_standard_layout) ||
1279 Tok.is(tok::kw___is_trivial) ||
1280 Tok.is(tok::kw___is_trivially_assignable) ||
1281 Tok.is(tok::kw___is_trivially_constructible) ||
1282 Tok.is(tok::kw___is_trivially_copyable) ||
1283 Tok.is(tok::kw___is_union) ||
Nico Weber7c3c5be2014-09-23 04:09:56 +00001284 Tok.is(tok::kw___is_unsigned) ||
Nico Weberb10c9202014-09-24 03:28:54 +00001285 Tok.is(tok::kw___is_void) ||
1286 Tok.is(tok::kw___is_volatile)))
Nico Weber7c3c5be2014-09-23 04:09:56 +00001287 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
1288 // name of struct templates, but some are keywords in GCC >= 4.3
1289 // and Clang. Therefore, when we see the token sequence "struct
1290 // X", make X into a normal identifier rather than a keyword, to
1291 // allow libstdc++ 4.2 and libc++ to work properly.
1292 TryKeywordIdentFallback(true);
Mike Stump11289f42009-09-09 15:08:12 +00001293
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001294 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001295 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001296 if (getLangOpts().CPlusPlus) {
Serge Pavlov458ea762014-07-16 05:16:52 +00001297 // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it
1298 // is a base-specifier-list.
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001299 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001300
Douglas Gregordf593fb2011-11-07 17:33:42 +00001301 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall413021a2010-07-30 06:26:29 +00001302 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +00001303 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001304 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Alp Tokerec543272013-12-24 09:48:30 +00001305 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001306 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001307
Douglas Gregor916462b2009-10-30 21:46:58 +00001308 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1309
Douglas Gregor67a65642009-02-17 23:15:12 +00001310 // Parse the (optional) class name or simple-template-id.
Craig Topper161e4db2014-05-21 06:02:52 +00001311 IdentifierInfo *Name = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001312 SourceLocation NameLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00001313 TemplateIdAnnotation *TemplateId = nullptr;
Douglas Gregor556877c2008-04-13 21:30:24 +00001314 if (Tok.is(tok::identifier)) {
1315 Name = Tok.getIdentifierInfo();
1316 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001317
David Blaikiebbafb8a2012-03-11 07:00:24 +00001318 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001319 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001320 // Eat the template argument list and try to continue parsing this as
1321 // a class (or template thereof).
1322 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001323 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +00001324 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +00001325 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001326 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001327 // We couldn't parse the template argument list at all, so don't
1328 // try to give any location information for the list.
1329 LAngleLoc = RAngleLoc = SourceLocation();
1330 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001331
Douglas Gregor916462b2009-10-30 21:46:58 +00001332 Diag(NameLoc, diag::err_explicit_spec_non_template)
Alp Toker01d65e12014-01-06 12:54:41 +00001333 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1334 << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
Joao Matose9a3ed42012-08-31 22:18:20 +00001335
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001336 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001337 // we've removed its template argument list.
1338 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1339 if (TemplateParams && TemplateParams->size() > 1) {
1340 TemplateParams->pop_back();
1341 } else {
Craig Topper161e4db2014-05-21 06:02:52 +00001342 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001343 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001344 = ParsedTemplateInfo::NonTemplate;
1345 }
1346 } else if (TemplateInfo.Kind
1347 == ParsedTemplateInfo::ExplicitInstantiation) {
1348 // Pretend this is just a forward declaration.
Craig Topper161e4db2014-05-21 06:02:52 +00001349 TemplateParams = nullptr;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001350 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001351 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001352 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001353 = SourceLocation();
1354 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1355 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001356 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001357 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001358 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001359 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001360 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001361
Douglas Gregore7c20652011-03-02 00:47:37 +00001362 if (TemplateId->Kind != TNK_Type_template &&
1363 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001364 // The template-name in the simple-template-id refers to
1365 // something other than a class template. Give an appropriate
1366 // error message and skip to the ';'.
1367 SourceRange Range(NameLoc);
1368 if (SS.isNotEmpty())
1369 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001370
Richard Smith72bfbd82013-12-04 00:28:23 +00001371 // FIXME: Name may be null here.
Douglas Gregor7f741122009-02-25 19:37:18 +00001372 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001373 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001374
Douglas Gregor7f741122009-02-25 19:37:18 +00001375 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001376 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor7f741122009-02-25 19:37:18 +00001377 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001378 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001379 }
1380
Richard Smithbfdb1082012-03-12 08:56:40 +00001381 // There are four options here.
1382 // - If we are in a trailing return type, this is always just a reference,
1383 // and we must not try to parse a definition. For instance,
1384 // [] () -> struct S { };
1385 // does not define a type.
1386 // - If we have 'struct foo {...', 'struct foo :...',
1387 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1388 // - If we have 'struct foo;', then this is either a forward declaration
1389 // or a friend declaration, which have to be treated differently.
1390 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001391 //
1392 // We also detect these erroneous cases to provide better diagnostic for
1393 // C++11 attributes parsing.
1394 // - attributes follow class name:
1395 // struct foo [[]] {};
1396 // - attributes appear before or after 'final':
1397 // struct foo [[]] final [[]] {};
1398 //
Richard Smithc5b05522012-03-12 07:56:15 +00001399 // However, in type-specifier-seq's, things look like declarations but are
1400 // just references, e.g.
1401 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001402 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001403 // &T::operator struct s;
Richard Smith649c7b062014-01-08 00:56:48 +00001404 // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
Michael Han9407e502012-11-26 22:54:45 +00001405
1406 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001407 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001408
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001409 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
John McCallfaf5fb42010-08-26 23:41:50 +00001410 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001411 if (DSC == DSC_trailing)
1412 TUK = Sema::TUK_Reference;
1413 else if (Tok.is(tok::l_brace) ||
1414 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001415 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001416 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001417 if (DS.isFriendSpecified()) {
1418 // C++ [class.friend]p2:
1419 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001420 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001421 << SourceRange(DS.getFriendSpecLoc());
1422
1423 // Skip everything up to the semicolon, so that this looks like a proper
1424 // friend class (or template thereof) declaration.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001425 SkipUntil(tok::semi, StopBeforeMatch);
John McCallfaf5fb42010-08-26 23:41:50 +00001426 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001427 } else {
1428 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001429 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001430 }
Richard Smith434516c2013-02-22 06:46:23 +00001431 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1432 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001433 // We can't tell if this is a definition or reference
1434 // until we skipped the 'final' and C++11 attribute specifiers.
1435 TentativeParsingAction PA(*this);
1436
1437 // Skip the 'final' keyword.
1438 ConsumeToken();
1439
1440 // Skip C++11 attribute specifiers.
1441 while (true) {
1442 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1443 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001444 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001445 break;
Richard Smith434516c2013-02-22 06:46:23 +00001446 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001447 ConsumeToken();
1448 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001449 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001450 break;
1451 } else {
1452 break;
1453 }
1454 }
1455
1456 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1457 TUK = Sema::TUK_Definition;
1458 else
1459 TUK = Sema::TUK_Reference;
1460
1461 PA.Revert();
Richard Smith649c7b062014-01-08 00:56:48 +00001462 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00001463 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001464 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001465 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001466 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001467 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Joao Matose9a3ed42012-08-31 22:18:20 +00001468 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00001469 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001470 DeclSpec::getSpecifierName(TagType, PPol));
Joao Matose9a3ed42012-08-31 22:18:20 +00001471 PP.EnterToken(Tok);
1472 Tok.setKind(tok::semi);
1473 }
Richard Smith369b9f92012-06-25 21:37:02 +00001474 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001475 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001476
Michael Han9407e502012-11-26 22:54:45 +00001477 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1478 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001479 if (TUK != Sema::TUK_Reference) {
1480 // If this is not a reference, then the only possible
1481 // valid place for C++11 attributes to appear here
1482 // is between class-key and class-name. If there are
1483 // any attributes after class-name, we try a fixit to move
1484 // them to the right place.
1485 SourceRange AttrRange = Attributes.Range;
1486 if (AttrRange.isValid()) {
1487 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1488 << AttrRange
1489 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1490 CharSourceRange(AttrRange, true))
1491 << FixItHint::CreateRemoval(AttrRange);
1492
1493 // Recover by adding misplaced attributes to the attribute list
1494 // of the class so they can be applied on the class later.
1495 attrs.takeAllFrom(Attributes);
1496 }
1497 }
Michael Han9407e502012-11-26 22:54:45 +00001498
John McCall6347b682012-05-07 06:16:58 +00001499 // If this is an elaborated type specifier, and we delayed
1500 // diagnostics before, just merge them into the current pool.
1501 if (shouldDelayDiagsInTag) {
1502 diagsFromTag.done();
1503 if (TUK == Sema::TUK_Reference)
1504 diagsFromTag.redelay();
1505 }
1506
John McCall413021a2010-07-30 06:26:29 +00001507 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001508 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001509 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1510 // We have a declaration or reference to an anonymous class.
1511 Diag(StartLoc, diag::err_anon_type_definition)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001512 << DeclSpec::getSpecifierName(TagType, Policy);
John McCall413021a2010-07-30 06:26:29 +00001513 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001514
David Majnemer3252fd02013-12-05 01:36:53 +00001515 // If we are parsing a definition and stop at a base-clause, continue on
1516 // until the semicolon. Continuing from the comma will just trick us into
1517 // thinking we are seeing a variable declaration.
1518 if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1519 SkipUntil(tok::semi, StopBeforeMatch);
1520 else
1521 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor556877c2008-04-13 21:30:24 +00001522 return;
1523 }
1524
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001525 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001526 DeclResult TagOrTempResult = true; // invalid
1527 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001528
Douglas Gregord6ab8742009-05-28 23:31:59 +00001529 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +00001530 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001531 // Explicit specialization, class template partial specialization,
1532 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001533 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001534 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001535 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001536 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001537 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001538 ProhibitAttributes(attrs);
1539
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001540 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001541 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001542 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001543 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001544 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001545 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001546 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001547 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001548 TemplateId->TemplateNameLoc,
1549 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001550 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001551 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001552 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001553
1554 // Friend template-ids are treated as references unless
1555 // they have template headers, in which case they're ill-formed
1556 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1557 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001558 } else if (TUK == Sema::TUK_Reference ||
1559 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001560 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001561 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001562 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001563 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001564 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001565 TemplateId->Template,
1566 TemplateId->TemplateNameLoc,
1567 TemplateId->LAngleLoc,
1568 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001569 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001570 } else {
1571 // This is an explicit specialization or a class template
1572 // partial specialization.
1573 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001574 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1575 // This looks like an explicit instantiation, because we have
1576 // something like
1577 //
1578 // template class Foo<X>
1579 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001580 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001581 // meant to be an explicit specialization, but the user forgot
1582 // the '<>' after 'template'.
Richard Smith003c5e12013-11-08 19:03:29 +00001583 // It this is friend declaration however, since it cannot have a
1584 // template header, it is most likely that the user meant to
1585 // remove the 'template' keyword.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001586 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith003c5e12013-11-08 19:03:29 +00001587 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001588
Richard Smith003c5e12013-11-08 19:03:29 +00001589 if (TUK == Sema::TUK_Friend) {
1590 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
Craig Topper161e4db2014-05-21 06:02:52 +00001591 TemplateParams = nullptr;
Richard Smith003c5e12013-11-08 19:03:29 +00001592 } else {
1593 SourceLocation LAngleLoc =
1594 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1595 Diag(TemplateId->TemplateNameLoc,
1596 diag::err_explicit_instantiation_with_definition)
1597 << SourceRange(TemplateInfo.TemplateLoc)
1598 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1599
1600 // Create a fake template parameter list that contains only
1601 // "template<>", so that we treat this construct as a class
1602 // template specialization.
1603 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
Craig Topper161e4db2014-05-21 06:02:52 +00001604 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, nullptr,
1605 0, LAngleLoc));
Richard Smith003c5e12013-11-08 19:03:29 +00001606 TemplateParams = &FakedParamLists;
1607 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001608 }
1609
1610 // Build the class template specialization.
Richard Smith4b55a9c2014-04-17 03:29:33 +00001611 TagOrTempResult = Actions.ActOnClassTemplateSpecialization(
1612 getCurScope(), TagType, TUK, StartLoc, DS.getModulePrivateSpecLoc(),
1613 *TemplateId, attrs.getList(),
Craig Topper161e4db2014-05-21 06:02:52 +00001614 MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0]
1615 : nullptr,
Richard Smith4b55a9c2014-04-17 03:29:33 +00001616 TemplateParams ? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001617 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001618 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001619 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001620 // Explicit instantiation of a member of a class template
1621 // specialization, e.g.,
1622 //
1623 // template struct Outer<int>::Inner;
1624 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001625 ProhibitAttributes(attrs);
1626
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001627 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001628 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001629 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001630 TemplateInfo.TemplateLoc,
1631 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001632 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001633 } else if (TUK == Sema::TUK_Friend &&
1634 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001635 ProhibitAttributes(attrs);
1636
John McCallace48cd2010-10-19 01:40:49 +00001637 TagOrTempResult =
1638 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1639 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001640 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001641 MultiTemplateParamsArg(
Craig Topper161e4db2014-05-21 06:02:52 +00001642 TemplateParams? &(*TemplateParams)[0]
1643 : nullptr,
John McCallace48cd2010-10-19 01:40:49 +00001644 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001645 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001646 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1647 ProhibitAttributes(attrs);
Richard Smith003c5e12013-11-08 19:03:29 +00001648
Larisse Voufo725de3e2013-06-21 00:08:46 +00001649 if (TUK == Sema::TUK_Definition &&
1650 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1651 // If the declarator-id is not a template-id, issue a diagnostic and
1652 // recover by ignoring the 'template' keyword.
1653 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1654 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Craig Topper161e4db2014-05-21 06:02:52 +00001655 TemplateParams = nullptr;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001656 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001657
John McCall7f41d982009-09-11 04:59:25 +00001658 bool IsDependent = false;
1659
John McCall32723e92010-10-19 18:40:57 +00001660 // Don't pass down template parameter lists if this is just a tag
1661 // reference. For example, we don't need the template parameters here:
1662 // template <class T> class A *makeA(T t);
1663 MultiTemplateParamsArg TParams;
1664 if (TUK != Sema::TUK_Reference && TemplateParams)
1665 TParams =
1666 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1667
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001668 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001669 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001670 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001671 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001672 TParams, Owned, IsDependent,
1673 SourceLocation(), false,
Richard Smith649c7b062014-01-08 00:56:48 +00001674 clang::TypeResult(),
1675 DSC == DSC_type_specifier);
John McCall7f41d982009-09-11 04:59:25 +00001676
1677 // If ActOnTag said the type was dependent, try again with the
1678 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001679 if (IsDependent) {
1680 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001681 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001682 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001683 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001684 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001685
Douglas Gregor556877c2008-04-13 21:30:24 +00001686 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001687 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001688 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001689 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001690 isCXX11FinalKeyword());
David Blaikiebbafb8a2012-03-11 07:00:24 +00001691 if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001692 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1693 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001694 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001695 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001696 }
1697
Craig Topper161e4db2014-05-21 06:02:52 +00001698 const char *PrevSpec = nullptr;
John McCallba7bf592010-08-24 05:47:05 +00001699 unsigned DiagID;
1700 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001701 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001702 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1703 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001704 PrevSpec, DiagID, TypeResult.get(), Policy);
John McCall7f41d982009-09-11 04:59:25 +00001705 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001706 Result = DS.SetTypeSpecType(TagType, StartLoc,
1707 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001708 PrevSpec, DiagID, TagOrTempResult.get(), Owned,
1709 Policy);
John McCall7f41d982009-09-11 04:59:25 +00001710 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001711 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001712 return;
1713 }
Mike Stump11289f42009-09-09 15:08:12 +00001714
John McCallba7bf592010-08-24 05:47:05 +00001715 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001716 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001717
Chris Lattnercf251412010-02-02 01:23:29 +00001718 // At this point, we've successfully parsed a class-specifier in 'definition'
1719 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1720 // going to look at what comes after it to improve error recovery. If an
1721 // impossible token occurs next, we assume that the programmer forgot a ; at
1722 // the end of the declaration and recover that way.
1723 //
Richard Smith369b9f92012-06-25 21:37:02 +00001724 // Also enforce C++ [temp]p3:
1725 // In a template-declaration which defines a class, no declarator
1726 // is permitted.
Richard Smith843f18f2014-08-13 02:13:15 +00001727 //
1728 // After a type-specifier, we don't expect a semicolon. This only happens in
1729 // C, since definitions are not permitted in this context in C++.
Joao Matose9a3ed42012-08-31 22:18:20 +00001730 if (TUK == Sema::TUK_Definition &&
Richard Smith843f18f2014-08-13 02:13:15 +00001731 (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) &&
Joao Matose9a3ed42012-08-31 22:18:20 +00001732 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001733 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001734 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Alp Toker383d2c42014-01-01 03:08:43 +00001735 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001736 DeclSpec::getSpecifierName(TagType, PPol));
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001737 // Push this token back into the preprocessor and change our current token
1738 // to ';' so that the rest of the code recovers as though there were an
1739 // ';' after the definition.
1740 PP.EnterToken(Tok);
1741 Tok.setKind(tok::semi);
1742 }
Chris Lattnercf251412010-02-02 01:23:29 +00001743 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001744}
1745
Mike Stump11289f42009-09-09 15:08:12 +00001746/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001747///
1748/// base-clause : [C++ class.derived]
1749/// ':' base-specifier-list
1750/// base-specifier-list:
1751/// base-specifier '...'[opt]
1752/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001753void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001754 assert(Tok.is(tok::colon) && "Not a base clause");
1755 ConsumeToken();
1756
Douglas Gregor29a92472008-10-22 17:49:05 +00001757 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001758 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001759
Douglas Gregor556877c2008-04-13 21:30:24 +00001760 while (true) {
1761 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001762 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001763 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001764 // Skip the rest of this base specifier, up until the comma or
1765 // opening brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001766 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor29a92472008-10-22 17:49:05 +00001767 } else {
1768 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001769 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001770 }
1771
1772 // If the next token is a comma, consume it and keep reading
1773 // base-specifiers.
Alp Toker97650562014-01-10 11:19:30 +00001774 if (!TryConsumeToken(tok::comma))
1775 break;
Douglas Gregor556877c2008-04-13 21:30:24 +00001776 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001777
1778 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001779 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001780}
1781
1782/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1783/// one entry in the base class list of a class specifier, for example:
1784/// class foo : public bar, virtual private baz {
1785/// 'public bar' and 'virtual private baz' are each base-specifiers.
1786///
1787/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001788/// attribute-specifier-seq[opt] base-type-specifier
1789/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1790/// base-type-specifier
1791/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1792/// base-type-specifier
Craig Topper9ad7e262014-10-31 06:57:07 +00001793BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001794 bool IsVirtual = false;
1795 SourceLocation StartLoc = Tok.getLocation();
1796
Richard Smith4c96e992013-02-19 23:47:15 +00001797 ParsedAttributesWithRange Attributes(AttrFactory);
1798 MaybeParseCXX11Attributes(Attributes);
1799
Douglas Gregor556877c2008-04-13 21:30:24 +00001800 // Parse the 'virtual' keyword.
Alp Toker97650562014-01-10 11:19:30 +00001801 if (TryConsumeToken(tok::kw_virtual))
Douglas Gregor556877c2008-04-13 21:30:24 +00001802 IsVirtual = true;
Douglas Gregor556877c2008-04-13 21:30:24 +00001803
Richard Smith4c96e992013-02-19 23:47:15 +00001804 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1805
Douglas Gregor556877c2008-04-13 21:30:24 +00001806 // Parse an (optional) access specifier.
1807 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001808 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001809 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001810
Richard Smith4c96e992013-02-19 23:47:15 +00001811 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1812
Douglas Gregor556877c2008-04-13 21:30:24 +00001813 // Parse the 'virtual' keyword (again!), in case it came after the
1814 // access specifier.
1815 if (Tok.is(tok::kw_virtual)) {
1816 SourceLocation VirtualLoc = ConsumeToken();
1817 if (IsVirtual) {
1818 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001819 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001820 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001821 }
1822
1823 IsVirtual = true;
1824 }
1825
Richard Smith4c96e992013-02-19 23:47:15 +00001826 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1827
Douglas Gregor831c93f2008-11-05 20:51:48 +00001828 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001829 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001830 SourceLocation BaseLoc;
1831 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001832 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001833 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001834
Douglas Gregor752a5952011-01-03 22:36:02 +00001835 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1836 // actually part of the base-specifier-list grammar productions, but we
1837 // parse it here for convenience.
1838 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00001839 TryConsumeToken(tok::ellipsis, EllipsisLoc);
1840
Mike Stump11289f42009-09-09 15:08:12 +00001841 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001842 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001843
Douglas Gregor556877c2008-04-13 21:30:24 +00001844 // Notify semantic analysis that we have parsed a complete
1845 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00001846 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1847 Access, BaseType.get(), BaseLoc,
1848 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001849}
1850
1851/// getAccessSpecifierIfPresent - Determine whether the next token is
1852/// a C++ access-specifier.
1853///
1854/// access-specifier: [C++ class.derived]
1855/// 'private'
1856/// 'protected'
1857/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001858AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001859 switch (Tok.getKind()) {
1860 default: return AS_none;
1861 case tok::kw_private: return AS_private;
1862 case tok::kw_protected: return AS_protected;
1863 case tok::kw_public: return AS_public;
1864 }
1865}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001866
Douglas Gregor433e0532012-04-16 18:27:27 +00001867/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith2331bbf2012-05-02 22:22:32 +00001868/// delayed, e.g., default arguments, create a late-parsed method declaration
1869/// record to handle the parsing at the end of the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00001870void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1871 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001872 // We just declared a member function. If this member function
Richard Smith2331bbf2012-05-02 22:22:32 +00001873 // has any default arguments, we'll need to parse them later.
Craig Topper161e4db2014-05-21 06:02:52 +00001874 LateParsedMethodDeclaration *LateMethod = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001875 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001876 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor433e0532012-04-16 18:27:27 +00001877
Alp Tokerc5350722014-02-26 22:27:52 +00001878 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
1879 if (LateMethod || FTI.Params[ParamIdx].DefaultArgTokens) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001880 if (!LateMethod) {
1881 // Push this method onto the stack of late-parsed method
1882 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001883 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1884 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001885 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001886
1887 // Add all of the parameters prior to this one (they don't
1888 // have default arguments).
Alp Tokerc5350722014-02-26 22:27:52 +00001889 LateMethod->DefaultArgs.reserve(FTI.NumParams);
Eli Friedman3af2a772009-07-22 21:45:50 +00001890 for (unsigned I = 0; I < ParamIdx; ++I)
1891 LateMethod->DefaultArgs.push_back(
Alp Tokerc5350722014-02-26 22:27:52 +00001892 LateParsedDefaultArgument(FTI.Params[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001893 }
1894
Douglas Gregor433e0532012-04-16 18:27:27 +00001895 // Add this parameter to the list of parameters (it may or may
Eli Friedman3af2a772009-07-22 21:45:50 +00001896 // not have a default argument).
Alp Tokerc5350722014-02-26 22:27:52 +00001897 LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
1898 FTI.Params[ParamIdx].Param, FTI.Params[ParamIdx].DefaultArgTokens));
Eli Friedman3af2a772009-07-22 21:45:50 +00001899 }
1900 }
1901}
1902
Richard Smith89645bc2013-01-02 12:01:23 +00001903/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001904/// virt-specifier.
1905///
1906/// virt-specifier:
1907/// override
1908/// final
Richard Smith89645bc2013-01-02 12:01:23 +00001909VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001910 if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001911 return VirtSpecifiers::VS_None;
1912
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001913 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001914
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001915 // Initialize the contextual keywords.
1916 if (!Ident_final) {
1917 Ident_final = &PP.getIdentifierTable().get("final");
1918 if (getLangOpts().MicrosoftExt)
1919 Ident_sealed = &PP.getIdentifierTable().get("sealed");
1920 Ident_override = &PP.getIdentifierTable().get("override");
Anders Carlsson56104902011-01-17 03:05:47 +00001921 }
1922
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001923 if (II == Ident_override)
1924 return VirtSpecifiers::VS_Override;
1925
1926 if (II == Ident_sealed)
1927 return VirtSpecifiers::VS_Sealed;
1928
1929 if (II == Ident_final)
1930 return VirtSpecifiers::VS_Final;
1931
Anders Carlsson56104902011-01-17 03:05:47 +00001932 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001933}
1934
Richard Smith89645bc2013-01-02 12:01:23 +00001935/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001936///
1937/// virt-specifier-seq:
1938/// virt-specifier
1939/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00001940void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
Richard Smith3d1a94c2014-08-12 00:22:39 +00001941 bool IsInterface,
1942 SourceLocation FriendLoc) {
Anders Carlsson56104902011-01-17 03:05:47 +00001943 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00001944 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001945 if (Specifier == VirtSpecifiers::VS_None)
1946 return;
1947
Richard Smith3d1a94c2014-08-12 00:22:39 +00001948 if (FriendLoc.isValid()) {
1949 Diag(Tok.getLocation(), diag::err_friend_decl_spec)
1950 << VirtSpecifiers::getSpecifierName(Specifier)
1951 << FixItHint::CreateRemoval(Tok.getLocation())
1952 << SourceRange(FriendLoc, FriendLoc);
1953 ConsumeToken();
1954 continue;
1955 }
1956
Anders Carlsson56104902011-01-17 03:05:47 +00001957 // C++ [class.mem]p8:
1958 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Craig Topper161e4db2014-05-21 06:02:52 +00001959 const char *PrevSpec = nullptr;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001960 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001961 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1962 << PrevSpec
1963 << FixItHint::CreateRemoval(Tok.getLocation());
1964
David Majnemera5433082013-10-18 00:33:31 +00001965 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
1966 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalldb632ac2012-09-25 07:32:39 +00001967 Diag(Tok.getLocation(), diag::err_override_control_interface)
1968 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemera5433082013-10-18 00:33:31 +00001969 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
1970 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalldb632ac2012-09-25 07:32:39 +00001971 } else {
David Majnemera5433082013-10-18 00:33:31 +00001972 Diag(Tok.getLocation(),
1973 getLangOpts().CPlusPlus11
1974 ? diag::warn_cxx98_compat_override_control_keyword
1975 : diag::ext_override_control_keyword)
1976 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalldb632ac2012-09-25 07:32:39 +00001977 }
Anders Carlsson56104902011-01-17 03:05:47 +00001978 ConsumeToken();
1979 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001980}
1981
Richard Smith89645bc2013-01-02 12:01:23 +00001982/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001983/// 'final' or Microsoft 'sealed' contextual keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00001984bool Parser::isCXX11FinalKeyword() const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001985 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
1986 return Specifier == VirtSpecifiers::VS_Final ||
1987 Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001988}
1989
Richard Smith72553fc2014-01-23 23:53:27 +00001990/// \brief Parse a C++ member-declarator up to, but not including, the optional
1991/// brace-or-equal-initializer or pure-specifier.
1992void Parser::ParseCXXMemberDeclaratorBeforeInitializer(
1993 Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
1994 LateParsedAttrList &LateParsedAttrs) {
1995 // member-declarator:
1996 // declarator pure-specifier[opt]
1997 // declarator brace-or-equal-initializer[opt]
1998 // identifier[opt] ':' constant-expression
Serge Pavlov458ea762014-07-16 05:16:52 +00001999 if (Tok.isNot(tok::colon))
Richard Smith72553fc2014-01-23 23:53:27 +00002000 ParseDeclarator(DeclaratorInfo);
Richard Smith3d1a94c2014-08-12 00:22:39 +00002001 else
2002 DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation());
Richard Smith72553fc2014-01-23 23:53:27 +00002003
2004 if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002005 assert(DeclaratorInfo.isPastIdentifier() &&
2006 "don't know where identifier would go yet?");
Richard Smith72553fc2014-01-23 23:53:27 +00002007 BitfieldSize = ParseConstantExpression();
2008 if (BitfieldSize.isInvalid())
2009 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2010 } else
Richard Smith3d1a94c2014-08-12 00:22:39 +00002011 ParseOptionalCXX11VirtSpecifierSeq(
2012 VS, getCurrentClass().IsInterface,
2013 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Richard Smith72553fc2014-01-23 23:53:27 +00002014
2015 // If a simple-asm-expr is present, parse it.
2016 if (Tok.is(tok::kw_asm)) {
2017 SourceLocation Loc;
2018 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
2019 if (AsmLabel.isInvalid())
2020 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
2021
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002022 DeclaratorInfo.setAsmLabel(AsmLabel.get());
Richard Smith72553fc2014-01-23 23:53:27 +00002023 DeclaratorInfo.SetRangeEnd(Loc);
2024 }
2025
2026 // If attributes exist after the declarator, but before an '{', parse them.
2027 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Richard Smith4b5a9492014-01-24 22:34:35 +00002028
2029 // For compatibility with code written to older Clang, also accept a
2030 // virt-specifier *after* the GNU attributes.
Aaron Ballman5d153e32014-08-04 17:03:51 +00002031 if (BitfieldSize.isUnset() && VS.isUnset()) {
Richard Smith3d1a94c2014-08-12 00:22:39 +00002032 ParseOptionalCXX11VirtSpecifierSeq(
2033 VS, getCurrentClass().IsInterface,
2034 DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
Aaron Ballman5d153e32014-08-04 17:03:51 +00002035 if (!VS.isUnset()) {
2036 // If we saw any GNU-style attributes that are known to GCC followed by a
2037 // virt-specifier, issue a GCC-compat warning.
2038 const AttributeList *Attr = DeclaratorInfo.getAttributes();
2039 while (Attr) {
2040 if (Attr->isKnownToGCC() && !Attr->isCXX11Attribute())
2041 Diag(Attr->getLoc(), diag::warn_gcc_attribute_location);
2042 Attr = Attr->getNext();
2043 }
2044 }
2045 }
Richard Smith72553fc2014-01-23 23:53:27 +00002046}
2047
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002048/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
2049///
2050/// member-declaration:
2051/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
2052/// function-definition ';'[opt]
2053/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
2054/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00002055/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002056/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002057/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002058///
2059/// member-declarator-list:
2060/// member-declarator
2061/// member-declarator-list ',' member-declarator
2062///
2063/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002064/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002065/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00002066/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002067/// identifier[opt] ':' constant-expression
2068///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002069/// virt-specifier-seq:
2070/// virt-specifier
2071/// virt-specifier-seq virt-specifier
2072///
2073/// virt-specifier:
2074/// override
2075/// final
David Majnemera5433082013-10-18 00:33:31 +00002076/// [MS] sealed
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00002077///
Sebastian Redl42e92c42009-04-12 17:16:29 +00002078/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002079/// '= 0'
2080///
2081/// constant-initializer:
2082/// '=' constant-expression
2083///
Douglas Gregor3447e762009-08-20 22:52:58 +00002084void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002085 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00002086 const ParsedTemplateInfo &TemplateInfo,
2087 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00002088 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002089 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00002090 Diag(Tok, diag::err_at_defs_cxx);
2091 else
2092 Diag(Tok, diag::err_at_in_class);
Richard Smithda35e962013-11-09 04:52:51 +00002093
Douglas Gregor23c84762011-04-14 17:21:19 +00002094 ConsumeToken();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002095 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor23c84762011-04-14 17:21:19 +00002096 return;
2097 }
Richard Smithda35e962013-11-09 04:52:51 +00002098
Serge Pavlov458ea762014-07-16 05:16:52 +00002099 // Turn on colon protection early, while parsing declspec, although there is
2100 // nothing to protect there. It prevents from false errors if error recovery
2101 // incorrectly determines where the declspec ends, as in the example:
2102 // struct A { enum class B { C }; };
2103 // const int C = 4;
2104 // struct D { A::B : C; };
2105 ColonProtectionRAIIObject X(*this);
2106
John McCalla0097262009-12-11 02:10:03 +00002107 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00002108 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00002109 if (!TemplateInfo.Kind &&
Nikola Smiljanic67860242014-09-26 00:28:20 +00002110 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2111 Tok.is(tok::kw___super))) {
Richard Smith45855df2012-05-09 08:23:23 +00002112 if (TryAnnotateCXXScopeToken())
2113 MalformedTypeSpec = true;
2114
2115 bool isAccessDecl;
2116 if (Tok.isNot(tok::annot_cxxscope))
2117 isAccessDecl = false;
2118 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00002119 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
2120 else
2121 isAccessDecl = NextToken().is(tok::kw_operator);
2122
2123 if (isAccessDecl) {
2124 // Collect the scope specifier token we annotated earlier.
2125 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002126 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2127 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00002128
Nico Weberef03e702014-09-10 00:59:37 +00002129 if (SS.isInvalid()) {
2130 SkipUntil(tok::semi);
2131 return;
2132 }
2133
John McCalla0097262009-12-11 02:10:03 +00002134 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00002135 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00002136 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +00002137 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
2138 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00002139 SkipUntil(tok::semi);
2140 return;
2141 }
2142
2143 // TODO: recover from mistakenly-qualified operator declarations.
Alp Toker383d2c42014-01-01 03:08:43 +00002144 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2145 "access declaration")) {
2146 SkipUntil(tok::semi);
John McCalla0097262009-12-11 02:10:03 +00002147 return;
Alp Toker383d2c42014-01-01 03:08:43 +00002148 }
John McCalla0097262009-12-11 02:10:03 +00002149
Douglas Gregor0be31a22010-07-02 17:43:08 +00002150 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002151 /* HasUsingKeyword */ false,
2152 SourceLocation(),
John McCalla0097262009-12-11 02:10:03 +00002153 SS, Name,
Craig Topper161e4db2014-05-21 06:02:52 +00002154 /* AttrList */ nullptr,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002155 /* HasTypenameKeyword */ false,
John McCalla0097262009-12-11 02:10:03 +00002156 SourceLocation());
2157 return;
2158 }
2159 }
2160
Aaron Ballmane7c544d2014-08-04 20:28:35 +00002161 // static_assert-declaration. A templated static_assert declaration is
2162 // diagnosed in Parser::ParseSingleDeclarationAfterTemplate.
2163 if (!TemplateInfo.Kind &&
2164 (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert))) {
Chris Lattner49836b42009-04-02 04:16:50 +00002165 SourceLocation DeclEnd;
2166 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002167 return;
2168 }
Mike Stump11289f42009-09-09 15:08:12 +00002169
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002170 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00002171 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00002172 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00002173 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00002174 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002175 AS, AccessAttrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002176 return;
2177 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002178
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002179 // Handle: member-declaration ::= '__extension__' member-declaration
2180 if (Tok.is(tok::kw___extension__)) {
2181 // __extension__ silences extension warnings in the subexpression.
2182 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2183 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002184 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2185 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002186 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002187
John McCall084e83d2011-03-24 11:26:52 +00002188 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002189 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002190 // Optional C++11 attribute-specifier
2191 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002192 // We need to keep these attributes for future diagnostic
2193 // before they are taken over by declaration specifier.
2194 FnAttrs.addAll(attrs.getList());
2195 FnAttrs.Range = attrs.Range;
2196
John McCall53fa7142010-12-24 02:08:15 +00002197 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002198
Douglas Gregorfec52632009-06-20 00:51:54 +00002199 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002200 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002201
Douglas Gregorfec52632009-06-20 00:51:54 +00002202 // Eat 'using'.
2203 SourceLocation UsingLoc = ConsumeToken();
2204
2205 if (Tok.is(tok::kw_namespace)) {
2206 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002207 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002208 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00002209 SourceLocation DeclEnd;
Richard Smith3f1b5d02011-05-05 21:57:07 +00002210 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +00002211 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2212 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00002213 }
2214 return;
2215 }
2216
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002217 // Hold late-parsed attributes so we can attach a Decl to them later.
2218 LateParsedAttrList CommonLateParsedAttrs;
2219
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002220 // decl-specifier-seq:
2221 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002222 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002223 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002224 if (MalformedTypeSpec)
2225 DS.SetTypeSpecError();
Richard Smith72553fc2014-01-23 23:53:27 +00002226
Serge Pavlov458ea762014-07-16 05:16:52 +00002227 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2228 &CommonLateParsedAttrs);
2229
2230 // Turn off colon protection that was set for declspec.
2231 X.restore();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002232
Richard Smith404dfb42013-11-19 22:47:36 +00002233 // If we had a free-standing type definition with a missing semicolon, we
2234 // may get this far before the problem becomes obvious.
2235 if (DS.hasTagDefinition() &&
2236 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2237 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2238 &CommonLateParsedAttrs))
2239 return;
2240
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002241 MultiTemplateParamsArg TemplateParams(
Craig Topper161e4db2014-05-21 06:02:52 +00002242 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data()
2243 : nullptr,
John McCall11083da2009-09-16 22:47:08 +00002244 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2245
Alp Toker35d87032013-12-30 23:29:50 +00002246 if (TryConsumeToken(tok::semi)) {
Michael Handdc016d2012-11-28 23:17:40 +00002247 if (DS.isFriendSpecified())
2248 ProhibitAttributes(FnAttrs);
2249
John McCall48871652010-08-21 09:40:31 +00002250 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00002251 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00002252 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00002253 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002254 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002255
John McCall28a6aea2009-11-04 02:18:39 +00002256 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002257 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002258
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002259 // Hold late-parsed attributes so we can attach a Decl to them later.
2260 LateParsedAttrList LateParsedAttrs;
2261
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002262 SourceLocation EqualLoc;
2263 bool HasInitializer = false;
2264 ExprResult Init;
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002265
Richard Smith72553fc2014-01-23 23:53:27 +00002266 SmallVector<Decl *, 8> DeclsInGroup;
2267 ExprResult BitfieldSize;
2268 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002269
Richard Smith72553fc2014-01-23 23:53:27 +00002270 // Parse the first declarator.
2271 ParseCXXMemberDeclaratorBeforeInitializer(DeclaratorInfo, VS, BitfieldSize,
2272 LateParsedAttrs);
Nico Weber24b2a822011-01-28 06:07:34 +00002273
Richard Smith72553fc2014-01-23 23:53:27 +00002274 // If this has neither a name nor a bit width, something has gone seriously
2275 // wrong. Skip until the semi-colon or }.
Richard Smith4b5a9492014-01-24 22:34:35 +00002276 if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002277 // If so, skip until the semi-colon or a }.
2278 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2279 TryConsumeToken(tok::semi);
2280 return;
2281 }
John Thompson5bc5cbe2009-11-25 22:58:06 +00002282
Richard Smith72553fc2014-01-23 23:53:27 +00002283 // Check for a member function definition.
Richard Smith4b5a9492014-01-24 22:34:35 +00002284 if (BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002285 // MSVC permits pure specifier on inline functions defined at class scope.
Francois Pichet3abc9b82011-05-11 02:14:46 +00002286 // Hence check for =0 before checking for function definition.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002287 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Richard Smith72553fc2014-01-23 23:53:27 +00002288 DeclaratorInfo.isFunctionDeclarator() &&
Francois Pichet3abc9b82011-05-11 02:14:46 +00002289 NextToken().is(tok::numeric_constant)) {
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002290 EqualLoc = ConsumeToken();
Francois Pichet3abc9b82011-05-11 02:14:46 +00002291 Init = ParseInitializer();
2292 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002293 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002294 else
2295 HasInitializer = true;
Francois Pichet3abc9b82011-05-11 02:14:46 +00002296 }
2297
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002298 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002299 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002300 //
2301 // In C++11, a non-function declarator followed by an open brace is a
2302 // braced-init-list for an in-class member initialization, not an
2303 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002304 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002305 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002306 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002307 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002308 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002309 } else if (Tok.is(tok::equal)) {
2310 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002311 if (KW.is(tok::kw_default))
2312 DefinitionKind = FDK_Defaulted;
2313 else if (KW.is(tok::kw_delete))
2314 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002315 }
2316 }
2317
Michael Handdc016d2012-11-28 23:17:40 +00002318 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2319 // to a friend declaration, that declaration shall be a definition.
2320 if (DeclaratorInfo.isFunctionDeclarator() &&
2321 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2322 // Diagnose attributes that appear before decl specifier:
2323 // [[]] friend int foo();
2324 ProhibitAttributes(FnAttrs);
2325 }
2326
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002327 if (DefinitionKind) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002328 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002329 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002330 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002331 SkipUntil(tok::r_brace);
Michael Handdc016d2012-11-28 23:17:40 +00002332
Douglas Gregor8a4db832011-01-19 16:41:58 +00002333 // Consume the optional ';'
Alp Toker35d87032013-12-30 23:29:50 +00002334 TryConsumeToken(tok::semi);
2335
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002336 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002337 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002338
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002339 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002340 Diag(DeclaratorInfo.getIdentifierLoc(),
2341 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002342
Richard Smith2603b092012-11-15 22:54:20 +00002343 // Recover by treating the 'typedef' as spurious.
2344 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002345 }
2346
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002347 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002348 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002349 VS, DefinitionKind, Init);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002350
David Majnemer23252a32013-08-01 04:22:55 +00002351 if (FunDecl) {
2352 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2353 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2354 }
2355 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2356 LateParsedAttrs[i]->addDecl(FunDecl);
2357 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002358 }
2359 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002360
2361 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002362 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002363 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002364
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002365 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002366 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002367 }
2368
2369 // member-declarator-list:
2370 // member-declarator
2371 // member-declarator-list ',' member-declarator
2372
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002373 while (1) {
Richard Smith2b013182012-06-10 03:12:00 +00002374 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002375 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith938f40b2011-06-11 17:19:42 +00002376 if (BitfieldSize.get()) {
2377 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002378 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith938f40b2011-06-11 17:19:42 +00002379 } else {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002380 HasInitializer = true;
Richard Smith2b013182012-06-10 03:12:00 +00002381 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2382 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smith2b013182012-06-10 03:12:00 +00002383 != DeclSpec::SCS_typedef)
2384 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002385 }
2386 }
2387
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002388 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002389 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002390 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002391
Craig Topper161e4db2014-05-21 06:02:52 +00002392 NamedDecl *ThisDecl = nullptr;
John McCall07e91c02009-08-06 02:15:43 +00002393 if (DS.isFriendSpecified()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002394 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
Michael Handdc016d2012-11-28 23:17:40 +00002395 // to a friend declaration, that declaration shall be a definition.
2396 //
Richard Smith72553fc2014-01-23 23:53:27 +00002397 // Diagnose attributes that appear in a friend member function declarator:
2398 // friend int foo [[]] ();
Michael Handdc016d2012-11-28 23:17:40 +00002399 SmallVector<SourceRange, 4> Ranges;
2400 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
Richard Smith72553fc2014-01-23 23:53:27 +00002401 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
2402 E = Ranges.end(); I != E; ++I)
2403 Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
Michael Handdc016d2012-11-28 23:17:40 +00002404
Douglas Gregor0be31a22010-07-02 17:43:08 +00002405 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002406 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002407 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002408 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002409 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002410 TemplateParams,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002411 BitfieldSize.get(),
Richard Smith2b013182012-06-10 03:12:00 +00002412 VS, HasInClassInit);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002413
2414 if (VarTemplateDecl *VT =
Craig Topper161e4db2014-05-21 06:02:52 +00002415 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : nullptr)
Larisse Voufo39a1e502013-08-06 01:03:05 +00002416 // Re-direct this decl to refer to the templated decl so that we can
2417 // initialize it.
2418 ThisDecl = VT->getTemplatedDecl();
2419
David Majnemer23252a32013-08-01 04:22:55 +00002420 if (ThisDecl && AccessAttrs)
Richard Smithf8a75c32013-08-29 00:47:48 +00002421 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00002422 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002423
Douglas Gregor728d00b2011-10-10 14:49:18 +00002424 // Handle the initializer.
David Blaikie35506f82013-01-30 01:22:18 +00002425 if (HasInClassInit != ICIS_NoInit &&
2426 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2427 DeclSpec::SCS_static) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002428 // The initializer was deferred; parse it and cache the tokens.
David Majnemer23252a32013-08-01 04:22:55 +00002429 Diag(Tok, getLangOpts().CPlusPlus11
2430 ? diag::warn_cxx98_compat_nonstatic_member_init
2431 : diag::ext_nonstatic_member_init);
Richard Smith5d164bc2011-10-15 05:09:34 +00002432
Richard Smith938f40b2011-06-11 17:19:42 +00002433 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002434 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2435 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002436 //
2437 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002438 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002439 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002440 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemer23252a32013-08-01 04:22:55 +00002441
2442 // Avoid later warnings about a class member of incomplete type.
David Blaikiecdd91db2012-02-14 09:00:46 +00002443 if (ThisDecl)
David Blaikiecdd91db2012-02-14 09:00:46 +00002444 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002445 } else
2446 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002447 } else if (HasInitializer) {
2448 // Normal initializer.
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002449 if (!Init.isUsable())
David Majnemer23252a32013-08-01 04:22:55 +00002450 Init = ParseCXXMemberInitializer(
2451 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2452
Douglas Gregor728d00b2011-10-10 14:49:18 +00002453 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002454 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002455 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002456 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smith74aeef52013-04-26 16:15:35 +00002457 DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002458 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor728d00b2011-10-10 14:49:18 +00002459 // No initializer.
Richard Smith74aeef52013-04-26 16:15:35 +00002460 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002461
Douglas Gregor728d00b2011-10-10 14:49:18 +00002462 if (ThisDecl) {
David Majnemer23252a32013-08-01 04:22:55 +00002463 if (!ThisDecl->isInvalidDecl()) {
2464 // Set the Decl for any late parsed attributes
2465 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2466 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2467
2468 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2469 LateParsedAttrs[i]->addDecl(ThisDecl);
2470 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002471 Actions.FinalizeDeclaration(ThisDecl);
2472 DeclsInGroup.push_back(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002473
2474 if (DeclaratorInfo.isFunctionDeclarator() &&
2475 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2476 DeclSpec::SCS_typedef)
2477 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002478 }
David Majnemer23252a32013-08-01 04:22:55 +00002479 LateParsedAttrs.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002480
2481 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002482
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002483 // If we don't have a comma, it is either the end of the list (a ';')
2484 // or an error, bail out.
Alp Toker094e5212014-01-05 03:27:11 +00002485 SourceLocation CommaLoc;
2486 if (!TryConsumeToken(tok::comma, CommaLoc))
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002487 break;
Mike Stump11289f42009-09-09 15:08:12 +00002488
Richard Smithc8a79032012-01-09 22:31:44 +00002489 if (Tok.isAtStartOfLine() &&
2490 !MightBeDeclarator(Declarator::MemberContext)) {
2491 // This comma was followed by a line-break and something which can't be
2492 // the start of a declarator. The comma was probably a typo for a
2493 // semicolon.
2494 Diag(CommaLoc, diag::err_expected_semi_declaration)
2495 << FixItHint::CreateReplacement(CommaLoc, ";");
2496 ExpectSemi = false;
2497 break;
2498 }
Mike Stump11289f42009-09-09 15:08:12 +00002499
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002500 // Parse the next declarator.
2501 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002502 VS.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002503 BitfieldSize = true;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002504 Init = true;
2505 HasInitializer = false;
Richard Smith8d06f422012-01-12 23:53:29 +00002506 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002507
Richard Smith72553fc2014-01-23 23:53:27 +00002508 // GNU attributes are allowed before the second and subsequent declarator.
John McCall53fa7142010-12-24 02:08:15 +00002509 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002510
Richard Smith72553fc2014-01-23 23:53:27 +00002511 ParseCXXMemberDeclaratorBeforeInitializer(DeclaratorInfo, VS, BitfieldSize,
2512 LateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002513 }
2514
Richard Smithc8a79032012-01-09 22:31:44 +00002515 if (ExpectSemi &&
2516 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002517 // Skip to end of block or statement.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002518 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002519 // If we stopped at a ';', eat it.
Alp Toker35d87032013-12-30 23:29:50 +00002520 TryConsumeToken(tok::semi);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002521 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002522 }
2523
Rafael Espindolaab417692013-07-09 12:05:01 +00002524 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002525}
2526
Richard Smith938f40b2011-06-11 17:19:42 +00002527/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2528/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2529/// function definition. The location of the '=', if any, will be placed in
2530/// EqualLoc.
2531///
2532/// pure-specifier:
2533/// '= 0'
Sebastian Redleef474c2012-02-22 10:50:08 +00002534///
Richard Smith938f40b2011-06-11 17:19:42 +00002535/// brace-or-equal-initializer:
2536/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002537/// braced-init-list
2538///
Richard Smith938f40b2011-06-11 17:19:42 +00002539/// initializer-clause:
2540/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002541/// braced-init-list
2542///
Richard Smithda35e962013-11-09 04:52:51 +00002543/// defaulted/deleted function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002544/// '=' 'default'
2545/// '=' 'delete'
2546///
2547/// Prior to C++0x, the assignment-expression in an initializer-clause must
2548/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002549ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002550 SourceLocation &EqualLoc) {
2551 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2552 && "Data member initializer not starting with '=' or '{'");
2553
Douglas Gregor926410d2012-02-21 02:22:07 +00002554 EnterExpressionEvaluationContext Context(Actions,
2555 Sema::PotentiallyEvaluated,
2556 D);
Alp Toker094e5212014-01-05 03:27:11 +00002557 if (TryConsumeToken(tok::equal, EqualLoc)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002558 if (Tok.is(tok::kw_delete)) {
2559 // In principle, an initializer of '= delete p;' is legal, but it will
2560 // never type-check. It's better to diagnose it as an ill-formed expression
2561 // than as an ill-formed deleted non-function member.
2562 // An initializer of '= delete p, foo' will never be parsed, because
2563 // a top-level comma always ends the initializer expression.
2564 const Token &Next = NextToken();
2565 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
Richard Smith34f30512013-11-23 04:06:09 +00002566 Next.is(tok::eof)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002567 if (IsFunction)
2568 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2569 << 1 /* delete */;
2570 else
2571 Diag(ConsumeToken(), diag::err_deleted_non_function);
Richard Smithedcb26e2014-06-11 00:49:52 +00002572 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002573 }
2574 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002575 if (IsFunction)
2576 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2577 << 0 /* default */;
2578 else
2579 Diag(ConsumeToken(), diag::err_default_special_members);
Richard Smithedcb26e2014-06-11 00:49:52 +00002580 return ExprError();
Richard Smith938f40b2011-06-11 17:19:42 +00002581 }
2582
Sebastian Redleef474c2012-02-22 10:50:08 +00002583 }
2584 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002585}
2586
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002587/// ParseCXXMemberSpecification - Parse the class definition.
2588///
2589/// member-specification:
2590/// member-declaration member-specification[opt]
2591/// access-specifier ':' member-specification[opt]
2592///
Joao Matose9a3ed42012-08-31 22:18:20 +00002593void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002594 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00002595 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002596 unsigned TagType, Decl *TagDecl) {
2597 assert((TagType == DeclSpec::TST_struct ||
2598 TagType == DeclSpec::TST_interface ||
2599 TagType == DeclSpec::TST_union ||
2600 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2601
John McCallfaf5fb42010-08-26 23:41:50 +00002602 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2603 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00002604
Douglas Gregoredf8f392010-01-16 20:52:59 +00002605 // Determine whether this is a non-nested class. Note that local
2606 // classes are *not* considered to be nested classes.
2607 bool NonNestedClass = true;
2608 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002609 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002610 if (S->isClassScope()) {
2611 // We're inside a class scope, so this is a nested class.
2612 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00002613
2614 // The Microsoft extension __interface does not permit nested classes.
2615 if (getCurrentClass().IsInterface) {
2616 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2617 << /*ErrorType=*/6
2618 << (isa<NamedDecl>(TagDecl)
2619 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
David Blaikieabe1a392014-04-02 05:58:29 +00002620 : "(anonymous)");
John McCalldb632ac2012-09-25 07:32:39 +00002621 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00002622 break;
2623 }
2624
2625 if ((S->getFlags() & Scope::FnScope)) {
2626 // If we're in a function or function template declared in the
2627 // body of a class, then this is a local class rather than a
2628 // nested class.
2629 const Scope *Parent = S->getParent();
2630 if (Parent->isTemplateParamScope())
2631 Parent = Parent->getParent();
2632 if (Parent->isClassScope())
2633 break;
2634 }
2635 }
2636 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002637
2638 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00002639 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002640
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002641 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00002642 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2643 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002644
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002645 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002646 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002647
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002648 SourceLocation FinalLoc;
David Majnemera5433082013-10-18 00:33:31 +00002649 bool IsFinalSpelledSealed = false;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002650
2651 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002652 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemera5433082013-10-18 00:33:31 +00002653 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2654 assert((Specifier == VirtSpecifiers::VS_Final ||
2655 Specifier == VirtSpecifiers::VS_Sealed) &&
2656 "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00002657 FinalLoc = ConsumeToken();
David Majnemera5433082013-10-18 00:33:31 +00002658 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002659
David Majnemera5433082013-10-18 00:33:31 +00002660 if (TagType == DeclSpec::TST_interface)
John McCalldb632ac2012-09-25 07:32:39 +00002661 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemera5433082013-10-18 00:33:31 +00002662 << VirtSpecifiers::getSpecifierName(Specifier);
2663 else if (Specifier == VirtSpecifiers::VS_Final)
2664 Diag(FinalLoc, getLangOpts().CPlusPlus11
2665 ? diag::warn_cxx98_compat_override_control_keyword
2666 : diag::ext_override_control_keyword)
2667 << VirtSpecifiers::getSpecifierName(Specifier);
2668 else if (Specifier == VirtSpecifiers::VS_Sealed)
2669 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han9407e502012-11-26 22:54:45 +00002670
Michael Han309af292013-01-07 16:57:11 +00002671 // Parse any C++11 attributes after 'final' keyword.
2672 // These attributes are not allowed to appear here,
2673 // and the only possible place for them to appertain
2674 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00002675 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002676 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002677
John McCall2d814c32009-12-19 21:48:58 +00002678 if (Tok.is(tok::colon)) {
2679 ParseBaseClause(TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002680 if (!Tok.is(tok::l_brace)) {
Ismail Pazarbasi129c44c2014-09-25 21:13:02 +00002681 bool SuggestFixIt = false;
2682 SourceLocation BraceLoc = PP.getLocForEndOfToken(PrevTokLocation);
2683 if (Tok.isAtStartOfLine()) {
2684 switch (Tok.getKind()) {
2685 case tok::kw_private:
2686 case tok::kw_protected:
2687 case tok::kw_public:
2688 SuggestFixIt = NextToken().getKind() == tok::colon;
2689 break;
2690 case tok::kw_static_assert:
2691 case tok::r_brace:
2692 case tok::kw_using:
2693 // base-clause can have simple-template-id; 'template' can't be there
2694 case tok::kw_template:
2695 SuggestFixIt = true;
2696 break;
2697 case tok::identifier:
2698 SuggestFixIt = isConstructorDeclarator(true);
2699 break;
2700 default:
2701 SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
2702 break;
2703 }
2704 }
2705 DiagnosticBuilder LBraceDiag =
2706 Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers);
2707 if (SuggestFixIt) {
2708 LBraceDiag << FixItHint::CreateInsertion(BraceLoc, " {");
2709 // Try recovering from missing { after base-clause.
2710 PP.EnterToken(Tok);
2711 Tok.setKind(tok::l_brace);
2712 } else {
2713 if (TagDecl)
2714 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
2715 return;
2716 }
John McCall2d814c32009-12-19 21:48:58 +00002717 }
2718 }
2719
2720 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002721 BalancedDelimiterTracker T(*this, tok::l_brace);
2722 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00002723
John McCall08bede42010-05-28 08:11:17 +00002724 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00002725 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemera5433082013-10-18 00:33:31 +00002726 IsFinalSpelledSealed,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002727 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00002728
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002729 // C++ 11p3: Members of a class defined with the keyword class are private
2730 // by default. Members of a class defined with the keywords struct or union
2731 // are public by default.
2732 AccessSpecifier CurAS;
2733 if (TagType == DeclSpec::TST_class)
2734 CurAS = AS_private;
2735 else
2736 CurAS = AS_public;
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002737 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002738
Douglas Gregor9377c822010-06-21 22:31:09 +00002739 if (TagDecl) {
2740 // While we still have something to read, read the member-declarations.
Richard Smith34f30512013-11-23 04:06:09 +00002741 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Douglas Gregor9377c822010-06-21 22:31:09 +00002742 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002743
David Blaikiebbafb8a2012-03-11 07:00:24 +00002744 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet8f981d52011-05-25 10:19:49 +00002745 Tok.is(tok::kw___if_not_exists))) {
2746 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2747 continue;
2748 }
2749
Douglas Gregor9377c822010-06-21 22:31:09 +00002750 // Check for extraneous top-level semicolon.
2751 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002752 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor9377c822010-06-21 22:31:09 +00002753 continue;
2754 }
2755
Eli Friedmanec52f922012-02-23 23:47:16 +00002756 if (Tok.is(tok::annot_pragma_vis)) {
2757 HandlePragmaVisibility();
2758 continue;
2759 }
2760
2761 if (Tok.is(tok::annot_pragma_pack)) {
2762 HandlePragmaPack();
2763 continue;
2764 }
2765
Argyrios Kyrtzidis5c2021b2012-10-12 17:39:59 +00002766 if (Tok.is(tok::annot_pragma_align)) {
2767 HandlePragmaAlign();
2768 continue;
2769 }
2770
Alexey Bataeva769e072013-03-22 06:34:35 +00002771 if (Tok.is(tok::annot_pragma_openmp)) {
2772 ParseOpenMPDeclarativeDirective();
2773 continue;
2774 }
2775
David Majnemer4bb09802014-02-10 19:50:15 +00002776 if (Tok.is(tok::annot_pragma_ms_pointers_to_members)) {
2777 HandlePragmaMSPointersToMembers();
2778 continue;
2779 }
2780
Warren Huntc3b18962014-04-08 22:30:47 +00002781 if (Tok.is(tok::annot_pragma_ms_pragma)) {
2782 HandlePragmaMSPragma();
2783 continue;
2784 }
2785
Richard Smithda35e962013-11-09 04:52:51 +00002786 // If we see a namespace here, a close brace was missing somewhere.
2787 if (Tok.is(tok::kw_namespace)) {
Richard Smith2ac43ad2013-11-15 23:00:02 +00002788 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
Richard Smithda35e962013-11-09 04:52:51 +00002789 break;
2790 }
2791
Douglas Gregor9377c822010-06-21 22:31:09 +00002792 AccessSpecifier AS = getAccessSpecifierIfPresent();
2793 if (AS != AS_none) {
2794 // Current token is a C++ access specifier.
2795 CurAS = AS;
2796 SourceLocation ASLoc = Tok.getLocation();
David Blaikieeba32c22011-10-13 06:08:43 +00002797 unsigned TokLength = Tok.getLength();
Douglas Gregor9377c822010-06-21 22:31:09 +00002798 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002799 AccessAttrs.clear();
2800 MaybeParseGNUAttributes(AccessAttrs);
2801
David Blaikieeba32c22011-10-13 06:08:43 +00002802 SourceLocation EndLoc;
Alp Toker35d87032013-12-30 23:29:50 +00002803 if (TryConsumeToken(tok::colon, EndLoc)) {
2804 } else if (TryConsumeToken(tok::semi, EndLoc)) {
2805 Diag(EndLoc, diag::err_expected)
2806 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002807 } else {
2808 EndLoc = ASLoc.getLocWithOffset(TokLength);
Alp Toker35d87032013-12-30 23:29:50 +00002809 Diag(EndLoc, diag::err_expected)
2810 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002811 }
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002812
John McCalldb632ac2012-09-25 07:32:39 +00002813 // The Microsoft extension __interface does not permit non-public
2814 // access specifiers.
2815 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2816 Diag(ASLoc, diag::err_access_specifier_interface)
2817 << (CurAS == AS_protected);
2818 }
2819
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002820 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2821 AccessAttrs.getList())) {
2822 // found another attribute than only annotations
2823 AccessAttrs.clear();
2824 }
2825
Douglas Gregor9377c822010-06-21 22:31:09 +00002826 continue;
2827 }
2828
Douglas Gregor9377c822010-06-21 22:31:09 +00002829 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002830 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002831 }
2832
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002833 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00002834 } else {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002835 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002836 }
Mike Stump11289f42009-09-09 15:08:12 +00002837
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002838 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002839 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002840 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002841
John McCall08bede42010-05-28 08:11:17 +00002842 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002843 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002844 T.getOpenLocation(),
2845 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002846 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002847
Douglas Gregor433e0532012-04-16 18:27:27 +00002848 // C++11 [class.mem]p2:
2849 // Within the class member-specification, the class is regarded as complete
Richard Smith2331bbf2012-05-02 22:22:32 +00002850 // within function bodies, default arguments, and
Douglas Gregor433e0532012-04-16 18:27:27 +00002851 // brace-or-equal-initializers for non-static data members (including such
2852 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00002853 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002854 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00002855 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002856 // declarations and the lexed inline method definitions, along with any
2857 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00002858 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002859 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002860 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00002861
2862 // We've finished with all pending member declarations.
2863 Actions.ActOnFinishCXXMemberDecls();
2864
Richard Smith938f40b2011-06-11 17:19:42 +00002865 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002866 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00002867 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002868 }
2869
John McCall08bede42010-05-28 08:11:17 +00002870 if (TagDecl)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002871 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2872 T.getCloseLocation());
John McCall2ff380a2010-03-17 00:38:33 +00002873
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002874 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002875 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002876 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002877}
Douglas Gregore8381c02008-11-05 04:29:56 +00002878
Richard Smith2ac43ad2013-11-15 23:00:02 +00002879void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithda35e962013-11-09 04:52:51 +00002880 assert(Tok.is(tok::kw_namespace));
2881
2882 // FIXME: Suggest where the close brace should have gone by looking
2883 // at indentation changes within the definition body.
Richard Smith2ac43ad2013-11-15 23:00:02 +00002884 Diag(D->getLocation(),
2885 diag::err_missing_end_of_definition) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002886 Diag(Tok.getLocation(),
Richard Smith2ac43ad2013-11-15 23:00:02 +00002887 diag::note_missing_end_of_definition_before) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002888
2889 // Push '};' onto the token stream to recover.
2890 PP.EnterToken(Tok);
2891
2892 Tok.startToken();
2893 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
2894 Tok.setKind(tok::semi);
2895 PP.EnterToken(Tok);
2896
2897 Tok.setKind(tok::r_brace);
2898}
2899
Douglas Gregore8381c02008-11-05 04:29:56 +00002900/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2901/// which explicitly initializes the members or base classes of a
2902/// class (C++ [class.base.init]). For example, the three initializers
2903/// after the ':' in the Derived constructor below:
2904///
2905/// @code
2906/// class Base { };
2907/// class Derived : Base {
2908/// int x;
2909/// float f;
2910/// public:
2911/// Derived(float f) : Base(), x(17), f(f) { }
2912/// };
2913/// @endcode
2914///
Mike Stump11289f42009-09-09 15:08:12 +00002915/// [C++] ctor-initializer:
2916/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00002917///
Mike Stump11289f42009-09-09 15:08:12 +00002918/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00002919/// mem-initializer ...[opt]
2920/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00002921void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00002922 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2923
John Wiegley1c0675e2011-04-28 01:08:34 +00002924 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2925 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00002926 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002927
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002928 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002929 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002930
Douglas Gregore8381c02008-11-05 04:29:56 +00002931 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002932 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00002933 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2934 MemInitializers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002935 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002936 } else {
2937 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2938 if (!MemInit.isInvalid())
2939 MemInitializers.push_back(MemInit.get());
2940 else
2941 AnyErrors = true;
2942 }
2943
Douglas Gregore8381c02008-11-05 04:29:56 +00002944 if (Tok.is(tok::comma))
2945 ConsumeToken();
2946 else if (Tok.is(tok::l_brace))
2947 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00002948 // If the next token looks like a base or member initializer, assume that
2949 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00002950 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2951 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2952 Diag(Loc, diag::err_ctor_init_missing_comma)
2953 << FixItHint::CreateInsertion(Loc, ", ");
2954 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00002955 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alp Tokerec543272013-12-24 09:48:30 +00002956 Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
2957 << tok::comma;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002958 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregore8381c02008-11-05 04:29:56 +00002959 break;
2960 }
2961 } while (true);
2962
David Blaikie3fc2f912013-01-17 05:26:25 +00002963 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002964 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00002965}
2966
2967/// ParseMemInitializer - Parse a C++ member initializer, which is
2968/// part of a constructor initializer that explicitly initializes one
2969/// member or base class (C++ [class.base.init]). See
2970/// ParseConstructorInitializer for an example.
2971///
2972/// [C++] mem-initializer:
2973/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002974/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00002975///
Douglas Gregore8381c02008-11-05 04:29:56 +00002976/// [C++] mem-initializer-id:
2977/// '::'[opt] nested-name-specifier[opt] class-name
2978/// identifier
Craig Topper9ad7e262014-10-31 06:57:07 +00002979MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00002980 // parse '::'[opt] nested-name-specifier[opt]
2981 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002982 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00002983 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002984 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002985 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00002986 if (TemplateId->Kind == TNK_Type_template ||
2987 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00002988 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002989 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00002990 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002991 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002992 }
David Blaikie186a8892012-01-24 06:03:59 +00002993 // Uses of decltype will already have been converted to annot_decltype by
2994 // ParseOptionalCXXScopeSpecifier at this point.
2995 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2996 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002997 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00002998 return true;
2999 }
Mike Stump11289f42009-09-09 15:08:12 +00003000
Craig Topper161e4db2014-05-21 06:02:52 +00003001 IdentifierInfo *II = nullptr;
David Blaikie186a8892012-01-24 06:03:59 +00003002 DeclSpec DS(AttrFactory);
3003 SourceLocation IdLoc = Tok.getLocation();
3004 if (Tok.is(tok::annot_decltype)) {
3005 // Get the decltype expression, if there is one.
3006 ParseDecltypeSpecifier(DS);
3007 } else {
3008 if (Tok.is(tok::identifier))
3009 // Get the identifier. This may be a member name or a class name,
3010 // but we'll let the semantic analysis determine which it is.
3011 II = Tok.getIdentifierInfo();
3012 ConsumeToken();
3013 }
3014
Douglas Gregore8381c02008-11-05 04:29:56 +00003015
3016 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003017 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003018 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3019
Sebastian Redla74948d2011-09-24 17:48:25 +00003020 ExprResult InitList = ParseBraceInitializer();
3021 if (InitList.isInvalid())
3022 return true;
3023
3024 SourceLocation EllipsisLoc;
Alp Toker094e5212014-01-05 03:27:11 +00003025 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redla74948d2011-09-24 17:48:25 +00003026
3027 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00003028 TemplateTypeTy, DS, IdLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003029 InitList.get(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00003030 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003031 BalancedDelimiterTracker T(*this, tok::l_paren);
3032 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00003033
Sebastian Redl3da34892011-06-05 12:23:16 +00003034 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00003035 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00003036 CommaLocsTy CommaLocs;
3037 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00003038 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00003039 return true;
3040 }
3041
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003042 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00003043
3044 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00003045 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00003046
3047 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00003048 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00003049 T.getOpenLocation(), ArgExprs,
3050 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00003051 }
3052
Alp Tokerec543272013-12-24 09:48:30 +00003053 if (getLangOpts().CPlusPlus11)
3054 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
3055 else
3056 return Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregore8381c02008-11-05 04:29:56 +00003057}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003058
Sebastian Redl965b0e32011-03-05 14:45:16 +00003059/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003060///
Douglas Gregor356513d2008-12-01 18:00:20 +00003061/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00003062/// dynamic-exception-specification
3063/// noexcept-specification
3064///
3065/// noexcept-specification:
3066/// 'noexcept'
3067/// 'noexcept' '(' constant-expression ')'
3068ExceptionSpecificationType
Richard Smith2331bbf2012-05-02 22:22:32 +00003069Parser::tryParseExceptionSpecification(
Douglas Gregor433e0532012-04-16 18:27:27 +00003070 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003071 SmallVectorImpl<ParsedType> &DynamicExceptions,
3072 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00003073 ExprResult &NoexceptExpr) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003074 ExceptionSpecificationType Result = EST_None;
3075
3076 // See if there's a dynamic specification.
3077 if (Tok.is(tok::kw_throw)) {
3078 Result = ParseDynamicExceptionSpecification(SpecificationRange,
3079 DynamicExceptions,
3080 DynamicExceptionRanges);
3081 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
3082 "Produced different number of exception types and ranges.");
3083 }
3084
3085 // If there's no noexcept specification, we're done.
3086 if (Tok.isNot(tok::kw_noexcept))
3087 return Result;
3088
Richard Smithb15c11c2011-10-17 23:06:20 +00003089 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3090
Sebastian Redl965b0e32011-03-05 14:45:16 +00003091 // If we already had a dynamic specification, parse the noexcept for,
3092 // recovery, but emit a diagnostic and don't store the results.
3093 SourceRange NoexceptRange;
3094 ExceptionSpecificationType NoexceptType = EST_None;
3095
3096 SourceLocation KeywordLoc = ConsumeToken();
3097 if (Tok.is(tok::l_paren)) {
3098 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003099 BalancedDelimiterTracker T(*this, tok::l_paren);
3100 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003101 NoexceptType = EST_ComputedNoexcept;
3102 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003103 // The argument must be contextually convertible to bool. We use
3104 // ActOnBooleanCondition for this purpose.
3105 if (!NoexceptExpr.isInvalid())
3106 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
3107 NoexceptExpr.get());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003108 T.consumeClose();
3109 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl965b0e32011-03-05 14:45:16 +00003110 } else {
3111 // There is no argument.
3112 NoexceptType = EST_BasicNoexcept;
3113 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
3114 }
3115
3116 if (Result == EST_None) {
3117 SpecificationRange = NoexceptRange;
3118 Result = NoexceptType;
3119
3120 // If there's a dynamic specification after a noexcept specification,
3121 // parse that and ignore the results.
3122 if (Tok.is(tok::kw_throw)) {
3123 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3124 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
3125 DynamicExceptionRanges);
3126 }
3127 } else {
3128 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
3129 }
3130
3131 return Result;
3132}
3133
Richard Smith8ca78a12013-06-13 02:02:51 +00003134static void diagnoseDynamicExceptionSpecification(
3135 Parser &P, const SourceRange &Range, bool IsNoexcept) {
3136 if (P.getLangOpts().CPlusPlus11) {
3137 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
3138 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
3139 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
3140 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
3141 }
3142}
3143
Sebastian Redl965b0e32011-03-05 14:45:16 +00003144/// ParseDynamicExceptionSpecification - Parse a C++
3145/// dynamic-exception-specification (C++ [except.spec]).
3146///
3147/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00003148/// 'throw' '(' type-id-list [opt] ')'
3149/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00003150///
Douglas Gregor356513d2008-12-01 18:00:20 +00003151/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00003152/// type-id ... [opt]
3153/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003154///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003155ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
3156 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003157 SmallVectorImpl<ParsedType> &Exceptions,
3158 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003159 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00003160
Sebastian Redl965b0e32011-03-05 14:45:16 +00003161 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003162 BalancedDelimiterTracker T(*this, tok::l_paren);
3163 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003164 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3165 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003166 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003167 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003168
Douglas Gregor356513d2008-12-01 18:00:20 +00003169 // Parse throw(...), a Microsoft extension that means "this function
3170 // can throw anything".
3171 if (Tok.is(tok::ellipsis)) {
3172 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003173 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00003174 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003175 T.consumeClose();
3176 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003177 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003178 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00003179 }
3180
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003181 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00003182 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003183 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00003184 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00003185
Douglas Gregor830837d2010-12-20 23:57:46 +00003186 if (Tok.is(tok::ellipsis)) {
3187 // C++0x [temp.variadic]p5:
3188 // - In a dynamic-exception-specification (15.4); the pattern is a
3189 // type-id.
3190 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003191 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00003192 if (!Res.isInvalid())
3193 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3194 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003195
Sebastian Redld6434562009-05-29 18:02:33 +00003196 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003197 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00003198 Ranges.push_back(Range);
3199 }
Alp Toker97650562014-01-10 11:19:30 +00003200
3201 if (!TryConsumeToken(tok::comma))
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003202 break;
3203 }
3204
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003205 T.consumeClose();
3206 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003207 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3208 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003209 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003210}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003211
Douglas Gregor7fb25412010-10-01 18:44:50 +00003212/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3213/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003214TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00003215 assert(Tok.is(tok::arrow) && "expected arrow");
3216
3217 ConsumeToken();
3218
Richard Smithbfdb1082012-03-12 08:56:40 +00003219 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003220}
3221
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003222/// \brief We have just started parsing the definition of a new class,
3223/// so push that class onto our stack of classes that is currently
3224/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003225Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003226Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3227 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003228 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003229 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003230 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003231 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003232}
3233
3234/// \brief Deallocate the given parsed class and all of its nested
3235/// classes.
3236void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003237 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3238 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003239 delete Class;
3240}
3241
3242/// \brief Pop the top class of the stack of classes that are
3243/// currently being parsed.
3244///
3245/// This routine should be called when we have finished parsing the
3246/// definition of a class, but have not yet popped the Scope
3247/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003248void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003249 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003250
John McCallc1465822011-02-14 07:13:47 +00003251 Actions.PopParsingClass(state);
3252
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003253 ParsingClass *Victim = ClassStack.top();
3254 ClassStack.pop();
3255 if (Victim->TopLevelClass) {
3256 // Deallocate all of the nested classes of this class,
3257 // recursively: we don't need to keep any of this information.
3258 DeallocateParsedClasses(Victim);
3259 return;
Mike Stump11289f42009-09-09 15:08:12 +00003260 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003261 assert(!ClassStack.empty() && "Missing top-level class?");
3262
Douglas Gregorefc46952010-10-12 16:25:54 +00003263 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003264 // The victim is a nested class, but we will not need to perform
3265 // any processing after the definition of this class since it has
3266 // no members whose handling was delayed. Therefore, we can just
3267 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003268 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003269 return;
3270 }
3271
3272 // This nested class has some members that will need to be processed
3273 // after the top-level class is completely defined. Therefore, add
3274 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003275 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003276 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003277 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003278}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003279
Richard Smith3dff2512012-04-10 03:25:07 +00003280/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3281///
3282/// \return the parsed identifier on success, and 0 if the next token is not an
3283/// attribute-token.
3284///
3285/// C++11 [dcl.attr.grammar]p3:
3286/// If a keyword or an alternative token that satisfies the syntactic
3287/// requirements of an identifier is contained in an attribute-token,
3288/// it is considered an identifier.
3289IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3290 switch (Tok.getKind()) {
3291 default:
3292 // Identifiers and keywords have identifier info attached.
3293 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3294 Loc = ConsumeToken();
3295 return II;
3296 }
Craig Topper161e4db2014-05-21 06:02:52 +00003297 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003298
3299 case tok::ampamp: // 'and'
3300 case tok::pipe: // 'bitor'
3301 case tok::pipepipe: // 'or'
3302 case tok::caret: // 'xor'
3303 case tok::tilde: // 'compl'
3304 case tok::amp: // 'bitand'
3305 case tok::ampequal: // 'and_eq'
3306 case tok::pipeequal: // 'or_eq'
3307 case tok::caretequal: // 'xor_eq'
3308 case tok::exclaim: // 'not'
3309 case tok::exclaimequal: // 'not_eq'
3310 // Alternative tokens do not have identifier info, but their spelling
3311 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003312 SmallString<8> SpellingBuf;
Richard Smith3dff2512012-04-10 03:25:07 +00003313 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003314 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003315 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003316 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003317 }
Craig Topper161e4db2014-05-21 06:02:52 +00003318 return nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003319 }
3320}
3321
Michael Han23214e52012-10-03 01:56:22 +00003322static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3323 IdentifierInfo *ScopeName) {
3324 switch (AttributeList::getKind(AttrName, ScopeName,
3325 AttributeList::AS_CXX11)) {
3326 case AttributeList::AT_CarriesDependency:
Aaron Ballman35f94212014-04-14 16:03:22 +00003327 case AttributeList::AT_Deprecated:
Michael Han23214e52012-10-03 01:56:22 +00003328 case AttributeList::AT_FallThrough:
Richard Smith10876ef2013-01-17 01:30:42 +00003329 case AttributeList::AT_CXX11NoReturn: {
Michael Han23214e52012-10-03 01:56:22 +00003330 return true;
3331 }
3332
3333 default:
3334 return false;
3335 }
3336}
3337
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003338/// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause.
3339///
3340/// [C++11] attribute-argument-clause:
3341/// '(' balanced-token-seq ')'
3342///
3343/// [C++11] balanced-token-seq:
3344/// balanced-token
3345/// balanced-token-seq balanced-token
3346///
3347/// [C++11] balanced-token:
3348/// '(' balanced-token-seq ')'
3349/// '[' balanced-token-seq ']'
3350/// '{' balanced-token-seq '}'
3351/// any token but '(', ')', '[', ']', '{', or '}'
3352bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName,
3353 SourceLocation AttrNameLoc,
3354 ParsedAttributes &Attrs,
3355 SourceLocation *EndLoc,
3356 IdentifierInfo *ScopeName,
3357 SourceLocation ScopeLoc) {
3358 assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
Aaron Ballman35f94212014-04-14 16:03:22 +00003359 SourceLocation LParenLoc = Tok.getLocation();
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003360
3361 // If the attribute isn't known, we will not attempt to parse any
3362 // arguments.
3363 if (!hasAttribute(AttrSyntax::CXX, ScopeName, AttrName,
3364 getTargetInfo().getTriple(), getLangOpts())) {
3365 // Eat the left paren, then skip to the ending right paren.
3366 ConsumeParen();
3367 SkipUntil(tok::r_paren);
3368 return false;
3369 }
3370
3371 if (ScopeName && ScopeName->getName() == "gnu")
3372 // GNU-scoped attributes have some special cases to handle GNU-specific
3373 // behaviors.
3374 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
Craig Topper161e4db2014-05-21 06:02:52 +00003375 ScopeLoc, AttributeList::AS_CXX11, nullptr);
Aaron Ballman35f94212014-04-14 16:03:22 +00003376 else {
3377 unsigned NumArgs =
3378 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
3379 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3380
3381 const AttributeList *Attr = Attrs.getList();
3382 if (Attr && IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) {
3383 // If the attribute is a standard or built-in attribute and we are
3384 // parsing an argument list, we need to determine whether this attribute
3385 // was allowed to have an argument list (such as [[deprecated]]), and how
3386 // many arguments were parsed (so we can diagnose on [[deprecated()]]).
Nikola Smiljanica9c45212014-05-28 11:19:43 +00003387 if (Attr->getMaxArgs() && !NumArgs) {
3388 // The attribute was allowed to have arguments, but none were provided
3389 // even though the attribute parsed successfully. This is an error.
3390 // FIXME: This is a good place for a fixit which removes the parens.
3391 Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
3392 return false;
3393 } else if (!Attr->getMaxArgs()) {
3394 // The attribute parsed successfully, but was not allowed to have any
3395 // arguments. It doesn't matter whether any were provided -- the
Aaron Ballman35f94212014-04-14 16:03:22 +00003396 // presence of the argument list (even if empty) is diagnosed.
3397 Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
3398 << AttrName;
3399 return false;
3400 }
3401 }
3402 }
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003403 return true;
3404}
3405
3406/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003407///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003408/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003409/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003410/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003411///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003412/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003413/// attribute[opt]
3414/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003415/// attribute '...'
3416/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003417///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003418/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003419/// attribute-token attribute-argument-clause[opt]
3420///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003421/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003422/// identifier
3423/// attribute-scoped-token
3424///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003425/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003426/// attribute-namespace '::' identifier
3427///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003428/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003429/// identifier
Richard Smith3dff2512012-04-10 03:25:07 +00003430void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003431 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003432 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003433 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003434 ParseAlignmentSpecifier(attrs, endLoc);
3435 return;
3436 }
3437
Alexis Hunt96d5c762009-11-21 08:43:09 +00003438 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003439 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003440
Richard Smithf679b5b2011-10-14 20:48:27 +00003441 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3442
Alexis Hunt96d5c762009-11-21 08:43:09 +00003443 ConsumeBracket();
3444 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003445
Richard Smith10876ef2013-01-17 01:30:42 +00003446 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3447
Richard Smith3dff2512012-04-10 03:25:07 +00003448 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003449 // attribute not present
Alp Toker97650562014-01-10 11:19:30 +00003450 if (TryConsumeToken(tok::comma))
Alexis Hunt96d5c762009-11-21 08:43:09 +00003451 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003452
Richard Smith3dff2512012-04-10 03:25:07 +00003453 SourceLocation ScopeLoc, AttrLoc;
Craig Topper161e4db2014-05-21 06:02:52 +00003454 IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr;
Richard Smith3dff2512012-04-10 03:25:07 +00003455
3456 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3457 if (!AttrName)
3458 // Break out to the "expected ']'" diagnostic.
3459 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003460
Alexis Hunt96d5c762009-11-21 08:43:09 +00003461 // scoped attribute
Alp Toker97650562014-01-10 11:19:30 +00003462 if (TryConsumeToken(tok::coloncolon)) {
Richard Smith3dff2512012-04-10 03:25:07 +00003463 ScopeName = AttrName;
3464 ScopeLoc = AttrLoc;
3465
3466 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3467 if (!AttrName) {
Alp Tokerec543272013-12-24 09:48:30 +00003468 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003469 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003470 continue;
3471 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003472 }
3473
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003474 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003475 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003476
Richard Smith10876ef2013-01-17 01:30:42 +00003477 if (StandardAttr &&
3478 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3479 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003480 << AttrName << SourceRange(SeenAttrs[AttrName]);
Richard Smith10876ef2013-01-17 01:30:42 +00003481
Michael Han23214e52012-10-03 01:56:22 +00003482 // Parse attribute arguments
Aaron Ballman35f94212014-04-14 16:03:22 +00003483 if (Tok.is(tok::l_paren))
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003484 AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3485 ScopeName, ScopeLoc);
Michael Han23214e52012-10-03 01:56:22 +00003486
3487 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003488 attrs.addNew(AttrName,
3489 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3490 AttrLoc),
Craig Topper161e4db2014-05-21 06:02:52 +00003491 ScopeName, ScopeLoc, nullptr, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003492
Alp Toker97650562014-01-10 11:19:30 +00003493 if (TryConsumeToken(tok::ellipsis))
Michael Han23214e52012-10-03 01:56:22 +00003494 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3495 << AttrName->getName();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003496 }
3497
Alp Toker383d2c42014-01-01 03:08:43 +00003498 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003499 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003500 if (endLoc)
3501 *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003502 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003503 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003504}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003505
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003506/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003507///
3508/// attribute-specifier-seq:
3509/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003510void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003511 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003512 assert(getLangOpts().CPlusPlus11);
3513
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003514 SourceLocation StartLoc = Tok.getLocation(), Loc;
3515 if (!endLoc)
3516 endLoc = &Loc;
3517
Douglas Gregor6f981002011-10-07 20:35:25 +00003518 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003519 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003520 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003521
3522 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003523}
3524
Richard Smithc2c8bb82013-10-15 01:34:54 +00003525void Parser::DiagnoseAndSkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00003526 // Start and end location of an attribute or an attribute list.
3527 SourceLocation StartLoc = Tok.getLocation();
Richard Smith955bf012014-06-19 11:42:00 +00003528 SourceLocation EndLoc = SkipCXX11Attributes();
3529
3530 if (EndLoc.isValid()) {
3531 SourceRange Range(StartLoc, EndLoc);
3532 Diag(StartLoc, diag::err_attributes_not_allowed)
3533 << Range;
3534 }
3535}
3536
3537SourceLocation Parser::SkipCXX11Attributes() {
Richard Smithc2c8bb82013-10-15 01:34:54 +00003538 SourceLocation EndLoc;
3539
Richard Smith955bf012014-06-19 11:42:00 +00003540 if (!isCXX11AttributeSpecifier())
3541 return EndLoc;
3542
Richard Smithc2c8bb82013-10-15 01:34:54 +00003543 do {
3544 if (Tok.is(tok::l_square)) {
3545 BalancedDelimiterTracker T(*this, tok::l_square);
3546 T.consumeOpen();
3547 T.skipToEnd();
3548 EndLoc = T.getCloseLocation();
3549 } else {
3550 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3551 ConsumeToken();
3552 BalancedDelimiterTracker T(*this, tok::l_paren);
3553 if (!T.consumeOpen())
3554 T.skipToEnd();
3555 EndLoc = T.getCloseLocation();
3556 }
3557 } while (isCXX11AttributeSpecifier());
3558
Richard Smith955bf012014-06-19 11:42:00 +00003559 return EndLoc;
Richard Smithc2c8bb82013-10-15 01:34:54 +00003560}
3561
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003562/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3563///
3564/// [MS] ms-attribute:
3565/// '[' token-seq ']'
3566///
3567/// [MS] ms-attribute-seq:
3568/// ms-attribute[opt]
3569/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00003570void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3571 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003572 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3573
3574 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003575 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003576 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003577 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
John McCall53fa7142010-12-24 02:08:15 +00003578 if (endLoc) *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003579 ExpectAndConsume(tok::r_square);
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003580 }
3581}
Francois Pichet8f981d52011-05-25 10:19:49 +00003582
3583void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3584 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00003585 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00003586 if (ParseMicrosoftIfExistsCondition(Result))
3587 return;
3588
Douglas Gregor43edb322011-10-24 22:31:10 +00003589 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3590 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00003591 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet8f981d52011-05-25 10:19:49 +00003592 return;
3593 }
Francois Pichet8f981d52011-05-25 10:19:49 +00003594
Douglas Gregor43edb322011-10-24 22:31:10 +00003595 switch (Result.Behavior) {
3596 case IEB_Parse:
3597 // Parse the declarations below.
3598 break;
3599
3600 case IEB_Dependent:
3601 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3602 << Result.IsIfExists;
3603 // Fall through to skip.
3604
3605 case IEB_Skip:
3606 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00003607 return;
3608 }
3609
Richard Smith34f30512013-11-23 04:06:09 +00003610 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003611 // __if_exists, __if_not_exists can nest.
3612 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3613 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3614 continue;
3615 }
3616
3617 // Check for extraneous top-level semicolon.
3618 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003619 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00003620 continue;
3621 }
3622
3623 AccessSpecifier AS = getAccessSpecifierIfPresent();
3624 if (AS != AS_none) {
3625 // Current token is a C++ access specifier.
3626 CurAS = AS;
3627 SourceLocation ASLoc = Tok.getLocation();
3628 ConsumeToken();
3629 if (Tok.is(tok::colon))
3630 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3631 else
Alp Toker35d87032013-12-30 23:29:50 +00003632 Diag(Tok, diag::err_expected) << tok::colon;
Francois Pichet8f981d52011-05-25 10:19:49 +00003633 ConsumeToken();
3634 continue;
3635 }
3636
3637 // Parse all the comma separated declarators.
Craig Topper161e4db2014-05-21 06:02:52 +00003638 ParseCXXClassMemberDeclaration(CurAS, nullptr);
Francois Pichet8f981d52011-05-25 10:19:49 +00003639 }
Douglas Gregor43edb322011-10-24 22:31:10 +00003640
3641 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00003642}