blob: c7c8625f2c0c2aa4edf3a705f87d88cce6953496 [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"
Jordan Rosea7d03842013-02-08 22:30:41 +000018#include "clang/Basic/CharInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Basic/OperatorKinds.h"
Chris Lattner60f36222009-01-29 05:15:15 +000020#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/DeclSpec.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Sema/Scope.h"
John McCalldb632ac2012-09-25 07:32:39 +000025#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000026#include "llvm/ADT/SmallString.h"
Chris Lattnera5235172007-08-25 06:57:03 +000027using namespace clang;
28
29/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redl67667942010-08-27 23:12:46 +000030/// may either be a top level namespace or a block-level namespace alias. If
31/// there was an inline keyword, it has already been parsed.
Chris Lattnera5235172007-08-25 06:57:03 +000032///
33/// namespace-definition: [C++ 7.3: basic.namespace]
34/// named-namespace-definition
35/// unnamed-namespace-definition
36///
37/// unnamed-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000038/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000039///
40/// named-namespace-definition:
41/// original-namespace-definition
42/// extension-namespace-definition
43///
44/// original-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000045/// 'inline'[opt] 'namespace' identifier attributes[opt]
46/// '{' namespace-body '}'
Chris Lattnera5235172007-08-25 06:57:03 +000047///
48/// extension-namespace-definition:
Sebastian Redl67667942010-08-27 23:12:46 +000049/// 'inline'[opt] 'namespace' original-namespace-name
50/// '{' namespace-body '}'
Mike Stump11289f42009-09-09 15:08:12 +000051///
Chris Lattnera5235172007-08-25 06:57:03 +000052/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
53/// 'namespace' identifier '=' qualified-namespace-specifier ';'
54///
John McCall48871652010-08-21 09:40:31 +000055Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redl67667942010-08-27 23:12:46 +000056 SourceLocation &DeclEnd,
57 SourceLocation InlineLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000058 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattnera5235172007-08-25 06:57:03 +000059 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian4bf82622011-08-22 17:59:19 +000060 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +000061
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000062 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000063 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000064 cutOffParsing();
65 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +000066 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +000067
Chris Lattnera5235172007-08-25 06:57:03 +000068 SourceLocation IdentLoc;
69 IdentifierInfo *Ident = 0;
Richard Trieu61384cb2011-05-26 20:11:09 +000070 std::vector<SourceLocation> ExtraIdentLoc;
71 std::vector<IdentifierInfo*> ExtraIdent;
72 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6b6bba42009-06-17 19:49:00 +000073
74 Token attrTok;
Mike Stump11289f42009-09-09 15:08:12 +000075
Chris Lattner76c72282007-10-09 17:33:22 +000076 if (Tok.is(tok::identifier)) {
Chris Lattnera5235172007-08-25 06:57:03 +000077 Ident = Tok.getIdentifierInfo();
78 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieu61384cb2011-05-26 20:11:09 +000079 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
80 ExtraNamespaceLoc.push_back(ConsumeToken());
81 ExtraIdent.push_back(Tok.getIdentifierInfo());
82 ExtraIdentLoc.push_back(ConsumeToken());
83 }
Chris Lattnera5235172007-08-25 06:57:03 +000084 }
Mike Stump11289f42009-09-09 15:08:12 +000085
Chris Lattnera5235172007-08-25 06:57:03 +000086 // Read label attributes, if present.
John McCall084e83d2011-03-24 11:26:52 +000087 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000088 if (Tok.is(tok::kw___attribute)) {
89 attrTok = Tok;
John McCall53fa7142010-12-24 02:08:15 +000090 ParseGNUAttributes(attrs);
Douglas Gregor6b6bba42009-06-17 19:49:00 +000091 }
Mike Stump11289f42009-09-09 15:08:12 +000092
Douglas Gregor6b6bba42009-06-17 19:49:00 +000093 if (Tok.is(tok::equal)) {
Nico Weber729f1e22012-10-27 23:44:27 +000094 if (Ident == 0) {
Alp Tokerec543272013-12-24 09:48:30 +000095 Diag(Tok, diag::err_expected) << tok::identifier;
Nico Weber729f1e22012-10-27 23:44:27 +000096 // Skip to end of the definition and eat the ';'.
97 SkipUntil(tok::semi);
98 return 0;
99 }
John McCall53fa7142010-12-24 02:08:15 +0000100 if (!attrs.empty())
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000101 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redl67667942010-08-27 23:12:46 +0000102 if (InlineLoc.isValid())
103 Diag(InlineLoc, diag::err_inline_namespace_alias)
104 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000105 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6b6bba42009-06-17 19:49:00 +0000106 }
Mike Stump11289f42009-09-09 15:08:12 +0000107
Richard Trieu61384cb2011-05-26 20:11:09 +0000108
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000109 BalancedDelimiterTracker T(*this, tok::l_brace);
110 if (T.consumeOpen()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000111 if (!ExtraIdent.empty()) {
112 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
113 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
114 }
Alp Tokerec543272013-12-24 09:48:30 +0000115
116 if (Ident)
117 Diag(Tok, diag::err_expected) << tok::l_brace;
118 else
119 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
120
John McCall48871652010-08-21 09:40:31 +0000121 return 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000122 }
Mike Stump11289f42009-09-09 15:08:12 +0000123
Douglas Gregor0be31a22010-07-02 17:43:08 +0000124 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
125 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
126 getCurScope()->getFnParent()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000127 if (!ExtraIdent.empty()) {
128 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
129 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
130 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000131 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000132 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +0000133 return 0;
Douglas Gregor05cfc292010-05-14 05:08:22 +0000134 }
135
Richard Trieu61384cb2011-05-26 20:11:09 +0000136 if (!ExtraIdent.empty()) {
137 TentativeParsingAction TPA(*this);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000138 SkipUntil(tok::r_brace, StopBeforeMatch);
Richard Trieu61384cb2011-05-26 20:11:09 +0000139 Token rBraceToken = Tok;
140 TPA.Revert();
141
142 if (!rBraceToken.is(tok::r_brace)) {
143 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
144 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
145 } else {
Benjamin Kramerf546f412011-05-26 21:32:30 +0000146 std::string NamespaceFix;
Richard Trieu61384cb2011-05-26 20:11:09 +0000147 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
148 E = ExtraIdent.end(); I != E; ++I) {
149 NamespaceFix += " { namespace ";
150 NamespaceFix += (*I)->getName();
151 }
Benjamin Kramerf546f412011-05-26 21:32:30 +0000152
Richard Trieu61384cb2011-05-26 20:11:09 +0000153 std::string RBraces;
Benjamin Kramerf546f412011-05-26 21:32:30 +0000154 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieu61384cb2011-05-26 20:11:09 +0000155 RBraces += "} ";
Benjamin Kramerf546f412011-05-26 21:32:30 +0000156
Richard Trieu61384cb2011-05-26 20:11:09 +0000157 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
158 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
159 ExtraIdentLoc.back()),
160 NamespaceFix)
161 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
162 }
163 }
164
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000165 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith5d164bc2011-10-15 05:09:34 +0000166 if (InlineLoc.isValid())
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000167 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000168 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000169
Chris Lattner4de55aa2009-03-29 14:02:43 +0000170 // Enter a scope for the namespace.
171 ParseScope NamespaceScope(this, Scope::DeclScope);
172
John McCall48871652010-08-21 09:40:31 +0000173 Decl *NamespcDecl =
Abramo Bagnarab5545be2011-03-08 12:38:20 +0000174 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000175 IdentLoc, Ident, T.getOpenLocation(),
176 attrs.getList());
Chris Lattner4de55aa2009-03-29 14:02:43 +0000177
John McCallfaf5fb42010-08-26 23:41:50 +0000178 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
179 "parsing namespace");
Mike Stump11289f42009-09-09 15:08:12 +0000180
Richard Trieu61384cb2011-05-26 20:11:09 +0000181 // Parse the contents of the namespace. This includes parsing recovery on
182 // any improperly nested namespaces.
183 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000184 InlineLoc, attrs, T);
Mike Stump11289f42009-09-09 15:08:12 +0000185
Chris Lattner4de55aa2009-03-29 14:02:43 +0000186 // Leave the namespace scope.
187 NamespaceScope.Exit();
188
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000189 DeclEnd = T.getCloseLocation();
190 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner4de55aa2009-03-29 14:02:43 +0000191
192 return NamespcDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000193}
Chris Lattner38376f12008-01-12 07:05:38 +0000194
Richard Trieu61384cb2011-05-26 20:11:09 +0000195/// ParseInnerNamespace - Parse the contents of a namespace.
196void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
197 std::vector<IdentifierInfo*>& Ident,
198 std::vector<SourceLocation>& NamespaceLoc,
199 unsigned int index, SourceLocation& InlineLoc,
Richard Trieu61384cb2011-05-26 20:11:09 +0000200 ParsedAttributes& attrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000201 BalancedDelimiterTracker &Tracker) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000202 if (index == Ident.size()) {
Richard Smith34f30512013-11-23 04:06:09 +0000203 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Richard Trieu61384cb2011-05-26 20:11:09 +0000204 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000205 MaybeParseCXX11Attributes(attrs);
Richard Trieu61384cb2011-05-26 20:11:09 +0000206 MaybeParseMicrosoftAttributes(attrs);
207 ParseExternalDeclaration(attrs);
208 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000209
210 // The caller is what called check -- we are simply calling
211 // the close for it.
212 Tracker.consumeClose();
Richard Trieu61384cb2011-05-26 20:11:09 +0000213
214 return;
215 }
216
217 // Parse improperly nested namespaces.
218 ParseScope NamespaceScope(this, Scope::DeclScope);
219 Decl *NamespcDecl =
220 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
221 NamespaceLoc[index], IdentLoc[index],
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000222 Ident[index], Tracker.getOpenLocation(),
223 attrs.getList());
Richard Trieu61384cb2011-05-26 20:11:09 +0000224
225 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000226 attrs, Tracker);
Richard Trieu61384cb2011-05-26 20:11:09 +0000227
228 NamespaceScope.Exit();
229
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000230 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieu61384cb2011-05-26 20:11:09 +0000231}
232
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000233/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
234/// alias definition.
235///
John McCall48871652010-08-21 09:40:31 +0000236Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall084e83d2011-03-24 11:26:52 +0000237 SourceLocation AliasLoc,
238 IdentifierInfo *Alias,
239 SourceLocation &DeclEnd) {
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000240 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump11289f42009-09-09 15:08:12 +0000241
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000242 ConsumeToken(); // eat the '='.
Mike Stump11289f42009-09-09 15:08:12 +0000243
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000244 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000245 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000246 cutOffParsing();
247 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000248 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000249
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000250 CXXScopeSpec SS;
251 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000252 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000253
254 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
255 Diag(Tok, diag::err_expected_namespace_name);
256 // Skip to end of the definition and eat the ';'.
257 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000258 return 0;
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000259 }
260
261 // Parse identifier.
Anders Carlsson47952ae2009-03-28 22:53:22 +0000262 IdentifierInfo *Ident = Tok.getIdentifierInfo();
263 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000264
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000265 // Eat the ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000266 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000267 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
268 SkipUntil(tok::semi);
Mike Stump11289f42009-09-09 15:08:12 +0000269
Douglas Gregor0be31a22010-07-02 17:43:08 +0000270 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson47952ae2009-03-28 22:53:22 +0000271 SS, IdentLoc, Ident);
Anders Carlsson1894f0d42009-03-28 04:07:16 +0000272}
273
Chris Lattner38376f12008-01-12 07:05:38 +0000274/// ParseLinkage - We know that the current token is a string_literal
275/// and just before that, that extern was seen.
276///
277/// linkage-specification: [C++ 7.5p2: dcl.link]
278/// 'extern' string-literal '{' declaration-seq[opt] '}'
279/// 'extern' string-literal declaration
280///
Chris Lattner8ea64422010-11-09 20:15:55 +0000281Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Richard Smith4ee696d2014-02-17 23:25:27 +0000282 assert(isTokenStringLiteral() && "Not a string literal!");
283 ExprResult Lang = ParseStringLiteralExpression(false);
Chris Lattner38376f12008-01-12 07:05:38 +0000284
Douglas Gregor07665a62009-01-05 19:45:36 +0000285 ParseScope LinkageScope(this, Scope::DeclScope);
Richard Smith4ee696d2014-02-17 23:25:27 +0000286 Decl *LinkageSpec =
287 Lang.isInvalid()
288 ? 0
289 : Actions.ActOnStartLinkageSpecification(
290 getCurScope(), DS.getSourceRange().getBegin(), Lang.take(),
291 Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
Douglas Gregor07665a62009-01-05 19:45:36 +0000292
John McCall084e83d2011-03-24 11:26:52 +0000293 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000294 MaybeParseCXX11Attributes(attrs);
John McCall53fa7142010-12-24 02:08:15 +0000295 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000296
Douglas Gregor07665a62009-01-05 19:45:36 +0000297 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnara4d423992011-05-01 16:25:54 +0000298 // Reset the source range in DS, as the leading "extern"
299 // does not really belong to the inner declaration ...
300 DS.SetRangeStart(SourceLocation());
301 DS.SetRangeEnd(SourceLocation());
302 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000303 DS.setExternInLinkageSpec(true);
John McCall53fa7142010-12-24 02:08:15 +0000304 ParseExternalDeclaration(attrs, &DS);
Richard Smith4ee696d2014-02-17 23:25:27 +0000305 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
306 getCurScope(), LinkageSpec, SourceLocation())
307 : 0;
Mike Stump11289f42009-09-09 15:08:12 +0000308 }
Douglas Gregor29ff7d02008-12-16 22:23:02 +0000309
Douglas Gregorb65a9132010-02-07 08:38:28 +0000310 DS.abort();
311
John McCall53fa7142010-12-24 02:08:15 +0000312 ProhibitAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000313
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000314 BalancedDelimiterTracker T(*this, tok::l_brace);
315 T.consumeOpen();
Richard Smith77944862014-03-02 05:58:18 +0000316
317 unsigned NestedModules = 0;
318 while (true) {
319 switch (Tok.getKind()) {
320 case tok::annot_module_begin:
321 ++NestedModules;
322 ParseTopLevelDecl();
323 continue;
324
325 case tok::annot_module_end:
326 if (!NestedModules)
327 break;
328 --NestedModules;
329 ParseTopLevelDecl();
330 continue;
331
332 case tok::annot_module_include:
333 ParseTopLevelDecl();
334 continue;
335
336 case tok::eof:
337 break;
338
339 case tok::r_brace:
340 if (!NestedModules)
341 break;
342 // Fall through.
343 default:
344 ParsedAttributesWithRange attrs(AttrFactory);
345 MaybeParseCXX11Attributes(attrs);
346 MaybeParseMicrosoftAttributes(attrs);
347 ParseExternalDeclaration(attrs);
348 continue;
349 }
350
351 break;
Chris Lattner38376f12008-01-12 07:05:38 +0000352 }
353
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000354 T.consumeClose();
Richard Smith4ee696d2014-02-17 23:25:27 +0000355 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
356 getCurScope(), LinkageSpec, T.getCloseLocation())
357 : 0;
Chris Lattner38376f12008-01-12 07:05:38 +0000358}
Douglas Gregor556877c2008-04-13 21:30:24 +0000359
Douglas Gregord7c4d982008-12-30 03:27:21 +0000360/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
361/// using-directive. Assumes that current token is 'using'.
John McCall48871652010-08-21 09:40:31 +0000362Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000363 const ParsedTemplateInfo &TemplateInfo,
364 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000365 ParsedAttributesWithRange &attrs,
366 Decl **OwnedType) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000367 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000368 ObjCDeclContextSwitch ObjCDC(*this);
369
Douglas Gregord7c4d982008-12-30 03:27:21 +0000370 // Eat 'using'.
371 SourceLocation UsingLoc = ConsumeToken();
372
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000373 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000374 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000375 cutOffParsing();
376 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000377 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000378
John McCall9b72f892010-11-10 02:40:36 +0000379 // 'using namespace' means this is a using-directive.
380 if (Tok.is(tok::kw_namespace)) {
381 // Template parameters are always an error here.
382 if (TemplateInfo.Kind) {
383 SourceRange R = TemplateInfo.getSourceRange();
384 Diag(UsingLoc, diag::err_templated_using_directive)
385 << R << FixItHint::CreateRemoval(R);
386 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000387
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000388 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall9b72f892010-11-10 02:40:36 +0000389 }
390
Richard Smithdda56e42011-04-15 14:24:37 +0000391 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +0000392
393 // Using declarations can't have attributes.
John McCall53fa7142010-12-24 02:08:15 +0000394 ProhibitAttributes(attrs);
Chris Lattner9b01ca12009-01-06 06:55:51 +0000395
Fariborz Jahanian4bf82622011-08-22 17:59:19 +0000396 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000397 AS_none, OwnedType);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000398}
399
400/// ParseUsingDirective - Parse C++ using-directive, assumes
401/// that current token is 'namespace' and 'using' was already parsed.
402///
403/// using-directive: [C++ 7.3.p4: namespace.udir]
404/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
405/// namespace-name ;
406/// [GNU] using-directive:
407/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
408/// namespace-name attributes[opt] ;
409///
John McCall48871652010-08-21 09:40:31 +0000410Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000411 SourceLocation UsingLoc,
412 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000413 ParsedAttributes &attrs) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000414 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
415
416 // Eat 'namespace'.
417 SourceLocation NamespcLoc = ConsumeToken();
418
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000419 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000420 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000421 cutOffParsing();
422 return 0;
Douglas Gregor7e90c6d2009-09-18 19:03:04 +0000423 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000424
Douglas Gregord7c4d982008-12-30 03:27:21 +0000425 CXXScopeSpec SS;
426 // Parse (optional) nested-name-specifier.
Douglas Gregordf593fb2011-11-07 17:33:42 +0000427 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000428
Douglas Gregord7c4d982008-12-30 03:27:21 +0000429 IdentifierInfo *NamespcName = 0;
430 SourceLocation IdentLoc = SourceLocation();
431
432 // Parse namespace-name.
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000433 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregord7c4d982008-12-30 03:27:21 +0000434 Diag(Tok, diag::err_expected_namespace_name);
435 // If there was invalid namespace name, skip to end of decl, and eat ';'.
436 SkipUntil(tok::semi);
437 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCall48871652010-08-21 09:40:31 +0000438 return 0;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000439 }
Mike Stump11289f42009-09-09 15:08:12 +0000440
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000441 // Parse identifier.
442 NamespcName = Tok.getIdentifierInfo();
443 IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000444
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000445 // Parse (optional) attributes (most likely GNU strong-using extension).
Alexis Hunt96d5c762009-11-21 08:43:09 +0000446 bool GNUAttr = false;
447 if (Tok.is(tok::kw___attribute)) {
448 GNUAttr = true;
John McCall53fa7142010-12-24 02:08:15 +0000449 ParseGNUAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000450 }
Mike Stump11289f42009-09-09 15:08:12 +0000451
Chris Lattnerce1da2c2009-01-06 07:27:21 +0000452 // Eat ';'.
Chris Lattner49836b42009-04-02 04:16:50 +0000453 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000454 if (ExpectAndConsume(tok::semi,
455 GNUAttr ? diag::err_expected_semi_after_attribute_list
456 : diag::err_expected_semi_after_namespace_name))
457 SkipUntil(tok::semi);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000458
Douglas Gregor0be31a22010-07-02 17:43:08 +0000459 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +0000460 IdentLoc, NamespcName, attrs.getList());
Douglas Gregord7c4d982008-12-30 03:27:21 +0000461}
462
Richard Smithdda56e42011-04-15 14:24:37 +0000463/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
464/// Assumes that 'using' was already seen.
Douglas Gregord7c4d982008-12-30 03:27:21 +0000465///
466/// using-declaration: [C++ 7.3.p3: namespace.udecl]
467/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregorfec52632009-06-20 00:51:54 +0000468/// unqualified-id
469/// 'using' :: unqualified-id
Douglas Gregord7c4d982008-12-30 03:27:21 +0000470///
Richard Smith810ad3e2013-01-29 10:02:16 +0000471/// alias-declaration: C++11 [dcl.dcl]p1
472/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smithdda56e42011-04-15 14:24:37 +0000473///
John McCall48871652010-08-21 09:40:31 +0000474Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall9b72f892010-11-10 02:40:36 +0000475 const ParsedTemplateInfo &TemplateInfo,
476 SourceLocation UsingLoc,
477 SourceLocation &DeclEnd,
Richard Smithcd1c0552011-07-01 19:46:12 +0000478 AccessSpecifier AS,
479 Decl **OwnedType) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000480 CXXScopeSpec SS;
John McCalle61f2ba2009-11-18 02:36:19 +0000481 SourceLocation TypenameLoc;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000482 bool HasTypenameKeyword = false;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000483
Richard Smithc2c8bb82013-10-15 01:34:54 +0000484 // Check for misplaced attributes before the identifier in an
485 // alias-declaration.
486 ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
487 MaybeParseCXX11Attributes(MisplacedAttrs);
Douglas Gregorfec52632009-06-20 00:51:54 +0000488
489 // Ignore optional 'typename'.
Douglas Gregor220f4272009-11-04 16:30:06 +0000490 // FIXME: This is wrong; we should parse this as a typename-specifier.
Alp Toker97650562014-01-10 11:19:30 +0000491 if (TryConsumeToken(tok::kw_typename, TypenameLoc))
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000492 HasTypenameKeyword = true;
Douglas Gregorfec52632009-06-20 00:51:54 +0000493
494 // Parse nested-name-specifier.
Richard Smith7447af42013-03-26 01:15:19 +0000495 IdentifierInfo *LastII = 0;
496 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
497 /*MayBePseudoDtor=*/0, /*IsTypename=*/false,
498 /*LastII=*/&LastII);
Douglas Gregorfec52632009-06-20 00:51:54 +0000499
Douglas Gregorfec52632009-06-20 00:51:54 +0000500 // Check nested-name specifier.
501 if (SS.isInvalid()) {
502 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000503 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000504 }
Douglas Gregor220f4272009-11-04 16:30:06 +0000505
Richard Smith7447af42013-03-26 01:15:19 +0000506 SourceLocation TemplateKWLoc;
507 UnqualifiedId Name;
508
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000509 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor220f4272009-11-04 16:30:06 +0000510 // destructor names and allow the action module to diagnose any semantic
511 // errors.
Richard Smith7447af42013-03-26 01:15:19 +0000512 //
513 // C++11 [class.qual]p2:
514 // [...] in a using-declaration that is a member-declaration, if the name
515 // specified after the nested-name-specifier is the same as the identifier
516 // or the simple-template-id's template-name in the last component of the
517 // nested-name-specifier, the name is [...] considered to name the
518 // constructor.
519 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
520 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
521 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
522 !SS.getScopeRep()->getAsNamespace() &&
523 !SS.getScopeRep()->getAsNamespaceAlias()) {
524 SourceLocation IdLoc = ConsumeToken();
525 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
526 Name.setConstructorName(Type, IdLoc, IdLoc);
527 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
528 /*AllowDestructorName=*/ true,
529 /*AllowConstructorName=*/ true, ParsedType(),
530 TemplateKWLoc, Name)) {
Douglas Gregorfec52632009-06-20 00:51:54 +0000531 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000532 return 0;
Douglas Gregorfec52632009-06-20 00:51:54 +0000533 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000534
Richard Smithc2c8bb82013-10-15 01:34:54 +0000535 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith37a45dd2013-10-24 01:21:09 +0000536 MaybeParseGNUAttributes(Attrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000537 MaybeParseCXX11Attributes(Attrs);
Richard Smithdda56e42011-04-15 14:24:37 +0000538
539 // Maybe this is an alias-declaration.
Richard Smithdda56e42011-04-15 14:24:37 +0000540 TypeResult TypeAlias;
Richard Smithc2c8bb82013-10-15 01:34:54 +0000541 bool IsAliasDecl = Tok.is(tok::equal);
Richard Smithdda56e42011-04-15 14:24:37 +0000542 if (IsAliasDecl) {
Richard Smithc2c8bb82013-10-15 01:34:54 +0000543 // If we had any misplaced attributes from earlier, this is where they
544 // should have been written.
545 if (MisplacedAttrs.Range.isValid()) {
546 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
547 << FixItHint::CreateInsertionFromRange(
548 Tok.getLocation(),
549 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
550 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
551 Attrs.takeAllFrom(MisplacedAttrs);
552 }
553
Richard Smithdda56e42011-04-15 14:24:37 +0000554 ConsumeToken();
555
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000556 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +0000557 diag::warn_cxx98_compat_alias_declaration :
558 diag::ext_alias_declaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000559
Richard Smith3f1b5d02011-05-05 21:57:07 +0000560 // Type alias templates cannot be specialized.
561 int SpecKind = -1;
Richard Smith14034022011-05-05 22:36:10 +0000562 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
563 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000564 SpecKind = 0;
565 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
566 SpecKind = 1;
567 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
568 SpecKind = 2;
569 if (SpecKind != -1) {
570 SourceRange Range;
571 if (SpecKind == 0)
572 Range = SourceRange(Name.TemplateId->LAngleLoc,
573 Name.TemplateId->RAngleLoc);
574 else
575 Range = TemplateInfo.getSourceRange();
576 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
577 << SpecKind << Range;
578 SkipUntil(tok::semi);
579 return 0;
580 }
581
Richard Smithdda56e42011-04-15 14:24:37 +0000582 // Name must be an identifier.
583 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
584 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
585 // No removal fixit: can't recover from this.
586 SkipUntil(tok::semi);
587 return 0;
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000588 } else if (HasTypenameKeyword)
Richard Smithdda56e42011-04-15 14:24:37 +0000589 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
590 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
591 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
592 else if (SS.isNotEmpty())
593 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
594 << FixItHint::CreateRemoval(SS.getRange());
595
Richard Smith3f1b5d02011-05-05 21:57:07 +0000596 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
597 Declarator::AliasTemplateContext :
Richard Smith54ecd982013-02-20 19:22:51 +0000598 Declarator::AliasDeclContext, AS, OwnedType,
599 &Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000600 } else {
601 // C++11 attributes are not allowed on a using-declaration, but GNU ones
602 // are.
Richard Smithc2c8bb82013-10-15 01:34:54 +0000603 ProhibitAttributes(MisplacedAttrs);
Richard Smith54ecd982013-02-20 19:22:51 +0000604 ProhibitAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000605
Richard Smithdda56e42011-04-15 14:24:37 +0000606 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith54ecd982013-02-20 19:22:51 +0000607 MaybeParseGNUAttributes(Attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +0000608 }
Mike Stump11289f42009-09-09 15:08:12 +0000609
Douglas Gregorfec52632009-06-20 00:51:54 +0000610 // Eat ';'.
611 DeclEnd = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000612 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
613 !Attrs.empty() ? "attributes list"
614 : IsAliasDecl ? "alias declaration"
615 : "using declaration"))
616 SkipUntil(tok::semi);
Douglas Gregorfec52632009-06-20 00:51:54 +0000617
John McCall9b72f892010-11-10 02:40:36 +0000618 // Diagnose an attempt to declare a templated using-declaration.
Richard Smith810ad3e2013-01-29 10:02:16 +0000619 // In C++11, alias-declarations can be templates:
Richard Smithdda56e42011-04-15 14:24:37 +0000620 // template <...> using id = type;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000621 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall9b72f892010-11-10 02:40:36 +0000622 SourceRange R = TemplateInfo.getSourceRange();
623 Diag(UsingLoc, diag::err_templated_using_declaration)
624 << R << FixItHint::CreateRemoval(R);
625
626 // Unfortunately, we have to bail out instead of recovering by
627 // ignoring the parameters, just in case the nested name specifier
628 // depends on the parameters.
629 return 0;
630 }
631
Douglas Gregor882a61a2011-09-26 14:30:28 +0000632 // "typename" keyword is allowed for identifiers only,
633 // because it may be a type definition.
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000634 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor882a61a2011-09-26 14:30:28 +0000635 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
636 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000637 // Proceed parsing, but reset the HasTypenameKeyword flag.
638 HasTypenameKeyword = false;
Douglas Gregor882a61a2011-09-26 14:30:28 +0000639 }
640
Richard Smith3f1b5d02011-05-05 21:57:07 +0000641 if (IsAliasDecl) {
642 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramercc4c49d2012-08-23 23:38:35 +0000643 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3f1b5d02011-05-05 21:57:07 +0000644 TemplateParams ? TemplateParams->data() : 0,
645 TemplateParams ? TemplateParams->size() : 0);
646 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith54ecd982013-02-20 19:22:51 +0000647 UsingLoc, Name, Attrs.getList(),
648 TypeAlias);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000649 }
Richard Smithdda56e42011-04-15 14:24:37 +0000650
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +0000651 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
652 /* HasUsingKeyword */ true, UsingLoc,
653 SS, Name, Attrs.getList(),
654 HasTypenameKeyword, TypenameLoc);
Douglas Gregord7c4d982008-12-30 03:27:21 +0000655}
656
Benjamin Kramere56f3932011-12-23 17:00:35 +0000657/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000658///
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000659/// [C++0x] static_assert-declaration:
660/// static_assert ( constant-expression , string-literal ) ;
661///
Benjamin Kramere56f3932011-12-23 17:00:35 +0000662/// [C11] static_assert-declaration:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000663/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000664///
John McCall48871652010-08-21 09:40:31 +0000665Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000666 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
667 "Not a static_assert declaration");
668
David Blaikiebbafb8a2012-03-11 07:00:24 +0000669 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramere56f3932011-12-23 17:00:35 +0000670 Diag(Tok, diag::ext_c11_static_assert);
Richard Smithb15c11c2011-10-17 23:06:20 +0000671 if (Tok.is(tok::kw_static_assert))
672 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000673
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000674 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000675
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000676 BalancedDelimiterTracker T(*this, tok::l_paren);
677 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000678 Diag(Tok, diag::err_expected) << tok::l_paren;
Richard Smith76965712012-09-13 19:12:50 +0000679 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000680 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000681 }
Mike Stump11289f42009-09-09 15:08:12 +0000682
John McCalldadc5752010-08-24 06:29:42 +0000683 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000684 if (AssertExpr.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000685 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000686 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000687 }
Mike Stump11289f42009-09-09 15:08:12 +0000688
Alp Toker383d2c42014-01-01 03:08:43 +0000689 if (ExpectAndConsume(tok::comma)) {
690 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +0000691 return 0;
Alp Toker383d2c42014-01-01 03:08:43 +0000692 }
Anders Carlssonb4cf3ad2009-03-13 23:29:20 +0000693
Richard Smithf506eaf2012-03-05 23:20:05 +0000694 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000695 Diag(Tok, diag::err_expected_string_literal)
696 << /*Source='static_assert'*/1;
Richard Smith76965712012-09-13 19:12:50 +0000697 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000698 return 0;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000699 }
Mike Stump11289f42009-09-09 15:08:12 +0000700
John McCalldadc5752010-08-24 06:29:42 +0000701 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smithd67aea22012-03-06 03:21:47 +0000702 if (AssertMessage.isInvalid()) {
Richard Smith76965712012-09-13 19:12:50 +0000703 SkipMalformedDecl();
John McCall48871652010-08-21 09:40:31 +0000704 return 0;
Richard Smithd67aea22012-03-06 03:21:47 +0000705 }
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000706
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000707 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +0000708
Chris Lattner49836b42009-04-02 04:16:50 +0000709 DeclEnd = Tok.getLocation();
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000710 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000711
John McCallb268a282010-08-23 23:25:46 +0000712 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
713 AssertExpr.take(),
Abramo Bagnaraea947882011-03-08 16:41:52 +0000714 AssertMessage.take(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000715 T.getCloseLocation());
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000716}
717
Richard Smith74aeef52013-04-26 16:15:35 +0000718/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson74948d02009-06-24 17:47:40 +0000719///
720/// 'decltype' ( expression )
Richard Smith74aeef52013-04-26 16:15:35 +0000721/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson74948d02009-06-24 17:47:40 +0000722///
David Blaikie15a430a2011-12-04 05:04:18 +0000723SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
724 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
725 && "Not a decltype specifier");
726
David Blaikie15a430a2011-12-04 05:04:18 +0000727 ExprResult Result;
728 SourceLocation StartLoc = Tok.getLocation();
729 SourceLocation EndLoc;
730
731 if (Tok.is(tok::annot_decltype)) {
732 Result = getExprAnnotation(Tok);
733 EndLoc = Tok.getAnnotationEndLoc();
734 ConsumeToken();
735 if (Result.isInvalid()) {
736 DS.SetTypeSpecError();
737 return EndLoc;
738 }
739 } else {
Richard Smith324df552012-02-24 22:30:04 +0000740 if (Tok.getIdentifierInfo()->isStr("decltype"))
741 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smithfd3da932012-02-24 18:10:23 +0000742
David Blaikie15a430a2011-12-04 05:04:18 +0000743 ConsumeToken();
744
745 BalancedDelimiterTracker T(*this, tok::l_paren);
746 if (T.expectAndConsume(diag::err_expected_lparen_after,
747 "decltype", tok::r_paren)) {
748 DS.SetTypeSpecError();
749 return T.getOpenLocation() == Tok.getLocation() ?
750 StartLoc : T.getOpenLocation();
751 }
752
Richard Smith74aeef52013-04-26 16:15:35 +0000753 // Check for C++1y 'decltype(auto)'.
754 if (Tok.is(tok::kw_auto)) {
755 // No need to disambiguate here: an expression can't start with 'auto',
756 // because the typename-specifier in a function-style cast operation can't
757 // be 'auto'.
758 Diag(Tok.getLocation(),
759 getLangOpts().CPlusPlus1y
760 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
761 : diag::ext_decltype_auto_type_specifier);
762 ConsumeToken();
763 } else {
764 // Parse the expression
David Blaikie15a430a2011-12-04 05:04:18 +0000765
Richard Smith74aeef52013-04-26 16:15:35 +0000766 // C++11 [dcl.type.simple]p4:
767 // The operand of the decltype specifier is an unevaluated operand.
768 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
769 0, /*IsDecltype=*/true);
770 Result = ParseExpression();
771 if (Result.isInvalid()) {
772 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +0000773 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smith74aeef52013-04-26 16:15:35 +0000774 EndLoc = ConsumeParen();
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000775 } else {
Richard Smith74aeef52013-04-26 16:15:35 +0000776 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
777 // Backtrack to get the location of the last token before the semi.
778 PP.RevertCachedTokens(2);
779 ConsumeToken(); // the semi.
780 EndLoc = ConsumeAnyToken();
781 assert(Tok.is(tok::semi));
782 } else {
783 EndLoc = Tok.getLocation();
784 }
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000785 }
Richard Smith74aeef52013-04-26 16:15:35 +0000786 return EndLoc;
Argyrios Kyrtzidisc38395a2012-10-26 22:53:44 +0000787 }
Richard Smith74aeef52013-04-26 16:15:35 +0000788
789 Result = Actions.ActOnDecltypeExpression(Result.take());
David Blaikie15a430a2011-12-04 05:04:18 +0000790 }
791
792 // Match the ')'
793 T.consumeClose();
794 if (T.getCloseLocation().isInvalid()) {
795 DS.SetTypeSpecError();
796 // FIXME: this should return the location of the last token
797 // that was consumed (by "consumeClose()")
798 return T.getCloseLocation();
799 }
800
Richard Smithfd555f62012-02-22 02:04:18 +0000801 if (Result.isInvalid()) {
802 DS.SetTypeSpecError();
803 return T.getCloseLocation();
804 }
805
David Blaikie15a430a2011-12-04 05:04:18 +0000806 EndLoc = T.getCloseLocation();
Anders Carlsson74948d02009-06-24 17:47:40 +0000807 }
Richard Smith74aeef52013-04-26 16:15:35 +0000808 assert(!Result.isInvalid());
Mike Stump11289f42009-09-09 15:08:12 +0000809
Anders Carlsson74948d02009-06-24 17:47:40 +0000810 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000811 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000812 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Anders Carlsson74948d02009-06-24 17:47:40 +0000813 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smith74aeef52013-04-26 16:15:35 +0000814 if (Result.get()
815 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000816 DiagID, Result.release(), Policy)
Richard Smith74aeef52013-04-26 16:15:35 +0000817 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000818 DiagID, Policy)) {
John McCall49bfce42009-08-03 20:12:06 +0000819 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie15a430a2011-12-04 05:04:18 +0000820 DS.SetTypeSpecError();
821 }
822 return EndLoc;
823}
824
825void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
826 SourceLocation StartLoc,
827 SourceLocation EndLoc) {
828 // make sure we have a token we can turn into an annotation token
829 if (PP.isBacktrackEnabled())
830 PP.RevertCachedTokens(1);
831 else
832 PP.EnterToken(Tok);
833
834 Tok.setKind(tok::annot_decltype);
Richard Smith74aeef52013-04-26 16:15:35 +0000835 setExprAnnotation(Tok,
836 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
837 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
838 ExprError());
David Blaikie15a430a2011-12-04 05:04:18 +0000839 Tok.setAnnotationEndLoc(EndLoc);
840 Tok.setLocation(StartLoc);
841 PP.AnnotateCachedTokens(Tok);
Anders Carlsson74948d02009-06-24 17:47:40 +0000842}
843
Alexis Hunt4a257072011-05-19 05:37:45 +0000844void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
845 assert(Tok.is(tok::kw___underlying_type) &&
846 "Not an underlying type specifier");
847
848 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000849 BalancedDelimiterTracker T(*this, tok::l_paren);
850 if (T.expectAndConsume(diag::err_expected_lparen_after,
851 "__underlying_type", tok::r_paren)) {
Alexis Hunt4a257072011-05-19 05:37:45 +0000852 return;
853 }
854
855 TypeResult Result = ParseTypeName();
856 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000857 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt4a257072011-05-19 05:37:45 +0000858 return;
859 }
860
861 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000862 T.consumeClose();
863 if (T.getCloseLocation().isInvalid())
Alexis Hunt4a257072011-05-19 05:37:45 +0000864 return;
865
866 const char *PrevSpec = 0;
867 unsigned DiagID;
Alexis Hunte852b102011-05-24 22:41:36 +0000868 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000869 DiagID, Result.release(),
870 Actions.getASTContext().getPrintingPolicy()))
Alexis Hunt4a257072011-05-19 05:37:45 +0000871 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanellaa90af722013-07-06 18:54:58 +0000872 DS.setTypeofParensRange(T.getRange());
Alexis Hunt4a257072011-05-19 05:37:45 +0000873}
874
David Blaikie00ee7a082011-10-25 15:01:20 +0000875/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
876/// class name or decltype-specifier. Note that we only check that the result
877/// names a type; semantic analysis will need to verify that the type names a
878/// class. The result is either a type or null, depending on whether a type
879/// name was found.
Douglas Gregor831c93f2008-11-05 20:51:48 +0000880///
Richard Smith4c96e992013-02-19 23:47:15 +0000881/// base-type-specifier: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000882/// class-or-decltype
Richard Smith4c96e992013-02-19 23:47:15 +0000883/// class-or-decltype: [C++11 class.derived]
David Blaikie00ee7a082011-10-25 15:01:20 +0000884/// nested-name-specifier[opt] class-name
885/// decltype-specifier
Richard Smith4c96e992013-02-19 23:47:15 +0000886/// class-name: [C++ class.name]
Douglas Gregor831c93f2008-11-05 20:51:48 +0000887/// identifier
Douglas Gregord54dfb82009-02-25 23:52:28 +0000888/// simple-template-id
Mike Stump11289f42009-09-09 15:08:12 +0000889///
Richard Smith4c96e992013-02-19 23:47:15 +0000890/// In C++98, instead of base-type-specifier, we have:
891///
892/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie1cd50022011-10-25 17:10:12 +0000893Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
894 SourceLocation &EndLocation) {
David Blaikiedd58d4c2011-10-25 18:46:41 +0000895 // Ignore attempts to use typename
896 if (Tok.is(tok::kw_typename)) {
897 Diag(Tok, diag::err_expected_class_name_not_template)
898 << FixItHint::CreateRemoval(Tok.getLocation());
899 ConsumeToken();
900 }
901
David Blaikieafa155f2011-10-25 18:17:58 +0000902 // Parse optional nested-name-specifier
903 CXXScopeSpec SS;
904 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
905
906 BaseLoc = Tok.getLocation();
907
David Blaikie1cd50022011-10-25 17:10:12 +0000908 // Parse decltype-specifier
David Blaikie15a430a2011-12-04 05:04:18 +0000909 // tok == kw_decltype is just error recovery, it can only happen when SS
910 // isn't empty
911 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikieafa155f2011-10-25 18:17:58 +0000912 if (SS.isNotEmpty())
913 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
914 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie1cd50022011-10-25 17:10:12 +0000915 // Fake up a Declarator to use with ActOnTypeName.
916 DeclSpec DS(AttrFactory);
917
David Blaikie7491e732011-12-08 04:53:15 +0000918 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie1cd50022011-10-25 17:10:12 +0000919
920 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
921 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
922 }
923
Douglas Gregord54dfb82009-02-25 23:52:28 +0000924 // Check whether we have a template-id that names a type.
925 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +0000926 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +0000927 if (TemplateId->Kind == TNK_Type_template ||
928 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +0000929 AnnotateTemplateIdTokenAsType();
Douglas Gregord54dfb82009-02-25 23:52:28 +0000930
931 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +0000932 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregord54dfb82009-02-25 23:52:28 +0000933 EndLocation = Tok.getAnnotationEndLoc();
934 ConsumeToken();
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000935
936 if (Type)
937 return Type;
938 return true;
Douglas Gregord54dfb82009-02-25 23:52:28 +0000939 }
940
941 // Fall through to produce an error below.
942 }
943
Douglas Gregor831c93f2008-11-05 20:51:48 +0000944 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000945 Diag(Tok, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000946 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +0000947 }
948
Douglas Gregor18473f32010-01-12 21:28:44 +0000949 IdentifierInfo *Id = Tok.getIdentifierInfo();
950 SourceLocation IdLoc = ConsumeToken();
951
952 if (Tok.is(tok::less)) {
953 // It looks the user intended to write a template-id here, but the
954 // template-name was wrong. Try to fix that.
955 TemplateNameKind TNK = TNK_Type_template;
956 TemplateTy Template;
Douglas Gregor0be31a22010-07-02 17:43:08 +0000957 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregore7c20652011-03-02 00:47:37 +0000958 &SS, Template, TNK)) {
Douglas Gregor18473f32010-01-12 21:28:44 +0000959 Diag(IdLoc, diag::err_unknown_template_name)
960 << Id;
961 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000962
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000963 if (!Template) {
964 TemplateArgList TemplateArgs;
965 SourceLocation LAngleLoc, RAngleLoc;
966 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
967 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor18473f32010-01-12 21:28:44 +0000968 return true;
Serge Pavlovb716b3c2013-08-10 05:54:47 +0000969 }
Douglas Gregor18473f32010-01-12 21:28:44 +0000970
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000971 // Form the template name
Douglas Gregor18473f32010-01-12 21:28:44 +0000972 UnqualifiedId TemplateName;
973 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000974
Douglas Gregor18473f32010-01-12 21:28:44 +0000975 // Parse the full template-id, then turn it into a type.
Abramo Bagnara7945c982012-01-27 09:46:47 +0000976 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
977 TemplateName, true))
Douglas Gregor18473f32010-01-12 21:28:44 +0000978 return true;
979 if (TNK == TNK_Dependent_template_name)
Douglas Gregore7c20652011-03-02 00:47:37 +0000980 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000981
Douglas Gregor18473f32010-01-12 21:28:44 +0000982 // If we didn't end up with a typename token, there's nothing more we
983 // can do.
984 if (Tok.isNot(tok::annot_typename))
985 return true;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +0000986
Douglas Gregor18473f32010-01-12 21:28:44 +0000987 // Retrieve the type from the annotation token, consume that token, and
988 // return.
989 EndLocation = Tok.getAnnotationEndLoc();
John McCallba7bf592010-08-24 05:47:05 +0000990 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor18473f32010-01-12 21:28:44 +0000991 ConsumeToken();
992 return Type;
993 }
994
Douglas Gregor831c93f2008-11-05 20:51:48 +0000995 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +0000996 IdentifierInfo *CorrectedII = 0;
Douglas Gregore7c20652011-03-02 00:47:37 +0000997 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor844cb502011-03-01 18:12:44 +0000998 false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +0000999 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrain9cb8e9f2012-06-22 23:37:05 +00001000 /*NonTrivialTypeSourceInfo=*/true,
1001 &CorrectedII);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001002 if (!Type) {
Douglas Gregorfe17d252010-02-16 19:09:40 +00001003 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001004 return true;
Douglas Gregor831c93f2008-11-05 20:51:48 +00001005 }
1006
1007 // Consume the identifier.
Douglas Gregor18473f32010-01-12 21:28:44 +00001008 EndLocation = IdLoc;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001009
1010 // Fake up a Declarator to use with ActOnTypeName.
John McCall084e83d2011-03-24 11:26:52 +00001011 DeclSpec DS(AttrFactory);
Nick Lewycky19b9f952010-07-26 16:56:01 +00001012 DS.SetRangeStart(IdLoc);
1013 DS.SetRangeEnd(EndLocation);
Douglas Gregore7c20652011-03-02 00:47:37 +00001014 DS.getTypeSpecScope() = SS;
Nick Lewycky19b9f952010-07-26 16:56:01 +00001015
1016 const char *PrevSpec = 0;
1017 unsigned DiagID;
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001018 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type,
1019 Actions.getASTContext().getPrintingPolicy());
Nick Lewycky19b9f952010-07-26 16:56:01 +00001020
1021 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1022 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001023}
1024
John McCall8d32c052012-05-22 21:28:12 +00001025void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
1026 while (Tok.is(tok::kw___single_inheritance) ||
1027 Tok.is(tok::kw___multiple_inheritance) ||
1028 Tok.is(tok::kw___virtual_inheritance)) {
1029 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1030 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman00e99962013-08-31 01:11:41 +00001031 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
Aaron Ballman8edb5c22013-12-18 23:44:18 +00001032 AttributeList::AS_Keyword);
John McCall8d32c052012-05-22 21:28:12 +00001033 }
1034}
1035
Richard Smith369b9f92012-06-25 21:37:02 +00001036/// Determine whether the following tokens are valid after a type-specifier
1037/// which could be a standalone declaration. This will conservatively return
1038/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith200f47c2012-07-02 19:14:01 +00001039bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smith369b9f92012-06-25 21:37:02 +00001040 // This switch enumerates the valid "follow" set for type-specifiers.
1041 switch (Tok.getKind()) {
1042 default: break;
1043 case tok::semi: // struct foo {...} ;
1044 case tok::star: // struct foo {...} * P;
1045 case tok::amp: // struct foo {...} & R = ...
Richard Smith1ac67d12013-01-19 03:48:05 +00001046 case tok::ampamp: // struct foo {...} && R = ...
Richard Smith369b9f92012-06-25 21:37:02 +00001047 case tok::identifier: // struct foo {...} V ;
1048 case tok::r_paren: //(struct foo {...} ) {4}
1049 case tok::annot_cxxscope: // struct foo {...} a:: b;
1050 case tok::annot_typename: // struct foo {...} a ::b;
1051 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1052 case tok::l_paren: // struct foo {...} ( x);
1053 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smith1ac67d12013-01-19 03:48:05 +00001054 case tok::kw_operator: // struct foo operator ++() {...}
Alp Tokerd3f79c52013-11-24 20:24:54 +00001055 case tok::kw___declspec: // struct foo {...} __declspec(...)
Richard Smith369b9f92012-06-25 21:37:02 +00001056 return true;
Richard Smith200f47c2012-07-02 19:14:01 +00001057 case tok::colon:
1058 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smith369b9f92012-06-25 21:37:02 +00001059 // Type qualifiers
1060 case tok::kw_const: // struct foo {...} const x;
1061 case tok::kw_volatile: // struct foo {...} volatile x;
1062 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001063 // Function specifiers
1064 // Note, no 'explicit'. An explicit function must be either a conversion
1065 // operator or a constructor. Either way, it can't have a return type.
1066 case tok::kw_inline: // struct foo inline f();
1067 case tok::kw_virtual: // struct foo virtual f();
1068 case tok::kw_friend: // struct foo friend f();
Richard Smith369b9f92012-06-25 21:37:02 +00001069 // Storage-class specifiers
1070 case tok::kw_static: // struct foo {...} static x;
1071 case tok::kw_extern: // struct foo {...} extern x;
1072 case tok::kw_typedef: // struct foo {...} typedef x;
1073 case tok::kw_register: // struct foo {...} register x;
1074 case tok::kw_auto: // struct foo {...} auto x;
1075 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smith1ac67d12013-01-19 03:48:05 +00001076 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smith369b9f92012-06-25 21:37:02 +00001077 case tok::kw_constexpr: // struct foo {...} constexpr x;
1078 // As shown above, type qualifiers and storage class specifiers absolutely
1079 // can occur after class specifiers according to the grammar. However,
1080 // almost no one actually writes code like this. If we see one of these,
1081 // it is much more likely that someone missed a semi colon and the
1082 // type/storage class specifier we're seeing is part of the *next*
1083 // intended declaration, as in:
1084 //
1085 // struct foo { ... }
1086 // typedef int X;
1087 //
1088 // We'd really like to emit a missing semicolon error instead of emitting
1089 // an error on the 'int' saying that you can't have two type specifiers in
1090 // the same declaration of X. Because of this, we look ahead past this
1091 // token to see if it's a type specifier. If so, we know the code is
1092 // otherwise invalid, so we can produce the expected semi error.
1093 if (!isKnownToBeTypeSpecifier(NextToken()))
1094 return true;
1095 break;
1096 case tok::r_brace: // struct bar { struct foo {...} }
1097 // Missing ';' at end of struct is accepted as an extension in C mode.
1098 if (!getLangOpts().CPlusPlus)
1099 return true;
1100 break;
Richard Smith1ac67d12013-01-19 03:48:05 +00001101 // C++11 attributes
1102 case tok::l_square: // enum E [[]] x
1103 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1104 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith52c5b872013-01-29 04:13:32 +00001105 case tok::greater:
1106 // template<class T = class X>
1107 return getLangOpts().CPlusPlus;
Richard Smith369b9f92012-06-25 21:37:02 +00001108 }
1109 return false;
1110}
1111
Douglas Gregor556877c2008-04-13 21:30:24 +00001112/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1113/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1114/// until we reach the start of a definition or see a token that
Richard Smithc5b05522012-03-12 07:56:15 +00001115/// cannot start a definition.
Douglas Gregor556877c2008-04-13 21:30:24 +00001116///
1117/// class-specifier: [C++ class]
1118/// class-head '{' member-specification[opt] '}'
1119/// class-head '{' member-specification[opt] '}' attributes[opt]
1120/// class-head:
1121/// class-key identifier[opt] base-clause[opt]
1122/// class-key nested-name-specifier identifier base-clause[opt]
1123/// class-key nested-name-specifier[opt] simple-template-id
1124/// base-clause[opt]
1125/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001126/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregor556877c2008-04-13 21:30:24 +00001127/// identifier base-clause[opt]
Mike Stump11289f42009-09-09 15:08:12 +00001128/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregor556877c2008-04-13 21:30:24 +00001129/// simple-template-id base-clause[opt]
1130/// class-key:
1131/// 'class'
1132/// 'struct'
1133/// 'union'
1134///
1135/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump11289f42009-09-09 15:08:12 +00001136/// class-key ::[opt] nested-name-specifier[opt] identifier
1137/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1138/// simple-template-id
Douglas Gregor556877c2008-04-13 21:30:24 +00001139///
1140/// Note that the C++ class-specifier and elaborated-type-specifier,
1141/// together, subsume the C99 struct-or-union-specifier:
1142///
1143/// struct-or-union-specifier: [C99 6.7.2.1]
1144/// struct-or-union identifier[opt] '{' struct-contents '}'
1145/// struct-or-union identifier
1146/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1147/// '}' attributes[opt]
1148/// [GNU] struct-or-union attributes[opt] identifier
1149/// struct-or-union:
1150/// 'struct'
1151/// 'union'
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001152void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1153 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001154 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregordf593fb2011-11-07 17:33:42 +00001155 AccessSpecifier AS,
Michael Han9407e502012-11-26 22:54:45 +00001156 bool EnteringContext, DeclSpecContext DSC,
Bill Wendling44426052012-12-20 19:22:21 +00001157 ParsedAttributesWithRange &Attributes) {
Joao Matose9a3ed42012-08-31 22:18:20 +00001158 DeclSpec::TST TagType;
1159 if (TagTokKind == tok::kw_struct)
1160 TagType = DeclSpec::TST_struct;
1161 else if (TagTokKind == tok::kw___interface)
1162 TagType = DeclSpec::TST_interface;
1163 else if (TagTokKind == tok::kw_class)
1164 TagType = DeclSpec::TST_class;
1165 else {
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001166 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1167 TagType = DeclSpec::TST_union;
1168 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001169
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001170 if (Tok.is(tok::code_completion)) {
1171 // Code completion for a struct, class, or union name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001172 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001173 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001174 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001175
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001176 // C++03 [temp.explicit] 14.7.2/8:
1177 // The usual access checking rules do not apply to names used to specify
1178 // explicit instantiations.
1179 //
1180 // As an extension we do not perform access checking on the names used to
1181 // specify explicit specializations either. This is important to allow
1182 // specializing traits classes for private types.
John McCall6347b682012-05-07 06:16:58 +00001183 //
1184 // Note that we don't suppress if this turns out to be an elaborated
1185 // type specifier.
1186 bool shouldDelayDiagsInTag =
1187 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1188 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1189 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth2d69ec72010-06-28 08:39:25 +00001190
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001191 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregor556877c2008-04-13 21:30:24 +00001192 // If attributes exist after tag, parse them.
Richard Smith37a45dd2013-10-24 01:21:09 +00001193 MaybeParseGNUAttributes(attrs);
Douglas Gregor556877c2008-04-13 21:30:24 +00001194
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001195 // If declspecs exist after tag, parse them.
John McCall0f8ccc42010-08-05 17:13:11 +00001196 while (Tok.is(tok::kw___declspec))
John McCall53fa7142010-12-24 02:08:15 +00001197 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001198
John McCall8d32c052012-05-22 21:28:12 +00001199 // Parse inheritance specifiers.
1200 if (Tok.is(tok::kw___single_inheritance) ||
1201 Tok.is(tok::kw___multiple_inheritance) ||
1202 Tok.is(tok::kw___virtual_inheritance))
Richard Smith37a45dd2013-10-24 01:21:09 +00001203 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCall8d32c052012-05-22 21:28:12 +00001204
Alexis Hunt96d5c762009-11-21 08:43:09 +00001205 // If C++0x attributes exist here, parse them.
1206 // FIXME: Are we consistent with the ordering of parsing of different
1207 // styles of attributes?
Richard Smith89645bc2013-01-02 12:01:23 +00001208 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00001209
Michael Han309af292013-01-07 16:57:11 +00001210 // Source location used by FIXIT to insert misplaced
1211 // C++11 attributes
1212 SourceLocation AttrFixitLoc = Tok.getLocation();
1213
Alp Toker53358e42013-12-17 14:12:30 +00001214 // GNU libstdc++ and libc++ use certain intrinsic names as the
1215 // name of struct templates, but some are keywords in GCC >= 4.3
1216 // MSVC and Clang. For compatibility, convert the token to an identifier
1217 // and issue a warning diagnostic.
1218 if (TagType == DeclSpec::TST_struct && !Tok.is(tok::identifier) &&
1219 !Tok.isAnnotation()) {
1220 const IdentifierInfo *II = Tok.getIdentifierInfo();
1221 // We rarely end up here so the following check is efficient.
1222 if (II && II->getName().startswith("__is_"))
1223 TryKeywordIdentFallback(true);
1224 }
Mike Stump11289f42009-09-09 15:08:12 +00001225
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001226 // Parse the (optional) nested-name-specifier.
John McCall9dab4e62009-12-12 11:40:51 +00001227 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001228 if (getLangOpts().CPlusPlus) {
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001229 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1230 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001231
Douglas Gregordf593fb2011-11-07 17:33:42 +00001232 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall413021a2010-07-30 06:26:29 +00001233 DS.SetTypeSpecError();
John McCall1f476a12010-02-26 08:45:28 +00001234 if (SS.isSet())
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001235 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
Alp Tokerec543272013-12-24 09:48:30 +00001236 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattnerd5c1c9d2009-12-10 00:32:41 +00001237 }
Douglas Gregor67a65642009-02-17 23:15:12 +00001238
Douglas Gregor916462b2009-10-30 21:46:58 +00001239 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1240
Douglas Gregor67a65642009-02-17 23:15:12 +00001241 // Parse the (optional) class name or simple-template-id.
Douglas Gregor556877c2008-04-13 21:30:24 +00001242 IdentifierInfo *Name = 0;
1243 SourceLocation NameLoc;
Douglas Gregor7f741122009-02-25 19:37:18 +00001244 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregor556877c2008-04-13 21:30:24 +00001245 if (Tok.is(tok::identifier)) {
1246 Name = Tok.getIdentifierInfo();
1247 NameLoc = ConsumeToken();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001248
David Blaikiebbafb8a2012-03-11 07:00:24 +00001249 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001250 // The name was supposed to refer to a template, but didn't.
Douglas Gregor916462b2009-10-30 21:46:58 +00001251 // Eat the template argument list and try to continue parsing this as
1252 // a class (or template thereof).
1253 TemplateArgList TemplateArgs;
Douglas Gregor916462b2009-10-30 21:46:58 +00001254 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregore7c20652011-03-02 00:47:37 +00001255 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor916462b2009-10-30 21:46:58 +00001256 true, LAngleLoc,
Douglas Gregorb53edfb2009-11-10 19:49:08 +00001257 TemplateArgs, RAngleLoc)) {
Douglas Gregor916462b2009-10-30 21:46:58 +00001258 // We couldn't parse the template argument list at all, so don't
1259 // try to give any location information for the list.
1260 LAngleLoc = RAngleLoc = SourceLocation();
1261 }
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001262
Douglas Gregor916462b2009-10-30 21:46:58 +00001263 Diag(NameLoc, diag::err_explicit_spec_non_template)
Alp Toker01d65e12014-01-06 12:54:41 +00001264 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1265 << TagTokKind << Name << SourceRange(LAngleLoc, RAngleLoc);
Joao Matose9a3ed42012-08-31 22:18:20 +00001266
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001267 // Strip off the last template parameter list if it was empty, since
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001268 // we've removed its template argument list.
1269 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1270 if (TemplateParams && TemplateParams->size() > 1) {
1271 TemplateParams->pop_back();
1272 } else {
1273 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001274 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001275 = ParsedTemplateInfo::NonTemplate;
1276 }
1277 } else if (TemplateInfo.Kind
1278 == ParsedTemplateInfo::ExplicitInstantiation) {
1279 // Pretend this is just a forward declaration.
Douglas Gregor916462b2009-10-30 21:46:58 +00001280 TemplateParams = 0;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001281 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor916462b2009-10-30 21:46:58 +00001282 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001283 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregor1d0015f2009-10-30 22:09:44 +00001284 = SourceLocation();
1285 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1286 = SourceLocation();
Douglas Gregor916462b2009-10-30 21:46:58 +00001287 }
Douglas Gregor916462b2009-10-30 21:46:58 +00001288 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001289 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001290 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor7f741122009-02-25 19:37:18 +00001291 NameLoc = ConsumeToken();
Douglas Gregor67a65642009-02-17 23:15:12 +00001292
Douglas Gregore7c20652011-03-02 00:47:37 +00001293 if (TemplateId->Kind != TNK_Type_template &&
1294 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001295 // The template-name in the simple-template-id refers to
1296 // something other than a class template. Give an appropriate
1297 // error message and skip to the ';'.
1298 SourceRange Range(NameLoc);
1299 if (SS.isNotEmpty())
1300 Range.setBegin(SS.getBeginLoc());
Douglas Gregor67a65642009-02-17 23:15:12 +00001301
Richard Smith72bfbd82013-12-04 00:28:23 +00001302 // FIXME: Name may be null here.
Douglas Gregor7f741122009-02-25 19:37:18 +00001303 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu30f93852013-06-19 22:25:01 +00001304 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump11289f42009-09-09 15:08:12 +00001305
Douglas Gregor7f741122009-02-25 19:37:18 +00001306 DS.SetTypeSpecError();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001307 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor7f741122009-02-25 19:37:18 +00001308 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001309 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001310 }
1311
Richard Smithbfdb1082012-03-12 08:56:40 +00001312 // There are four options here.
1313 // - If we are in a trailing return type, this is always just a reference,
1314 // and we must not try to parse a definition. For instance,
1315 // [] () -> struct S { };
1316 // does not define a type.
1317 // - If we have 'struct foo {...', 'struct foo :...',
1318 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1319 // - If we have 'struct foo;', then this is either a forward declaration
1320 // or a friend declaration, which have to be treated differently.
1321 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han9407e502012-11-26 22:54:45 +00001322 //
1323 // We also detect these erroneous cases to provide better diagnostic for
1324 // C++11 attributes parsing.
1325 // - attributes follow class name:
1326 // struct foo [[]] {};
1327 // - attributes appear before or after 'final':
1328 // struct foo [[]] final [[]] {};
1329 //
Richard Smithc5b05522012-03-12 07:56:15 +00001330 // However, in type-specifier-seq's, things look like declarations but are
1331 // just references, e.g.
1332 // new struct s;
Sebastian Redl2b372722010-02-03 21:21:43 +00001333 // or
Richard Smithc5b05522012-03-12 07:56:15 +00001334 // &T::operator struct s;
Richard Smith649c7b062014-01-08 00:56:48 +00001335 // For these, DSC is DSC_type_specifier or DSC_alias_declaration.
Michael Han9407e502012-11-26 22:54:45 +00001336
1337 // If there are attributes after class name, parse them.
Richard Smith89645bc2013-01-02 12:01:23 +00001338 MaybeParseCXX11Attributes(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00001339
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001340 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
John McCallfaf5fb42010-08-26 23:41:50 +00001341 Sema::TagUseKind TUK;
Richard Smithbfdb1082012-03-12 08:56:40 +00001342 if (DSC == DSC_trailing)
1343 TUK = Sema::TUK_Reference;
1344 else if (Tok.is(tok::l_brace) ||
1345 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001346 (isCXX11FinalKeyword() &&
David Blaikie9933a5a2012-03-12 15:39:49 +00001347 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregor3dad8422009-09-26 06:47:28 +00001348 if (DS.isFriendSpecified()) {
1349 // C++ [class.friend]p2:
1350 // A class shall not be defined in a friend declaration.
Richard Smith0f8ee222012-01-10 01:33:14 +00001351 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregor3dad8422009-09-26 06:47:28 +00001352 << SourceRange(DS.getFriendSpecLoc());
1353
1354 // Skip everything up to the semicolon, so that this looks like a proper
1355 // friend class (or template thereof) declaration.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001356 SkipUntil(tok::semi, StopBeforeMatch);
John McCallfaf5fb42010-08-26 23:41:50 +00001357 TUK = Sema::TUK_Friend;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001358 } else {
1359 // Okay, this is a class definition.
John McCallfaf5fb42010-08-26 23:41:50 +00001360 TUK = Sema::TUK_Definition;
Douglas Gregor3dad8422009-09-26 06:47:28 +00001361 }
Richard Smith434516c2013-02-22 06:46:23 +00001362 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1363 NextToken().is(tok::kw_alignas))) {
Michael Han9407e502012-11-26 22:54:45 +00001364 // We can't tell if this is a definition or reference
1365 // until we skipped the 'final' and C++11 attribute specifiers.
1366 TentativeParsingAction PA(*this);
1367
1368 // Skip the 'final' keyword.
1369 ConsumeToken();
1370
1371 // Skip C++11 attribute specifiers.
1372 while (true) {
1373 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1374 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001375 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001376 break;
Richard Smith434516c2013-02-22 06:46:23 +00001377 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han9407e502012-11-26 22:54:45 +00001378 ConsumeToken();
1379 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001380 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han9407e502012-11-26 22:54:45 +00001381 break;
1382 } else {
1383 break;
1384 }
1385 }
1386
1387 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1388 TUK = Sema::TUK_Definition;
1389 else
1390 TUK = Sema::TUK_Reference;
1391
1392 PA.Revert();
Richard Smith649c7b062014-01-08 00:56:48 +00001393 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00001394 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00001395 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallfaf5fb42010-08-26 23:41:50 +00001396 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matose9a3ed42012-08-31 22:18:20 +00001397 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001398 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Joao Matose9a3ed42012-08-31 22:18:20 +00001399 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00001400 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001401 DeclSpec::getSpecifierName(TagType, PPol));
Joao Matose9a3ed42012-08-31 22:18:20 +00001402 PP.EnterToken(Tok);
1403 Tok.setKind(tok::semi);
1404 }
Richard Smith369b9f92012-06-25 21:37:02 +00001405 } else
John McCallfaf5fb42010-08-26 23:41:50 +00001406 TUK = Sema::TUK_Reference;
Douglas Gregor556877c2008-04-13 21:30:24 +00001407
Michael Han9407e502012-11-26 22:54:45 +00001408 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1409 // to caller to handle.
Michael Han309af292013-01-07 16:57:11 +00001410 if (TUK != Sema::TUK_Reference) {
1411 // If this is not a reference, then the only possible
1412 // valid place for C++11 attributes to appear here
1413 // is between class-key and class-name. If there are
1414 // any attributes after class-name, we try a fixit to move
1415 // them to the right place.
1416 SourceRange AttrRange = Attributes.Range;
1417 if (AttrRange.isValid()) {
1418 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1419 << AttrRange
1420 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1421 CharSourceRange(AttrRange, true))
1422 << FixItHint::CreateRemoval(AttrRange);
1423
1424 // Recover by adding misplaced attributes to the attribute list
1425 // of the class so they can be applied on the class later.
1426 attrs.takeAllFrom(Attributes);
1427 }
1428 }
Michael Han9407e502012-11-26 22:54:45 +00001429
John McCall6347b682012-05-07 06:16:58 +00001430 // If this is an elaborated type specifier, and we delayed
1431 // diagnostics before, just merge them into the current pool.
1432 if (shouldDelayDiagsInTag) {
1433 diagsFromTag.done();
1434 if (TUK == Sema::TUK_Reference)
1435 diagsFromTag.redelay();
1436 }
1437
John McCall413021a2010-07-30 06:26:29 +00001438 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallfaf5fb42010-08-26 23:41:50 +00001439 TUK != Sema::TUK_Definition)) {
John McCall413021a2010-07-30 06:26:29 +00001440 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1441 // We have a declaration or reference to an anonymous class.
1442 Diag(StartLoc, diag::err_anon_type_definition)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001443 << DeclSpec::getSpecifierName(TagType, Policy);
John McCall413021a2010-07-30 06:26:29 +00001444 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001445
David Majnemer3252fd02013-12-05 01:36:53 +00001446 // If we are parsing a definition and stop at a base-clause, continue on
1447 // until the semicolon. Continuing from the comma will just trick us into
1448 // thinking we are seeing a variable declaration.
1449 if (TUK == Sema::TUK_Definition && Tok.is(tok::colon))
1450 SkipUntil(tok::semi, StopBeforeMatch);
1451 else
1452 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor556877c2008-04-13 21:30:24 +00001453 return;
1454 }
1455
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001456 // Create the tag portion of the class or class template.
John McCall48871652010-08-21 09:40:31 +00001457 DeclResult TagOrTempResult = true; // invalid
1458 TypeResult TypeResult = true; // invalid
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001459
Douglas Gregord6ab8742009-05-28 23:31:59 +00001460 bool Owned = false;
John McCall06f6fe8d2009-09-04 01:14:41 +00001461 if (TemplateId) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001462 // Explicit specialization, class template partial specialization,
1463 // or explicit instantiation.
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001464 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor7f741122009-02-25 19:37:18 +00001465 TemplateId->NumArgs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001466 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001467 TUK == Sema::TUK_Declaration) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001468 // This is an explicit instantiation of a class template.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001469 ProhibitAttributes(attrs);
1470
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001471 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001472 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001473 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001474 TemplateInfo.TemplateLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001475 TagType,
Mike Stump11289f42009-09-09 15:08:12 +00001476 StartLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001477 SS,
John McCall3e56fd42010-08-23 07:28:44 +00001478 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001479 TemplateId->TemplateNameLoc,
1480 TemplateId->LAngleLoc,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001481 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001482 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001483 attrs.getList());
John McCallb7c5c272010-04-14 00:24:33 +00001484
1485 // Friend template-ids are treated as references unless
1486 // they have template headers, in which case they're ill-formed
1487 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1488 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallfaf5fb42010-08-26 23:41:50 +00001489 } else if (TUK == Sema::TUK_Reference ||
1490 (TUK == Sema::TUK_Friend &&
John McCallb7c5c272010-04-14 00:24:33 +00001491 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001492 ProhibitAttributes(attrs);
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001493 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001494 TemplateId->SS,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001495 TemplateId->TemplateKWLoc,
Douglas Gregore7c20652011-03-02 00:47:37 +00001496 TemplateId->Template,
1497 TemplateId->TemplateNameLoc,
1498 TemplateId->LAngleLoc,
1499 TemplateArgsPtr,
Abramo Bagnara48c05be2012-02-06 14:41:24 +00001500 TemplateId->RAngleLoc);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001501 } else {
1502 // This is an explicit specialization or a class template
1503 // partial specialization.
1504 TemplateParameterLists FakedParamLists;
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001505 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1506 // This looks like an explicit instantiation, because we have
1507 // something like
1508 //
1509 // template class Foo<X>
1510 //
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001511 // but it actually has a definition. Most likely, this was
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001512 // meant to be an explicit specialization, but the user forgot
1513 // the '<>' after 'template'.
Richard Smith003c5e12013-11-08 19:03:29 +00001514 // It this is friend declaration however, since it cannot have a
1515 // template header, it is most likely that the user meant to
1516 // remove the 'template' keyword.
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001517 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith003c5e12013-11-08 19:03:29 +00001518 "Expected a definition here");
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001519
Richard Smith003c5e12013-11-08 19:03:29 +00001520 if (TUK == Sema::TUK_Friend) {
1521 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
1522 TemplateParams = 0;
1523 } else {
1524 SourceLocation LAngleLoc =
1525 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1526 Diag(TemplateId->TemplateNameLoc,
1527 diag::err_explicit_instantiation_with_definition)
1528 << SourceRange(TemplateInfo.TemplateLoc)
1529 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1530
1531 // Create a fake template parameter list that contains only
1532 // "template<>", so that we treat this construct as a class
1533 // template specialization.
1534 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1535 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1536 LAngleLoc));
1537 TemplateParams = &FakedParamLists;
1538 }
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001539 }
1540
1541 // Build the class template specialization.
1542 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001543 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregor3c7cd6a2011-09-09 20:53:38 +00001544 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall3e56fd42010-08-23 07:28:44 +00001545 TemplateId->Template,
Mike Stump11289f42009-09-09 15:08:12 +00001546 TemplateId->TemplateNameLoc,
1547 TemplateId->LAngleLoc,
Douglas Gregor7f741122009-02-25 19:37:18 +00001548 TemplateArgsPtr,
Mike Stump11289f42009-09-09 15:08:12 +00001549 TemplateId->RAngleLoc,
John McCall53fa7142010-12-24 02:08:15 +00001550 attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001551 MultiTemplateParamsArg(
Douglas Gregor67a65642009-02-17 23:15:12 +00001552 TemplateParams? &(*TemplateParams)[0] : 0,
1553 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001554 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001555 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallfaf5fb42010-08-26 23:41:50 +00001556 TUK == Sema::TUK_Declaration) {
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001557 // Explicit instantiation of a member of a class template
1558 // specialization, e.g.,
1559 //
1560 // template struct Outer<int>::Inner;
1561 //
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001562 ProhibitAttributes(attrs);
1563
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001564 TagOrTempResult
Douglas Gregor0be31a22010-07-02 17:43:08 +00001565 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor43e75172009-09-04 06:33:52 +00001566 TemplateInfo.ExternLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001567 TemplateInfo.TemplateLoc,
1568 TagType, StartLoc, SS, Name,
John McCall53fa7142010-12-24 02:08:15 +00001569 NameLoc, attrs.getList());
John McCallace48cd2010-10-19 01:40:49 +00001570 } else if (TUK == Sema::TUK_Friend &&
1571 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001572 ProhibitAttributes(attrs);
1573
John McCallace48cd2010-10-19 01:40:49 +00001574 TagOrTempResult =
1575 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1576 TagType, StartLoc, SS,
John McCall53fa7142010-12-24 02:08:15 +00001577 Name, NameLoc, attrs.getList(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001578 MultiTemplateParamsArg(
John McCallace48cd2010-10-19 01:40:49 +00001579 TemplateParams? &(*TemplateParams)[0] : 0,
1580 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001581 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001582 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1583 ProhibitAttributes(attrs);
Richard Smith003c5e12013-11-08 19:03:29 +00001584
Larisse Voufo725de3e2013-06-21 00:08:46 +00001585 if (TUK == Sema::TUK_Definition &&
1586 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1587 // If the declarator-id is not a template-id, issue a diagnostic and
1588 // recover by ignoring the 'template' keyword.
1589 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1590 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Larisse Voufob9bbaba2013-06-22 13:56:11 +00001591 TemplateParams = 0;
Larisse Voufo725de3e2013-06-21 00:08:46 +00001592 }
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001593
John McCall7f41d982009-09-11 04:59:25 +00001594 bool IsDependent = false;
1595
John McCall32723e92010-10-19 18:40:57 +00001596 // Don't pass down template parameter lists if this is just a tag
1597 // reference. For example, we don't need the template parameters here:
1598 // template <class T> class A *makeA(T t);
1599 MultiTemplateParamsArg TParams;
1600 if (TUK != Sema::TUK_Reference && TemplateParams)
1601 TParams =
1602 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1603
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001604 // Declaration or definition of a class type
John McCallace48cd2010-10-19 01:40:49 +00001605 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall53fa7142010-12-24 02:08:15 +00001606 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregor2820e692011-09-09 19:05:14 +00001607 DS.getModulePrivateSpecLoc(),
Richard Smith0f8ee222012-01-10 01:33:14 +00001608 TParams, Owned, IsDependent,
1609 SourceLocation(), false,
Richard Smith649c7b062014-01-08 00:56:48 +00001610 clang::TypeResult(),
1611 DSC == DSC_type_specifier);
John McCall7f41d982009-09-11 04:59:25 +00001612
1613 // If ActOnTag said the type was dependent, try again with the
1614 // less common call.
John McCallace48cd2010-10-19 01:40:49 +00001615 if (IsDependent) {
1616 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001617 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001618 SS, Name, StartLoc, NameLoc);
John McCallace48cd2010-10-19 01:40:49 +00001619 }
Douglas Gregor2ec748c2009-05-14 00:28:11 +00001620 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001621
Douglas Gregor556877c2008-04-13 21:30:24 +00001622 // If there is a body, parse it and inform the actions module.
John McCallfaf5fb42010-08-26 23:41:50 +00001623 if (TUK == Sema::TUK_Definition) {
John McCall2d814c32009-12-19 21:48:58 +00001624 assert(Tok.is(tok::l_brace) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001625 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith89645bc2013-01-02 12:01:23 +00001626 isCXX11FinalKeyword());
David Blaikiebbafb8a2012-03-11 07:00:24 +00001627 if (getLangOpts().CPlusPlus)
Michael Han309af292013-01-07 16:57:11 +00001628 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1629 TagOrTempResult.get());
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00001630 else
Douglas Gregorc08f4892009-03-25 00:13:59 +00001631 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001632 }
1633
John McCallba7bf592010-08-24 05:47:05 +00001634 const char *PrevSpec = 0;
1635 unsigned DiagID;
1636 bool Result;
John McCall7f41d982009-09-11 04:59:25 +00001637 if (!TypeResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001638 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1639 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001640 PrevSpec, DiagID, TypeResult.get(), Policy);
John McCall7f41d982009-09-11 04:59:25 +00001641 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00001642 Result = DS.SetTypeSpecType(TagType, StartLoc,
1643 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001644 PrevSpec, DiagID, TagOrTempResult.get(), Owned,
1645 Policy);
John McCall7f41d982009-09-11 04:59:25 +00001646 } else {
Douglas Gregorcd72ba92009-02-06 22:42:48 +00001647 DS.SetTypeSpecError();
Anders Carlssonf83c9fa2009-05-11 22:27:47 +00001648 return;
1649 }
Mike Stump11289f42009-09-09 15:08:12 +00001650
John McCallba7bf592010-08-24 05:47:05 +00001651 if (Result)
John McCall49bfce42009-08-03 20:12:06 +00001652 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00001653
Chris Lattnercf251412010-02-02 01:23:29 +00001654 // At this point, we've successfully parsed a class-specifier in 'definition'
1655 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1656 // going to look at what comes after it to improve error recovery. If an
1657 // impossible token occurs next, we assume that the programmer forgot a ; at
1658 // the end of the declaration and recover that way.
1659 //
Richard Smith369b9f92012-06-25 21:37:02 +00001660 // Also enforce C++ [temp]p3:
1661 // In a template-declaration which defines a class, no declarator
1662 // is permitted.
Joao Matose9a3ed42012-08-31 22:18:20 +00001663 if (TUK == Sema::TUK_Definition &&
1664 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001665 if (Tok.isNot(tok::semi)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001666 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Alp Toker383d2c42014-01-01 03:08:43 +00001667 ExpectAndConsume(tok::semi, diag::err_expected_after,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001668 DeclSpec::getSpecifierName(TagType, PPol));
Argyrios Kyrtzidise6f69132012-12-17 20:10:43 +00001669 // Push this token back into the preprocessor and change our current token
1670 // to ';' so that the rest of the code recovers as though there were an
1671 // ';' after the definition.
1672 PP.EnterToken(Tok);
1673 Tok.setKind(tok::semi);
1674 }
Chris Lattnercf251412010-02-02 01:23:29 +00001675 }
Douglas Gregor556877c2008-04-13 21:30:24 +00001676}
1677
Mike Stump11289f42009-09-09 15:08:12 +00001678/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregor556877c2008-04-13 21:30:24 +00001679///
1680/// base-clause : [C++ class.derived]
1681/// ':' base-specifier-list
1682/// base-specifier-list:
1683/// base-specifier '...'[opt]
1684/// base-specifier-list ',' base-specifier '...'[opt]
John McCall48871652010-08-21 09:40:31 +00001685void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001686 assert(Tok.is(tok::colon) && "Not a base clause");
1687 ConsumeToken();
1688
Douglas Gregor29a92472008-10-22 17:49:05 +00001689 // Build up an array of parsed base specifiers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001690 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregor29a92472008-10-22 17:49:05 +00001691
Douglas Gregor556877c2008-04-13 21:30:24 +00001692 while (true) {
1693 // Parse a base-specifier.
Douglas Gregor29a92472008-10-22 17:49:05 +00001694 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregorf8298252009-01-26 22:44:13 +00001695 if (Result.isInvalid()) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001696 // Skip the rest of this base specifier, up until the comma or
1697 // opening brace.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001698 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor29a92472008-10-22 17:49:05 +00001699 } else {
1700 // Add this to our array of base specifiers.
Douglas Gregorf8298252009-01-26 22:44:13 +00001701 BaseInfo.push_back(Result.get());
Douglas Gregor556877c2008-04-13 21:30:24 +00001702 }
1703
1704 // If the next token is a comma, consume it and keep reading
1705 // base-specifiers.
Alp Toker97650562014-01-10 11:19:30 +00001706 if (!TryConsumeToken(tok::comma))
1707 break;
Douglas Gregor556877c2008-04-13 21:30:24 +00001708 }
Douglas Gregor29a92472008-10-22 17:49:05 +00001709
1710 // Attach the base specifiers
Jay Foad7d0479f2009-05-21 09:52:38 +00001711 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregor556877c2008-04-13 21:30:24 +00001712}
1713
1714/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1715/// one entry in the base class list of a class specifier, for example:
1716/// class foo : public bar, virtual private baz {
1717/// 'public bar' and 'virtual private baz' are each base-specifiers.
1718///
1719/// base-specifier: [C++ class.derived]
Richard Smith4c96e992013-02-19 23:47:15 +00001720/// attribute-specifier-seq[opt] base-type-specifier
1721/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1722/// base-type-specifier
1723/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1724/// base-type-specifier
John McCall48871652010-08-21 09:40:31 +00001725Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregor556877c2008-04-13 21:30:24 +00001726 bool IsVirtual = false;
1727 SourceLocation StartLoc = Tok.getLocation();
1728
Richard Smith4c96e992013-02-19 23:47:15 +00001729 ParsedAttributesWithRange Attributes(AttrFactory);
1730 MaybeParseCXX11Attributes(Attributes);
1731
Douglas Gregor556877c2008-04-13 21:30:24 +00001732 // Parse the 'virtual' keyword.
Alp Toker97650562014-01-10 11:19:30 +00001733 if (TryConsumeToken(tok::kw_virtual))
Douglas Gregor556877c2008-04-13 21:30:24 +00001734 IsVirtual = true;
Douglas Gregor556877c2008-04-13 21:30:24 +00001735
Richard Smith4c96e992013-02-19 23:47:15 +00001736 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1737
Douglas Gregor556877c2008-04-13 21:30:24 +00001738 // Parse an (optional) access specifier.
1739 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall553c0792010-01-23 00:46:32 +00001740 if (Access != AS_none)
Douglas Gregor556877c2008-04-13 21:30:24 +00001741 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001742
Richard Smith4c96e992013-02-19 23:47:15 +00001743 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1744
Douglas Gregor556877c2008-04-13 21:30:24 +00001745 // Parse the 'virtual' keyword (again!), in case it came after the
1746 // access specifier.
1747 if (Tok.is(tok::kw_virtual)) {
1748 SourceLocation VirtualLoc = ConsumeToken();
1749 if (IsVirtual) {
1750 // Complain about duplicate 'virtual'
Chris Lattner6d29c102008-11-18 07:48:38 +00001751 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregora771f462010-03-31 17:46:05 +00001752 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001753 }
1754
1755 IsVirtual = true;
1756 }
1757
Richard Smith4c96e992013-02-19 23:47:15 +00001758 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1759
Douglas Gregor831c93f2008-11-05 20:51:48 +00001760 // Parse the class-name.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001761 SourceLocation EndLocation;
David Blaikie1cd50022011-10-25 17:10:12 +00001762 SourceLocation BaseLoc;
1763 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001764 if (BaseType.isInvalid())
Douglas Gregor831c93f2008-11-05 20:51:48 +00001765 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001766
Douglas Gregor752a5952011-01-03 22:36:02 +00001767 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1768 // actually part of the base-specifier-list grammar productions, but we
1769 // parse it here for convenience.
1770 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00001771 TryConsumeToken(tok::ellipsis, EllipsisLoc);
1772
Mike Stump11289f42009-09-09 15:08:12 +00001773 // Find the complete source range for the base-specifier.
Douglas Gregord54dfb82009-02-25 23:52:28 +00001774 SourceRange Range(StartLoc, EndLocation);
Mike Stump11289f42009-09-09 15:08:12 +00001775
Douglas Gregor556877c2008-04-13 21:30:24 +00001776 // Notify semantic analysis that we have parsed a complete
1777 // base-specifier.
Richard Smith4c96e992013-02-19 23:47:15 +00001778 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1779 Access, BaseType.get(), BaseLoc,
1780 EllipsisLoc);
Douglas Gregor556877c2008-04-13 21:30:24 +00001781}
1782
1783/// getAccessSpecifierIfPresent - Determine whether the next token is
1784/// a C++ access-specifier.
1785///
1786/// access-specifier: [C++ class.derived]
1787/// 'private'
1788/// 'protected'
1789/// 'public'
Mike Stump11289f42009-09-09 15:08:12 +00001790AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregor556877c2008-04-13 21:30:24 +00001791 switch (Tok.getKind()) {
1792 default: return AS_none;
1793 case tok::kw_private: return AS_private;
1794 case tok::kw_protected: return AS_protected;
1795 case tok::kw_public: return AS_public;
1796 }
1797}
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001798
Douglas Gregor433e0532012-04-16 18:27:27 +00001799/// \brief If the given declarator has any parts for which parsing has to be
Richard Smith2331bbf2012-05-02 22:22:32 +00001800/// delayed, e.g., default arguments, create a late-parsed method declaration
1801/// record to handle the parsing at the end of the class definition.
Douglas Gregor433e0532012-04-16 18:27:27 +00001802void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1803 Decl *ThisDecl) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001804 // We just declared a member function. If this member function
Richard Smith2331bbf2012-05-02 22:22:32 +00001805 // has any default arguments, we'll need to parse them later.
Eli Friedman3af2a772009-07-22 21:45:50 +00001806 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001807 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001808 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor433e0532012-04-16 18:27:27 +00001809
Alp Tokerc5350722014-02-26 22:27:52 +00001810 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
1811 if (LateMethod || FTI.Params[ParamIdx].DefaultArgTokens) {
Eli Friedman3af2a772009-07-22 21:45:50 +00001812 if (!LateMethod) {
1813 // Push this method onto the stack of late-parsed method
1814 // declarations.
Douglas Gregorefc46952010-10-12 16:25:54 +00001815 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1816 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001817 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedman3af2a772009-07-22 21:45:50 +00001818
1819 // Add all of the parameters prior to this one (they don't
1820 // have default arguments).
Alp Tokerc5350722014-02-26 22:27:52 +00001821 LateMethod->DefaultArgs.reserve(FTI.NumParams);
Eli Friedman3af2a772009-07-22 21:45:50 +00001822 for (unsigned I = 0; I < ParamIdx; ++I)
1823 LateMethod->DefaultArgs.push_back(
Alp Tokerc5350722014-02-26 22:27:52 +00001824 LateParsedDefaultArgument(FTI.Params[I].Param));
Eli Friedman3af2a772009-07-22 21:45:50 +00001825 }
1826
Douglas Gregor433e0532012-04-16 18:27:27 +00001827 // Add this parameter to the list of parameters (it may or may
Eli Friedman3af2a772009-07-22 21:45:50 +00001828 // not have a default argument).
Alp Tokerc5350722014-02-26 22:27:52 +00001829 LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
1830 FTI.Params[ParamIdx].Param, FTI.Params[ParamIdx].DefaultArgTokens));
Eli Friedman3af2a772009-07-22 21:45:50 +00001831 }
1832 }
1833}
1834
Richard Smith89645bc2013-01-02 12:01:23 +00001835/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001836/// virt-specifier.
1837///
1838/// virt-specifier:
1839/// override
1840/// final
Richard Smith89645bc2013-01-02 12:01:23 +00001841VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001842 if (!getLangOpts().CPlusPlus || Tok.isNot(tok::identifier))
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001843 return VirtSpecifiers::VS_None;
1844
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001845 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001846
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001847 // Initialize the contextual keywords.
1848 if (!Ident_final) {
1849 Ident_final = &PP.getIdentifierTable().get("final");
1850 if (getLangOpts().MicrosoftExt)
1851 Ident_sealed = &PP.getIdentifierTable().get("sealed");
1852 Ident_override = &PP.getIdentifierTable().get("override");
Anders Carlsson56104902011-01-17 03:05:47 +00001853 }
1854
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001855 if (II == Ident_override)
1856 return VirtSpecifiers::VS_Override;
1857
1858 if (II == Ident_sealed)
1859 return VirtSpecifiers::VS_Sealed;
1860
1861 if (II == Ident_final)
1862 return VirtSpecifiers::VS_Final;
1863
Anders Carlsson56104902011-01-17 03:05:47 +00001864 return VirtSpecifiers::VS_None;
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001865}
1866
Richard Smith89645bc2013-01-02 12:01:23 +00001867/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001868///
1869/// virt-specifier-seq:
1870/// virt-specifier
1871/// virt-specifier-seq virt-specifier
Richard Smith89645bc2013-01-02 12:01:23 +00001872void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalldb632ac2012-09-25 07:32:39 +00001873 bool IsInterface) {
Anders Carlsson56104902011-01-17 03:05:47 +00001874 while (true) {
Richard Smith89645bc2013-01-02 12:01:23 +00001875 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlsson56104902011-01-17 03:05:47 +00001876 if (Specifier == VirtSpecifiers::VS_None)
1877 return;
1878
1879 // C++ [class.mem]p8:
1880 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001881 const char *PrevSpec = 0;
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001882 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlsson56104902011-01-17 03:05:47 +00001883 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1884 << PrevSpec
1885 << FixItHint::CreateRemoval(Tok.getLocation());
1886
David Majnemera5433082013-10-18 00:33:31 +00001887 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
1888 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalldb632ac2012-09-25 07:32:39 +00001889 Diag(Tok.getLocation(), diag::err_override_control_interface)
1890 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemera5433082013-10-18 00:33:31 +00001891 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
1892 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalldb632ac2012-09-25 07:32:39 +00001893 } else {
David Majnemera5433082013-10-18 00:33:31 +00001894 Diag(Tok.getLocation(),
1895 getLangOpts().CPlusPlus11
1896 ? diag::warn_cxx98_compat_override_control_keyword
1897 : diag::ext_override_control_keyword)
1898 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalldb632ac2012-09-25 07:32:39 +00001899 }
Anders Carlsson56104902011-01-17 03:05:47 +00001900 ConsumeToken();
1901 }
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001902}
1903
Richard Smith89645bc2013-01-02 12:01:23 +00001904/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001905/// 'final' or Microsoft 'sealed' contextual keyword.
Richard Smith89645bc2013-01-02 12:01:23 +00001906bool Parser::isCXX11FinalKeyword() const {
Alp Tokerbb4b86a2014-01-09 00:13:52 +00001907 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
1908 return Specifier == VirtSpecifiers::VS_Final ||
1909 Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001910}
1911
Richard Smith72553fc2014-01-23 23:53:27 +00001912/// \brief Parse a C++ member-declarator up to, but not including, the optional
1913/// brace-or-equal-initializer or pure-specifier.
1914void Parser::ParseCXXMemberDeclaratorBeforeInitializer(
1915 Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
1916 LateParsedAttrList &LateParsedAttrs) {
1917 // member-declarator:
1918 // declarator pure-specifier[opt]
1919 // declarator brace-or-equal-initializer[opt]
1920 // identifier[opt] ':' constant-expression
1921 if (Tok.isNot(tok::colon)) {
1922 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
1923 // is a bitfield.
1924 // FIXME: This should only apply when parsing the id-expression (see
1925 // PR18587).
1926 ColonProtectionRAIIObject X(*this);
1927 ParseDeclarator(DeclaratorInfo);
1928 }
1929
1930 if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(tok::colon)) {
1931 BitfieldSize = ParseConstantExpression();
1932 if (BitfieldSize.isInvalid())
1933 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
1934 } else
1935 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
1936
1937 // If a simple-asm-expr is present, parse it.
1938 if (Tok.is(tok::kw_asm)) {
1939 SourceLocation Loc;
1940 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1941 if (AsmLabel.isInvalid())
1942 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
1943
1944 DeclaratorInfo.setAsmLabel(AsmLabel.release());
1945 DeclaratorInfo.SetRangeEnd(Loc);
1946 }
1947
1948 // If attributes exist after the declarator, but before an '{', parse them.
1949 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Richard Smith4b5a9492014-01-24 22:34:35 +00001950
1951 // For compatibility with code written to older Clang, also accept a
1952 // virt-specifier *after* the GNU attributes.
1953 // FIXME: If we saw any attributes that are known to GCC followed by a
1954 // virt-specifier, issue a GCC-compat warning.
1955 if (BitfieldSize.isUnset() && VS.isUnset())
1956 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith72553fc2014-01-23 23:53:27 +00001957}
1958
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001959/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1960///
1961/// member-declaration:
1962/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1963/// function-definition ';'[opt]
1964/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1965/// using-declaration [TODO]
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001966/// [C++0x] static_assert-declaration
Anders Carlssondfbbdf62009-03-26 00:52:18 +00001967/// template-declaration
Chris Lattnerd19c1c02008-12-18 01:12:00 +00001968/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001969///
1970/// member-declarator-list:
1971/// member-declarator
1972/// member-declarator-list ',' member-declarator
1973///
1974/// member-declarator:
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001975/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001976/// declarator constant-initializer[opt]
Richard Smith938f40b2011-06-11 17:19:42 +00001977/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001978/// identifier[opt] ':' constant-expression
1979///
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001980/// virt-specifier-seq:
1981/// virt-specifier
1982/// virt-specifier-seq virt-specifier
1983///
1984/// virt-specifier:
1985/// override
1986/// final
David Majnemera5433082013-10-18 00:33:31 +00001987/// [MS] sealed
Anders Carlsson11fdbbc2011-01-16 23:56:42 +00001988///
Sebastian Redl42e92c42009-04-12 17:16:29 +00001989/// pure-specifier:
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00001990/// '= 0'
1991///
1992/// constant-initializer:
1993/// '=' constant-expression
1994///
Douglas Gregor3447e762009-08-20 22:52:58 +00001995void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00001996 AttributeList *AccessAttrs,
John McCall796c2a52010-07-16 08:13:16 +00001997 const ParsedTemplateInfo &TemplateInfo,
1998 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor23c84762011-04-14 17:21:19 +00001999 if (Tok.is(tok::at)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002000 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor23c84762011-04-14 17:21:19 +00002001 Diag(Tok, diag::err_at_defs_cxx);
2002 else
2003 Diag(Tok, diag::err_at_in_class);
Richard Smithda35e962013-11-09 04:52:51 +00002004
Douglas Gregor23c84762011-04-14 17:21:19 +00002005 ConsumeToken();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002006 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor23c84762011-04-14 17:21:19 +00002007 return;
2008 }
Richard Smithda35e962013-11-09 04:52:51 +00002009
John McCalla0097262009-12-11 02:10:03 +00002010 // Access declarations.
Richard Smith45855df2012-05-09 08:23:23 +00002011 bool MalformedTypeSpec = false;
John McCalla0097262009-12-11 02:10:03 +00002012 if (!TemplateInfo.Kind &&
Richard Smith45855df2012-05-09 08:23:23 +00002013 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
2014 if (TryAnnotateCXXScopeToken())
2015 MalformedTypeSpec = true;
2016
2017 bool isAccessDecl;
2018 if (Tok.isNot(tok::annot_cxxscope))
2019 isAccessDecl = false;
2020 else if (NextToken().is(tok::identifier))
John McCalla0097262009-12-11 02:10:03 +00002021 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
2022 else
2023 isAccessDecl = NextToken().is(tok::kw_operator);
2024
2025 if (isAccessDecl) {
2026 // Collect the scope specifier token we annotated earlier.
2027 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002028 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2029 /*EnteringContext=*/false);
John McCalla0097262009-12-11 02:10:03 +00002030
2031 // Try to parse an unqualified-id.
Abramo Bagnara7945c982012-01-27 09:46:47 +00002032 SourceLocation TemplateKWLoc;
John McCalla0097262009-12-11 02:10:03 +00002033 UnqualifiedId Name;
Abramo Bagnara7945c982012-01-27 09:46:47 +00002034 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
2035 TemplateKWLoc, Name)) {
John McCalla0097262009-12-11 02:10:03 +00002036 SkipUntil(tok::semi);
2037 return;
2038 }
2039
2040 // TODO: recover from mistakenly-qualified operator declarations.
Alp Toker383d2c42014-01-01 03:08:43 +00002041 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2042 "access declaration")) {
2043 SkipUntil(tok::semi);
John McCalla0097262009-12-11 02:10:03 +00002044 return;
Alp Toker383d2c42014-01-01 03:08:43 +00002045 }
John McCalla0097262009-12-11 02:10:03 +00002046
Douglas Gregor0be31a22010-07-02 17:43:08 +00002047 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002048 /* HasUsingKeyword */ false,
2049 SourceLocation(),
John McCalla0097262009-12-11 02:10:03 +00002050 SS, Name,
2051 /* AttrList */ 0,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002052 /* HasTypenameKeyword */ false,
John McCalla0097262009-12-11 02:10:03 +00002053 SourceLocation());
2054 return;
2055 }
2056 }
2057
Anders Carlssonf24fcff62009-03-11 16:27:10 +00002058 // static_assert-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00002059 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor3447e762009-08-20 22:52:58 +00002060 // FIXME: Check for templates
Chris Lattner49836b42009-04-02 04:16:50 +00002061 SourceLocation DeclEnd;
2062 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002063 return;
2064 }
Mike Stump11289f42009-09-09 15:08:12 +00002065
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002066 if (Tok.is(tok::kw_template)) {
Mike Stump11289f42009-09-09 15:08:12 +00002067 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor3447e762009-08-20 22:52:58 +00002068 "Nested template improperly parsed?");
Chris Lattner49836b42009-04-02 04:16:50 +00002069 SourceLocation DeclEnd;
Mike Stump11289f42009-09-09 15:08:12 +00002070 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002071 AS, AccessAttrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002072 return;
2073 }
Anders Carlssondfbbdf62009-03-26 00:52:18 +00002074
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002075 // Handle: member-declaration ::= '__extension__' member-declaration
2076 if (Tok.is(tok::kw___extension__)) {
2077 // __extension__ silences extension warnings in the subexpression.
2078 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2079 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002080 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2081 TemplateInfo, TemplateDiags);
Chris Lattnerd19c1c02008-12-18 01:12:00 +00002082 }
Douglas Gregorfec52632009-06-20 00:51:54 +00002083
John McCall084e83d2011-03-24 11:26:52 +00002084 ParsedAttributesWithRange attrs(AttrFactory);
Michael Handdc016d2012-11-28 23:17:40 +00002085 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002086 // Optional C++11 attribute-specifier
2087 MaybeParseCXX11Attributes(attrs);
Michael Handdc016d2012-11-28 23:17:40 +00002088 // We need to keep these attributes for future diagnostic
2089 // before they are taken over by declaration specifier.
2090 FnAttrs.addAll(attrs.getList());
2091 FnAttrs.Range = attrs.Range;
2092
John McCall53fa7142010-12-24 02:08:15 +00002093 MaybeParseMicrosoftAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002094
Douglas Gregorfec52632009-06-20 00:51:54 +00002095 if (Tok.is(tok::kw_using)) {
John McCall53fa7142010-12-24 02:08:15 +00002096 ProhibitAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002097
Douglas Gregorfec52632009-06-20 00:51:54 +00002098 // Eat 'using'.
2099 SourceLocation UsingLoc = ConsumeToken();
2100
2101 if (Tok.is(tok::kw_namespace)) {
2102 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002103 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002104 } else {
Douglas Gregorfec52632009-06-20 00:51:54 +00002105 SourceLocation DeclEnd;
Richard Smith3f1b5d02011-05-05 21:57:07 +00002106 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall9b72f892010-11-10 02:40:36 +00002107 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2108 UsingLoc, DeclEnd, AS);
Douglas Gregorfec52632009-06-20 00:51:54 +00002109 }
2110 return;
2111 }
2112
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002113 // Hold late-parsed attributes so we can attach a Decl to them later.
2114 LateParsedAttrList CommonLateParsedAttrs;
2115
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002116 // decl-specifier-seq:
2117 // Parse the common declaration-specifiers piece.
John McCall796c2a52010-07-16 08:13:16 +00002118 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall53fa7142010-12-24 02:08:15 +00002119 DS.takeAttributesFrom(attrs);
Richard Smith45855df2012-05-09 08:23:23 +00002120 if (MalformedTypeSpec)
2121 DS.SetTypeSpecError();
Richard Smith72553fc2014-01-23 23:53:27 +00002122
2123 {
2124 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
2125 // is a bitfield.
2126 ColonProtectionRAIIObject X(*this);
2127 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2128 &CommonLateParsedAttrs);
2129 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002130
Richard Smith404dfb42013-11-19 22:47:36 +00002131 // If we had a free-standing type definition with a missing semicolon, we
2132 // may get this far before the problem becomes obvious.
2133 if (DS.hasTagDefinition() &&
2134 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2135 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSC_class,
2136 &CommonLateParsedAttrs))
2137 return;
2138
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002139 MultiTemplateParamsArg TemplateParams(
John McCall11083da2009-09-16 22:47:08 +00002140 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2141 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2142
Alp Toker35d87032013-12-30 23:29:50 +00002143 if (TryConsumeToken(tok::semi)) {
Michael Handdc016d2012-11-28 23:17:40 +00002144 if (DS.isFriendSpecified())
2145 ProhibitAttributes(FnAttrs);
2146
John McCall48871652010-08-21 09:40:31 +00002147 Decl *TheDecl =
Chandler Carruth7c9856d2011-05-03 18:35:10 +00002148 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCall796c2a52010-07-16 08:13:16 +00002149 DS.complete(TheDecl);
John McCall07e91c02009-08-06 02:15:43 +00002150 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002151 }
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002152
John McCall28a6aea2009-11-04 02:18:39 +00002153 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber24b2a822011-01-28 06:07:34 +00002154 VirtSpecifiers VS;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002155
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002156 // Hold late-parsed attributes so we can attach a Decl to them later.
2157 LateParsedAttrList LateParsedAttrs;
2158
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002159 SourceLocation EqualLoc;
2160 bool HasInitializer = false;
2161 ExprResult Init;
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002162
Richard Smith72553fc2014-01-23 23:53:27 +00002163 SmallVector<Decl *, 8> DeclsInGroup;
2164 ExprResult BitfieldSize;
2165 bool ExpectSemi = true;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002166
Richard Smith72553fc2014-01-23 23:53:27 +00002167 // Parse the first declarator.
2168 ParseCXXMemberDeclaratorBeforeInitializer(DeclaratorInfo, VS, BitfieldSize,
2169 LateParsedAttrs);
Nico Weber24b2a822011-01-28 06:07:34 +00002170
Richard Smith72553fc2014-01-23 23:53:27 +00002171 // If this has neither a name nor a bit width, something has gone seriously
2172 // wrong. Skip until the semi-colon or }.
Richard Smith4b5a9492014-01-24 22:34:35 +00002173 if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002174 // If so, skip until the semi-colon or a }.
2175 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2176 TryConsumeToken(tok::semi);
2177 return;
2178 }
John Thompson5bc5cbe2009-11-25 22:58:06 +00002179
Richard Smith72553fc2014-01-23 23:53:27 +00002180 // Check for a member function definition.
Richard Smith4b5a9492014-01-24 22:34:35 +00002181 if (BitfieldSize.isUnset()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002182 // MSVC permits pure specifier on inline functions defined at class scope.
Francois Pichet3abc9b82011-05-11 02:14:46 +00002183 // Hence check for =0 before checking for function definition.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002184 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Richard Smith72553fc2014-01-23 23:53:27 +00002185 DeclaratorInfo.isFunctionDeclarator() &&
Francois Pichet3abc9b82011-05-11 02:14:46 +00002186 NextToken().is(tok::numeric_constant)) {
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002187 EqualLoc = ConsumeToken();
Francois Pichet3abc9b82011-05-11 02:14:46 +00002188 Init = ParseInitializer();
2189 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002190 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002191 else
2192 HasInitializer = true;
Francois Pichet3abc9b82011-05-11 02:14:46 +00002193 }
2194
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002195 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002196 // function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002197 //
2198 // In C++11, a non-function declarator followed by an open brace is a
2199 // braced-init-list for an in-class member initialization, not an
2200 // erroneous function definition.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002201 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002202 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002203 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith938f40b2011-06-11 17:19:42 +00002204 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002205 DefinitionKind = FDK_Definition;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002206 } else if (Tok.is(tok::equal)) {
2207 const Token &KW = NextToken();
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002208 if (KW.is(tok::kw_default))
2209 DefinitionKind = FDK_Defaulted;
2210 else if (KW.is(tok::kw_delete))
2211 DefinitionKind = FDK_Deleted;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002212 }
2213 }
2214
Michael Handdc016d2012-11-28 23:17:40 +00002215 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2216 // to a friend declaration, that declaration shall be a definition.
2217 if (DeclaratorInfo.isFunctionDeclarator() &&
2218 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2219 // Diagnose attributes that appear before decl specifier:
2220 // [[]] friend int foo();
2221 ProhibitAttributes(FnAttrs);
2222 }
2223
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002224 if (DefinitionKind) {
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002225 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu0d730542012-01-21 02:59:18 +00002226 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002227 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00002228 SkipUntil(tok::r_brace);
Michael Handdc016d2012-11-28 23:17:40 +00002229
Douglas Gregor8a4db832011-01-19 16:41:58 +00002230 // Consume the optional ';'
Alp Toker35d87032013-12-30 23:29:50 +00002231 TryConsumeToken(tok::semi);
2232
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002233 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002234 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002235
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002236 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu0d730542012-01-21 02:59:18 +00002237 Diag(DeclaratorInfo.getIdentifierLoc(),
2238 diag::err_function_declared_typedef);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002239
Richard Smith2603b092012-11-15 22:54:20 +00002240 // Recover by treating the 'typedef' as spurious.
2241 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002242 }
2243
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002244 Decl *FunDecl =
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002245 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor5d1b4e32011-11-07 20:56:01 +00002246 VS, DefinitionKind, Init);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002247
David Majnemer23252a32013-08-01 04:22:55 +00002248 if (FunDecl) {
2249 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2250 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2251 }
2252 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2253 LateParsedAttrs[i]->addDecl(FunDecl);
2254 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002255 }
2256 LateParsedAttrs.clear();
Alexis Hunt5a7fa252011-05-12 06:15:49 +00002257
2258 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu2f7dc462012-05-16 19:04:59 +00002259 if (Tok.is(tok::semi))
Richard Smith87f5dc52012-07-23 05:45:25 +00002260 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor8a4db832011-01-19 16:41:58 +00002261
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002262 return;
Argyrios Kyrtzidisf4ebe9e2008-06-28 08:10:48 +00002263 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002264 }
2265
2266 // member-declarator-list:
2267 // member-declarator
2268 // member-declarator-list ',' member-declarator
2269
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002270 while (1) {
Richard Smith2b013182012-06-10 03:12:00 +00002271 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002272 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith938f40b2011-06-11 17:19:42 +00002273 if (BitfieldSize.get()) {
2274 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002275 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith938f40b2011-06-11 17:19:42 +00002276 } else {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002277 HasInitializer = true;
Richard Smith2b013182012-06-10 03:12:00 +00002278 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2279 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smith2b013182012-06-10 03:12:00 +00002280 != DeclSpec::SCS_typedef)
2281 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith938f40b2011-06-11 17:19:42 +00002282 }
2283 }
2284
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002285 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002286 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidised983422008-07-01 10:37:29 +00002287 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall07e91c02009-08-06 02:15:43 +00002288
Rafael Espindola0a67e2f2013-01-08 20:44:06 +00002289 NamedDecl *ThisDecl = 0;
John McCall07e91c02009-08-06 02:15:43 +00002290 if (DS.isFriendSpecified()) {
Richard Smith72553fc2014-01-23 23:53:27 +00002291 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
Michael Handdc016d2012-11-28 23:17:40 +00002292 // to a friend declaration, that declaration shall be a definition.
2293 //
Richard Smith72553fc2014-01-23 23:53:27 +00002294 // Diagnose attributes that appear in a friend member function declarator:
2295 // friend int foo [[]] ();
Michael Handdc016d2012-11-28 23:17:40 +00002296 SmallVector<SourceRange, 4> Ranges;
2297 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
Richard Smith72553fc2014-01-23 23:53:27 +00002298 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
2299 E = Ranges.end(); I != E; ++I)
2300 Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
Michael Handdc016d2012-11-28 23:17:40 +00002301
Richard Smith72553fc2014-01-23 23:53:27 +00002302 // TODO: handle initializers, VS, bitfields, 'delete'
Douglas Gregor0be31a22010-07-02 17:43:08 +00002303 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002304 TemplateParams);
Douglas Gregor3447e762009-08-20 22:52:58 +00002305 } else {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002306 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall07e91c02009-08-06 02:15:43 +00002307 DeclaratorInfo,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002308 TemplateParams,
John McCall07e91c02009-08-06 02:15:43 +00002309 BitfieldSize.release(),
Richard Smith2b013182012-06-10 03:12:00 +00002310 VS, HasInClassInit);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002311
2312 if (VarTemplateDecl *VT =
2313 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : 0)
2314 // Re-direct this decl to refer to the templated decl so that we can
2315 // initialize it.
2316 ThisDecl = VT->getTemplatedDecl();
2317
David Majnemer23252a32013-08-01 04:22:55 +00002318 if (ThisDecl && AccessAttrs)
Richard Smithf8a75c32013-08-29 00:47:48 +00002319 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor3447e762009-08-20 22:52:58 +00002320 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002321
Douglas Gregor728d00b2011-10-10 14:49:18 +00002322 // Handle the initializer.
David Blaikie35506f82013-01-30 01:22:18 +00002323 if (HasInClassInit != ICIS_NoInit &&
2324 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2325 DeclSpec::SCS_static) {
Douglas Gregor728d00b2011-10-10 14:49:18 +00002326 // The initializer was deferred; parse it and cache the tokens.
David Majnemer23252a32013-08-01 04:22:55 +00002327 Diag(Tok, getLangOpts().CPlusPlus11
2328 ? diag::warn_cxx98_compat_nonstatic_member_init
2329 : diag::ext_nonstatic_member_init);
Richard Smith5d164bc2011-10-15 05:09:34 +00002330
Richard Smith938f40b2011-06-11 17:19:42 +00002331 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smith2b013182012-06-10 03:12:00 +00002332 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2333 // declarator is followed by an initializer.
Richard Smith938f40b2011-06-11 17:19:42 +00002334 //
2335 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikiecdd91db2012-02-14 09:00:46 +00002336 // initializer in the grammar, so this is ill-formed.
Richard Smith938f40b2011-06-11 17:19:42 +00002337 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataevee6507d2013-11-18 08:17:37 +00002338 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemer23252a32013-08-01 04:22:55 +00002339
2340 // Avoid later warnings about a class member of incomplete type.
David Blaikiecdd91db2012-02-14 09:00:46 +00002341 if (ThisDecl)
David Blaikiecdd91db2012-02-14 09:00:46 +00002342 ThisDecl->setInvalidDecl();
Richard Smith938f40b2011-06-11 17:19:42 +00002343 } else
2344 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002345 } else if (HasInitializer) {
2346 // Normal initializer.
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002347 if (!Init.isUsable())
David Majnemer23252a32013-08-01 04:22:55 +00002348 Init = ParseCXXMemberInitializer(
2349 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2350
Douglas Gregor728d00b2011-10-10 14:49:18 +00002351 if (Init.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00002352 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002353 else if (ThisDecl)
Sebastian Redleef474c2012-02-22 10:50:08 +00002354 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smith74aeef52013-04-26 16:15:35 +00002355 DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002356 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor728d00b2011-10-10 14:49:18 +00002357 // No initializer.
Richard Smith74aeef52013-04-26 16:15:35 +00002358 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemer23252a32013-08-01 04:22:55 +00002359
Douglas Gregor728d00b2011-10-10 14:49:18 +00002360 if (ThisDecl) {
David Majnemer23252a32013-08-01 04:22:55 +00002361 if (!ThisDecl->isInvalidDecl()) {
2362 // Set the Decl for any late parsed attributes
2363 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2364 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2365
2366 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2367 LateParsedAttrs[i]->addDecl(ThisDecl);
2368 }
Douglas Gregor728d00b2011-10-10 14:49:18 +00002369 Actions.FinalizeDeclaration(ThisDecl);
2370 DeclsInGroup.push_back(ThisDecl);
David Majnemer23252a32013-08-01 04:22:55 +00002371
2372 if (DeclaratorInfo.isFunctionDeclarator() &&
2373 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2374 DeclSpec::SCS_typedef)
2375 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor728d00b2011-10-10 14:49:18 +00002376 }
David Majnemer23252a32013-08-01 04:22:55 +00002377 LateParsedAttrs.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002378
2379 DeclaratorInfo.complete(ThisDecl);
Richard Smith938f40b2011-06-11 17:19:42 +00002380
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002381 // If we don't have a comma, it is either the end of the list (a ';')
2382 // or an error, bail out.
Alp Toker094e5212014-01-05 03:27:11 +00002383 SourceLocation CommaLoc;
2384 if (!TryConsumeToken(tok::comma, CommaLoc))
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002385 break;
Mike Stump11289f42009-09-09 15:08:12 +00002386
Richard Smithc8a79032012-01-09 22:31:44 +00002387 if (Tok.isAtStartOfLine() &&
2388 !MightBeDeclarator(Declarator::MemberContext)) {
2389 // This comma was followed by a line-break and something which can't be
2390 // the start of a declarator. The comma was probably a typo for a
2391 // semicolon.
2392 Diag(CommaLoc, diag::err_expected_semi_declaration)
2393 << FixItHint::CreateReplacement(CommaLoc, ";");
2394 ExpectSemi = false;
2395 break;
2396 }
Mike Stump11289f42009-09-09 15:08:12 +00002397
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002398 // Parse the next declarator.
2399 DeclaratorInfo.clear();
Nico Weber24b2a822011-01-28 06:07:34 +00002400 VS.clear();
Douglas Gregor728d00b2011-10-10 14:49:18 +00002401 BitfieldSize = true;
Douglas Gregor50cefbf2011-10-17 17:09:53 +00002402 Init = true;
2403 HasInitializer = false;
Richard Smith8d06f422012-01-12 23:53:29 +00002404 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002405
Richard Smith72553fc2014-01-23 23:53:27 +00002406 // GNU attributes are allowed before the second and subsequent declarator.
John McCall53fa7142010-12-24 02:08:15 +00002407 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002408
Richard Smith72553fc2014-01-23 23:53:27 +00002409 ParseCXXMemberDeclaratorBeforeInitializer(DeclaratorInfo, VS, BitfieldSize,
2410 LateParsedAttrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002411 }
2412
Richard Smithc8a79032012-01-09 22:31:44 +00002413 if (ExpectSemi &&
2414 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattner916dbf12010-02-02 00:43:15 +00002415 // Skip to end of block or statement.
Alexey Bataevee6507d2013-11-18 08:17:37 +00002416 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner916dbf12010-02-02 00:43:15 +00002417 // If we stopped at a ';', eat it.
Alp Toker35d87032013-12-30 23:29:50 +00002418 TryConsumeToken(tok::semi);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002419 return;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002420 }
2421
Rafael Espindolaab417692013-07-09 12:05:01 +00002422 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002423}
2424
Richard Smith938f40b2011-06-11 17:19:42 +00002425/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2426/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2427/// function definition. The location of the '=', if any, will be placed in
2428/// EqualLoc.
2429///
2430/// pure-specifier:
2431/// '= 0'
Sebastian Redleef474c2012-02-22 10:50:08 +00002432///
Richard Smith938f40b2011-06-11 17:19:42 +00002433/// brace-or-equal-initializer:
2434/// '=' initializer-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002435/// braced-init-list
2436///
Richard Smith938f40b2011-06-11 17:19:42 +00002437/// initializer-clause:
2438/// assignment-expression
Sebastian Redleef474c2012-02-22 10:50:08 +00002439/// braced-init-list
2440///
Richard Smithda35e962013-11-09 04:52:51 +00002441/// defaulted/deleted function-definition:
Richard Smith938f40b2011-06-11 17:19:42 +00002442/// '=' 'default'
2443/// '=' 'delete'
2444///
2445/// Prior to C++0x, the assignment-expression in an initializer-clause must
2446/// be a constant-expression.
Douglas Gregor926410d2012-02-21 02:22:07 +00002447ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith938f40b2011-06-11 17:19:42 +00002448 SourceLocation &EqualLoc) {
2449 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2450 && "Data member initializer not starting with '=' or '{'");
2451
Douglas Gregor926410d2012-02-21 02:22:07 +00002452 EnterExpressionEvaluationContext Context(Actions,
2453 Sema::PotentiallyEvaluated,
2454 D);
Alp Toker094e5212014-01-05 03:27:11 +00002455 if (TryConsumeToken(tok::equal, EqualLoc)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002456 if (Tok.is(tok::kw_delete)) {
2457 // In principle, an initializer of '= delete p;' is legal, but it will
2458 // never type-check. It's better to diagnose it as an ill-formed expression
2459 // than as an ill-formed deleted non-function member.
2460 // An initializer of '= delete p, foo' will never be parsed, because
2461 // a top-level comma always ends the initializer expression.
2462 const Token &Next = NextToken();
2463 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
Richard Smith34f30512013-11-23 04:06:09 +00002464 Next.is(tok::eof)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002465 if (IsFunction)
2466 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2467 << 1 /* delete */;
2468 else
2469 Diag(ConsumeToken(), diag::err_deleted_non_function);
2470 return ExprResult();
2471 }
2472 } else if (Tok.is(tok::kw_default)) {
Richard Smith938f40b2011-06-11 17:19:42 +00002473 if (IsFunction)
2474 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2475 << 0 /* default */;
2476 else
2477 Diag(ConsumeToken(), diag::err_default_special_members);
2478 return ExprResult();
2479 }
2480
Sebastian Redleef474c2012-02-22 10:50:08 +00002481 }
2482 return ParseInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00002483}
2484
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002485/// ParseCXXMemberSpecification - Parse the class definition.
2486///
2487/// member-specification:
2488/// member-declaration member-specification[opt]
2489/// access-specifier ':' member-specification[opt]
2490///
Joao Matose9a3ed42012-08-31 22:18:20 +00002491void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han309af292013-01-07 16:57:11 +00002492 SourceLocation AttrFixitLoc,
Richard Smith4c96e992013-02-19 23:47:15 +00002493 ParsedAttributesWithRange &Attrs,
Joao Matose9a3ed42012-08-31 22:18:20 +00002494 unsigned TagType, Decl *TagDecl) {
2495 assert((TagType == DeclSpec::TST_struct ||
2496 TagType == DeclSpec::TST_interface ||
2497 TagType == DeclSpec::TST_union ||
2498 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2499
John McCallfaf5fb42010-08-26 23:41:50 +00002500 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2501 "parsing struct/union/class body");
Mike Stump11289f42009-09-09 15:08:12 +00002502
Douglas Gregoredf8f392010-01-16 20:52:59 +00002503 // Determine whether this is a non-nested class. Note that local
2504 // classes are *not* considered to be nested classes.
2505 bool NonNestedClass = true;
2506 if (!ClassStack.empty()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002507 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00002508 if (S->isClassScope()) {
2509 // We're inside a class scope, so this is a nested class.
2510 NonNestedClass = false;
John McCalldb632ac2012-09-25 07:32:39 +00002511
2512 // The Microsoft extension __interface does not permit nested classes.
2513 if (getCurrentClass().IsInterface) {
2514 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2515 << /*ErrorType=*/6
2516 << (isa<NamedDecl>(TagDecl)
2517 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2518 : "<anonymous>");
2519 }
Douglas Gregoredf8f392010-01-16 20:52:59 +00002520 break;
2521 }
2522
2523 if ((S->getFlags() & Scope::FnScope)) {
2524 // If we're in a function or function template declared in the
2525 // body of a class, then this is a local class rather than a
2526 // nested class.
2527 const Scope *Parent = S->getParent();
2528 if (Parent->isTemplateParamScope())
2529 Parent = Parent->getParent();
2530 if (Parent->isClassScope())
2531 break;
2532 }
2533 }
2534 }
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002535
2536 // Enter a scope for the class.
Douglas Gregor658b9552009-01-09 22:42:13 +00002537 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002538
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002539 // Note that we are parsing a new (potentially-nested) class definition.
John McCalldb632ac2012-09-25 07:32:39 +00002540 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2541 TagType == DeclSpec::TST_interface);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002542
Douglas Gregorcd72ba92009-02-06 22:42:48 +00002543 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002544 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002545
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002546 SourceLocation FinalLoc;
David Majnemera5433082013-10-18 00:33:31 +00002547 bool IsFinalSpelledSealed = false;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002548
2549 // Parse the optional 'final' keyword.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002550 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemera5433082013-10-18 00:33:31 +00002551 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2552 assert((Specifier == VirtSpecifiers::VS_Final ||
2553 Specifier == VirtSpecifiers::VS_Sealed) &&
2554 "not a class definition");
Richard Smithda261112011-10-15 04:21:46 +00002555 FinalLoc = ConsumeToken();
David Majnemera5433082013-10-18 00:33:31 +00002556 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002557
David Majnemera5433082013-10-18 00:33:31 +00002558 if (TagType == DeclSpec::TST_interface)
John McCalldb632ac2012-09-25 07:32:39 +00002559 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemera5433082013-10-18 00:33:31 +00002560 << VirtSpecifiers::getSpecifierName(Specifier);
2561 else if (Specifier == VirtSpecifiers::VS_Final)
2562 Diag(FinalLoc, getLangOpts().CPlusPlus11
2563 ? diag::warn_cxx98_compat_override_control_keyword
2564 : diag::ext_override_control_keyword)
2565 << VirtSpecifiers::getSpecifierName(Specifier);
2566 else if (Specifier == VirtSpecifiers::VS_Sealed)
2567 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han9407e502012-11-26 22:54:45 +00002568
Michael Han309af292013-01-07 16:57:11 +00002569 // Parse any C++11 attributes after 'final' keyword.
2570 // These attributes are not allowed to appear here,
2571 // and the only possible place for them to appertain
2572 // to the class would be between class-key and class-name.
Richard Smith4c96e992013-02-19 23:47:15 +00002573 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonf9eb63b2011-03-25 14:46:08 +00002574 }
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00002575
John McCall2d814c32009-12-19 21:48:58 +00002576 if (Tok.is(tok::colon)) {
2577 ParseBaseClause(TagDecl);
2578
2579 if (!Tok.is(tok::l_brace)) {
2580 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCall2ff380a2010-03-17 00:38:33 +00002581
2582 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002583 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCall2d814c32009-12-19 21:48:58 +00002584 return;
2585 }
2586 }
2587
2588 assert(Tok.is(tok::l_brace));
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002589 BalancedDelimiterTracker T(*this, tok::l_brace);
2590 T.consumeOpen();
John McCall2d814c32009-12-19 21:48:58 +00002591
John McCall08bede42010-05-28 08:11:17 +00002592 if (TagDecl)
Anders Carlsson30f29442011-03-25 14:31:08 +00002593 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemera5433082013-10-18 00:33:31 +00002594 IsFinalSpelledSealed,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002595 T.getOpenLocation());
John McCall1c7e6ec2009-12-20 07:58:13 +00002596
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002597 // C++ 11p3: Members of a class defined with the keyword class are private
2598 // by default. Members of a class defined with the keywords struct or union
2599 // are public by default.
2600 AccessSpecifier CurAS;
2601 if (TagType == DeclSpec::TST_class)
2602 CurAS = AS_private;
2603 else
2604 CurAS = AS_public;
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002605 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002606
Douglas Gregor9377c822010-06-21 22:31:09 +00002607 if (TagDecl) {
2608 // While we still have something to read, read the member-declarations.
Richard Smith34f30512013-11-23 04:06:09 +00002609 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Douglas Gregor9377c822010-06-21 22:31:09 +00002610 // Each iteration of this loop reads one member-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002611
David Blaikiebbafb8a2012-03-11 07:00:24 +00002612 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet8f981d52011-05-25 10:19:49 +00002613 Tok.is(tok::kw___if_not_exists))) {
2614 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2615 continue;
2616 }
2617
Douglas Gregor9377c822010-06-21 22:31:09 +00002618 // Check for extraneous top-level semicolon.
2619 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00002620 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor9377c822010-06-21 22:31:09 +00002621 continue;
2622 }
2623
Eli Friedmanec52f922012-02-23 23:47:16 +00002624 if (Tok.is(tok::annot_pragma_vis)) {
2625 HandlePragmaVisibility();
2626 continue;
2627 }
2628
2629 if (Tok.is(tok::annot_pragma_pack)) {
2630 HandlePragmaPack();
2631 continue;
2632 }
2633
Argyrios Kyrtzidis5c2021b2012-10-12 17:39:59 +00002634 if (Tok.is(tok::annot_pragma_align)) {
2635 HandlePragmaAlign();
2636 continue;
2637 }
2638
Alexey Bataeva769e072013-03-22 06:34:35 +00002639 if (Tok.is(tok::annot_pragma_openmp)) {
2640 ParseOpenMPDeclarativeDirective();
2641 continue;
2642 }
2643
David Majnemer4bb09802014-02-10 19:50:15 +00002644 if (Tok.is(tok::annot_pragma_ms_pointers_to_members)) {
2645 HandlePragmaMSPointersToMembers();
2646 continue;
2647 }
2648
Richard Smithda35e962013-11-09 04:52:51 +00002649 // If we see a namespace here, a close brace was missing somewhere.
2650 if (Tok.is(tok::kw_namespace)) {
Richard Smith2ac43ad2013-11-15 23:00:02 +00002651 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
Richard Smithda35e962013-11-09 04:52:51 +00002652 break;
2653 }
2654
Douglas Gregor9377c822010-06-21 22:31:09 +00002655 AccessSpecifier AS = getAccessSpecifierIfPresent();
2656 if (AS != AS_none) {
2657 // Current token is a C++ access specifier.
2658 CurAS = AS;
2659 SourceLocation ASLoc = Tok.getLocation();
David Blaikieeba32c22011-10-13 06:08:43 +00002660 unsigned TokLength = Tok.getLength();
Douglas Gregor9377c822010-06-21 22:31:09 +00002661 ConsumeToken();
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002662 AccessAttrs.clear();
2663 MaybeParseGNUAttributes(AccessAttrs);
2664
David Blaikieeba32c22011-10-13 06:08:43 +00002665 SourceLocation EndLoc;
Alp Toker35d87032013-12-30 23:29:50 +00002666 if (TryConsumeToken(tok::colon, EndLoc)) {
2667 } else if (TryConsumeToken(tok::semi, EndLoc)) {
2668 Diag(EndLoc, diag::err_expected)
2669 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002670 } else {
2671 EndLoc = ASLoc.getLocWithOffset(TokLength);
Alp Toker35d87032013-12-30 23:29:50 +00002672 Diag(EndLoc, diag::err_expected)
2673 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
David Blaikieeba32c22011-10-13 06:08:43 +00002674 }
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002675
John McCalldb632ac2012-09-25 07:32:39 +00002676 // The Microsoft extension __interface does not permit non-public
2677 // access specifiers.
2678 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2679 Diag(ASLoc, diag::err_access_specifier_interface)
2680 << (CurAS == AS_protected);
2681 }
2682
Erik Verbruggenfd979b12011-10-17 09:54:52 +00002683 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2684 AccessAttrs.getList())) {
2685 // found another attribute than only annotations
2686 AccessAttrs.clear();
2687 }
2688
Douglas Gregor9377c822010-06-21 22:31:09 +00002689 continue;
2690 }
2691
Douglas Gregor9377c822010-06-21 22:31:09 +00002692 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00002693 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002694 }
2695
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002696 T.consumeClose();
Douglas Gregor9377c822010-06-21 22:31:09 +00002697 } else {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002698 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002699 }
Mike Stump11289f42009-09-09 15:08:12 +00002700
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002701 // If attributes exist after class contents, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002702 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002703 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002704
John McCall08bede42010-05-28 08:11:17 +00002705 if (TagDecl)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002706 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002707 T.getOpenLocation(),
2708 T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00002709 attrs.getList());
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002710
Douglas Gregor433e0532012-04-16 18:27:27 +00002711 // C++11 [class.mem]p2:
2712 // Within the class member-specification, the class is regarded as complete
Richard Smith2331bbf2012-05-02 22:22:32 +00002713 // within function bodies, default arguments, and
Douglas Gregor433e0532012-04-16 18:27:27 +00002714 // brace-or-equal-initializers for non-static data members (including such
2715 // things in nested classes).
Douglas Gregor9377c822010-06-21 22:31:09 +00002716 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002717 // We are not inside a nested class. This class and its nested classes
Douglas Gregor4d87df52008-12-16 21:30:33 +00002718 // are complete and we can parse the delayed portions of method
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002719 // declarations and the lexed inline method definitions, along with any
2720 // delayed attributes.
Douglas Gregor428119e2010-06-16 23:45:56 +00002721 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00002722 ParseLexedAttributes(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002723 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smith84973e52012-04-21 18:42:51 +00002724
2725 // We've finished with all pending member declarations.
2726 Actions.ActOnFinishCXXMemberDecls();
2727
Richard Smith938f40b2011-06-11 17:19:42 +00002728 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002729 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregor428119e2010-06-16 23:45:56 +00002730 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002731 }
2732
John McCall08bede42010-05-28 08:11:17 +00002733 if (TagDecl)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002734 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2735 T.getCloseLocation());
John McCall2ff380a2010-03-17 00:38:33 +00002736
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002737 // Leave the class scope.
Douglas Gregore44a2ad2009-05-27 23:11:45 +00002738 ParsingDef.Pop();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002739 ClassScope.Exit();
Argyrios Kyrtzidis7bbb20e2008-06-24 22:12:16 +00002740}
Douglas Gregore8381c02008-11-05 04:29:56 +00002741
Richard Smith2ac43ad2013-11-15 23:00:02 +00002742void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithda35e962013-11-09 04:52:51 +00002743 assert(Tok.is(tok::kw_namespace));
2744
2745 // FIXME: Suggest where the close brace should have gone by looking
2746 // at indentation changes within the definition body.
Richard Smith2ac43ad2013-11-15 23:00:02 +00002747 Diag(D->getLocation(),
2748 diag::err_missing_end_of_definition) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002749 Diag(Tok.getLocation(),
Richard Smith2ac43ad2013-11-15 23:00:02 +00002750 diag::note_missing_end_of_definition_before) << D;
Richard Smithda35e962013-11-09 04:52:51 +00002751
2752 // Push '};' onto the token stream to recover.
2753 PP.EnterToken(Tok);
2754
2755 Tok.startToken();
2756 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
2757 Tok.setKind(tok::semi);
2758 PP.EnterToken(Tok);
2759
2760 Tok.setKind(tok::r_brace);
2761}
2762
Douglas Gregore8381c02008-11-05 04:29:56 +00002763/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2764/// which explicitly initializes the members or base classes of a
2765/// class (C++ [class.base.init]). For example, the three initializers
2766/// after the ':' in the Derived constructor below:
2767///
2768/// @code
2769/// class Base { };
2770/// class Derived : Base {
2771/// int x;
2772/// float f;
2773/// public:
2774/// Derived(float f) : Base(), x(17), f(f) { }
2775/// };
2776/// @endcode
2777///
Mike Stump11289f42009-09-09 15:08:12 +00002778/// [C++] ctor-initializer:
2779/// ':' mem-initializer-list
Douglas Gregore8381c02008-11-05 04:29:56 +00002780///
Mike Stump11289f42009-09-09 15:08:12 +00002781/// [C++] mem-initializer-list:
Douglas Gregor44e7df62011-01-04 00:32:56 +00002782/// mem-initializer ...[opt]
2783/// mem-initializer ...[opt] , mem-initializer-list
John McCall48871652010-08-21 09:40:31 +00002784void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregore8381c02008-11-05 04:29:56 +00002785 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2786
John Wiegley1c0675e2011-04-28 01:08:34 +00002787 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2788 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregore8381c02008-11-05 04:29:56 +00002789 SourceLocation ColonLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002790
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002791 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002792 bool AnyErrors = false;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00002793
Douglas Gregore8381c02008-11-05 04:29:56 +00002794 do {
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002795 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko27cb3dd02013-06-23 22:58:02 +00002796 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2797 MemInitializers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002798 return cutOffParsing();
Douglas Gregoreaeeca92010-08-28 00:00:50 +00002799 } else {
2800 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2801 if (!MemInit.isInvalid())
2802 MemInitializers.push_back(MemInit.get());
2803 else
2804 AnyErrors = true;
2805 }
2806
Douglas Gregore8381c02008-11-05 04:29:56 +00002807 if (Tok.is(tok::comma))
2808 ConsumeToken();
2809 else if (Tok.is(tok::l_brace))
2810 break;
Douglas Gregor3465e262010-09-07 14:35:10 +00002811 // If the next token looks like a base or member initializer, assume that
2812 // we're just missing a comma.
Douglas Gregorce66d022010-09-07 14:51:08 +00002813 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2814 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2815 Diag(Loc, diag::err_ctor_init_missing_comma)
2816 << FixItHint::CreateInsertion(Loc, ", ");
2817 } else {
Douglas Gregore8381c02008-11-05 04:29:56 +00002818 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Alp Tokerec543272013-12-24 09:48:30 +00002819 Diag(Tok.getLocation(), diag::err_expected_either) << tok::l_brace
2820 << tok::comma;
Alexey Bataevee6507d2013-11-18 08:17:37 +00002821 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregore8381c02008-11-05 04:29:56 +00002822 break;
2823 }
2824 } while (true);
2825
David Blaikie3fc2f912013-01-17 05:26:25 +00002826 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor7ae2d772010-01-31 09:12:51 +00002827 AnyErrors);
Douglas Gregore8381c02008-11-05 04:29:56 +00002828}
2829
2830/// ParseMemInitializer - Parse a C++ member initializer, which is
2831/// part of a constructor initializer that explicitly initializes one
2832/// member or base class (C++ [class.base.init]). See
2833/// ParseConstructorInitializer for an example.
2834///
2835/// [C++] mem-initializer:
2836/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redl3da34892011-06-05 12:23:16 +00002837/// [C++0x] mem-initializer-id braced-init-list
Mike Stump11289f42009-09-09 15:08:12 +00002838///
Douglas Gregore8381c02008-11-05 04:29:56 +00002839/// [C++] mem-initializer-id:
2840/// '::'[opt] nested-name-specifier[opt] class-name
2841/// identifier
John McCall48871652010-08-21 09:40:31 +00002842Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanian302bb662009-06-30 23:26:25 +00002843 // parse '::'[opt] nested-name-specifier[opt]
2844 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00002845 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallba7bf592010-08-24 05:47:05 +00002846 ParsedType TemplateTypeTy;
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002847 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002848 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor46c59612010-01-12 17:52:59 +00002849 if (TemplateId->Kind == TNK_Type_template ||
2850 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregore7c20652011-03-02 00:47:37 +00002851 AnnotateTemplateIdTokenAsType();
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002852 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallba7bf592010-08-24 05:47:05 +00002853 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002854 }
Fariborz Jahanianc1fc3ec2009-07-01 19:21:19 +00002855 }
David Blaikie186a8892012-01-24 06:03:59 +00002856 // Uses of decltype will already have been converted to annot_decltype by
2857 // ParseOptionalCXXScopeSpecifier at this point.
2858 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2859 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002860 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregore8381c02008-11-05 04:29:56 +00002861 return true;
2862 }
Mike Stump11289f42009-09-09 15:08:12 +00002863
David Blaikie186a8892012-01-24 06:03:59 +00002864 IdentifierInfo *II = 0;
2865 DeclSpec DS(AttrFactory);
2866 SourceLocation IdLoc = Tok.getLocation();
2867 if (Tok.is(tok::annot_decltype)) {
2868 // Get the decltype expression, if there is one.
2869 ParseDecltypeSpecifier(DS);
2870 } else {
2871 if (Tok.is(tok::identifier))
2872 // Get the identifier. This may be a member name or a class name,
2873 // but we'll let the semantic analysis determine which it is.
2874 II = Tok.getIdentifierInfo();
2875 ConsumeToken();
2876 }
2877
Douglas Gregore8381c02008-11-05 04:29:56 +00002878
2879 // Parse the '('.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002880 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00002881 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2882
Sebastian Redla74948d2011-09-24 17:48:25 +00002883 ExprResult InitList = ParseBraceInitializer();
2884 if (InitList.isInvalid())
2885 return true;
2886
2887 SourceLocation EllipsisLoc;
Alp Toker094e5212014-01-05 03:27:11 +00002888 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redla74948d2011-09-24 17:48:25 +00002889
2890 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002891 TemplateTypeTy, DS, IdLoc,
2892 InitList.take(), EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00002893 } else if(Tok.is(tok::l_paren)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002894 BalancedDelimiterTracker T(*this, tok::l_paren);
2895 T.consumeOpen();
Douglas Gregore8381c02008-11-05 04:29:56 +00002896
Sebastian Redl3da34892011-06-05 12:23:16 +00002897 // Parse the optional expression-list.
Benjamin Kramerf0623432012-08-23 22:51:59 +00002898 ExprVector ArgExprs;
Sebastian Redl3da34892011-06-05 12:23:16 +00002899 CommaLocsTy CommaLocs;
2900 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002901 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redl3da34892011-06-05 12:23:16 +00002902 return true;
2903 }
2904
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002905 T.consumeClose();
Sebastian Redl3da34892011-06-05 12:23:16 +00002906
2907 SourceLocation EllipsisLoc;
Alp Toker97650562014-01-10 11:19:30 +00002908 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Sebastian Redl3da34892011-06-05 12:23:16 +00002909
2910 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikie186a8892012-01-24 06:03:59 +00002911 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenko139474d2013-05-09 23:51:52 +00002912 T.getOpenLocation(), ArgExprs,
2913 T.getCloseLocation(), EllipsisLoc);
Douglas Gregore8381c02008-11-05 04:29:56 +00002914 }
2915
Alp Tokerec543272013-12-24 09:48:30 +00002916 if (getLangOpts().CPlusPlus11)
2917 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
2918 else
2919 return Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregore8381c02008-11-05 04:29:56 +00002920}
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002921
Sebastian Redl965b0e32011-03-05 14:45:16 +00002922/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002923///
Douglas Gregor356513d2008-12-01 18:00:20 +00002924/// exception-specification:
Sebastian Redl965b0e32011-03-05 14:45:16 +00002925/// dynamic-exception-specification
2926/// noexcept-specification
2927///
2928/// noexcept-specification:
2929/// 'noexcept'
2930/// 'noexcept' '(' constant-expression ')'
2931ExceptionSpecificationType
Richard Smith2331bbf2012-05-02 22:22:32 +00002932Parser::tryParseExceptionSpecification(
Douglas Gregor433e0532012-04-16 18:27:27 +00002933 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002934 SmallVectorImpl<ParsedType> &DynamicExceptions,
2935 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00002936 ExprResult &NoexceptExpr) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00002937 ExceptionSpecificationType Result = EST_None;
2938
2939 // See if there's a dynamic specification.
2940 if (Tok.is(tok::kw_throw)) {
2941 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2942 DynamicExceptions,
2943 DynamicExceptionRanges);
2944 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2945 "Produced different number of exception types and ranges.");
2946 }
2947
2948 // If there's no noexcept specification, we're done.
2949 if (Tok.isNot(tok::kw_noexcept))
2950 return Result;
2951
Richard Smithb15c11c2011-10-17 23:06:20 +00002952 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2953
Sebastian Redl965b0e32011-03-05 14:45:16 +00002954 // If we already had a dynamic specification, parse the noexcept for,
2955 // recovery, but emit a diagnostic and don't store the results.
2956 SourceRange NoexceptRange;
2957 ExceptionSpecificationType NoexceptType = EST_None;
2958
2959 SourceLocation KeywordLoc = ConsumeToken();
2960 if (Tok.is(tok::l_paren)) {
2961 // There is an argument.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002962 BalancedDelimiterTracker T(*this, tok::l_paren);
2963 T.consumeOpen();
Sebastian Redl965b0e32011-03-05 14:45:16 +00002964 NoexceptType = EST_ComputedNoexcept;
2965 NoexceptExpr = ParseConstantExpression();
Sebastian Redlfa453cf2011-03-12 11:50:43 +00002966 // The argument must be contextually convertible to bool. We use
2967 // ActOnBooleanCondition for this purpose.
2968 if (!NoexceptExpr.isInvalid())
2969 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2970 NoexceptExpr.get());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002971 T.consumeClose();
2972 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl965b0e32011-03-05 14:45:16 +00002973 } else {
2974 // There is no argument.
2975 NoexceptType = EST_BasicNoexcept;
2976 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2977 }
2978
2979 if (Result == EST_None) {
2980 SpecificationRange = NoexceptRange;
2981 Result = NoexceptType;
2982
2983 // If there's a dynamic specification after a noexcept specification,
2984 // parse that and ignore the results.
2985 if (Tok.is(tok::kw_throw)) {
2986 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2987 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2988 DynamicExceptionRanges);
2989 }
2990 } else {
2991 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2992 }
2993
2994 return Result;
2995}
2996
Richard Smith8ca78a12013-06-13 02:02:51 +00002997static void diagnoseDynamicExceptionSpecification(
2998 Parser &P, const SourceRange &Range, bool IsNoexcept) {
2999 if (P.getLangOpts().CPlusPlus11) {
3000 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
3001 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
3002 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
3003 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
3004 }
3005}
3006
Sebastian Redl965b0e32011-03-05 14:45:16 +00003007/// ParseDynamicExceptionSpecification - Parse a C++
3008/// dynamic-exception-specification (C++ [except.spec]).
3009///
3010/// dynamic-exception-specification:
Douglas Gregor356513d2008-12-01 18:00:20 +00003011/// 'throw' '(' type-id-list [opt] ')'
3012/// [MS] 'throw' '(' '...' ')'
Mike Stump11289f42009-09-09 15:08:12 +00003013///
Douglas Gregor356513d2008-12-01 18:00:20 +00003014/// type-id-list:
Douglas Gregor830837d2010-12-20 23:57:46 +00003015/// type-id ... [opt]
3016/// type-id-list ',' type-id ... [opt]
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003017///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003018ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
3019 SourceRange &SpecificationRange,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003020 SmallVectorImpl<ParsedType> &Exceptions,
3021 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003022 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump11289f42009-09-09 15:08:12 +00003023
Sebastian Redl965b0e32011-03-05 14:45:16 +00003024 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003025 BalancedDelimiterTracker T(*this, tok::l_paren);
3026 if (T.consumeOpen()) {
Sebastian Redl965b0e32011-03-05 14:45:16 +00003027 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3028 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003029 return EST_DynamicNone;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003030 }
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003031
Douglas Gregor356513d2008-12-01 18:00:20 +00003032 // Parse throw(...), a Microsoft extension that means "this function
3033 // can throw anything".
3034 if (Tok.is(tok::ellipsis)) {
3035 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003036 if (!getLangOpts().MicrosoftExt)
Douglas Gregor356513d2008-12-01 18:00:20 +00003037 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003038 T.consumeClose();
3039 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003040 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003041 return EST_MSAny;
Douglas Gregor356513d2008-12-01 18:00:20 +00003042 }
3043
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003044 // Parse the sequence of type-ids.
Sebastian Redld6434562009-05-29 18:02:33 +00003045 SourceRange Range;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003046 while (Tok.isNot(tok::r_paren)) {
Sebastian Redld6434562009-05-29 18:02:33 +00003047 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl965b0e32011-03-05 14:45:16 +00003048
Douglas Gregor830837d2010-12-20 23:57:46 +00003049 if (Tok.is(tok::ellipsis)) {
3050 // C++0x [temp.variadic]p5:
3051 // - In a dynamic-exception-specification (15.4); the pattern is a
3052 // type-id.
3053 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl965b0e32011-03-05 14:45:16 +00003054 Range.setEnd(Ellipsis);
Douglas Gregor830837d2010-12-20 23:57:46 +00003055 if (!Res.isInvalid())
3056 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3057 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003058
Sebastian Redld6434562009-05-29 18:02:33 +00003059 if (!Res.isInvalid()) {
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003060 Exceptions.push_back(Res.get());
Sebastian Redld6434562009-05-29 18:02:33 +00003061 Ranges.push_back(Range);
3062 }
Alp Toker97650562014-01-10 11:19:30 +00003063
3064 if (!TryConsumeToken(tok::comma))
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003065 break;
3066 }
3067
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003068 T.consumeClose();
3069 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith8ca78a12013-06-13 02:02:51 +00003070 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3071 Exceptions.empty());
Sebastian Redlfa453cf2011-03-12 11:50:43 +00003072 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003073}
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003074
Douglas Gregor7fb25412010-10-01 18:44:50 +00003075/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3076/// function declaration.
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003077TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregor7fb25412010-10-01 18:44:50 +00003078 assert(Tok.is(tok::arrow) && "expected arrow");
3079
3080 ConsumeToken();
3081
Richard Smithbfdb1082012-03-12 08:56:40 +00003082 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregor7fb25412010-10-01 18:44:50 +00003083}
3084
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003085/// \brief We have just started parsing the definition of a new class,
3086/// so push that class onto our stack of classes that is currently
3087/// being parsed.
John McCallc1465822011-02-14 07:13:47 +00003088Sema::ParsingClassState
John McCalldb632ac2012-09-25 07:32:39 +00003089Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3090 bool IsInterface) {
Douglas Gregoredf8f392010-01-16 20:52:59 +00003091 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003092 "Nested class without outer class");
John McCalldb632ac2012-09-25 07:32:39 +00003093 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCallc1465822011-02-14 07:13:47 +00003094 return Actions.PushParsingClass();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003095}
3096
3097/// \brief Deallocate the given parsed class and all of its nested
3098/// classes.
3099void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregorefc46952010-10-12 16:25:54 +00003100 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3101 delete Class->LateParsedDeclarations[I];
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003102 delete Class;
3103}
3104
3105/// \brief Pop the top class of the stack of classes that are
3106/// currently being parsed.
3107///
3108/// This routine should be called when we have finished parsing the
3109/// definition of a class, but have not yet popped the Scope
3110/// associated with the class's definition.
John McCallc1465822011-02-14 07:13:47 +00003111void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003112 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump11289f42009-09-09 15:08:12 +00003113
John McCallc1465822011-02-14 07:13:47 +00003114 Actions.PopParsingClass(state);
3115
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003116 ParsingClass *Victim = ClassStack.top();
3117 ClassStack.pop();
3118 if (Victim->TopLevelClass) {
3119 // Deallocate all of the nested classes of this class,
3120 // recursively: we don't need to keep any of this information.
3121 DeallocateParsedClasses(Victim);
3122 return;
Mike Stump11289f42009-09-09 15:08:12 +00003123 }
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003124 assert(!ClassStack.empty() && "Missing top-level class?");
3125
Douglas Gregorefc46952010-10-12 16:25:54 +00003126 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003127 // The victim is a nested class, but we will not need to perform
3128 // any processing after the definition of this class since it has
3129 // no members whose handling was delayed. Therefore, we can just
3130 // remove this nested class.
Douglas Gregorefc46952010-10-12 16:25:54 +00003131 DeallocateParsedClasses(Victim);
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003132 return;
3133 }
3134
3135 // This nested class has some members that will need to be processed
3136 // after the top-level class is completely defined. Therefore, add
3137 // it to the list of nested classes within its parent.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003138 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregorefc46952010-10-12 16:25:54 +00003139 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor0be31a22010-07-02 17:43:08 +00003140 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregore44a2ad2009-05-27 23:11:45 +00003141}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003142
Richard Smith3dff2512012-04-10 03:25:07 +00003143/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3144///
3145/// \return the parsed identifier on success, and 0 if the next token is not an
3146/// attribute-token.
3147///
3148/// C++11 [dcl.attr.grammar]p3:
3149/// If a keyword or an alternative token that satisfies the syntactic
3150/// requirements of an identifier is contained in an attribute-token,
3151/// it is considered an identifier.
3152IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3153 switch (Tok.getKind()) {
3154 default:
3155 // Identifiers and keywords have identifier info attached.
3156 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3157 Loc = ConsumeToken();
3158 return II;
3159 }
3160 return 0;
3161
3162 case tok::ampamp: // 'and'
3163 case tok::pipe: // 'bitor'
3164 case tok::pipepipe: // 'or'
3165 case tok::caret: // 'xor'
3166 case tok::tilde: // 'compl'
3167 case tok::amp: // 'bitand'
3168 case tok::ampequal: // 'and_eq'
3169 case tok::pipeequal: // 'or_eq'
3170 case tok::caretequal: // 'xor_eq'
3171 case tok::exclaim: // 'not'
3172 case tok::exclaimequal: // 'not_eq'
3173 // Alternative tokens do not have identifier info, but their spelling
3174 // starts with an alphabetical character.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003175 SmallString<8> SpellingBuf;
Richard Smith3dff2512012-04-10 03:25:07 +00003176 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rosea7d03842013-02-08 22:30:41 +00003177 if (isLetter(Spelling[0])) {
Richard Smith3dff2512012-04-10 03:25:07 +00003178 Loc = ConsumeToken();
Benjamin Kramer5c17f9c2012-04-22 20:43:30 +00003179 return &PP.getIdentifierTable().get(Spelling);
Richard Smith3dff2512012-04-10 03:25:07 +00003180 }
3181 return 0;
3182 }
3183}
3184
Michael Han23214e52012-10-03 01:56:22 +00003185static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3186 IdentifierInfo *ScopeName) {
3187 switch (AttributeList::getKind(AttrName, ScopeName,
3188 AttributeList::AS_CXX11)) {
3189 case AttributeList::AT_CarriesDependency:
3190 case AttributeList::AT_FallThrough:
Richard Smith10876ef2013-01-17 01:30:42 +00003191 case AttributeList::AT_CXX11NoReturn: {
Michael Han23214e52012-10-03 01:56:22 +00003192 return true;
3193 }
3194
3195 default:
3196 return false;
3197 }
3198}
3199
Richard Smith3dff2512012-04-10 03:25:07 +00003200/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003201/// only parses standard attributes.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003202///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003203/// [C++11] attribute-specifier:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003204/// '[' '[' attribute-list ']' ']'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003205/// alignment-specifier
Alexis Hunt96d5c762009-11-21 08:43:09 +00003206///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003207/// [C++11] attribute-list:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003208/// attribute[opt]
3209/// attribute-list ',' attribute[opt]
Richard Smith3dff2512012-04-10 03:25:07 +00003210/// attribute '...'
3211/// attribute-list ',' attribute '...'
Alexis Hunt96d5c762009-11-21 08:43:09 +00003212///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003213/// [C++11] attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003214/// attribute-token attribute-argument-clause[opt]
3215///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003216/// [C++11] attribute-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003217/// identifier
3218/// attribute-scoped-token
3219///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003220/// [C++11] attribute-scoped-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003221/// attribute-namespace '::' identifier
3222///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003223/// [C++11] attribute-namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003224/// identifier
3225///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003226/// [C++11] attribute-argument-clause:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003227/// '(' balanced-token-seq ')'
3228///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003229/// [C++11] balanced-token-seq:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003230/// balanced-token
3231/// balanced-token-seq balanced-token
3232///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003233/// [C++11] balanced-token:
Alexis Hunt96d5c762009-11-21 08:43:09 +00003234/// '(' balanced-token-seq ')'
3235/// '[' balanced-token-seq ']'
3236/// '{' balanced-token-seq '}'
3237/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smith3dff2512012-04-10 03:25:07 +00003238void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003239 SourceLocation *endLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003240 if (Tok.is(tok::kw_alignas)) {
Richard Smithf679b5b2011-10-14 20:48:27 +00003241 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003242 ParseAlignmentSpecifier(attrs, endLoc);
3243 return;
3244 }
3245
Alexis Hunt96d5c762009-11-21 08:43:09 +00003246 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003247 && "Not a C++11 attribute list");
Alexis Hunt96d5c762009-11-21 08:43:09 +00003248
Richard Smithf679b5b2011-10-14 20:48:27 +00003249 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3250
Alexis Hunt96d5c762009-11-21 08:43:09 +00003251 ConsumeBracket();
3252 ConsumeBracket();
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003253
Richard Smith10876ef2013-01-17 01:30:42 +00003254 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3255
Richard Smith3dff2512012-04-10 03:25:07 +00003256 while (Tok.isNot(tok::r_square)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00003257 // attribute not present
Alp Toker97650562014-01-10 11:19:30 +00003258 if (TryConsumeToken(tok::comma))
Alexis Hunt96d5c762009-11-21 08:43:09 +00003259 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003260
Richard Smith3dff2512012-04-10 03:25:07 +00003261 SourceLocation ScopeLoc, AttrLoc;
3262 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3263
3264 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3265 if (!AttrName)
3266 // Break out to the "expected ']'" diagnostic.
3267 break;
Kovarththanan Rajaratnamba2c6522010-03-13 10:17:05 +00003268
Alexis Hunt96d5c762009-11-21 08:43:09 +00003269 // scoped attribute
Alp Toker97650562014-01-10 11:19:30 +00003270 if (TryConsumeToken(tok::coloncolon)) {
Richard Smith3dff2512012-04-10 03:25:07 +00003271 ScopeName = AttrName;
3272 ScopeLoc = AttrLoc;
3273
3274 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3275 if (!AttrName) {
Alp Tokerec543272013-12-24 09:48:30 +00003276 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003277 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003278 continue;
3279 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00003280 }
3281
Michael Han23214e52012-10-03 01:56:22 +00003282 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003283 bool AttrParsed = false;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003284
Richard Smith10876ef2013-01-17 01:30:42 +00003285 if (StandardAttr &&
3286 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3287 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3288 << AttrName << SourceRange(SeenAttrs[AttrName]);
3289
Michael Han23214e52012-10-03 01:56:22 +00003290 // Parse attribute arguments
3291 if (Tok.is(tok::l_paren)) {
3292 if (ScopeName && ScopeName->getName() == "gnu") {
3293 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +00003294 ScopeName, ScopeLoc, AttributeList::AS_CXX11, 0);
Michael Han23214e52012-10-03 01:56:22 +00003295 AttrParsed = true;
3296 } else {
3297 if (StandardAttr)
3298 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3299 << AttrName->getName();
3300
3301 // FIXME: handle other formats of c++11 attribute arguments
3302 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003303 SkipUntil(tok::r_paren);
Michael Han23214e52012-10-03 01:56:22 +00003304 }
3305 }
3306
3307 if (!AttrParsed)
Richard Smith84837d52012-05-03 18:27:39 +00003308 attrs.addNew(AttrName,
3309 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3310 AttrLoc),
Aaron Ballman00e99962013-08-31 01:11:41 +00003311 ScopeName, ScopeLoc, 0, 0, AttributeList::AS_CXX11);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003312
Alp Toker97650562014-01-10 11:19:30 +00003313 if (TryConsumeToken(tok::ellipsis))
Michael Han23214e52012-10-03 01:56:22 +00003314 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3315 << AttrName->getName();
Alexis Hunt96d5c762009-11-21 08:43:09 +00003316 }
3317
Alp Toker383d2c42014-01-01 03:08:43 +00003318 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003319 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003320 if (endLoc)
3321 *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003322 if (ExpectAndConsume(tok::r_square))
Alexey Bataevee6507d2013-11-18 08:17:37 +00003323 SkipUntil(tok::r_square);
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003324}
Alexis Hunt96d5c762009-11-21 08:43:09 +00003325
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003326/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003327///
3328/// attribute-specifier-seq:
3329/// attribute-specifier-seq[opt] attribute-specifier
Richard Smith3dff2512012-04-10 03:25:07 +00003330void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003331 SourceLocation *endLoc) {
Richard Smith4cabd042013-02-22 09:15:49 +00003332 assert(getLangOpts().CPlusPlus11);
3333
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003334 SourceLocation StartLoc = Tok.getLocation(), Loc;
3335 if (!endLoc)
3336 endLoc = &Loc;
3337
Douglas Gregor6f981002011-10-07 20:35:25 +00003338 do {
Richard Smith3dff2512012-04-10 03:25:07 +00003339 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003340 } while (isCXX11AttributeSpecifier());
Peter Collingbourne49eedec2011-09-29 18:04:05 +00003341
3342 attrs.Range = SourceRange(StartLoc, *endLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003343}
3344
Richard Smithc2c8bb82013-10-15 01:34:54 +00003345void Parser::DiagnoseAndSkipCXX11Attributes() {
3346 if (!isCXX11AttributeSpecifier())
3347 return;
3348
3349 // Start and end location of an attribute or an attribute list.
3350 SourceLocation StartLoc = Tok.getLocation();
3351 SourceLocation EndLoc;
3352
3353 do {
3354 if (Tok.is(tok::l_square)) {
3355 BalancedDelimiterTracker T(*this, tok::l_square);
3356 T.consumeOpen();
3357 T.skipToEnd();
3358 EndLoc = T.getCloseLocation();
3359 } else {
3360 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3361 ConsumeToken();
3362 BalancedDelimiterTracker T(*this, tok::l_paren);
3363 if (!T.consumeOpen())
3364 T.skipToEnd();
3365 EndLoc = T.getCloseLocation();
3366 }
3367 } while (isCXX11AttributeSpecifier());
3368
3369 if (EndLoc.isValid()) {
3370 SourceRange Range(StartLoc, EndLoc);
3371 Diag(StartLoc, diag::err_attributes_not_allowed)
3372 << Range;
3373 }
3374}
3375
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003376/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3377///
3378/// [MS] ms-attribute:
3379/// '[' token-seq ']'
3380///
3381/// [MS] ms-attribute-seq:
3382/// ms-attribute[opt]
3383/// ms-attribute ms-attribute-seq
John McCall53fa7142010-12-24 02:08:15 +00003384void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3385 SourceLocation *endLoc) {
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003386 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3387
3388 while (Tok.is(tok::l_square)) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00003389 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003390 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003391 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
John McCall53fa7142010-12-24 02:08:15 +00003392 if (endLoc) *endLoc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +00003393 ExpectAndConsume(tok::r_square);
Francois Pichetc2bc5ac2010-10-11 12:59:39 +00003394 }
3395}
Francois Pichet8f981d52011-05-25 10:19:49 +00003396
3397void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3398 AccessSpecifier& CurAS) {
Douglas Gregor43edb322011-10-24 22:31:10 +00003399 IfExistsCondition Result;
Francois Pichet8f981d52011-05-25 10:19:49 +00003400 if (ParseMicrosoftIfExistsCondition(Result))
3401 return;
3402
Douglas Gregor43edb322011-10-24 22:31:10 +00003403 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3404 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00003405 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet8f981d52011-05-25 10:19:49 +00003406 return;
3407 }
Francois Pichet8f981d52011-05-25 10:19:49 +00003408
Douglas Gregor43edb322011-10-24 22:31:10 +00003409 switch (Result.Behavior) {
3410 case IEB_Parse:
3411 // Parse the declarations below.
3412 break;
3413
3414 case IEB_Dependent:
3415 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3416 << Result.IsIfExists;
3417 // Fall through to skip.
3418
3419 case IEB_Skip:
3420 Braces.skipToEnd();
Francois Pichet8f981d52011-05-25 10:19:49 +00003421 return;
3422 }
3423
Richard Smith34f30512013-11-23 04:06:09 +00003424 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Francois Pichet8f981d52011-05-25 10:19:49 +00003425 // __if_exists, __if_not_exists can nest.
3426 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3427 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3428 continue;
3429 }
3430
3431 // Check for extraneous top-level semicolon.
3432 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003433 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet8f981d52011-05-25 10:19:49 +00003434 continue;
3435 }
3436
3437 AccessSpecifier AS = getAccessSpecifierIfPresent();
3438 if (AS != AS_none) {
3439 // Current token is a C++ access specifier.
3440 CurAS = AS;
3441 SourceLocation ASLoc = Tok.getLocation();
3442 ConsumeToken();
3443 if (Tok.is(tok::colon))
3444 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3445 else
Alp Toker35d87032013-12-30 23:29:50 +00003446 Diag(Tok, diag::err_expected) << tok::colon;
Francois Pichet8f981d52011-05-25 10:19:49 +00003447 ConsumeToken();
3448 continue;
3449 }
3450
3451 // Parse all the comma separated declarators.
Erik Verbruggenca98f2a2011-10-13 09:41:32 +00003452 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet8f981d52011-05-25 10:19:49 +00003453 }
Douglas Gregor43edb322011-10-24 22:31:10 +00003454
3455 Braces.consumeClose();
Francois Pichet8f981d52011-05-25 10:19:49 +00003456}