blob: a04b33c6270f37bc1cfd097c907056aca617eab7 [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();
67 return 0;
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;
71 IdentifierInfo *Ident = 0;
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)) {
Nico Weber729f1e22012-10-27 23:44:27 +000096 if (Ident == 0) {
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);
100 return 0;
101 }
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
John McCall48871652010-08-21 09:40:31 +0000123 return 0;
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);
John McCall48871652010-08-21 09:40:31 +0000135 return 0;
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();
249 return 0;
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);
John McCall48871652010-08-21 09:40:31 +0000260 return 0;
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()
290 ? 0
291 : Actions.ActOnStartLinkageSpecification(
292 getCurScope(), DS.getSourceRange().getBegin(), Lang.take(),
293 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())
309 : 0;
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())
359 : 0;
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();
378 return 0;
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();
424 return 0;
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
Douglas Gregord7c4d982008-12-30 03:27:21 +0000431 IdentifierInfo *NamespcName = 0;
432 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?
John McCall48871652010-08-21 09:40:31 +0000440 return 0;
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
496 // Parse nested-name-specifier.
Richard Smith7447af42013-03-26 01:15:19 +0000497 IdentifierInfo *LastII = 0;
498 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
499 /*MayBePseudoDtor=*/0, /*IsTypename=*/false,
500 /*LastII=*/&LastII);
Douglas Gregorfec52632009-06-20 00:51:54 +0000501
Douglas Gregorfec52632009-06-20 00:51:54 +0000502 // Check nested-name specifier.
503 if (SS.isInvalid()) {
504 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000505 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000506 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000507
Richard Smith7447af42013-03-26 01:15:19 +0000508 SourceLocation TemplateKWLoc;
509 UnqualifiedId Name;
510
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000511 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000512 // destructor names and allow the action module to diagnose any semantic
513 // errors.
Richard Smith7447af42013-03-26 01:15:19 +0000514 //
515 // C++11 [class.qual]p2:
516 // [...] in a using-declaration that is a member-declaration, if the name
517 // specified after the nested-name-specifier is the same as the identifier
518 // or the simple-template-id's template-name in the last component of the
519 // nested-name-specifier, the name is [...] considered to name the
520 // constructor.
521 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
522 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
523 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
524 !SS.getScopeRep()->getAsNamespace() &&
525 !SS.getScopeRep()->getAsNamespaceAlias()) {
526 SourceLocation IdLoc = ConsumeToken();
527 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
528 Name.setConstructorName(Type, IdLoc, IdLoc);
529 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
530 /*AllowDestructorName=*/ true,
531 /*AllowConstructorName=*/ true, ParsedType(),
532 TemplateKWLoc, Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000533 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000534 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000535 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000536
Richard Smithc2c8bb82013-10-15 01:34:54 +0000537 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith37a45dd2013-10-24 01:21:09 +0000538 MaybeParseGNUAttributes(Attrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000539 MaybeParseCXX11Attributes(Attrs);
Richard Smithdda56e42011-04-15 14:24:37 +0000540
541 // Maybe this is an alias-declaration.
Richard Smithdda56e42011-04-15 14:24:37 +0000542 TypeResult TypeAlias;
Richard Smithc2c8bb82013-10-15 01:34:54 +0000543 bool IsAliasDecl = Tok.is(tok::equal);
Richard Smithdda56e42011-04-15 14:24:37 +0000544 if (IsAliasDecl) {
Richard Smithc2c8bb82013-10-15 01:34:54 +0000545 // If we had any misplaced attributes from earlier, this is where they
546 // should have been written.
547 if (MisplacedAttrs.Range.isValid()) {
548 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
549 << FixItHint::CreateInsertionFromRange(
550 Tok.getLocation(),
551 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
552 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
553 Attrs.takeAllFrom(MisplacedAttrs);
554 }
555
Richard Smithdda56e42011-04-15 14:24:37 +0000556 ConsumeToken();
557
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000558 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000559 diag::warn_cxx98_compat_alias_declaration :
560 diag::ext_alias_declaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000561
Richard Smith3f1b5d02011-05-05 21:57:07 +0000562 // Type alias templates cannot be specialized.
563 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000564 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
565 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000566 SpecKind = 0;
567 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
568 SpecKind = 1;
569 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
570 SpecKind = 2;
571 if (SpecKind != -1) {
572 SourceRange Range;
573 if (SpecKind == 0)
574 Range = SourceRange(Name.TemplateId->LAngleLoc,
575 Name.TemplateId->RAngleLoc);
576 else
577 Range = TemplateInfo.getSourceRange();
578 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
579 << SpecKind << Range;
580 SkipUntil(tok::semi);
581 return 0;
582 }
583
Richard Smithdda56e42011-04-15 14:24:37 +0000584 // Name must be an identifier.
585 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
586 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
587 // No removal fixit: can't recover from this.
588 SkipUntil(tok::semi);
589 return 0;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000590 } else if (HasTypenameKeyword)
Richard Smithdda56e42011-04-15 14:24:37 +0000591 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
592 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
593 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
594 else if (SS.isNotEmpty())
595 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
596 << FixItHint::CreateRemoval(SS.getRange());
597
Richard Smith3f1b5d02011-05-05 21:57:07 +0000598 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
599 Declarator::AliasTemplateContext :
Richard Smith54ecd982013-02-20 19:22:51 +0000600 Declarator::AliasDeclContext, AS, OwnedType,
601 &Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000602 } else {
603 // C++11 attributes are not allowed on a using-declaration, but GNU ones
604 // are.
Richard Smithc2c8bb82013-10-15 01:34:54 +0000605 ProhibitAttributes(MisplacedAttrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000606 ProhibitAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000607
Richard Smithdda56e42011-04-15 14:24:37 +0000608 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith54ecd982013-02-20 19:22:51 +0000609 MaybeParseGNUAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000610 }
Mike Stump11289f42009-09-09 15:08:12 +0000611
Douglas Gregorfec52632009-06-20 00:51:54 +0000612 // Eat ';'.
613 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000614 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
615 !Attrs.empty() ? "attributes list"
616 : IsAliasDecl ? "alias declaration"
617 : "using declaration"))
618 SkipUntil(tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000619
John McCall9b72f892010-11-10 02:40:36 +0000620 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000621 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000622 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000623 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-11-10 02:40:36 +0000624 SourceRange R = TemplateInfo.getSourceRange();
625 Diag(UsingLoc, diag::err_templated_using_declaration)
626 << R << FixItHint::CreateRemoval(R);
627
628 // Unfortunately, we have to bail out instead of recovering by
629 // ignoring the parameters, just in case the nested name specifier
630 // depends on the parameters.
631 return 0;
632 }
633
Douglas Gregor882a61a2011-09-26 14:30:28 +0000634 // "typename" keyword is allowed for identifiers only,
635 // because it may be a type definition.
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000636 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor882a61a2011-09-26 14:30:28 +0000637 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
638 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000639 // Proceed parsing, but reset the HasTypenameKeyword flag.
640 HasTypenameKeyword = false;
Douglas Gregor882a61a2011-09-26 14:30:28 +0000641 }
642
Richard Smith3f1b5d02011-05-05 21:57:07 +0000643 if (IsAliasDecl) {
644 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000645 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3f1b5d02011-05-05 21:57:07 +0000646 TemplateParams ? TemplateParams->data() : 0,
647 TemplateParams ? TemplateParams->size() : 0);
648 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith54ecd982013-02-20 19:22:51 +0000649 UsingLoc, Name, Attrs.getList(),
650 TypeAlias);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000651 }
Richard Smithdda56e42011-04-15 14:24:37 +0000652
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000653 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
654 /* HasUsingKeyword */ true, UsingLoc,
655 SS, Name, Attrs.getList(),
656 HasTypenameKeyword, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000657}
658
Benjamin Kramere56f3932011-12-23 17:00:35 +0000659/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000660///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000661/// [C++0x] static_assert-declaration:
662/// static_assert ( constant-expression , string-literal ) ;
663///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000664/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000665/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000666///
John McCall48871652010-08-21 09:40:31 +0000667Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000668 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
669 "Not a static_assert declaration");
670
David Blaikiebbafb8a2012-03-11 07:00:24 +0000671 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000672 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000673 if (Tok.is(tok::kw_static_assert))
674 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000675
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000676 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000677
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000678 BalancedDelimiterTracker T(*this, tok::l_paren);
679 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000680 Diag(Tok, diag::err_expected) << tok::l_paren;
Richard Smith76965712012-09-13 19:12:50 +0000681 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000682 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000683 }
Mike Stump11289f42009-09-09 15:08:12 +0000684
John McCalldadc5752010-08-24 06:29:42 +0000685 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000686 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000687 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000688 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000689 }
Mike Stump11289f42009-09-09 15:08:12 +0000690
Alp Toker383d2c42014-01-01 03:08:43 +0000691 if (ExpectAndConsume(tok::comma)) {
692 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000693 return 0;
Alp Toker383d2c42014-01-01 03:08:43 +0000694 }
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000695
Richard Smithf506eaf2012-03-05 23:20:05 +0000696 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000697 Diag(Tok, diag::err_expected_string_literal)
698 << /*Source='static_assert'*/1;
Richard Smith76965712012-09-13 19:12:50 +0000699 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000700 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000701 }
Mike Stump11289f42009-09-09 15:08:12 +0000702
John McCalldadc5752010-08-24 06:29:42 +0000703 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smithd67aea22012-03-06 03:21:47 +0000704 if (AssertMessage.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000705 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000706 return 0;
Richard Smithd67aea22012-03-06 03:21:47 +0000707 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000708
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000709 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000710
Chris Lattner49836b42009-04-02 04:16:50 +0000711 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000712 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000713
John McCallb268a282010-08-23 23:25:46 +0000714 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
715 AssertExpr.take(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000716 AssertMessage.take(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000717 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000718}
719
Richard Smith74aeef52013-04-26 16:15:35 +0000720/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000721///
722/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000723/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000724///
David Blaikie15a430a2011-12-04 05:04:18 +0000725SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
726 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
727 && "Not a decltype specifier");
728
David Blaikie15a430a2011-12-04 05:04:18 +0000729 ExprResult Result;
730 SourceLocation StartLoc = Tok.getLocation();
731 SourceLocation EndLoc;
732
733 if (Tok.is(tok::annot_decltype)) {
734 Result = getExprAnnotation(Tok);
735 EndLoc = Tok.getAnnotationEndLoc();
736 ConsumeToken();
737 if (Result.isInvalid()) {
738 DS.SetTypeSpecError();
739 return EndLoc;
740 }
741 } else {
Richard Smith324df552012-02-24 22:30:04 +0000742 if (Tok.getIdentifierInfo()->isStr("decltype"))
743 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000744
David Blaikie15a430a2011-12-04 05:04:18 +0000745 ConsumeToken();
746
747 BalancedDelimiterTracker T(*this, tok::l_paren);
748 if (T.expectAndConsume(diag::err_expected_lparen_after,
749 "decltype", tok::r_paren)) {
750 DS.SetTypeSpecError();
751 return T.getOpenLocation() == Tok.getLocation() ?
752 StartLoc : T.getOpenLocation();
753 }
754
Richard Smith74aeef52013-04-26 16:15:35 +0000755 // Check for C++1y 'decltype(auto)'.
756 if (Tok.is(tok::kw_auto)) {
757 // No need to disambiguate here: an expression can't start with 'auto',
758 // because the typename-specifier in a function-style cast operation can't
759 // be 'auto'.
760 Diag(Tok.getLocation(),
761 getLangOpts().CPlusPlus1y
762 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
763 : diag::ext_decltype_auto_type_specifier);
764 ConsumeToken();
765 } else {
766 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000767
Richard Smith74aeef52013-04-26 16:15:35 +0000768 // C++11 [dcl.type.simple]p4:
769 // The operand of the decltype specifier is an unevaluated operand.
770 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
771 0, /*IsDecltype=*/true);
772 Result = ParseExpression();
773 if (Result.isInvalid()) {
774 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000775 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smith74aeef52013-04-26 16:15:35 +0000776 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000777 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000778 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
779 // Backtrack to get the location of the last token before the semi.
780 PP.RevertCachedTokens(2);
781 ConsumeToken(); // the semi.
782 EndLoc = ConsumeAnyToken();
783 assert(Tok.is(tok::semi));
784 } else {
785 EndLoc = Tok.getLocation();
786 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000787 }
Richard Smith74aeef52013-04-26 16:15:35 +0000788 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000789 }
Richard Smith74aeef52013-04-26 16:15:35 +0000790
791 Result = Actions.ActOnDecltypeExpression(Result.take());
David Blaikie15a430a2011-12-04 05:04:18 +0000792 }
793
794 // Match the ')'
795 T.consumeClose();
796 if (T.getCloseLocation().isInvalid()) {
797 DS.SetTypeSpecError();
798 // FIXME: this should return the location of the last token
799 // that was consumed (by "consumeClose()")
800 return T.getCloseLocation();
801 }
802
Richard Smithfd555f62012-02-22 02:04:18 +0000803 if (Result.isInvalid()) {
804 DS.SetTypeSpecError();
805 return T.getCloseLocation();
806 }
807
David Blaikie15a430a2011-12-04 05:04:18 +0000808 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000809 }
Richard Smith74aeef52013-04-26 16:15:35 +0000810 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000811
Anders Carlsson74948d02009-06-24 17:47:40 +0000812 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000813 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000814 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Anders Carlsson74948d02009-06-24 17:47:40 +0000815 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000816 if (Result.get()
817 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000818 DiagID, Result.release(), Policy)
Richard Smith74aeef52013-04-26 16:15:35 +0000819 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000820 DiagID, Policy)) {
John McCall49bfce42009-08-03 20:12:06 +0000821 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000822 DS.SetTypeSpecError();
823 }
824 return EndLoc;
825}
826
827void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
828 SourceLocation StartLoc,
829 SourceLocation EndLoc) {
830 // make sure we have a token we can turn into an annotation token
831 if (PP.isBacktrackEnabled())
832 PP.RevertCachedTokens(1);
833 else
834 PP.EnterToken(Tok);
835
836 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000837 setExprAnnotation(Tok,
838 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
839 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
840 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000841 Tok.setAnnotationEndLoc(EndLoc);
842 Tok.setLocation(StartLoc);
843 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000844}
845
Alexis Hunt4a257072011-05-19 05:37:45 +0000846void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
847 assert(Tok.is(tok::kw___underlying_type) &&
848 "Not an underlying type specifier");
849
850 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000851 BalancedDelimiterTracker T(*this, tok::l_paren);
852 if (T.expectAndConsume(diag::err_expected_lparen_after,
853 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +0000854 return;
855 }
856
857 TypeResult Result = ParseTypeName();
858 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000859 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt4a257072011-05-19 05:37:45 +0000860 return;
861 }
862
863 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000864 T.consumeClose();
865 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000866 return;
867
868 const char *PrevSpec = 0;
869 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000870 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000871 DiagID, Result.release(),
872 Actions.getASTContext().getPrintingPolicy()))
Alexis Hunt4a257072011-05-19 05:37:45 +0000873 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanellaa90af722013-07-06 18:54:58 +0000874 DS.setTypeofParensRange(T.getRange());
Alexis Hunt4a257072011-05-19 05:37:45 +0000875}
876
David Blaikie00ee7a082011-10-25 15:01:20 +0000877/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
878/// class name or decltype-specifier. Note that we only check that the result
879/// names a type; semantic analysis will need to verify that the type names a
880/// class. The result is either a type or null, depending on whether a type
881/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +0000882///
Richard Smith4c96e992013-02-19 23:47:15 +0000883/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000884/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +0000885/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000886/// nested-name-specifier[opt] class-name
887/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +0000888/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +0000889/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000890/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000891///
Richard Smith4c96e992013-02-19 23:47:15 +0000892/// In C++98, instead of base-type-specifier, we have:
893///
894/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie1cd50022011-10-25 17:10:12 +0000895Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
896 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +0000897 // Ignore attempts to use typename
898 if (Tok.is(tok::kw_typename)) {
899 Diag(Tok, diag::err_expected_class_name_not_template)
900 << FixItHint::CreateRemoval(Tok.getLocation());
901 ConsumeToken();
902 }
903
David Blaikieafa155f2011-10-25 18:17:58 +0000904 // Parse optional nested-name-specifier
905 CXXScopeSpec SS;
906 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
907
908 BaseLoc = Tok.getLocation();
909
David Blaikie1cd50022011-10-25 17:10:12 +0000910 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +0000911 // tok == kw_decltype is just error recovery, it can only happen when SS
912 // isn't empty
913 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +0000914 if (SS.isNotEmpty())
915 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
916 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +0000917 // Fake up a Declarator to use with ActOnTypeName.
918 DeclSpec DS(AttrFactory);
919
David Blaikie7491e732011-12-08 04:53:15 +0000920 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +0000921
922 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
923 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
924 }
925
Douglas Gregord54dfb82009-02-25 23:52:28 +0000926 // Check whether we have a template-id that names a type.
927 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000928 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +0000929 if (TemplateId->Kind == TNK_Type_template ||
930 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000931 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000932
933 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000934 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000935 EndLocation = Tok.getAnnotationEndLoc();
936 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000937
938 if (Type)
939 return Type;
940 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000941 }
942
943 // Fall through to produce an error below.
944 }
945
Douglas Gregor831c93f2008-11-05 20:51:48 +0000946 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000947 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000948 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000949 }
950
Douglas Gregor18473f32010-01-12 21:28:44 +0000951 IdentifierInfo *Id = Tok.getIdentifierInfo();
952 SourceLocation IdLoc = ConsumeToken();
953
954 if (Tok.is(tok::less)) {
955 // It looks the user intended to write a template-id here, but the
956 // template-name was wrong. Try to fix that.
957 TemplateNameKind TNK = TNK_Type_template;
958 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000959 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000960 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000961 Diag(IdLoc, diag::err_unknown_template_name)
962 << Id;
963 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000964
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000965 if (!Template) {
966 TemplateArgList TemplateArgs;
967 SourceLocation LAngleLoc, RAngleLoc;
968 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
969 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor18473f32010-01-12 21:28:44 +0000970 return true;
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000971 }
Douglas Gregor18473f32010-01-12 21:28:44 +0000972
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000973 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000974 UnqualifiedId TemplateName;
975 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000976
Douglas Gregor18473f32010-01-12 21:28:44 +0000977 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +0000978 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
979 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +0000980 return true;
981 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +0000982 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000983
Douglas Gregor18473f32010-01-12 21:28:44 +0000984 // If we didn't end up with a typename token, there's nothing more we
985 // can do.
986 if (Tok.isNot(tok::annot_typename))
987 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000988
Douglas Gregor18473f32010-01-12 21:28:44 +0000989 // Retrieve the type from the annotation token, consume that token, and
990 // return.
991 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +0000992 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +0000993 ConsumeToken();
994 return Type;
995 }
996
Douglas Gregor831c93f2008-11-05 20:51:48 +0000997 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000998 IdentifierInfo *CorrectedII = 0;
Douglas Gregore7c20652011-03-02 00:47:37 +0000999 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +00001000 false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00001001 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +00001002 /*NonTrivialTypeSourceInfo=*/true,
1003 &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001004 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001005 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001006 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +00001007 }
1008
1009 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +00001010 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001011
1012 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +00001013 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +00001014 DS.SetRangeStart(IdLoc);
1015 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +00001016 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001017
1018 const char *PrevSpec = 0;
1019 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001020 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
1021 Actions.getASTContext().getPrintingPolicy());
Nick Lewycky19b9f952010-07-26 16:56:01 +00001022
1023 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1024 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001025}
1026
John McCall8d32c052012-05-22 21:28:12 +00001027void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
1028 while (Tok.is(tok::kw___single_inheritance) ||
1029 Tok.is(tok::kw___multiple_inheritance) ||
1030 Tok.is(tok::kw___virtual_inheritance)) {
1031 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1032 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +00001033 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
Aaron Ballman8edb5c22013-12-18 23:44:18 +00001034 AttributeList::AS_Keyword);
John McCall8d32c052012-05-22 21:28:12 +00001035 }
1036}
1037
Richard Smith369b9f92012-06-25 21:37:02 +00001038/// Determine whether the following tokens are valid after a type-specifier
1039/// which could be a standalone declaration. This will conservatively return
1040/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +00001041bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +00001042 // This switch enumerates the valid "follow" set for type-specifiers.
1043 switch (Tok.getKind()) {
1044 default: break;
1045 case tok::semi: // struct foo {...} ;
1046 case tok::star: // struct foo {...} * P;
1047 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +00001048 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +00001049 case tok::identifier: // struct foo {...} V ;
1050 case tok::r_paren: //(struct foo {...} ) {4}
1051 case tok::annot_cxxscope: // struct foo {...} a:: b;
1052 case tok::annot_typename: // struct foo {...} a ::b;
1053 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1054 case tok::l_paren: // struct foo {...} ( x);
1055 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001056 case tok::kw_operator: // struct foo operator ++() {...}
Alp Tokerd3f79c52013-11-24 20:24:54 +00001057 case tok::kw___declspec: // struct foo {...} __declspec(...)
Richard Smith369b9f92012-06-25 21:37:02 +00001058 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001059 case tok::colon:
1060 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smith369b9f92012-06-25 21:37:02 +00001061 // Type qualifiers
1062 case tok::kw_const: // struct foo {...} const x;
1063 case tok::kw_volatile: // struct foo {...} volatile x;
1064 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001065 // Function specifiers
1066 // Note, no 'explicit'. An explicit function must be either a conversion
1067 // operator or a constructor. Either way, it can't have a return type.
1068 case tok::kw_inline: // struct foo inline f();
1069 case tok::kw_virtual: // struct foo virtual f();
1070 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001071 // Storage-class specifiers
1072 case tok::kw_static: // struct foo {...} static x;
1073 case tok::kw_extern: // struct foo {...} extern x;
1074 case tok::kw_typedef: // struct foo {...} typedef x;
1075 case tok::kw_register: // struct foo {...} register x;
1076 case tok::kw_auto: // struct foo {...} auto x;
1077 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001078 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001079 case tok::kw_constexpr: // struct foo {...} constexpr x;
1080 // As shown above, type qualifiers and storage class specifiers absolutely
1081 // can occur after class specifiers according to the grammar. However,
1082 // almost no one actually writes code like this. If we see one of these,
1083 // it is much more likely that someone missed a semi colon and the
1084 // type/storage class specifier we're seeing is part of the *next*
1085 // intended declaration, as in:
1086 //
1087 // struct foo { ... }
1088 // typedef int X;
1089 //
1090 // We'd really like to emit a missing semicolon error instead of emitting
1091 // an error on the 'int' saying that you can't have two type specifiers in
1092 // the same declaration of X. Because of this, we look ahead past this
1093 // token to see if it's a type specifier. If so, we know the code is
1094 // otherwise invalid, so we can produce the expected semi error.
1095 if (!isKnownToBeTypeSpecifier(NextToken()))
1096 return true;
1097 break;
1098 case tok::r_brace: // struct bar { struct foo {...} }
1099 // Missing ';' at end of struct is accepted as an extension in C mode.
1100 if (!getLangOpts().CPlusPlus)
1101 return true;
1102 break;
Richard Smith1ac67d12013-01-19 03:48:05 +00001103 // C++11 attributes
1104 case tok::l_square: // enum E [[]] x
1105 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1106 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith52c5b872013-01-29 04:13:32 +00001107 case tok::greater:
1108 // template<class T = class X>
1109 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001110 }
1111 return false;
1112}
1113
Douglas Gregor556877c2008-04-13 21:30:24 +00001114/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1115/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1116/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001117/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001118///
1119/// class-specifier: [C++ class]
1120/// class-head '{' member-specification[opt] '}'
1121/// class-head '{' member-specification[opt] '}' attributes[opt]
1122/// class-head:
1123/// class-key identifier[opt] base-clause[opt]
1124/// class-key nested-name-specifier identifier base-clause[opt]
1125/// class-key nested-name-specifier[opt] simple-template-id
1126/// base-clause[opt]
1127/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001128/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001129/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001130/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001131/// simple-template-id base-clause[opt]
1132/// class-key:
1133/// 'class'
1134/// 'struct'
1135/// 'union'
1136///
1137/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001138/// class-key ::[opt] nested-name-specifier[opt] identifier
1139/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1140/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001141///
1142/// Note that the C++ class-specifier and elaborated-type-specifier,
1143/// together, subsume the C99 struct-or-union-specifier:
1144///
1145/// struct-or-union-specifier: [C99 6.7.2.1]
1146/// struct-or-union identifier[opt] '{' struct-contents '}'
1147/// struct-or-union identifier
1148/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1149/// '}' attributes[opt]
1150/// [GNU] struct-or-union attributes[opt] identifier
1151/// struct-or-union:
1152/// 'struct'
1153/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001154void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1155 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001156 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001157 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001158 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001159 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001160 DeclSpec::TST TagType;
1161 if (TagTokKind == tok::kw_struct)
1162 TagType = DeclSpec::TST_struct;
1163 else if (TagTokKind == tok::kw___interface)
1164 TagType = DeclSpec::TST_interface;
1165 else if (TagTokKind == tok::kw_class)
1166 TagType = DeclSpec::TST_class;
1167 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001168 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1169 TagType = DeclSpec::TST_union;
1170 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001171
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001172 if (Tok.is(tok::code_completion)) {
1173 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001174 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001175 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001176 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001177
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001178 // C++03 [temp.explicit] 14.7.2/8:
1179 // The usual access checking rules do not apply to names used to specify
1180 // explicit instantiations.
1181 //
1182 // As an extension we do not perform access checking on the names used to
1183 // specify explicit specializations either. This is important to allow
1184 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001185 //
1186 // Note that we don't suppress if this turns out to be an elaborated
1187 // type specifier.
1188 bool shouldDelayDiagsInTag =
1189 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1190 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1191 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001192
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001193 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001194 // If attributes exist after tag, parse them.
Richard Smith37a45dd2013-10-24 01:21:09 +00001195 MaybeParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +00001196
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001197 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +00001198 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +00001199 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001200
John McCall8d32c052012-05-22 21:28:12 +00001201 // Parse inheritance specifiers.
1202 if (Tok.is(tok::kw___single_inheritance) ||
1203 Tok.is(tok::kw___multiple_inheritance) ||
1204 Tok.is(tok::kw___virtual_inheritance))
Richard Smith37a45dd2013-10-24 01:21:09 +00001205 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCall8d32c052012-05-22 21:28:12 +00001206
Alexis Hunt96d5c762009-11-21 08:43:09 +00001207 // If C++0x attributes exist here, parse them.
1208 // FIXME: Are we consistent with the ordering of parsing of different
1209 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001210 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001211
Michael Han309af292013-01-07 16:57:11 +00001212 // Source location used by FIXIT to insert misplaced
1213 // C++11 attributes
1214 SourceLocation AttrFixitLoc = Tok.getLocation();
1215
Alp Toker53358e42013-12-17 14:12:30 +00001216 // GNU libstdc++ and libc++ use certain intrinsic names as the
1217 // name of struct templates, but some are keywords in GCC >= 4.3
1218 // MSVC and Clang. For compatibility, convert the token to an identifier
1219 // and issue a warning diagnostic.
1220 if (TagType == DeclSpec::TST_struct && !Tok.is(tok::identifier) &&
1221 !Tok.isAnnotation()) {
1222 const IdentifierInfo *II = Tok.getIdentifierInfo();
1223 // We rarely end up here so the following check is efficient.
1224 if (II && II->getName().startswith("__is_"))
1225 TryKeywordIdentFallback(true);
1226 }
Mike Stump11289f42009-09-09 15:08:12 +00001227
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001228 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001229 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001230 if (getLangOpts().CPlusPlus) {
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001231 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1232 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001233
Douglas Gregordf593fb2011-11-07 17:33:42 +00001234 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall413021a2010-07-30 06:26:29 +00001235 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +00001236 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001237 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Alp Tokerec543272013-12-24 09:48:30 +00001238 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001239 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001240
Douglas Gregor916462b2009-10-30 21:46:58 +00001241 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1242
Douglas Gregor67a65642009-02-17 23:15:12 +00001243 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +00001244 IdentifierInfo *Name = 0;
1245 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +00001246 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +00001247 if (Tok.is(tok::identifier)) {
1248 Name = Tok.getIdentifierInfo();
1249 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001250
David Blaikiebbafb8a2012-03-11 07:00:24 +00001251 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001252 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001253 // Eat the template argument list and try to continue parsing this as
1254 // a class (or template thereof).
1255 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001256 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +00001257 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +00001258 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001259 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001260 // We couldn't parse the template argument list at all, so don't
1261 // try to give any location information for the list.
1262 LAngleLoc = RAngleLoc = SourceLocation();
1263 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001264
Douglas Gregor916462b2009-10-30 21:46:58 +00001265 Diag(NameLoc, diag::err_explicit_spec_non_template)
Alp Toker01d65e12014-01-06 12:54:41 +00001266 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1267 << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
Joao Matose9a3ed42012-08-31 22:18:20 +00001268
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001269 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001270 // we've removed its template argument list.
1271 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1272 if (TemplateParams && TemplateParams->size() > 1) {
1273 TemplateParams->pop_back();
1274 } else {
1275 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001276 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001277 = ParsedTemplateInfo::NonTemplate;
1278 }
1279 } else if (TemplateInfo.Kind
1280 == ParsedTemplateInfo::ExplicitInstantiation) {
1281 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +00001282 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001283 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001284 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001285 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001286 = SourceLocation();
1287 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1288 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001289 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001290 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001291 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001292 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001293 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001294
Douglas Gregore7c20652011-03-02 00:47:37 +00001295 if (TemplateId->Kind != TNK_Type_template &&
1296 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001297 // The template-name in the simple-template-id refers to
1298 // something other than a class template. Give an appropriate
1299 // error message and skip to the ';'.
1300 SourceRange Range(NameLoc);
1301 if (SS.isNotEmpty())
1302 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001303
Richard Smith72bfbd82013-12-04 00:28:23 +00001304 // FIXME: Name may be null here.
Douglas Gregor7f741122009-02-25 19:37:18 +00001305 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001306 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001307
Douglas Gregor7f741122009-02-25 19:37:18 +00001308 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001309 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor7f741122009-02-25 19:37:18 +00001310 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001311 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001312 }
1313
Richard Smithbfdb1082012-03-12 08:56:40 +00001314 // There are four options here.
1315 // - If we are in a trailing return type, this is always just a reference,
1316 // and we must not try to parse a definition. For instance,
1317 // [] () -> struct S { };
1318 // does not define a type.
1319 // - If we have 'struct foo {...', 'struct foo :...',
1320 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1321 // - If we have 'struct foo;', then this is either a forward declaration
1322 // or a friend declaration, which have to be treated differently.
1323 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001324 //
1325 // We also detect these erroneous cases to provide better diagnostic for
1326 // C++11 attributes parsing.
1327 // - attributes follow class name:
1328 // struct foo [[]] {};
1329 // - attributes appear before or after 'final':
1330 // struct foo [[]] final [[]] {};
1331 //
Richard Smithc5b05522012-03-12 07:56:15 +00001332 // However, in type-specifier-seq's, things look like declarations but are
1333 // just references, e.g.
1334 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001335 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001336 // &T::operator struct s;
Richard Smith649c7b062014-01-08 00:56:48 +00001337 // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
Michael Han9407e502012-11-26 22:54:45 +00001338
1339 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001340 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001341
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001342 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
John McCallfaf5fb42010-08-26 23:41:50 +00001343 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001344 if (DSC == DSC_trailing)
1345 TUK = Sema::TUK_Reference;
1346 else if (Tok.is(tok::l_brace) ||
1347 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001348 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001349 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001350 if (DS.isFriendSpecified()) {
1351 // C++ [class.friend]p2:
1352 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001353 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001354 << SourceRange(DS.getFriendSpecLoc());
1355
1356 // Skip everything up to the semicolon, so that this looks like a proper
1357 // friend class (or template thereof) declaration.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001358 SkipUntil(tok::semi, StopBeforeMatch);
John McCallfaf5fb42010-08-26 23:41:50 +00001359 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001360 } else {
1361 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001362 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001363 }
Richard Smith434516c2013-02-22 06:46:23 +00001364 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1365 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001366 // We can't tell if this is a definition or reference
1367 // until we skipped the 'final' and C++11 attribute specifiers.
1368 TentativeParsingAction PA(*this);
1369
1370 // Skip the 'final' keyword.
1371 ConsumeToken();
1372
1373 // Skip C++11 attribute specifiers.
1374 while (true) {
1375 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1376 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001377 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001378 break;
Richard Smith434516c2013-02-22 06:46:23 +00001379 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001380 ConsumeToken();
1381 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001382 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001383 break;
1384 } else {
1385 break;
1386 }
1387 }
1388
1389 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1390 TUK = Sema::TUK_Definition;
1391 else
1392 TUK = Sema::TUK_Reference;
1393
1394 PA.Revert();
Richard Smith649c7b062014-01-08 00:56:48 +00001395 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00001396 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001397 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001398 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001399 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001400 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Joao Matose9a3ed42012-08-31 22:18:20 +00001401 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00001402 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001403 DeclSpec::getSpecifierName(TagType, PPol));
Joao Matose9a3ed42012-08-31 22:18:20 +00001404 PP.EnterToken(Tok);
1405 Tok.setKind(tok::semi);
1406 }
Richard Smith369b9f92012-06-25 21:37:02 +00001407 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001408 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001409
Michael Han9407e502012-11-26 22:54:45 +00001410 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1411 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001412 if (TUK != Sema::TUK_Reference) {
1413 // If this is not a reference, then the only possible
1414 // valid place for C++11 attributes to appear here
1415 // is between class-key and class-name. If there are
1416 // any attributes after class-name, we try a fixit to move
1417 // them to the right place.
1418 SourceRange AttrRange = Attributes.Range;
1419 if (AttrRange.isValid()) {
1420 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1421 << AttrRange
1422 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1423 CharSourceRange(AttrRange, true))
1424 << FixItHint::CreateRemoval(AttrRange);
1425
1426 // Recover by adding misplaced attributes to the attribute list
1427 // of the class so they can be applied on the class later.
1428 attrs.takeAllFrom(Attributes);
1429 }
1430 }
Michael Han9407e502012-11-26 22:54:45 +00001431
John McCall6347b682012-05-07 06:16:58 +00001432 // If this is an elaborated type specifier, and we delayed
1433 // diagnostics before, just merge them into the current pool.
1434 if (shouldDelayDiagsInTag) {
1435 diagsFromTag.done();
1436 if (TUK == Sema::TUK_Reference)
1437 diagsFromTag.redelay();
1438 }
1439
John McCall413021a2010-07-30 06:26:29 +00001440 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001441 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001442 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1443 // We have a declaration or reference to an anonymous class.
1444 Diag(StartLoc, diag::err_anon_type_definition)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001445 << DeclSpec::getSpecifierName(TagType, Policy);
John McCall413021a2010-07-30 06:26:29 +00001446 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001447
David Majnemer3252fd02013-12-05 01:36:53 +00001448 // If we are parsing a definition and stop at a base-clause, continue on
1449 // until the semicolon. Continuing from the comma will just trick us into
1450 // thinking we are seeing a variable declaration.
1451 if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1452 SkipUntil(tok::semi, StopBeforeMatch);
1453 else
1454 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor556877c2008-04-13 21:30:24 +00001455 return;
1456 }
1457
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001458 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001459 DeclResult TagOrTempResult = true; // invalid
1460 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001461
Douglas Gregord6ab8742009-05-28 23:31:59 +00001462 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +00001463 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001464 // Explicit specialization, class template partial specialization,
1465 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001466 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001467 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001468 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001469 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001470 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001471 ProhibitAttributes(attrs);
1472
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001473 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001474 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001475 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001476 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001477 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001478 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001479 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001480 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001481 TemplateId->TemplateNameLoc,
1482 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001483 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001484 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001485 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001486
1487 // Friend template-ids are treated as references unless
1488 // they have template headers, in which case they're ill-formed
1489 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1490 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001491 } else if (TUK == Sema::TUK_Reference ||
1492 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001493 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001494 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001495 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001496 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001497 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001498 TemplateId->Template,
1499 TemplateId->TemplateNameLoc,
1500 TemplateId->LAngleLoc,
1501 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001502 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001503 } else {
1504 // This is an explicit specialization or a class template
1505 // partial specialization.
1506 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001507 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1508 // This looks like an explicit instantiation, because we have
1509 // something like
1510 //
1511 // template class Foo<X>
1512 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001513 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001514 // meant to be an explicit specialization, but the user forgot
1515 // the '<>' after 'template'.
Richard Smith003c5e12013-11-08 19:03:29 +00001516 // It this is friend declaration however, since it cannot have a
1517 // template header, it is most likely that the user meant to
1518 // remove the 'template' keyword.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001519 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith003c5e12013-11-08 19:03:29 +00001520 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001521
Richard Smith003c5e12013-11-08 19:03:29 +00001522 if (TUK == Sema::TUK_Friend) {
1523 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
1524 TemplateParams = 0;
1525 } else {
1526 SourceLocation LAngleLoc =
1527 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1528 Diag(TemplateId->TemplateNameLoc,
1529 diag::err_explicit_instantiation_with_definition)
1530 << SourceRange(TemplateInfo.TemplateLoc)
1531 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1532
1533 // Create a fake template parameter list that contains only
1534 // "template<>", so that we treat this construct as a class
1535 // template specialization.
1536 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1537 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1538 LAngleLoc));
1539 TemplateParams = &FakedParamLists;
1540 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001541 }
1542
1543 // Build the class template specialization.
1544 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001545 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor3c7cd6a2011-09-09 20:53:38 +00001546 StartLoc, DS.getModulePrivateSpecLoc(), 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 Gregor7f741122009-02-25 19:37:18 +00001550 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001551 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001552 attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001553 MultiTemplateParamsArg(
Douglas Gregor67a65642009-02-17 23:15:12 +00001554 TemplateParams? &(*TemplateParams)[0] : 0,
1555 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001556 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001557 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001558 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001559 // Explicit instantiation of a member of a class template
1560 // specialization, e.g.,
1561 //
1562 // template struct Outer<int>::Inner;
1563 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001564 ProhibitAttributes(attrs);
1565
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001566 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001567 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001568 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001569 TemplateInfo.TemplateLoc,
1570 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001571 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001572 } else if (TUK == Sema::TUK_Friend &&
1573 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001574 ProhibitAttributes(attrs);
1575
John McCallace48cd2010-10-19 01:40:49 +00001576 TagOrTempResult =
1577 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1578 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001579 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001580 MultiTemplateParamsArg(
John McCallace48cd2010-10-19 01:40:49 +00001581 TemplateParams? &(*TemplateParams)[0] : 0,
1582 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001583 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001584 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1585 ProhibitAttributes(attrs);
Richard Smith003c5e12013-11-08 19:03:29 +00001586
Larisse Voufo725de3e2013-06-21 00:08:46 +00001587 if (TUK == Sema::TUK_Definition &&
1588 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1589 // If the declarator-id is not a template-id, issue a diagnostic and
1590 // recover by ignoring the 'template' keyword.
1591 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1592 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001593 TemplateParams = 0;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001594 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001595
John McCall7f41d982009-09-11 04:59:25 +00001596 bool IsDependent = false;
1597
John McCall32723e92010-10-19 18:40:57 +00001598 // Don't pass down template parameter lists if this is just a tag
1599 // reference. For example, we don't need the template parameters here:
1600 // template <class T> class A *makeA(T t);
1601 MultiTemplateParamsArg TParams;
1602 if (TUK != Sema::TUK_Reference && TemplateParams)
1603 TParams =
1604 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1605
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001606 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001607 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001608 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001609 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001610 TParams, Owned, IsDependent,
1611 SourceLocation(), false,
Richard Smith649c7b062014-01-08 00:56:48 +00001612 clang::TypeResult(),
1613 DSC == DSC_type_specifier);
John McCall7f41d982009-09-11 04:59:25 +00001614
1615 // If ActOnTag said the type was dependent, try again with the
1616 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001617 if (IsDependent) {
1618 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001619 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001620 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001621 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001622 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001623
Douglas Gregor556877c2008-04-13 21:30:24 +00001624 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001625 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001626 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001627 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001628 isCXX11FinalKeyword());
David Blaikiebbafb8a2012-03-11 07:00:24 +00001629 if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001630 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1631 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001632 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001633 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001634 }
1635
John McCallba7bf592010-08-24 05:47:05 +00001636 const char *PrevSpec = 0;
1637 unsigned DiagID;
1638 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001639 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001640 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1641 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001642 PrevSpec, DiagID, TypeResult.get(), Policy);
John McCall7f41d982009-09-11 04:59:25 +00001643 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001644 Result = DS.SetTypeSpecType(TagType, StartLoc,
1645 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001646 PrevSpec, DiagID, TagOrTempResult.get(), Owned,
1647 Policy);
John McCall7f41d982009-09-11 04:59:25 +00001648 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001649 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001650 return;
1651 }
Mike Stump11289f42009-09-09 15:08:12 +00001652
John McCallba7bf592010-08-24 05:47:05 +00001653 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001654 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001655
Chris Lattnercf251412010-02-02 01:23:29 +00001656 // At this point, we've successfully parsed a class-specifier in 'definition'
1657 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1658 // going to look at what comes after it to improve error recovery. If an
1659 // impossible token occurs next, we assume that the programmer forgot a ; at
1660 // the end of the declaration and recover that way.
1661 //
Richard Smith369b9f92012-06-25 21:37:02 +00001662 // Also enforce C++ [temp]p3:
1663 // In a template-declaration which defines a class, no declarator
1664 // is permitted.
Joao Matose9a3ed42012-08-31 22:18:20 +00001665 if (TUK == Sema::TUK_Definition &&
1666 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001667 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001668 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Alp Toker383d2c42014-01-01 03:08:43 +00001669 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001670 DeclSpec::getSpecifierName(TagType, PPol));
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001671 // Push this token back into the preprocessor and change our current token
1672 // to ';' so that the rest of the code recovers as though there were an
1673 // ';' after the definition.
1674 PP.EnterToken(Tok);
1675 Tok.setKind(tok::semi);
1676 }
Chris Lattnercf251412010-02-02 01:23:29 +00001677 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001678}
1679
Mike Stump11289f42009-09-09 15:08:12 +00001680/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001681///
1682/// base-clause : [C++ class.derived]
1683/// ':' base-specifier-list
1684/// base-specifier-list:
1685/// base-specifier '...'[opt]
1686/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001687void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001688 assert(Tok.is(tok::colon) && "Not a base clause");
1689 ConsumeToken();
1690
Douglas Gregor29a92472008-10-22 17:49:05 +00001691 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001692 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001693
Douglas Gregor556877c2008-04-13 21:30:24 +00001694 while (true) {
1695 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001696 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001697 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001698 // Skip the rest of this base specifier, up until the comma or
1699 // opening brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001700 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor29a92472008-10-22 17:49:05 +00001701 } else {
1702 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001703 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001704 }
1705
1706 // If the next token is a comma, consume it and keep reading
1707 // base-specifiers.
Alp Toker97650562014-01-10 11:19:30 +00001708 if (!TryConsumeToken(tok::comma))
1709 break;
Douglas Gregor556877c2008-04-13 21:30:24 +00001710 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001711
1712 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001713 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001714}
1715
1716/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1717/// one entry in the base class list of a class specifier, for example:
1718/// class foo : public bar, virtual private baz {
1719/// 'public bar' and 'virtual private baz' are each base-specifiers.
1720///
1721/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001722/// attribute-specifier-seq[opt] base-type-specifier
1723/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1724/// base-type-specifier
1725/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1726/// base-type-specifier
John McCall48871652010-08-21 09:40:31 +00001727Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001728 bool IsVirtual = false;
1729 SourceLocation StartLoc = Tok.getLocation();
1730
Richard Smith4c96e992013-02-19 23:47:15 +00001731 ParsedAttributesWithRange Attributes(AttrFactory);
1732 MaybeParseCXX11Attributes(Attributes);
1733
Douglas Gregor556877c2008-04-13 21:30:24 +00001734 // Parse the 'virtual' keyword.
Alp Toker97650562014-01-10 11:19:30 +00001735 if (TryConsumeToken(tok::kw_virtual))
Douglas Gregor556877c2008-04-13 21:30:24 +00001736 IsVirtual = true;
Douglas Gregor556877c2008-04-13 21:30:24 +00001737
Richard Smith4c96e992013-02-19 23:47:15 +00001738 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1739
Douglas Gregor556877c2008-04-13 21:30:24 +00001740 // Parse an (optional) access specifier.
1741 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001742 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001743 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001744
Richard Smith4c96e992013-02-19 23:47:15 +00001745 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1746
Douglas Gregor556877c2008-04-13 21:30:24 +00001747 // Parse the 'virtual' keyword (again!), in case it came after the
1748 // access specifier.
1749 if (Tok.is(tok::kw_virtual)) {
1750 SourceLocation VirtualLoc = ConsumeToken();
1751 if (IsVirtual) {
1752 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001753 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001754 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001755 }
1756
1757 IsVirtual = true;
1758 }
1759
Richard Smith4c96e992013-02-19 23:47:15 +00001760 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1761
Douglas Gregor831c93f2008-11-05 20:51:48 +00001762 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001763 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001764 SourceLocation BaseLoc;
1765 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001766 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001767 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001768
Douglas Gregor752a5952011-01-03 22:36:02 +00001769 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1770 // actually part of the base-specifier-list grammar productions, but we
1771 // parse it here for convenience.
1772 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00001773 TryConsumeToken(tok::ellipsis, EllipsisLoc);
1774
Mike Stump11289f42009-09-09 15:08:12 +00001775 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001776 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001777
Douglas Gregor556877c2008-04-13 21:30:24 +00001778 // Notify semantic analysis that we have parsed a complete
1779 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00001780 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1781 Access, BaseType.get(), BaseLoc,
1782 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001783}
1784
1785/// getAccessSpecifierIfPresent - Determine whether the next token is
1786/// a C++ access-specifier.
1787///
1788/// access-specifier: [C++ class.derived]
1789/// 'private'
1790/// 'protected'
1791/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001792AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001793 switch (Tok.getKind()) {
1794 default: return AS_none;
1795 case tok::kw_private: return AS_private;
1796 case tok::kw_protected: return AS_protected;
1797 case tok::kw_public: return AS_public;
1798 }
1799}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001800
Douglas Gregor433e0532012-04-16 18:27:27 +00001801/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith2331bbf2012-05-02 22:22:32 +00001802/// delayed, e.g., default arguments, create a late-parsed method declaration
1803/// record to handle the parsing at the end of the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00001804void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1805 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001806 // We just declared a member function. If this member function
Richard Smith2331bbf2012-05-02 22:22:32 +00001807 // has any default arguments, we'll need to parse them later.
Eli Friedman3af2a772009-07-22 21:45:50 +00001808 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001809 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001810 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor433e0532012-04-16 18:27:27 +00001811
Alp Tokerc5350722014-02-26 22:27:52 +00001812 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
1813 if (LateMethod || FTI.Params[ParamIdx].DefaultArgTokens) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001814 if (!LateMethod) {
1815 // Push this method onto the stack of late-parsed method
1816 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001817 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1818 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001819 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001820
1821 // Add all of the parameters prior to this one (they don't
1822 // have default arguments).
Alp Tokerc5350722014-02-26 22:27:52 +00001823 LateMethod->DefaultArgs.reserve(FTI.NumParams);
Eli Friedman3af2a772009-07-22 21:45:50 +00001824 for (unsigned I = 0; I < ParamIdx; ++I)
1825 LateMethod->DefaultArgs.push_back(
Alp Tokerc5350722014-02-26 22:27:52 +00001826 LateParsedDefaultArgument(FTI.Params[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001827 }
1828
Douglas Gregor433e0532012-04-16 18:27:27 +00001829 // Add this parameter to the list of parameters (it may or may
Eli Friedman3af2a772009-07-22 21:45:50 +00001830 // not have a default argument).
Alp Tokerc5350722014-02-26 22:27:52 +00001831 LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
1832 FTI.Params[ParamIdx].Param, FTI.Params[ParamIdx].DefaultArgTokens));
Eli Friedman3af2a772009-07-22 21:45:50 +00001833 }
1834 }
1835}
1836
Richard Smith89645bc2013-01-02 12:01:23 +00001837/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001838/// virt-specifier.
1839///
1840/// virt-specifier:
1841/// override
1842/// final
Richard Smith89645bc2013-01-02 12:01:23 +00001843VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001844 if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001845 return VirtSpecifiers::VS_None;
1846
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001847 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001848
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001849 // Initialize the contextual keywords.
1850 if (!Ident_final) {
1851 Ident_final = &PP.getIdentifierTable().get("final");
1852 if (getLangOpts().MicrosoftExt)
1853 Ident_sealed = &PP.getIdentifierTable().get("sealed");
1854 Ident_override = &PP.getIdentifierTable().get("override");
Anders Carlsson56104902011-01-17 03:05:47 +00001855 }
1856
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001857 if (II == Ident_override)
1858 return VirtSpecifiers::VS_Override;
1859
1860 if (II == Ident_sealed)
1861 return VirtSpecifiers::VS_Sealed;
1862
1863 if (II == Ident_final)
1864 return VirtSpecifiers::VS_Final;
1865
Anders Carlsson56104902011-01-17 03:05:47 +00001866 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001867}
1868
Richard Smith89645bc2013-01-02 12:01:23 +00001869/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001870///
1871/// virt-specifier-seq:
1872/// virt-specifier
1873/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00001874void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalldb632ac2012-09-25 07:32:39 +00001875 bool IsInterface) {
Anders Carlsson56104902011-01-17 03:05:47 +00001876 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00001877 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001878 if (Specifier == VirtSpecifiers::VS_None)
1879 return;
1880
1881 // C++ [class.mem]p8:
1882 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001883 const char *PrevSpec = 0;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001884 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001885 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1886 << PrevSpec
1887 << FixItHint::CreateRemoval(Tok.getLocation());
1888
David Majnemera5433082013-10-18 00:33:31 +00001889 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
1890 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalldb632ac2012-09-25 07:32:39 +00001891 Diag(Tok.getLocation(), diag::err_override_control_interface)
1892 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemera5433082013-10-18 00:33:31 +00001893 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
1894 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalldb632ac2012-09-25 07:32:39 +00001895 } else {
David Majnemera5433082013-10-18 00:33:31 +00001896 Diag(Tok.getLocation(),
1897 getLangOpts().CPlusPlus11
1898 ? diag::warn_cxx98_compat_override_control_keyword
1899 : diag::ext_override_control_keyword)
1900 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalldb632ac2012-09-25 07:32:39 +00001901 }
Anders Carlsson56104902011-01-17 03:05:47 +00001902 ConsumeToken();
1903 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001904}
1905
Richard Smith89645bc2013-01-02 12:01:23 +00001906/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001907/// 'final' or Microsoft 'sealed' contextual keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00001908bool Parser::isCXX11FinalKeyword() const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001909 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
1910 return Specifier == VirtSpecifiers::VS_Final ||
1911 Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001912}
1913
Richard Smith72553fc2014-01-23 23:53:27 +00001914/// \brief Parse a C++ member-declarator up to, but not including, the optional
1915/// brace-or-equal-initializer or pure-specifier.
1916void Parser::ParseCXXMemberDeclaratorBeforeInitializer(
1917 Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
1918 LateParsedAttrList &LateParsedAttrs) {
1919 // member-declarator:
1920 // declarator pure-specifier[opt]
1921 // declarator brace-or-equal-initializer[opt]
1922 // identifier[opt] ':' constant-expression
1923 if (Tok.isNot(tok::colon)) {
1924 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1925 // is a bitfield.
1926 // FIXME: This should only apply when parsing the id-expression (see
1927 // PR18587).
1928 ColonProtectionRAIIObject X(*this);
1929 ParseDeclarator(DeclaratorInfo);
1930 }
1931
1932 if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
1933 BitfieldSize = ParseConstantExpression();
1934 if (BitfieldSize.isInvalid())
1935 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
1936 } else
1937 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
1938
1939 // If a simple-asm-expr is present, parse it.
1940 if (Tok.is(tok::kw_asm)) {
1941 SourceLocation Loc;
1942 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1943 if (AsmLabel.isInvalid())
1944 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
1945
1946 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1947 DeclaratorInfo.SetRangeEnd(Loc);
1948 }
1949
1950 // If attributes exist after the declarator, but before an '{', parse them.
1951 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Richard Smith4b5a9492014-01-24 22:34:35 +00001952
1953 // For compatibility with code written to older Clang, also accept a
1954 // virt-specifier *after* the GNU attributes.
1955 // FIXME: If we saw any attributes that are known to GCC followed by a
1956 // virt-specifier, issue a GCC-compat warning.
1957 if (BitfieldSize.isUnset() && VS.isUnset())
1958 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith72553fc2014-01-23 23:53:27 +00001959}
1960
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001961/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1962///
1963/// member-declaration:
1964/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1965/// function-definition ';'[opt]
1966/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1967/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001968/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001969/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001970/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001971///
1972/// member-declarator-list:
1973/// member-declarator
1974/// member-declarator-list ',' member-declarator
1975///
1976/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001977/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001978/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00001979/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001980/// identifier[opt] ':' constant-expression
1981///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001982/// virt-specifier-seq:
1983/// virt-specifier
1984/// virt-specifier-seq virt-specifier
1985///
1986/// virt-specifier:
1987/// override
1988/// final
David Majnemera5433082013-10-18 00:33:31 +00001989/// [MS] sealed
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001990///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001991/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001992/// '= 0'
1993///
1994/// constant-initializer:
1995/// '=' constant-expression
1996///
Douglas Gregor3447e762009-08-20 22:52:58 +00001997void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00001998 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00001999 const ParsedTemplateInfo &TemplateInfo,
2000 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00002001 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002002 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00002003 Diag(Tok, diag::err_at_defs_cxx);
2004 else
2005 Diag(Tok, diag::err_at_in_class);
Richard Smithda35e962013-11-09 04:52:51 +00002006
Douglas Gregor23c84762011-04-14 17:21:19 +00002007 ConsumeToken();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002008 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor23c84762011-04-14 17:21:19 +00002009 return;
2010 }
Richard Smithda35e962013-11-09 04:52:51 +00002011
John McCalla0097262009-12-11 02:10:03 +00002012 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00002013 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00002014 if (!TemplateInfo.Kind &&
Richard Smith45855df2012-05-09 08:23:23 +00002015 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
2016 if (TryAnnotateCXXScopeToken())
2017 MalformedTypeSpec = true;
2018
2019 bool isAccessDecl;
2020 if (Tok.isNot(tok::annot_cxxscope))
2021 isAccessDecl = false;
2022 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00002023 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
2024 else
2025 isAccessDecl = NextToken().is(tok::kw_operator);
2026
2027 if (isAccessDecl) {
2028 // Collect the scope specifier token we annotated earlier.
2029 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002030 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2031 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00002032
2033 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00002034 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00002035 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +00002036 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
2037 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00002038 SkipUntil(tok::semi);
2039 return;
2040 }
2041
2042 // TODO: recover from mistakenly-qualified operator declarations.
Alp Toker383d2c42014-01-01 03:08:43 +00002043 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2044 "access declaration")) {
2045 SkipUntil(tok::semi);
John McCalla0097262009-12-11 02:10:03 +00002046 return;
Alp Toker383d2c42014-01-01 03:08:43 +00002047 }
John McCalla0097262009-12-11 02:10:03 +00002048
Douglas Gregor0be31a22010-07-02 17:43:08 +00002049 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002050 /* HasUsingKeyword */ false,
2051 SourceLocation(),
John McCalla0097262009-12-11 02:10:03 +00002052 SS, Name,
2053 /* AttrList */ 0,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002054 /* HasTypenameKeyword */ false,
John McCalla0097262009-12-11 02:10:03 +00002055 SourceLocation());
2056 return;
2057 }
2058 }
2059
Anders Carlssonf24fcff62009-03-11 16:27:10 +00002060 // static_assert-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00002061 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00002062 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00002063 SourceLocation DeclEnd;
2064 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002065 return;
2066 }
Mike Stump11289f42009-09-09 15:08:12 +00002067
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002068 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00002069 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00002070 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00002071 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00002072 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002073 AS, AccessAttrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002074 return;
2075 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002076
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002077 // Handle: member-declaration ::= '__extension__' member-declaration
2078 if (Tok.is(tok::kw___extension__)) {
2079 // __extension__ silences extension warnings in the subexpression.
2080 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2081 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002082 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2083 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002084 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002085
John McCall084e83d2011-03-24 11:26:52 +00002086 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002087 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002088 // Optional C++11 attribute-specifier
2089 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002090 // We need to keep these attributes for future diagnostic
2091 // before they are taken over by declaration specifier.
2092 FnAttrs.addAll(attrs.getList());
2093 FnAttrs.Range = attrs.Range;
2094
John McCall53fa7142010-12-24 02:08:15 +00002095 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002096
Douglas Gregorfec52632009-06-20 00:51:54 +00002097 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002098 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002099
Douglas Gregorfec52632009-06-20 00:51:54 +00002100 // Eat 'using'.
2101 SourceLocation UsingLoc = ConsumeToken();
2102
2103 if (Tok.is(tok::kw_namespace)) {
2104 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002105 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002106 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00002107 SourceLocation DeclEnd;
Richard Smith3f1b5d02011-05-05 21:57:07 +00002108 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +00002109 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2110 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00002111 }
2112 return;
2113 }
2114
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002115 // Hold late-parsed attributes so we can attach a Decl to them later.
2116 LateParsedAttrList CommonLateParsedAttrs;
2117
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002118 // decl-specifier-seq:
2119 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002120 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002121 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002122 if (MalformedTypeSpec)
2123 DS.SetTypeSpecError();
Richard Smith72553fc2014-01-23 23:53:27 +00002124
2125 {
2126 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
2127 // is a bitfield.
2128 ColonProtectionRAIIObject X(*this);
2129 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2130 &CommonLateParsedAttrs);
2131 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002132
Richard Smith404dfb42013-11-19 22:47:36 +00002133 // If we had a free-standing type definition with a missing semicolon, we
2134 // may get this far before the problem becomes obvious.
2135 if (DS.hasTagDefinition() &&
2136 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2137 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2138 &CommonLateParsedAttrs))
2139 return;
2140
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002141 MultiTemplateParamsArg TemplateParams(
John McCall11083da2009-09-16 22:47:08 +00002142 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2143 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2144
Alp Toker35d87032013-12-30 23:29:50 +00002145 if (TryConsumeToken(tok::semi)) {
Michael Handdc016d2012-11-28 23:17:40 +00002146 if (DS.isFriendSpecified())
2147 ProhibitAttributes(FnAttrs);
2148
John McCall48871652010-08-21 09:40:31 +00002149 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00002150 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00002151 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00002152 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002153 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002154
John McCall28a6aea2009-11-04 02:18:39 +00002155 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002156 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002157
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002158 // Hold late-parsed attributes so we can attach a Decl to them later.
2159 LateParsedAttrList LateParsedAttrs;
2160
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002161 SourceLocation EqualLoc;
2162 bool HasInitializer = false;
2163 ExprResult Init;
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002164
Richard Smith72553fc2014-01-23 23:53:27 +00002165 SmallVector<Decl *, 8> DeclsInGroup;
2166 ExprResult BitfieldSize;
2167 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002168
Richard Smith72553fc2014-01-23 23:53:27 +00002169 // Parse the first declarator.
2170 ParseCXXMemberDeclaratorBeforeInitializer(DeclaratorInfo, VS, BitfieldSize,
2171 LateParsedAttrs);
Nico Weber24b2a822011-01-28 06:07:34 +00002172
Richard Smith72553fc2014-01-23 23:53:27 +00002173 // If this has neither a name nor a bit width, something has gone seriously
2174 // wrong. Skip until the semi-colon or }.
Richard Smith4b5a9492014-01-24 22:34:35 +00002175 if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002176 // If so, skip until the semi-colon or a }.
2177 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2178 TryConsumeToken(tok::semi);
2179 return;
2180 }
John Thompson5bc5cbe2009-11-25 22:58:06 +00002181
Richard Smith72553fc2014-01-23 23:53:27 +00002182 // Check for a member function definition.
Richard Smith4b5a9492014-01-24 22:34:35 +00002183 if (BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002184 // MSVC permits pure specifier on inline functions defined at class scope.
Francois Pichet3abc9b82011-05-11 02:14:46 +00002185 // Hence check for =0 before checking for function definition.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002186 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Richard Smith72553fc2014-01-23 23:53:27 +00002187 DeclaratorInfo.isFunctionDeclarator() &&
Francois Pichet3abc9b82011-05-11 02:14:46 +00002188 NextToken().is(tok::numeric_constant)) {
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002189 EqualLoc = ConsumeToken();
Francois Pichet3abc9b82011-05-11 02:14:46 +00002190 Init = ParseInitializer();
2191 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002192 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002193 else
2194 HasInitializer = true;
Francois Pichet3abc9b82011-05-11 02:14:46 +00002195 }
2196
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002197 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002198 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002199 //
2200 // In C++11, a non-function declarator followed by an open brace is a
2201 // braced-init-list for an in-class member initialization, not an
2202 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002203 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002204 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002205 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002206 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002207 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002208 } else if (Tok.is(tok::equal)) {
2209 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002210 if (KW.is(tok::kw_default))
2211 DefinitionKind = FDK_Defaulted;
2212 else if (KW.is(tok::kw_delete))
2213 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002214 }
2215 }
2216
Michael Handdc016d2012-11-28 23:17:40 +00002217 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2218 // to a friend declaration, that declaration shall be a definition.
2219 if (DeclaratorInfo.isFunctionDeclarator() &&
2220 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2221 // Diagnose attributes that appear before decl specifier:
2222 // [[]] friend int foo();
2223 ProhibitAttributes(FnAttrs);
2224 }
2225
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002226 if (DefinitionKind) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002227 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002228 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002229 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002230 SkipUntil(tok::r_brace);
Michael Handdc016d2012-11-28 23:17:40 +00002231
Douglas Gregor8a4db832011-01-19 16:41:58 +00002232 // Consume the optional ';'
Alp Toker35d87032013-12-30 23:29:50 +00002233 TryConsumeToken(tok::semi);
2234
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002235 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002236 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002237
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002238 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002239 Diag(DeclaratorInfo.getIdentifierLoc(),
2240 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002241
Richard Smith2603b092012-11-15 22:54:20 +00002242 // Recover by treating the 'typedef' as spurious.
2243 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002244 }
2245
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002246 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002247 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002248 VS, DefinitionKind, Init);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002249
David Majnemer23252a32013-08-01 04:22:55 +00002250 if (FunDecl) {
2251 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2252 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2253 }
2254 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2255 LateParsedAttrs[i]->addDecl(FunDecl);
2256 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002257 }
2258 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002259
2260 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002261 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002262 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002263
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002264 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002265 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002266 }
2267
2268 // member-declarator-list:
2269 // member-declarator
2270 // member-declarator-list ',' member-declarator
2271
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002272 while (1) {
Richard Smith2b013182012-06-10 03:12:00 +00002273 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002274 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith938f40b2011-06-11 17:19:42 +00002275 if (BitfieldSize.get()) {
2276 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002277 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith938f40b2011-06-11 17:19:42 +00002278 } else {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002279 HasInitializer = true;
Richard Smith2b013182012-06-10 03:12:00 +00002280 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2281 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smith2b013182012-06-10 03:12:00 +00002282 != DeclSpec::SCS_typedef)
2283 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002284 }
2285 }
2286
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002287 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002288 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002289 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002290
Rafael Espindola0a67e2f2013-01-08 20:44:06 +00002291 NamedDecl *ThisDecl = 0;
John McCall07e91c02009-08-06 02:15:43 +00002292 if (DS.isFriendSpecified()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002293 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
Michael Handdc016d2012-11-28 23:17:40 +00002294 // to a friend declaration, that declaration shall be a definition.
2295 //
Richard Smith72553fc2014-01-23 23:53:27 +00002296 // Diagnose attributes that appear in a friend member function declarator:
2297 // friend int foo [[]] ();
Michael Handdc016d2012-11-28 23:17:40 +00002298 SmallVector<SourceRange, 4> Ranges;
2299 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
Richard Smith72553fc2014-01-23 23:53:27 +00002300 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
2301 E = Ranges.end(); I != E; ++I)
2302 Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
Michael Handdc016d2012-11-28 23:17:40 +00002303
Richard Smith72553fc2014-01-23 23:53:27 +00002304 // TODO: handle initializers, VS, bitfields, 'delete'
Douglas Gregor0be31a22010-07-02 17:43:08 +00002305 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002306 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002307 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002308 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002309 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002310 TemplateParams,
John McCall07e91c02009-08-06 02:15:43 +00002311 BitfieldSize.release(),
Richard Smith2b013182012-06-10 03:12:00 +00002312 VS, HasInClassInit);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002313
2314 if (VarTemplateDecl *VT =
2315 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : 0)
2316 // Re-direct this decl to refer to the templated decl so that we can
2317 // initialize it.
2318 ThisDecl = VT->getTemplatedDecl();
2319
David Majnemer23252a32013-08-01 04:22:55 +00002320 if (ThisDecl && AccessAttrs)
Richard Smithf8a75c32013-08-29 00:47:48 +00002321 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00002322 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002323
Douglas Gregor728d00b2011-10-10 14:49:18 +00002324 // Handle the initializer.
David Blaikie35506f82013-01-30 01:22:18 +00002325 if (HasInClassInit != ICIS_NoInit &&
2326 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2327 DeclSpec::SCS_static) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002328 // The initializer was deferred; parse it and cache the tokens.
David Majnemer23252a32013-08-01 04:22:55 +00002329 Diag(Tok, getLangOpts().CPlusPlus11
2330 ? diag::warn_cxx98_compat_nonstatic_member_init
2331 : diag::ext_nonstatic_member_init);
Richard Smith5d164bc2011-10-15 05:09:34 +00002332
Richard Smith938f40b2011-06-11 17:19:42 +00002333 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002334 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2335 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002336 //
2337 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002338 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002339 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002340 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemer23252a32013-08-01 04:22:55 +00002341
2342 // Avoid later warnings about a class member of incomplete type.
David Blaikiecdd91db2012-02-14 09:00:46 +00002343 if (ThisDecl)
David Blaikiecdd91db2012-02-14 09:00:46 +00002344 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002345 } else
2346 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002347 } else if (HasInitializer) {
2348 // Normal initializer.
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002349 if (!Init.isUsable())
David Majnemer23252a32013-08-01 04:22:55 +00002350 Init = ParseCXXMemberInitializer(
2351 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2352
Douglas Gregor728d00b2011-10-10 14:49:18 +00002353 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002354 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002355 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002356 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smith74aeef52013-04-26 16:15:35 +00002357 DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002358 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor728d00b2011-10-10 14:49:18 +00002359 // No initializer.
Richard Smith74aeef52013-04-26 16:15:35 +00002360 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002361
Douglas Gregor728d00b2011-10-10 14:49:18 +00002362 if (ThisDecl) {
David Majnemer23252a32013-08-01 04:22:55 +00002363 if (!ThisDecl->isInvalidDecl()) {
2364 // Set the Decl for any late parsed attributes
2365 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2366 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2367
2368 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2369 LateParsedAttrs[i]->addDecl(ThisDecl);
2370 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002371 Actions.FinalizeDeclaration(ThisDecl);
2372 DeclsInGroup.push_back(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002373
2374 if (DeclaratorInfo.isFunctionDeclarator() &&
2375 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2376 DeclSpec::SCS_typedef)
2377 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002378 }
David Majnemer23252a32013-08-01 04:22:55 +00002379 LateParsedAttrs.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002380
2381 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002382
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002383 // If we don't have a comma, it is either the end of the list (a ';')
2384 // or an error, bail out.
Alp Toker094e5212014-01-05 03:27:11 +00002385 SourceLocation CommaLoc;
2386 if (!TryConsumeToken(tok::comma, CommaLoc))
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002387 break;
Mike Stump11289f42009-09-09 15:08:12 +00002388
Richard Smithc8a79032012-01-09 22:31:44 +00002389 if (Tok.isAtStartOfLine() &&
2390 !MightBeDeclarator(Declarator::MemberContext)) {
2391 // This comma was followed by a line-break and something which can't be
2392 // the start of a declarator. The comma was probably a typo for a
2393 // semicolon.
2394 Diag(CommaLoc, diag::err_expected_semi_declaration)
2395 << FixItHint::CreateReplacement(CommaLoc, ";");
2396 ExpectSemi = false;
2397 break;
2398 }
Mike Stump11289f42009-09-09 15:08:12 +00002399
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002400 // Parse the next declarator.
2401 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002402 VS.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002403 BitfieldSize = true;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002404 Init = true;
2405 HasInitializer = false;
Richard Smith8d06f422012-01-12 23:53:29 +00002406 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002407
Richard Smith72553fc2014-01-23 23:53:27 +00002408 // GNU attributes are allowed before the second and subsequent declarator.
John McCall53fa7142010-12-24 02:08:15 +00002409 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002410
Richard Smith72553fc2014-01-23 23:53:27 +00002411 ParseCXXMemberDeclaratorBeforeInitializer(DeclaratorInfo, VS, BitfieldSize,
2412 LateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002413 }
2414
Richard Smithc8a79032012-01-09 22:31:44 +00002415 if (ExpectSemi &&
2416 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002417 // Skip to end of block or statement.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002418 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002419 // If we stopped at a ';', eat it.
Alp Toker35d87032013-12-30 23:29:50 +00002420 TryConsumeToken(tok::semi);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002421 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002422 }
2423
Rafael Espindolaab417692013-07-09 12:05:01 +00002424 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002425}
2426
Richard Smith938f40b2011-06-11 17:19:42 +00002427/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2428/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2429/// function definition. The location of the '=', if any, will be placed in
2430/// EqualLoc.
2431///
2432/// pure-specifier:
2433/// '= 0'
Sebastian Redleef474c2012-02-22 10:50:08 +00002434///
Richard Smith938f40b2011-06-11 17:19:42 +00002435/// brace-or-equal-initializer:
2436/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002437/// braced-init-list
2438///
Richard Smith938f40b2011-06-11 17:19:42 +00002439/// initializer-clause:
2440/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002441/// braced-init-list
2442///
Richard Smithda35e962013-11-09 04:52:51 +00002443/// defaulted/deleted function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002444/// '=' 'default'
2445/// '=' 'delete'
2446///
2447/// Prior to C++0x, the assignment-expression in an initializer-clause must
2448/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002449ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002450 SourceLocation &EqualLoc) {
2451 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2452 && "Data member initializer not starting with '=' or '{'");
2453
Douglas Gregor926410d2012-02-21 02:22:07 +00002454 EnterExpressionEvaluationContext Context(Actions,
2455 Sema::PotentiallyEvaluated,
2456 D);
Alp Toker094e5212014-01-05 03:27:11 +00002457 if (TryConsumeToken(tok::equal, EqualLoc)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002458 if (Tok.is(tok::kw_delete)) {
2459 // In principle, an initializer of '= delete p;' is legal, but it will
2460 // never type-check. It's better to diagnose it as an ill-formed expression
2461 // than as an ill-formed deleted non-function member.
2462 // An initializer of '= delete p, foo' will never be parsed, because
2463 // a top-level comma always ends the initializer expression.
2464 const Token &Next = NextToken();
2465 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
Richard Smith34f30512013-11-23 04:06:09 +00002466 Next.is(tok::eof)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002467 if (IsFunction)
2468 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2469 << 1 /* delete */;
2470 else
2471 Diag(ConsumeToken(), diag::err_deleted_non_function);
2472 return ExprResult();
2473 }
2474 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002475 if (IsFunction)
2476 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2477 << 0 /* default */;
2478 else
2479 Diag(ConsumeToken(), diag::err_default_special_members);
2480 return ExprResult();
2481 }
2482
Sebastian Redleef474c2012-02-22 10:50:08 +00002483 }
2484 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002485}
2486
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002487/// ParseCXXMemberSpecification - Parse the class definition.
2488///
2489/// member-specification:
2490/// member-declaration member-specification[opt]
2491/// access-specifier ':' member-specification[opt]
2492///
Joao Matose9a3ed42012-08-31 22:18:20 +00002493void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002494 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00002495 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002496 unsigned TagType, Decl *TagDecl) {
2497 assert((TagType == DeclSpec::TST_struct ||
2498 TagType == DeclSpec::TST_interface ||
2499 TagType == DeclSpec::TST_union ||
2500 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2501
John McCallfaf5fb42010-08-26 23:41:50 +00002502 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2503 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00002504
Douglas Gregoredf8f392010-01-16 20:52:59 +00002505 // Determine whether this is a non-nested class. Note that local
2506 // classes are *not* considered to be nested classes.
2507 bool NonNestedClass = true;
2508 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002509 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002510 if (S->isClassScope()) {
2511 // We're inside a class scope, so this is a nested class.
2512 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00002513
2514 // The Microsoft extension __interface does not permit nested classes.
2515 if (getCurrentClass().IsInterface) {
2516 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2517 << /*ErrorType=*/6
2518 << (isa<NamedDecl>(TagDecl)
2519 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
David Blaikieabe1a392014-04-02 05:58:29 +00002520 : "(anonymous)");
John McCalldb632ac2012-09-25 07:32:39 +00002521 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00002522 break;
2523 }
2524
2525 if ((S->getFlags() & Scope::FnScope)) {
2526 // If we're in a function or function template declared in the
2527 // body of a class, then this is a local class rather than a
2528 // nested class.
2529 const Scope *Parent = S->getParent();
2530 if (Parent->isTemplateParamScope())
2531 Parent = Parent->getParent();
2532 if (Parent->isClassScope())
2533 break;
2534 }
2535 }
2536 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002537
2538 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00002539 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002540
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002541 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00002542 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2543 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002544
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002545 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002546 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002547
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002548 SourceLocation FinalLoc;
David Majnemera5433082013-10-18 00:33:31 +00002549 bool IsFinalSpelledSealed = false;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002550
2551 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002552 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemera5433082013-10-18 00:33:31 +00002553 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2554 assert((Specifier == VirtSpecifiers::VS_Final ||
2555 Specifier == VirtSpecifiers::VS_Sealed) &&
2556 "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00002557 FinalLoc = ConsumeToken();
David Majnemera5433082013-10-18 00:33:31 +00002558 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002559
David Majnemera5433082013-10-18 00:33:31 +00002560 if (TagType == DeclSpec::TST_interface)
John McCalldb632ac2012-09-25 07:32:39 +00002561 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemera5433082013-10-18 00:33:31 +00002562 << VirtSpecifiers::getSpecifierName(Specifier);
2563 else if (Specifier == VirtSpecifiers::VS_Final)
2564 Diag(FinalLoc, getLangOpts().CPlusPlus11
2565 ? diag::warn_cxx98_compat_override_control_keyword
2566 : diag::ext_override_control_keyword)
2567 << VirtSpecifiers::getSpecifierName(Specifier);
2568 else if (Specifier == VirtSpecifiers::VS_Sealed)
2569 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han9407e502012-11-26 22:54:45 +00002570
Michael Han309af292013-01-07 16:57:11 +00002571 // Parse any C++11 attributes after 'final' keyword.
2572 // These attributes are not allowed to appear here,
2573 // and the only possible place for them to appertain
2574 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00002575 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002576 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002577
John McCall2d814c32009-12-19 21:48:58 +00002578 if (Tok.is(tok::colon)) {
2579 ParseBaseClause(TagDecl);
2580
2581 if (!Tok.is(tok::l_brace)) {
2582 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00002583
2584 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002585 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002586 return;
2587 }
2588 }
2589
2590 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002591 BalancedDelimiterTracker T(*this, tok::l_brace);
2592 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00002593
John McCall08bede42010-05-28 08:11:17 +00002594 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00002595 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemera5433082013-10-18 00:33:31 +00002596 IsFinalSpelledSealed,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002597 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00002598
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002599 // C++ 11p3: Members of a class defined with the keyword class are private
2600 // by default. Members of a class defined with the keywords struct or union
2601 // are public by default.
2602 AccessSpecifier CurAS;
2603 if (TagType == DeclSpec::TST_class)
2604 CurAS = AS_private;
2605 else
2606 CurAS = AS_public;
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002607 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002608
Douglas Gregor9377c822010-06-21 22:31:09 +00002609 if (TagDecl) {
2610 // While we still have something to read, read the member-declarations.
Richard Smith34f30512013-11-23 04:06:09 +00002611 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Douglas Gregor9377c822010-06-21 22:31:09 +00002612 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002613
David Blaikiebbafb8a2012-03-11 07:00:24 +00002614 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet8f981d52011-05-25 10:19:49 +00002615 Tok.is(tok::kw___if_not_exists))) {
2616 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2617 continue;
2618 }
2619
Douglas Gregor9377c822010-06-21 22:31:09 +00002620 // Check for extraneous top-level semicolon.
2621 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002622 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor9377c822010-06-21 22:31:09 +00002623 continue;
2624 }
2625
Eli Friedmanec52f922012-02-23 23:47:16 +00002626 if (Tok.is(tok::annot_pragma_vis)) {
2627 HandlePragmaVisibility();
2628 continue;
2629 }
2630
2631 if (Tok.is(tok::annot_pragma_pack)) {
2632 HandlePragmaPack();
2633 continue;
2634 }
2635
Argyrios Kyrtzidis5c2021b2012-10-12 17:39:59 +00002636 if (Tok.is(tok::annot_pragma_align)) {
2637 HandlePragmaAlign();
2638 continue;
2639 }
2640
Alexey Bataeva769e072013-03-22 06:34:35 +00002641 if (Tok.is(tok::annot_pragma_openmp)) {
2642 ParseOpenMPDeclarativeDirective();
2643 continue;
2644 }
2645
David Majnemer4bb09802014-02-10 19:50:15 +00002646 if (Tok.is(tok::annot_pragma_ms_pointers_to_members)) {
2647 HandlePragmaMSPointersToMembers();
2648 continue;
2649 }
2650
Warren Huntc3b18962014-04-08 22:30:47 +00002651 if (Tok.is(tok::annot_pragma_ms_pragma)) {
2652 HandlePragmaMSPragma();
2653 continue;
2654 }
2655
Richard Smithda35e962013-11-09 04:52:51 +00002656 // If we see a namespace here, a close brace was missing somewhere.
2657 if (Tok.is(tok::kw_namespace)) {
Richard Smith2ac43ad2013-11-15 23:00:02 +00002658 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
Richard Smithda35e962013-11-09 04:52:51 +00002659 break;
2660 }
2661
Douglas Gregor9377c822010-06-21 22:31:09 +00002662 AccessSpecifier AS = getAccessSpecifierIfPresent();
2663 if (AS != AS_none) {
2664 // Current token is a C++ access specifier.
2665 CurAS = AS;
2666 SourceLocation ASLoc = Tok.getLocation();
David Blaikieeba32c22011-10-13 06:08:43 +00002667 unsigned TokLength = Tok.getLength();
Douglas Gregor9377c822010-06-21 22:31:09 +00002668 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002669 AccessAttrs.clear();
2670 MaybeParseGNUAttributes(AccessAttrs);
2671
David Blaikieeba32c22011-10-13 06:08:43 +00002672 SourceLocation EndLoc;
Alp Toker35d87032013-12-30 23:29:50 +00002673 if (TryConsumeToken(tok::colon, EndLoc)) {
2674 } else if (TryConsumeToken(tok::semi, EndLoc)) {
2675 Diag(EndLoc, diag::err_expected)
2676 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002677 } else {
2678 EndLoc = ASLoc.getLocWithOffset(TokLength);
Alp Toker35d87032013-12-30 23:29:50 +00002679 Diag(EndLoc, diag::err_expected)
2680 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002681 }
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002682
John McCalldb632ac2012-09-25 07:32:39 +00002683 // The Microsoft extension __interface does not permit non-public
2684 // access specifiers.
2685 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2686 Diag(ASLoc, diag::err_access_specifier_interface)
2687 << (CurAS == AS_protected);
2688 }
2689
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002690 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2691 AccessAttrs.getList())) {
2692 // found another attribute than only annotations
2693 AccessAttrs.clear();
2694 }
2695
Douglas Gregor9377c822010-06-21 22:31:09 +00002696 continue;
2697 }
2698
Douglas Gregor9377c822010-06-21 22:31:09 +00002699 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002700 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002701 }
2702
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002703 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00002704 } else {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002705 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002706 }
Mike Stump11289f42009-09-09 15:08:12 +00002707
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002708 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002709 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002710 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002711
John McCall08bede42010-05-28 08:11:17 +00002712 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002713 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002714 T.getOpenLocation(),
2715 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002716 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002717
Douglas Gregor433e0532012-04-16 18:27:27 +00002718 // C++11 [class.mem]p2:
2719 // Within the class member-specification, the class is regarded as complete
Richard Smith2331bbf2012-05-02 22:22:32 +00002720 // within function bodies, default arguments, and
Douglas Gregor433e0532012-04-16 18:27:27 +00002721 // brace-or-equal-initializers for non-static data members (including such
2722 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00002723 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002724 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00002725 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002726 // declarations and the lexed inline method definitions, along with any
2727 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00002728 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002729 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002730 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00002731
2732 // We've finished with all pending member declarations.
2733 Actions.ActOnFinishCXXMemberDecls();
2734
Richard Smith938f40b2011-06-11 17:19:42 +00002735 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002736 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00002737 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002738 }
2739
John McCall08bede42010-05-28 08:11:17 +00002740 if (TagDecl)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002741 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2742 T.getCloseLocation());
John McCall2ff380a2010-03-17 00:38:33 +00002743
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002744 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002745 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002746 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002747}
Douglas Gregore8381c02008-11-05 04:29:56 +00002748
Richard Smith2ac43ad2013-11-15 23:00:02 +00002749void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithda35e962013-11-09 04:52:51 +00002750 assert(Tok.is(tok::kw_namespace));
2751
2752 // FIXME: Suggest where the close brace should have gone by looking
2753 // at indentation changes within the definition body.
Richard Smith2ac43ad2013-11-15 23:00:02 +00002754 Diag(D->getLocation(),
2755 diag::err_missing_end_of_definition) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002756 Diag(Tok.getLocation(),
Richard Smith2ac43ad2013-11-15 23:00:02 +00002757 diag::note_missing_end_of_definition_before) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002758
2759 // Push '};' onto the token stream to recover.
2760 PP.EnterToken(Tok);
2761
2762 Tok.startToken();
2763 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
2764 Tok.setKind(tok::semi);
2765 PP.EnterToken(Tok);
2766
2767 Tok.setKind(tok::r_brace);
2768}
2769
Douglas Gregore8381c02008-11-05 04:29:56 +00002770/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2771/// which explicitly initializes the members or base classes of a
2772/// class (C++ [class.base.init]). For example, the three initializers
2773/// after the ':' in the Derived constructor below:
2774///
2775/// @code
2776/// class Base { };
2777/// class Derived : Base {
2778/// int x;
2779/// float f;
2780/// public:
2781/// Derived(float f) : Base(), x(17), f(f) { }
2782/// };
2783/// @endcode
2784///
Mike Stump11289f42009-09-09 15:08:12 +00002785/// [C++] ctor-initializer:
2786/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00002787///
Mike Stump11289f42009-09-09 15:08:12 +00002788/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00002789/// mem-initializer ...[opt]
2790/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00002791void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00002792 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2793
John Wiegley1c0675e2011-04-28 01:08:34 +00002794 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2795 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00002796 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002797
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002798 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002799 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002800
Douglas Gregore8381c02008-11-05 04:29:56 +00002801 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002802 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00002803 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2804 MemInitializers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002805 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002806 } else {
2807 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2808 if (!MemInit.isInvalid())
2809 MemInitializers.push_back(MemInit.get());
2810 else
2811 AnyErrors = true;
2812 }
2813
Douglas Gregore8381c02008-11-05 04:29:56 +00002814 if (Tok.is(tok::comma))
2815 ConsumeToken();
2816 else if (Tok.is(tok::l_brace))
2817 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00002818 // If the next token looks like a base or member initializer, assume that
2819 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00002820 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2821 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2822 Diag(Loc, diag::err_ctor_init_missing_comma)
2823 << FixItHint::CreateInsertion(Loc, ", ");
2824 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00002825 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alp Tokerec543272013-12-24 09:48:30 +00002826 Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
2827 << tok::comma;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002828 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregore8381c02008-11-05 04:29:56 +00002829 break;
2830 }
2831 } while (true);
2832
David Blaikie3fc2f912013-01-17 05:26:25 +00002833 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002834 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00002835}
2836
2837/// ParseMemInitializer - Parse a C++ member initializer, which is
2838/// part of a constructor initializer that explicitly initializes one
2839/// member or base class (C++ [class.base.init]). See
2840/// ParseConstructorInitializer for an example.
2841///
2842/// [C++] mem-initializer:
2843/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002844/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00002845///
Douglas Gregore8381c02008-11-05 04:29:56 +00002846/// [C++] mem-initializer-id:
2847/// '::'[opt] nested-name-specifier[opt] class-name
2848/// identifier
John McCall48871652010-08-21 09:40:31 +00002849Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00002850 // parse '::'[opt] nested-name-specifier[opt]
2851 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002852 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00002853 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002854 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002855 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00002856 if (TemplateId->Kind == TNK_Type_template ||
2857 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00002858 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002859 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00002860 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002861 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002862 }
David Blaikie186a8892012-01-24 06:03:59 +00002863 // Uses of decltype will already have been converted to annot_decltype by
2864 // ParseOptionalCXXScopeSpecifier at this point.
2865 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2866 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002867 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00002868 return true;
2869 }
Mike Stump11289f42009-09-09 15:08:12 +00002870
David Blaikie186a8892012-01-24 06:03:59 +00002871 IdentifierInfo *II = 0;
2872 DeclSpec DS(AttrFactory);
2873 SourceLocation IdLoc = Tok.getLocation();
2874 if (Tok.is(tok::annot_decltype)) {
2875 // Get the decltype expression, if there is one.
2876 ParseDecltypeSpecifier(DS);
2877 } else {
2878 if (Tok.is(tok::identifier))
2879 // Get the identifier. This may be a member name or a class name,
2880 // but we'll let the semantic analysis determine which it is.
2881 II = Tok.getIdentifierInfo();
2882 ConsumeToken();
2883 }
2884
Douglas Gregore8381c02008-11-05 04:29:56 +00002885
2886 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002887 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002888 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2889
Sebastian Redla74948d2011-09-24 17:48:25 +00002890 ExprResult InitList = ParseBraceInitializer();
2891 if (InitList.isInvalid())
2892 return true;
2893
2894 SourceLocation EllipsisLoc;
Alp Toker094e5212014-01-05 03:27:11 +00002895 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redla74948d2011-09-24 17:48:25 +00002896
2897 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002898 TemplateTypeTy, DS, IdLoc,
2899 InitList.take(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00002900 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002901 BalancedDelimiterTracker T(*this, tok::l_paren);
2902 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00002903
Sebastian Redl3da34892011-06-05 12:23:16 +00002904 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00002905 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00002906 CommaLocsTy CommaLocs;
2907 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002908 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00002909 return true;
2910 }
2911
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002912 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00002913
2914 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00002915 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00002916
2917 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002918 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00002919 T.getOpenLocation(), ArgExprs,
2920 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00002921 }
2922
Alp Tokerec543272013-12-24 09:48:30 +00002923 if (getLangOpts().CPlusPlus11)
2924 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
2925 else
2926 return Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregore8381c02008-11-05 04:29:56 +00002927}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002928
Sebastian Redl965b0e32011-03-05 14:45:16 +00002929/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002930///
Douglas Gregor356513d2008-12-01 18:00:20 +00002931/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00002932/// dynamic-exception-specification
2933/// noexcept-specification
2934///
2935/// noexcept-specification:
2936/// 'noexcept'
2937/// 'noexcept' '(' constant-expression ')'
2938ExceptionSpecificationType
Richard Smith2331bbf2012-05-02 22:22:32 +00002939Parser::tryParseExceptionSpecification(
Douglas Gregor433e0532012-04-16 18:27:27 +00002940 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002941 SmallVectorImpl<ParsedType> &DynamicExceptions,
2942 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00002943 ExprResult &NoexceptExpr) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002944 ExceptionSpecificationType Result = EST_None;
2945
2946 // See if there's a dynamic specification.
2947 if (Tok.is(tok::kw_throw)) {
2948 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2949 DynamicExceptions,
2950 DynamicExceptionRanges);
2951 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2952 "Produced different number of exception types and ranges.");
2953 }
2954
2955 // If there's no noexcept specification, we're done.
2956 if (Tok.isNot(tok::kw_noexcept))
2957 return Result;
2958
Richard Smithb15c11c2011-10-17 23:06:20 +00002959 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2960
Sebastian Redl965b0e32011-03-05 14:45:16 +00002961 // If we already had a dynamic specification, parse the noexcept for,
2962 // recovery, but emit a diagnostic and don't store the results.
2963 SourceRange NoexceptRange;
2964 ExceptionSpecificationType NoexceptType = EST_None;
2965
2966 SourceLocation KeywordLoc = ConsumeToken();
2967 if (Tok.is(tok::l_paren)) {
2968 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002969 BalancedDelimiterTracker T(*this, tok::l_paren);
2970 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00002971 NoexceptType = EST_ComputedNoexcept;
2972 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002973 // The argument must be contextually convertible to bool. We use
2974 // ActOnBooleanCondition for this purpose.
2975 if (!NoexceptExpr.isInvalid())
2976 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2977 NoexceptExpr.get());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002978 T.consumeClose();
2979 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl965b0e32011-03-05 14:45:16 +00002980 } else {
2981 // There is no argument.
2982 NoexceptType = EST_BasicNoexcept;
2983 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2984 }
2985
2986 if (Result == EST_None) {
2987 SpecificationRange = NoexceptRange;
2988 Result = NoexceptType;
2989
2990 // If there's a dynamic specification after a noexcept specification,
2991 // parse that and ignore the results.
2992 if (Tok.is(tok::kw_throw)) {
2993 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2994 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2995 DynamicExceptionRanges);
2996 }
2997 } else {
2998 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2999 }
3000
3001 return Result;
3002}
3003
Richard Smith8ca78a12013-06-13 02:02:51 +00003004static void diagnoseDynamicExceptionSpecification(
3005 Parser &P, const SourceRange &Range, bool IsNoexcept) {
3006 if (P.getLangOpts().CPlusPlus11) {
3007 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
3008 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
3009 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
3010 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
3011 }
3012}
3013
Sebastian Redl965b0e32011-03-05 14:45:16 +00003014/// ParseDynamicExceptionSpecification - Parse a C++
3015/// dynamic-exception-specification (C++ [except.spec]).
3016///
3017/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00003018/// 'throw' '(' type-id-list [opt] ')'
3019/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00003020///
Douglas Gregor356513d2008-12-01 18:00:20 +00003021/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00003022/// type-id ... [opt]
3023/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003024///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003025ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
3026 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003027 SmallVectorImpl<ParsedType> &Exceptions,
3028 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003029 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00003030
Sebastian Redl965b0e32011-03-05 14:45:16 +00003031 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003032 BalancedDelimiterTracker T(*this, tok::l_paren);
3033 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003034 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3035 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003036 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003037 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003038
Douglas Gregor356513d2008-12-01 18:00:20 +00003039 // Parse throw(...), a Microsoft extension that means "this function
3040 // can throw anything".
3041 if (Tok.is(tok::ellipsis)) {
3042 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003043 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00003044 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003045 T.consumeClose();
3046 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003047 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003048 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00003049 }
3050
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003051 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00003052 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003053 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00003054 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00003055
Douglas Gregor830837d2010-12-20 23:57:46 +00003056 if (Tok.is(tok::ellipsis)) {
3057 // C++0x [temp.variadic]p5:
3058 // - In a dynamic-exception-specification (15.4); the pattern is a
3059 // type-id.
3060 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003061 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00003062 if (!Res.isInvalid())
3063 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3064 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003065
Sebastian Redld6434562009-05-29 18:02:33 +00003066 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003067 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00003068 Ranges.push_back(Range);
3069 }
Alp Toker97650562014-01-10 11:19:30 +00003070
3071 if (!TryConsumeToken(tok::comma))
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003072 break;
3073 }
3074
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003075 T.consumeClose();
3076 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003077 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3078 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003079 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003080}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003081
Douglas Gregor7fb25412010-10-01 18:44:50 +00003082/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3083/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003084TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00003085 assert(Tok.is(tok::arrow) && "expected arrow");
3086
3087 ConsumeToken();
3088
Richard Smithbfdb1082012-03-12 08:56:40 +00003089 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003090}
3091
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003092/// \brief We have just started parsing the definition of a new class,
3093/// so push that class onto our stack of classes that is currently
3094/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003095Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003096Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3097 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003098 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003099 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003100 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003101 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003102}
3103
3104/// \brief Deallocate the given parsed class and all of its nested
3105/// classes.
3106void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003107 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3108 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003109 delete Class;
3110}
3111
3112/// \brief Pop the top class of the stack of classes that are
3113/// currently being parsed.
3114///
3115/// This routine should be called when we have finished parsing the
3116/// definition of a class, but have not yet popped the Scope
3117/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003118void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003119 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003120
John McCallc1465822011-02-14 07:13:47 +00003121 Actions.PopParsingClass(state);
3122
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003123 ParsingClass *Victim = ClassStack.top();
3124 ClassStack.pop();
3125 if (Victim->TopLevelClass) {
3126 // Deallocate all of the nested classes of this class,
3127 // recursively: we don't need to keep any of this information.
3128 DeallocateParsedClasses(Victim);
3129 return;
Mike Stump11289f42009-09-09 15:08:12 +00003130 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003131 assert(!ClassStack.empty() && "Missing top-level class?");
3132
Douglas Gregorefc46952010-10-12 16:25:54 +00003133 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003134 // The victim is a nested class, but we will not need to perform
3135 // any processing after the definition of this class since it has
3136 // no members whose handling was delayed. Therefore, we can just
3137 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003138 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003139 return;
3140 }
3141
3142 // This nested class has some members that will need to be processed
3143 // after the top-level class is completely defined. Therefore, add
3144 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003145 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003146 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003147 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003148}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003149
Richard Smith3dff2512012-04-10 03:25:07 +00003150/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3151///
3152/// \return the parsed identifier on success, and 0 if the next token is not an
3153/// attribute-token.
3154///
3155/// C++11 [dcl.attr.grammar]p3:
3156/// If a keyword or an alternative token that satisfies the syntactic
3157/// requirements of an identifier is contained in an attribute-token,
3158/// it is considered an identifier.
3159IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3160 switch (Tok.getKind()) {
3161 default:
3162 // Identifiers and keywords have identifier info attached.
3163 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3164 Loc = ConsumeToken();
3165 return II;
3166 }
3167 return 0;
3168
3169 case tok::ampamp: // 'and'
3170 case tok::pipe: // 'bitor'
3171 case tok::pipepipe: // 'or'
3172 case tok::caret: // 'xor'
3173 case tok::tilde: // 'compl'
3174 case tok::amp: // 'bitand'
3175 case tok::ampequal: // 'and_eq'
3176 case tok::pipeequal: // 'or_eq'
3177 case tok::caretequal: // 'xor_eq'
3178 case tok::exclaim: // 'not'
3179 case tok::exclaimequal: // 'not_eq'
3180 // Alternative tokens do not have identifier info, but their spelling
3181 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003182 SmallString<8> SpellingBuf;
Richard Smith3dff2512012-04-10 03:25:07 +00003183 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003184 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003185 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003186 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003187 }
3188 return 0;
3189 }
3190}
3191
Michael Han23214e52012-10-03 01:56:22 +00003192static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3193 IdentifierInfo *ScopeName) {
3194 switch (AttributeList::getKind(AttrName, ScopeName,
3195 AttributeList::AS_CXX11)) {
3196 case AttributeList::AT_CarriesDependency:
3197 case AttributeList::AT_FallThrough:
Richard Smith10876ef2013-01-17 01:30:42 +00003198 case AttributeList::AT_CXX11NoReturn: {
Michael Han23214e52012-10-03 01:56:22 +00003199 return true;
3200 }
3201
3202 default:
3203 return false;
3204 }
3205}
3206
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003207/// ParseCXX11AttributeArgs -- Parse a C++11 attribute-argument-clause.
3208///
3209/// [C++11] attribute-argument-clause:
3210/// '(' balanced-token-seq ')'
3211///
3212/// [C++11] balanced-token-seq:
3213/// balanced-token
3214/// balanced-token-seq balanced-token
3215///
3216/// [C++11] balanced-token:
3217/// '(' balanced-token-seq ')'
3218/// '[' balanced-token-seq ']'
3219/// '{' balanced-token-seq '}'
3220/// any token but '(', ')', '[', ']', '{', or '}'
3221bool Parser::ParseCXX11AttributeArgs(IdentifierInfo *AttrName,
3222 SourceLocation AttrNameLoc,
3223 ParsedAttributes &Attrs,
3224 SourceLocation *EndLoc,
3225 IdentifierInfo *ScopeName,
3226 SourceLocation ScopeLoc) {
3227 assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
3228
3229 // If the attribute isn't known, we will not attempt to parse any
3230 // arguments.
3231 if (!hasAttribute(AttrSyntax::CXX, ScopeName, AttrName,
3232 getTargetInfo().getTriple(), getLangOpts())) {
3233 // Eat the left paren, then skip to the ending right paren.
3234 ConsumeParen();
3235 SkipUntil(tok::r_paren);
3236 return false;
3237 }
3238
3239 if (ScopeName && ScopeName->getName() == "gnu")
3240 // GNU-scoped attributes have some special cases to handle GNU-specific
3241 // behaviors.
3242 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
3243 ScopeLoc, AttributeList::AS_CXX11, 0);
3244 else
3245 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
3246 ScopeLoc, AttributeList::AS_CXX11);
3247 return true;
3248}
3249
3250/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003251///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003252/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003253/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003254/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003255///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003256/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003257/// attribute[opt]
3258/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003259/// attribute '...'
3260/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003261///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003262/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003263/// attribute-token attribute-argument-clause[opt]
3264///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003265/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003266/// identifier
3267/// attribute-scoped-token
3268///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003269/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003270/// attribute-namespace '::' identifier
3271///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003272/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003273/// identifier
Richard Smith3dff2512012-04-10 03:25:07 +00003274void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003275 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003276 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003277 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003278 ParseAlignmentSpecifier(attrs, endLoc);
3279 return;
3280 }
3281
Alexis Hunt96d5c762009-11-21 08:43:09 +00003282 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003283 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003284
Richard Smithf679b5b2011-10-14 20:48:27 +00003285 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3286
Alexis Hunt96d5c762009-11-21 08:43:09 +00003287 ConsumeBracket();
3288 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003289
Richard Smith10876ef2013-01-17 01:30:42 +00003290 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3291
Richard Smith3dff2512012-04-10 03:25:07 +00003292 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003293 // attribute not present
Alp Toker97650562014-01-10 11:19:30 +00003294 if (TryConsumeToken(tok::comma))
Alexis Hunt96d5c762009-11-21 08:43:09 +00003295 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003296
Richard Smith3dff2512012-04-10 03:25:07 +00003297 SourceLocation ScopeLoc, AttrLoc;
3298 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3299
3300 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3301 if (!AttrName)
3302 // Break out to the "expected ']'" diagnostic.
3303 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003304
Alexis Hunt96d5c762009-11-21 08:43:09 +00003305 // scoped attribute
Alp Toker97650562014-01-10 11:19:30 +00003306 if (TryConsumeToken(tok::coloncolon)) {
Richard Smith3dff2512012-04-10 03:25:07 +00003307 ScopeName = AttrName;
3308 ScopeLoc = AttrLoc;
3309
3310 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3311 if (!AttrName) {
Alp Tokerec543272013-12-24 09:48:30 +00003312 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003313 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003314 continue;
3315 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003316 }
3317
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003318 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003319 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003320
Richard Smith10876ef2013-01-17 01:30:42 +00003321 if (StandardAttr &&
3322 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3323 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003324 << AttrName << SourceRange(SeenAttrs[AttrName]);
Richard Smith10876ef2013-01-17 01:30:42 +00003325
Michael Han23214e52012-10-03 01:56:22 +00003326 // Parse attribute arguments
3327 if (Tok.is(tok::l_paren)) {
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003328 if (StandardAttr)
3329 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
Michael Han23214e52012-10-03 01:56:22 +00003330 << AttrName->getName();
3331
Aaron Ballmanb8e20392014-03-31 17:32:39 +00003332 AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3333 ScopeName, ScopeLoc);
Michael Han23214e52012-10-03 01:56:22 +00003334 }
3335
3336 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003337 attrs.addNew(AttrName,
3338 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3339 AttrLoc),
Aaron Ballman00e99962013-08-31 01:11:41 +00003340 ScopeName, ScopeLoc, 0, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003341
Alp Toker97650562014-01-10 11:19:30 +00003342 if (TryConsumeToken(tok::ellipsis))
Michael Han23214e52012-10-03 01:56:22 +00003343 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3344 << AttrName->getName();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003345 }
3346
Alp Toker383d2c42014-01-01 03:08:43 +00003347 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003348 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003349 if (endLoc)
3350 *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003351 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003352 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003353}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003354
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003355/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003356///
3357/// attribute-specifier-seq:
3358/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003359void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003360 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003361 assert(getLangOpts().CPlusPlus11);
3362
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003363 SourceLocation StartLoc = Tok.getLocation(), Loc;
3364 if (!endLoc)
3365 endLoc = &Loc;
3366
Douglas Gregor6f981002011-10-07 20:35:25 +00003367 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003368 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003369 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003370
3371 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003372}
3373
Richard Smithc2c8bb82013-10-15 01:34:54 +00003374void Parser::DiagnoseAndSkipCXX11Attributes() {
3375 if (!isCXX11AttributeSpecifier())
3376 return;
3377
3378 // Start and end location of an attribute or an attribute list.
3379 SourceLocation StartLoc = Tok.getLocation();
3380 SourceLocation EndLoc;
3381
3382 do {
3383 if (Tok.is(tok::l_square)) {
3384 BalancedDelimiterTracker T(*this, tok::l_square);
3385 T.consumeOpen();
3386 T.skipToEnd();
3387 EndLoc = T.getCloseLocation();
3388 } else {
3389 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3390 ConsumeToken();
3391 BalancedDelimiterTracker T(*this, tok::l_paren);
3392 if (!T.consumeOpen())
3393 T.skipToEnd();
3394 EndLoc = T.getCloseLocation();
3395 }
3396 } while (isCXX11AttributeSpecifier());
3397
3398 if (EndLoc.isValid()) {
3399 SourceRange Range(StartLoc, EndLoc);
3400 Diag(StartLoc, diag::err_attributes_not_allowed)
3401 << Range;
3402 }
3403}
3404
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003405/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3406///
3407/// [MS] ms-attribute:
3408/// '[' token-seq ']'
3409///
3410/// [MS] ms-attribute-seq:
3411/// ms-attribute[opt]
3412/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00003413void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3414 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003415 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3416
3417 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003418 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003419 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003420 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
John McCall53fa7142010-12-24 02:08:15 +00003421 if (endLoc) *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003422 ExpectAndConsume(tok::r_square);
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003423 }
3424}
Francois Pichet8f981d52011-05-25 10:19:49 +00003425
3426void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3427 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00003428 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00003429 if (ParseMicrosoftIfExistsCondition(Result))
3430 return;
3431
Douglas Gregor43edb322011-10-24 22:31:10 +00003432 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3433 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00003434 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet8f981d52011-05-25 10:19:49 +00003435 return;
3436 }
Francois Pichet8f981d52011-05-25 10:19:49 +00003437
Douglas Gregor43edb322011-10-24 22:31:10 +00003438 switch (Result.Behavior) {
3439 case IEB_Parse:
3440 // Parse the declarations below.
3441 break;
3442
3443 case IEB_Dependent:
3444 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3445 << Result.IsIfExists;
3446 // Fall through to skip.
3447
3448 case IEB_Skip:
3449 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00003450 return;
3451 }
3452
Richard Smith34f30512013-11-23 04:06:09 +00003453 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003454 // __if_exists, __if_not_exists can nest.
3455 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3456 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3457 continue;
3458 }
3459
3460 // Check for extraneous top-level semicolon.
3461 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003462 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00003463 continue;
3464 }
3465
3466 AccessSpecifier AS = getAccessSpecifierIfPresent();
3467 if (AS != AS_none) {
3468 // Current token is a C++ access specifier.
3469 CurAS = AS;
3470 SourceLocation ASLoc = Tok.getLocation();
3471 ConsumeToken();
3472 if (Tok.is(tok::colon))
3473 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3474 else
Alp Toker35d87032013-12-30 23:29:50 +00003475 Diag(Tok, diag::err_expected) << tok::colon;
Francois Pichet8f981d52011-05-25 10:19:49 +00003476 ConsumeToken();
3477 continue;
3478 }
3479
3480 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00003481 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet8f981d52011-05-25 10:19:49 +00003482 }
Douglas Gregor43edb322011-10-24 22:31:10 +00003483
3484 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00003485}