blob: bf615f02bab985a5fba365ab15c6bde15fca8fa4 [file] [log] [blame]
Chris Lattner8f08cb72007-08-25 06:57:03 +00001//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Lattner8f08cb72007-08-25 06:57:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor1b7f8982008-04-14 00:13:42 +000014#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/OperatorKinds.h"
Larisse Voufoef4579c2013-08-06 01:03:05 +000018#include "clang/AST/DeclTemplate.h"
Chris Lattner500d3292009-01-29 05:15:15 +000019#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/DeclSpec.h"
John McCall19510852010-08-20 18:27:03 +000021#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000022#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Sema/Scope.h"
John McCalle402e722012-09-25 07:32:39 +000024#include "clang/Sema/SemaDiagnostic.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Chris Lattner8f08cb72007-08-25 06:57:03 +000026using namespace clang;
27
28/// ParseNamespace - We know that the current token is a namespace keyword. This
Sebastian Redld078e642010-08-27 23:12:46 +000029/// may either be a top level namespace or a block-level namespace alias. If
30/// there was an inline keyword, it has already been parsed.
Chris Lattner8f08cb72007-08-25 06:57:03 +000031///
32/// namespace-definition: [C++ 7.3: basic.namespace]
33/// named-namespace-definition
34/// unnamed-namespace-definition
35///
36/// unnamed-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000037/// 'inline'[opt] 'namespace' attributes[opt] '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000038///
39/// named-namespace-definition:
40/// original-namespace-definition
41/// extension-namespace-definition
42///
43/// original-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000044/// 'inline'[opt] 'namespace' identifier attributes[opt]
45/// '{' namespace-body '}'
Chris Lattner8f08cb72007-08-25 06:57:03 +000046///
47/// extension-namespace-definition:
Sebastian Redld078e642010-08-27 23:12:46 +000048/// 'inline'[opt] 'namespace' original-namespace-name
49/// '{' namespace-body '}'
Mike Stump1eb44332009-09-09 15:08:12 +000050///
Chris Lattner8f08cb72007-08-25 06:57:03 +000051/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
52/// 'namespace' identifier '=' qualified-namespace-specifier ';'
53///
John McCalld226f652010-08-21 09:40:31 +000054Decl *Parser::ParseNamespace(unsigned Context,
Sebastian Redld078e642010-08-27 23:12:46 +000055 SourceLocation &DeclEnd,
56 SourceLocation InlineLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000057 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
Chris Lattner8f08cb72007-08-25 06:57:03 +000058 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +000059 ObjCDeclContextSwitch ObjCDC(*this);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +000060
Douglas Gregor49f40bd2009-09-18 19:03:04 +000061 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +000062 Actions.CodeCompleteNamespaceDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +000063 cutOffParsing();
64 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +000065 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +000066
Chris Lattner8f08cb72007-08-25 06:57:03 +000067 SourceLocation IdentLoc;
68 IdentifierInfo *Ident = 0;
Richard Trieuf858bd82011-05-26 20:11:09 +000069 std::vector<SourceLocation> ExtraIdentLoc;
70 std::vector<IdentifierInfo*> ExtraIdent;
71 std::vector<SourceLocation> ExtraNamespaceLoc;
Douglas Gregor6a588dd2009-06-17 19:49:00 +000072
73 Token attrTok;
Mike Stump1eb44332009-09-09 15:08:12 +000074
Chris Lattner04d66662007-10-09 17:33:22 +000075 if (Tok.is(tok::identifier)) {
Chris Lattner8f08cb72007-08-25 06:57:03 +000076 Ident = Tok.getIdentifierInfo();
77 IdentLoc = ConsumeToken(); // eat the identifier.
Richard Trieuf858bd82011-05-26 20:11:09 +000078 while (Tok.is(tok::coloncolon) && NextToken().is(tok::identifier)) {
79 ExtraNamespaceLoc.push_back(ConsumeToken());
80 ExtraIdent.push_back(Tok.getIdentifierInfo());
81 ExtraIdentLoc.push_back(ConsumeToken());
82 }
Chris Lattner8f08cb72007-08-25 06:57:03 +000083 }
Mike Stump1eb44332009-09-09 15:08:12 +000084
Chris Lattner8f08cb72007-08-25 06:57:03 +000085 // Read label attributes, if present.
John McCall0b7e6782011-03-24 11:26:52 +000086 ParsedAttributes attrs(AttrFactory);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000087 if (Tok.is(tok::kw___attribute)) {
88 attrTok = Tok;
John McCall7f040a92010-12-24 02:08:15 +000089 ParseGNUAttributes(attrs);
Douglas Gregor6a588dd2009-06-17 19:49:00 +000090 }
Mike Stump1eb44332009-09-09 15:08:12 +000091
Douglas Gregor6a588dd2009-06-17 19:49:00 +000092 if (Tok.is(tok::equal)) {
Nico Webere1bb3292012-10-27 23:44:27 +000093 if (Ident == 0) {
94 Diag(Tok, diag::err_expected_ident);
95 // Skip to end of the definition and eat the ';'.
96 SkipUntil(tok::semi);
97 return 0;
98 }
John McCall7f040a92010-12-24 02:08:15 +000099 if (!attrs.empty())
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000100 Diag(attrTok, diag::err_unexpected_namespace_attributes_alias);
Sebastian Redld078e642010-08-27 23:12:46 +0000101 if (InlineLoc.isValid())
102 Diag(InlineLoc, diag::err_inline_namespace_alias)
103 << FixItHint::CreateRemoval(InlineLoc);
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000104 return ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
Douglas Gregor6a588dd2009-06-17 19:49:00 +0000105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Richard Trieuf858bd82011-05-26 20:11:09 +0000107
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000108 BalancedDelimiterTracker T(*this, tok::l_brace);
109 if (T.consumeOpen()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000110 if (!ExtraIdent.empty()) {
111 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
112 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
113 }
Mike Stump1eb44332009-09-09 15:08:12 +0000114 Diag(Tok, Ident ? diag::err_expected_lbrace :
Chris Lattner51448322009-03-29 14:02:43 +0000115 diag::err_expected_ident_lbrace);
John McCalld226f652010-08-21 09:40:31 +0000116 return 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000117 }
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Douglas Gregor23c94db2010-07-02 17:43:08 +0000119 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
120 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
121 getCurScope()->getFnParent()) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000122 if (!ExtraIdent.empty()) {
123 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
124 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
125 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000126 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000127 SkipUntil(tok::r_brace);
John McCalld226f652010-08-21 09:40:31 +0000128 return 0;
Douglas Gregor95f1b152010-05-14 05:08:22 +0000129 }
130
Richard Trieuf858bd82011-05-26 20:11:09 +0000131 if (!ExtraIdent.empty()) {
132 TentativeParsingAction TPA(*this);
Alexey Bataev8fe24752013-11-18 08:17:37 +0000133 SkipUntil(tok::r_brace, StopBeforeMatch);
Richard Trieuf858bd82011-05-26 20:11:09 +0000134 Token rBraceToken = Tok;
135 TPA.Revert();
136
137 if (!rBraceToken.is(tok::r_brace)) {
138 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
139 << SourceRange(ExtraNamespaceLoc.front(), ExtraIdentLoc.back());
140 } else {
Benjamin Kramer9910df02011-05-26 21:32:30 +0000141 std::string NamespaceFix;
Richard Trieuf858bd82011-05-26 20:11:09 +0000142 for (std::vector<IdentifierInfo*>::iterator I = ExtraIdent.begin(),
143 E = ExtraIdent.end(); I != E; ++I) {
144 NamespaceFix += " { namespace ";
145 NamespaceFix += (*I)->getName();
146 }
Benjamin Kramer9910df02011-05-26 21:32:30 +0000147
Richard Trieuf858bd82011-05-26 20:11:09 +0000148 std::string RBraces;
Benjamin Kramer9910df02011-05-26 21:32:30 +0000149 for (unsigned i = 0, e = ExtraIdent.size(); i != e; ++i)
Richard Trieuf858bd82011-05-26 20:11:09 +0000150 RBraces += "} ";
Benjamin Kramer9910df02011-05-26 21:32:30 +0000151
Richard Trieuf858bd82011-05-26 20:11:09 +0000152 Diag(ExtraNamespaceLoc[0], diag::err_nested_namespaces_with_double_colon)
153 << FixItHint::CreateReplacement(SourceRange(ExtraNamespaceLoc.front(),
154 ExtraIdentLoc.back()),
155 NamespaceFix)
156 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
157 }
158 }
159
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000160 // If we're still good, complain about inline namespaces in non-C++0x now.
Richard Smith7fe62082011-10-15 05:09:34 +0000161 if (InlineLoc.isValid())
Richard Smith80ad52f2013-01-02 11:42:31 +0000162 Diag(InlineLoc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000163 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000164
Chris Lattner51448322009-03-29 14:02:43 +0000165 // Enter a scope for the namespace.
166 ParseScope NamespaceScope(this, Scope::DeclScope);
167
John McCalld226f652010-08-21 09:40:31 +0000168 Decl *NamespcDecl =
Abramo Bagnaraacba90f2011-03-08 12:38:20 +0000169 Actions.ActOnStartNamespaceDef(getCurScope(), InlineLoc, NamespaceLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000170 IdentLoc, Ident, T.getOpenLocation(),
171 attrs.getList());
Chris Lattner51448322009-03-29 14:02:43 +0000172
John McCallf312b1e2010-08-26 23:41:50 +0000173 PrettyDeclStackTraceEntry CrashInfo(Actions, NamespcDecl, NamespaceLoc,
174 "parsing namespace");
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Richard Trieuf858bd82011-05-26 20:11:09 +0000176 // Parse the contents of the namespace. This includes parsing recovery on
177 // any improperly nested namespaces.
178 ParseInnerNamespace(ExtraIdentLoc, ExtraIdent, ExtraNamespaceLoc, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000179 InlineLoc, attrs, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner51448322009-03-29 14:02:43 +0000181 // Leave the namespace scope.
182 NamespaceScope.Exit();
183
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000184 DeclEnd = T.getCloseLocation();
185 Actions.ActOnFinishNamespaceDef(NamespcDecl, DeclEnd);
Chris Lattner51448322009-03-29 14:02:43 +0000186
187 return NamespcDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000188}
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000189
Richard Trieuf858bd82011-05-26 20:11:09 +0000190/// ParseInnerNamespace - Parse the contents of a namespace.
191void Parser::ParseInnerNamespace(std::vector<SourceLocation>& IdentLoc,
192 std::vector<IdentifierInfo*>& Ident,
193 std::vector<SourceLocation>& NamespaceLoc,
194 unsigned int index, SourceLocation& InlineLoc,
Richard Trieuf858bd82011-05-26 20:11:09 +0000195 ParsedAttributes& attrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000196 BalancedDelimiterTracker &Tracker) {
Richard Trieuf858bd82011-05-26 20:11:09 +0000197 if (index == Ident.size()) {
198 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
199 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000200 MaybeParseCXX11Attributes(attrs);
Richard Trieuf858bd82011-05-26 20:11:09 +0000201 MaybeParseMicrosoftAttributes(attrs);
202 ParseExternalDeclaration(attrs);
203 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000204
205 // The caller is what called check -- we are simply calling
206 // the close for it.
207 Tracker.consumeClose();
Richard Trieuf858bd82011-05-26 20:11:09 +0000208
209 return;
210 }
211
212 // Parse improperly nested namespaces.
213 ParseScope NamespaceScope(this, Scope::DeclScope);
214 Decl *NamespcDecl =
215 Actions.ActOnStartNamespaceDef(getCurScope(), SourceLocation(),
216 NamespaceLoc[index], IdentLoc[index],
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000217 Ident[index], Tracker.getOpenLocation(),
218 attrs.getList());
Richard Trieuf858bd82011-05-26 20:11:09 +0000219
220 ParseInnerNamespace(IdentLoc, Ident, NamespaceLoc, ++index, InlineLoc,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000221 attrs, Tracker);
Richard Trieuf858bd82011-05-26 20:11:09 +0000222
223 NamespaceScope.Exit();
224
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000225 Actions.ActOnFinishNamespaceDef(NamespcDecl, Tracker.getCloseLocation());
Richard Trieuf858bd82011-05-26 20:11:09 +0000226}
227
Anders Carlssonf67606a2009-03-28 04:07:16 +0000228/// ParseNamespaceAlias - Parse the part after the '=' in a namespace
229/// alias definition.
230///
John McCalld226f652010-08-21 09:40:31 +0000231Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000232 SourceLocation AliasLoc,
233 IdentifierInfo *Alias,
234 SourceLocation &DeclEnd) {
Anders Carlssonf67606a2009-03-28 04:07:16 +0000235 assert(Tok.is(tok::equal) && "Not equal token");
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Anders Carlssonf67606a2009-03-28 04:07:16 +0000237 ConsumeToken(); // eat the '='.
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000239 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000240 Actions.CodeCompleteNamespaceAliasDecl(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000241 cutOffParsing();
242 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000243 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000244
Anders Carlssonf67606a2009-03-28 04:07:16 +0000245 CXXScopeSpec SS;
246 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000247 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000248
249 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
250 Diag(Tok, diag::err_expected_namespace_name);
251 // Skip to end of the definition and eat the ';'.
252 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000253 return 0;
Anders Carlssonf67606a2009-03-28 04:07:16 +0000254 }
255
256 // Parse identifier.
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000257 IdentifierInfo *Ident = Tok.getIdentifierInfo();
258 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Anders Carlssonf67606a2009-03-28 04:07:16 +0000260 // Eat the ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000261 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000262 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name,
263 "", tok::semi);
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Douglas Gregor23c94db2010-07-02 17:43:08 +0000265 return Actions.ActOnNamespaceAliasDef(getCurScope(), NamespaceLoc, AliasLoc, Alias,
Anders Carlsson03bd5a12009-03-28 22:53:22 +0000266 SS, IdentLoc, Ident);
Anders Carlssonf67606a2009-03-28 04:07:16 +0000267}
268
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000269/// ParseLinkage - We know that the current token is a string_literal
270/// and just before that, that extern was seen.
271///
272/// linkage-specification: [C++ 7.5p2: dcl.link]
273/// 'extern' string-literal '{' declaration-seq[opt] '}'
274/// 'extern' string-literal declaration
275///
Chris Lattner7d642712010-11-09 20:15:55 +0000276Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, unsigned Context) {
Douglas Gregorc19923d2008-11-21 16:10:08 +0000277 assert(Tok.is(tok::string_literal) && "Not a string literal!");
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000278 SmallString<8> LangBuffer;
Douglas Gregor453091c2010-03-16 22:30:13 +0000279 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000280 StringRef Lang = PP.getSpelling(Tok, LangBuffer, &Invalid);
Douglas Gregor453091c2010-03-16 22:30:13 +0000281 if (Invalid)
John McCalld226f652010-08-21 09:40:31 +0000282 return 0;
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000283
Richard Smith99831e42012-03-06 03:21:47 +0000284 // FIXME: This is incorrect: linkage-specifiers are parsed in translation
285 // phase 7, so string-literal concatenation is supposed to occur.
286 // extern "" "C" "" "+" "+" { } is legal.
287 if (Tok.hasUDSuffix())
288 Diag(Tok, diag::err_invalid_string_udl);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000289 SourceLocation Loc = ConsumeStringToken();
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000290
Douglas Gregor074149e2009-01-05 19:45:36 +0000291 ParseScope LinkageScope(this, Scope::DeclScope);
John McCalld226f652010-08-21 09:40:31 +0000292 Decl *LinkageSpec
Douglas Gregor23c94db2010-07-02 17:43:08 +0000293 = Actions.ActOnStartLinkageSpecification(getCurScope(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000294 DS.getSourceRange().getBegin(),
Benjamin Kramerd5663812010-05-03 13:08:54 +0000295 Loc, Lang,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000296 Tok.is(tok::l_brace) ? Tok.getLocation()
Douglas Gregor074149e2009-01-05 19:45:36 +0000297 : SourceLocation());
298
John McCall0b7e6782011-03-24 11:26:52 +0000299 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000300 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000301 MaybeParseMicrosoftAttributes(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000302
Douglas Gregor074149e2009-01-05 19:45:36 +0000303 if (Tok.isNot(tok::l_brace)) {
Abramo Bagnaraf41e33c2011-05-01 16:25:54 +0000304 // Reset the source range in DS, as the leading "extern"
305 // does not really belong to the inner declaration ...
306 DS.SetRangeStart(SourceLocation());
307 DS.SetRangeEnd(SourceLocation());
308 // ... but anyway remember that such an "extern" was seen.
Abramo Bagnara35f9a192010-07-30 16:47:02 +0000309 DS.setExternInLinkageSpec(true);
John McCall7f040a92010-12-24 02:08:15 +0000310 ParseExternalDeclaration(attrs, &DS);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000311 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor074149e2009-01-05 19:45:36 +0000312 SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000313 }
Douglas Gregorf44515a2008-12-16 22:23:02 +0000314
Douglas Gregor63a01132010-02-07 08:38:28 +0000315 DS.abort();
316
John McCall7f040a92010-12-24 02:08:15 +0000317 ProhibitAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000318
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000319 BalancedDelimiterTracker T(*this, tok::l_brace);
320 T.consumeOpen();
Douglas Gregorf44515a2008-12-16 22:23:02 +0000321 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
John McCall0b7e6782011-03-24 11:26:52 +0000322 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000323 MaybeParseCXX11Attributes(attrs);
John McCall7f040a92010-12-24 02:08:15 +0000324 MaybeParseMicrosoftAttributes(attrs);
325 ParseExternalDeclaration(attrs);
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000326 }
327
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000328 T.consumeClose();
Chris Lattner7d642712010-11-09 20:15:55 +0000329 return Actions.ActOnFinishLinkageSpecification(getCurScope(), LinkageSpec,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000330 T.getCloseLocation());
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000331}
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000332
Douglas Gregorf780abc2008-12-30 03:27:21 +0000333/// ParseUsingDirectiveOrDeclaration - Parse C++ using using-declaration or
334/// using-directive. Assumes that current token is 'using'.
John McCalld226f652010-08-21 09:40:31 +0000335Decl *Parser::ParseUsingDirectiveOrDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000336 const ParsedTemplateInfo &TemplateInfo,
337 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000338 ParsedAttributesWithRange &attrs,
339 Decl **OwnedType) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000340 assert(Tok.is(tok::kw_using) && "Not using token");
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000341 ObjCDeclContextSwitch ObjCDC(*this);
342
Douglas Gregorf780abc2008-12-30 03:27:21 +0000343 // Eat 'using'.
344 SourceLocation UsingLoc = ConsumeToken();
345
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000346 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000347 Actions.CodeCompleteUsing(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000348 cutOffParsing();
349 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000350 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000351
John McCall78b81052010-11-10 02:40:36 +0000352 // 'using namespace' means this is a using-directive.
353 if (Tok.is(tok::kw_namespace)) {
354 // Template parameters are always an error here.
355 if (TemplateInfo.Kind) {
356 SourceRange R = TemplateInfo.getSourceRange();
357 Diag(UsingLoc, diag::err_templated_using_directive)
358 << R << FixItHint::CreateRemoval(R);
359 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000360
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000361 return ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs);
John McCall78b81052010-11-10 02:40:36 +0000362 }
363
Richard Smith162e1c12011-04-15 14:24:37 +0000364 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +0000365
366 // Using declarations can't have attributes.
John McCall7f040a92010-12-24 02:08:15 +0000367 ProhibitAttributes(attrs);
Chris Lattner2f274772009-01-06 06:55:51 +0000368
Fariborz Jahanian9735c5e2011-08-22 17:59:19 +0000369 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000370 AS_none, OwnedType);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000371}
372
373/// ParseUsingDirective - Parse C++ using-directive, assumes
374/// that current token is 'namespace' and 'using' was already parsed.
375///
376/// using-directive: [C++ 7.3.p4: namespace.udir]
377/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
378/// namespace-name ;
379/// [GNU] using-directive:
380/// 'using' 'namespace' ::[opt] nested-name-specifier[opt]
381/// namespace-name attributes[opt] ;
382///
John McCalld226f652010-08-21 09:40:31 +0000383Decl *Parser::ParseUsingDirective(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000384 SourceLocation UsingLoc,
385 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000386 ParsedAttributes &attrs) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000387 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
388
389 // Eat 'namespace'.
390 SourceLocation NamespcLoc = ConsumeToken();
391
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000392 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000393 Actions.CodeCompleteUsingDirective(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000394 cutOffParsing();
395 return 0;
Douglas Gregor49f40bd2009-09-18 19:03:04 +0000396 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000397
Douglas Gregorf780abc2008-12-30 03:27:21 +0000398 CXXScopeSpec SS;
399 // Parse (optional) nested-name-specifier.
Douglas Gregorefaa93a2011-11-07 17:33:42 +0000400 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000401
Douglas Gregorf780abc2008-12-30 03:27:21 +0000402 IdentifierInfo *NamespcName = 0;
403 SourceLocation IdentLoc = SourceLocation();
404
405 // Parse namespace-name.
Chris Lattner823c44e2009-01-06 07:27:21 +0000406 if (SS.isInvalid() || Tok.isNot(tok::identifier)) {
Douglas Gregorf780abc2008-12-30 03:27:21 +0000407 Diag(Tok, diag::err_expected_namespace_name);
408 // If there was invalid namespace name, skip to end of decl, and eat ';'.
409 SkipUntil(tok::semi);
410 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
John McCalld226f652010-08-21 09:40:31 +0000411 return 0;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000412 }
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Chris Lattner823c44e2009-01-06 07:27:21 +0000414 // Parse identifier.
415 NamespcName = Tok.getIdentifierInfo();
416 IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Chris Lattner823c44e2009-01-06 07:27:21 +0000418 // Parse (optional) attributes (most likely GNU strong-using extension).
Sean Huntbbd37c62009-11-21 08:43:09 +0000419 bool GNUAttr = false;
420 if (Tok.is(tok::kw___attribute)) {
421 GNUAttr = true;
John McCall7f040a92010-12-24 02:08:15 +0000422 ParseGNUAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000423 }
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattner823c44e2009-01-06 07:27:21 +0000425 // Eat ';'.
Chris Lattner97144fc2009-04-02 04:16:50 +0000426 DeclEnd = Tok.getLocation();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000427 ExpectAndConsume(tok::semi,
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000428 GNUAttr ? diag::err_expected_semi_after_attribute_list
429 : diag::err_expected_semi_after_namespace_name,
430 "", tok::semi);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000431
Douglas Gregor23c94db2010-07-02 17:43:08 +0000432 return Actions.ActOnUsingDirective(getCurScope(), UsingLoc, NamespcLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +0000433 IdentLoc, NamespcName, attrs.getList());
Douglas Gregorf780abc2008-12-30 03:27:21 +0000434}
435
Richard Smith162e1c12011-04-15 14:24:37 +0000436/// ParseUsingDeclaration - Parse C++ using-declaration or alias-declaration.
437/// Assumes that 'using' was already seen.
Douglas Gregorf780abc2008-12-30 03:27:21 +0000438///
439/// using-declaration: [C++ 7.3.p3: namespace.udecl]
440/// 'using' 'typename'[opt] ::[opt] nested-name-specifier
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000441/// unqualified-id
442/// 'using' :: unqualified-id
Douglas Gregorf780abc2008-12-30 03:27:21 +0000443///
Richard Smithd03de6a2013-01-29 10:02:16 +0000444/// alias-declaration: C++11 [dcl.dcl]p1
445/// 'using' identifier attribute-specifier-seq[opt] = type-id ;
Richard Smith162e1c12011-04-15 14:24:37 +0000446///
John McCalld226f652010-08-21 09:40:31 +0000447Decl *Parser::ParseUsingDeclaration(unsigned Context,
John McCall78b81052010-11-10 02:40:36 +0000448 const ParsedTemplateInfo &TemplateInfo,
449 SourceLocation UsingLoc,
450 SourceLocation &DeclEnd,
Richard Smithc89edf52011-07-01 19:46:12 +0000451 AccessSpecifier AS,
452 Decl **OwnedType) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000453 CXXScopeSpec SS;
John McCall7ba107a2009-11-18 02:36:19 +0000454 SourceLocation TypenameLoc;
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000455 bool HasTypenameKeyword = false;
Sean Hunt2edf0a22012-06-23 05:07:58 +0000456
Richard Smith5eed7e02013-10-15 01:34:54 +0000457 // Check for misplaced attributes before the identifier in an
458 // alias-declaration.
459 ParsedAttributesWithRange MisplacedAttrs(AttrFactory);
460 MaybeParseCXX11Attributes(MisplacedAttrs);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000461
462 // Ignore optional 'typename'.
Douglas Gregor12c118a2009-11-04 16:30:06 +0000463 // FIXME: This is wrong; we should parse this as a typename-specifier.
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000464 if (Tok.is(tok::kw_typename)) {
Richard Smith6b3d3e52013-02-20 19:22:51 +0000465 TypenameLoc = ConsumeToken();
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000466 HasTypenameKeyword = true;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000467 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000468
469 // Parse nested-name-specifier.
Richard Smith2db075b2013-03-26 01:15:19 +0000470 IdentifierInfo *LastII = 0;
471 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false,
472 /*MayBePseudoDtor=*/0, /*IsTypename=*/false,
473 /*LastII=*/&LastII);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000474
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000475 // Check nested-name specifier.
476 if (SS.isInvalid()) {
477 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000478 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000479 }
Douglas Gregor12c118a2009-11-04 16:30:06 +0000480
Richard Smith2db075b2013-03-26 01:15:19 +0000481 SourceLocation TemplateKWLoc;
482 UnqualifiedId Name;
483
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000484 // Parse the unqualified-id. We allow parsing of both constructor and
Douglas Gregor12c118a2009-11-04 16:30:06 +0000485 // destructor names and allow the action module to diagnose any semantic
486 // errors.
Richard Smith2db075b2013-03-26 01:15:19 +0000487 //
488 // C++11 [class.qual]p2:
489 // [...] in a using-declaration that is a member-declaration, if the name
490 // specified after the nested-name-specifier is the same as the identifier
491 // or the simple-template-id's template-name in the last component of the
492 // nested-name-specifier, the name is [...] considered to name the
493 // constructor.
494 if (getLangOpts().CPlusPlus11 && Context == Declarator::MemberContext &&
495 Tok.is(tok::identifier) && NextToken().is(tok::semi) &&
496 SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
497 !SS.getScopeRep()->getAsNamespace() &&
498 !SS.getScopeRep()->getAsNamespaceAlias()) {
499 SourceLocation IdLoc = ConsumeToken();
500 ParsedType Type = Actions.getInheritingConstructorName(SS, IdLoc, *LastII);
501 Name.setConstructorName(Type, IdLoc, IdLoc);
502 } else if (ParseUnqualifiedId(SS, /*EnteringContext=*/ false,
503 /*AllowDestructorName=*/ true,
504 /*AllowConstructorName=*/ true, ParsedType(),
505 TemplateKWLoc, Name)) {
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000506 SkipUntil(tok::semi);
John McCalld226f652010-08-21 09:40:31 +0000507 return 0;
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000508 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000509
Richard Smith5eed7e02013-10-15 01:34:54 +0000510 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smithdf1cce52013-10-24 01:21:09 +0000511 MaybeParseGNUAttributes(Attrs);
Richard Smith6b3d3e52013-02-20 19:22:51 +0000512 MaybeParseCXX11Attributes(Attrs);
Richard Smith162e1c12011-04-15 14:24:37 +0000513
514 // Maybe this is an alias-declaration.
Richard Smith162e1c12011-04-15 14:24:37 +0000515 TypeResult TypeAlias;
Richard Smith5eed7e02013-10-15 01:34:54 +0000516 bool IsAliasDecl = Tok.is(tok::equal);
Richard Smith162e1c12011-04-15 14:24:37 +0000517 if (IsAliasDecl) {
Richard Smith5eed7e02013-10-15 01:34:54 +0000518 // If we had any misplaced attributes from earlier, this is where they
519 // should have been written.
520 if (MisplacedAttrs.Range.isValid()) {
521 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
522 << FixItHint::CreateInsertionFromRange(
523 Tok.getLocation(),
524 CharSourceRange::getTokenRange(MisplacedAttrs.Range))
525 << FixItHint::CreateRemoval(MisplacedAttrs.Range);
526 Attrs.takeAllFrom(MisplacedAttrs);
527 }
528
Richard Smith162e1c12011-04-15 14:24:37 +0000529 ConsumeToken();
530
Richard Smith80ad52f2013-01-02 11:42:31 +0000531 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +0000532 diag::warn_cxx98_compat_alias_declaration :
533 diag::ext_alias_declaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000534
Richard Smith3e4c6c42011-05-05 21:57:07 +0000535 // Type alias templates cannot be specialized.
536 int SpecKind = -1;
Richard Smith536e9c12011-05-05 22:36:10 +0000537 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
538 Name.getKind() == UnqualifiedId::IK_TemplateId)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000539 SpecKind = 0;
540 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
541 SpecKind = 1;
542 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
543 SpecKind = 2;
544 if (SpecKind != -1) {
545 SourceRange Range;
546 if (SpecKind == 0)
547 Range = SourceRange(Name.TemplateId->LAngleLoc,
548 Name.TemplateId->RAngleLoc);
549 else
550 Range = TemplateInfo.getSourceRange();
551 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
552 << SpecKind << Range;
553 SkipUntil(tok::semi);
554 return 0;
555 }
556
Richard Smith162e1c12011-04-15 14:24:37 +0000557 // Name must be an identifier.
558 if (Name.getKind() != UnqualifiedId::IK_Identifier) {
559 Diag(Name.StartLocation, diag::err_alias_declaration_not_identifier);
560 // No removal fixit: can't recover from this.
561 SkipUntil(tok::semi);
562 return 0;
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000563 } else if (HasTypenameKeyword)
Richard Smith162e1c12011-04-15 14:24:37 +0000564 Diag(TypenameLoc, diag::err_alias_declaration_not_identifier)
565 << FixItHint::CreateRemoval(SourceRange(TypenameLoc,
566 SS.isNotEmpty() ? SS.getEndLoc() : TypenameLoc));
567 else if (SS.isNotEmpty())
568 Diag(SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
569 << FixItHint::CreateRemoval(SS.getRange());
570
Richard Smith3e4c6c42011-05-05 21:57:07 +0000571 TypeAlias = ParseTypeName(0, TemplateInfo.Kind ?
572 Declarator::AliasTemplateContext :
Richard Smith6b3d3e52013-02-20 19:22:51 +0000573 Declarator::AliasDeclContext, AS, OwnedType,
574 &Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000575 } else {
576 // C++11 attributes are not allowed on a using-declaration, but GNU ones
577 // are.
Richard Smith5eed7e02013-10-15 01:34:54 +0000578 ProhibitAttributes(MisplacedAttrs);
Richard Smith6b3d3e52013-02-20 19:22:51 +0000579 ProhibitAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000580
Richard Smith162e1c12011-04-15 14:24:37 +0000581 // Parse (optional) attributes (most likely GNU strong-using extension).
Richard Smith6b3d3e52013-02-20 19:22:51 +0000582 MaybeParseGNUAttributes(Attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000585 // Eat ';'.
586 DeclEnd = Tok.getLocation();
587 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000588 !Attrs.empty() ? "attributes list" :
Richard Smith162e1c12011-04-15 14:24:37 +0000589 IsAliasDecl ? "alias declaration" : "using declaration",
Douglas Gregor12c118a2009-11-04 16:30:06 +0000590 tok::semi);
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000591
John McCall78b81052010-11-10 02:40:36 +0000592 // Diagnose an attempt to declare a templated using-declaration.
Richard Smithd03de6a2013-01-29 10:02:16 +0000593 // In C++11, alias-declarations can be templates:
Richard Smith162e1c12011-04-15 14:24:37 +0000594 // template <...> using id = type;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000595 if (TemplateInfo.Kind && !IsAliasDecl) {
John McCall78b81052010-11-10 02:40:36 +0000596 SourceRange R = TemplateInfo.getSourceRange();
597 Diag(UsingLoc, diag::err_templated_using_declaration)
598 << R << FixItHint::CreateRemoval(R);
599
600 // Unfortunately, we have to bail out instead of recovering by
601 // ignoring the parameters, just in case the nested name specifier
602 // depends on the parameters.
603 return 0;
604 }
605
Douglas Gregor480b53c2011-09-26 14:30:28 +0000606 // "typename" keyword is allowed for identifiers only,
607 // because it may be a type definition.
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000608 if (HasTypenameKeyword && Name.getKind() != UnqualifiedId::IK_Identifier) {
Douglas Gregor480b53c2011-09-26 14:30:28 +0000609 Diag(Name.getSourceRange().getBegin(), diag::err_typename_identifiers_only)
610 << FixItHint::CreateRemoval(SourceRange(TypenameLoc));
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000611 // Proceed parsing, but reset the HasTypenameKeyword flag.
612 HasTypenameKeyword = false;
Douglas Gregor480b53c2011-09-26 14:30:28 +0000613 }
614
Richard Smith3e4c6c42011-05-05 21:57:07 +0000615 if (IsAliasDecl) {
616 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
Benjamin Kramer5354e772012-08-23 23:38:35 +0000617 MultiTemplateParamsArg TemplateParamsArg(
Richard Smith3e4c6c42011-05-05 21:57:07 +0000618 TemplateParams ? TemplateParams->data() : 0,
619 TemplateParams ? TemplateParams->size() : 0);
620 return Actions.ActOnAliasDeclaration(getCurScope(), AS, TemplateParamsArg,
Richard Smith6b3d3e52013-02-20 19:22:51 +0000621 UsingLoc, Name, Attrs.getList(),
622 TypeAlias);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000623 }
Richard Smith162e1c12011-04-15 14:24:37 +0000624
Enea Zaffanella8d030c72013-07-22 10:54:09 +0000625 return Actions.ActOnUsingDeclaration(getCurScope(), AS,
626 /* HasUsingKeyword */ true, UsingLoc,
627 SS, Name, Attrs.getList(),
628 HasTypenameKeyword, TypenameLoc);
Douglas Gregorf780abc2008-12-30 03:27:21 +0000629}
630
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000631/// ParseStaticAssertDeclaration - Parse C++0x or C11 static_assert-declaration.
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000632///
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000633/// [C++0x] static_assert-declaration:
634/// static_assert ( constant-expression , string-literal ) ;
635///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000636/// [C11] static_assert-declaration:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000637/// _Static_assert ( constant-expression , string-literal ) ;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000638///
John McCalld226f652010-08-21 09:40:31 +0000639Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000640 assert((Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) &&
641 "Not a static_assert declaration");
642
David Blaikie4e4d0842012-03-11 07:00:24 +0000643 if (Tok.is(tok::kw__Static_assert) && !getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000644 Diag(Tok, diag::ext_c11_static_assert);
Richard Smith841804b2011-10-17 23:06:20 +0000645 if (Tok.is(tok::kw_static_assert))
646 Diag(Tok, diag::warn_cxx98_compat_static_assert);
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000647
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000648 SourceLocation StaticAssertLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000650 BalancedDelimiterTracker T(*this, tok::l_paren);
651 if (T.consumeOpen()) {
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000652 Diag(Tok, diag::err_expected_lparen);
Richard Smith3686c712012-09-13 19:12:50 +0000653 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000654 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000655 }
Mike Stump1eb44332009-09-09 15:08:12 +0000656
John McCall60d7b3a2010-08-24 06:29:42 +0000657 ExprResult AssertExpr(ParseConstantExpression());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000658 if (AssertExpr.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000659 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000660 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000661 }
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Anders Carlssonad5f9602009-03-13 23:29:20 +0000663 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::semi))
John McCalld226f652010-08-21 09:40:31 +0000664 return 0;
Anders Carlssonad5f9602009-03-13 23:29:20 +0000665
Richard Smith0cc323c2012-03-05 23:20:05 +0000666 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000667 Diag(Tok, diag::err_expected_string_literal)
668 << /*Source='static_assert'*/1;
Richard Smith3686c712012-09-13 19:12:50 +0000669 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000670 return 0;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000671 }
Mike Stump1eb44332009-09-09 15:08:12 +0000672
John McCall60d7b3a2010-08-24 06:29:42 +0000673 ExprResult AssertMessage(ParseStringLiteralExpression());
Richard Smith99831e42012-03-06 03:21:47 +0000674 if (AssertMessage.isInvalid()) {
Richard Smith3686c712012-09-13 19:12:50 +0000675 SkipMalformedDecl();
John McCalld226f652010-08-21 09:40:31 +0000676 return 0;
Richard Smith99831e42012-03-06 03:21:47 +0000677 }
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000678
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000679 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Chris Lattner97144fc2009-04-02 04:16:50 +0000681 DeclEnd = Tok.getLocation();
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000682 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000683
John McCall9ae2f072010-08-23 23:25:46 +0000684 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc,
685 AssertExpr.take(),
Abramo Bagnaraa2026c92011-03-08 16:41:52 +0000686 AssertMessage.take(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000687 T.getCloseLocation());
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000688}
689
Richard Smitha2c36462013-04-26 16:15:35 +0000690/// ParseDecltypeSpecifier - Parse a C++11 decltype specifier.
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000691///
692/// 'decltype' ( expression )
Richard Smitha2c36462013-04-26 16:15:35 +0000693/// 'decltype' ( 'auto' ) [C++1y]
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000694///
David Blaikie42d6d0c2011-12-04 05:04:18 +0000695SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
696 assert((Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype))
697 && "Not a decltype specifier");
698
David Blaikie42d6d0c2011-12-04 05:04:18 +0000699 ExprResult Result;
700 SourceLocation StartLoc = Tok.getLocation();
701 SourceLocation EndLoc;
702
703 if (Tok.is(tok::annot_decltype)) {
704 Result = getExprAnnotation(Tok);
705 EndLoc = Tok.getAnnotationEndLoc();
706 ConsumeToken();
707 if (Result.isInvalid()) {
708 DS.SetTypeSpecError();
709 return EndLoc;
710 }
711 } else {
Richard Smithc7b55432012-02-24 22:30:04 +0000712 if (Tok.getIdentifierInfo()->isStr("decltype"))
713 Diag(Tok, diag::warn_cxx98_compat_decltype);
Richard Smith39304fa2012-02-24 18:10:23 +0000714
David Blaikie42d6d0c2011-12-04 05:04:18 +0000715 ConsumeToken();
716
717 BalancedDelimiterTracker T(*this, tok::l_paren);
718 if (T.expectAndConsume(diag::err_expected_lparen_after,
719 "decltype", tok::r_paren)) {
720 DS.SetTypeSpecError();
721 return T.getOpenLocation() == Tok.getLocation() ?
722 StartLoc : T.getOpenLocation();
723 }
724
Richard Smitha2c36462013-04-26 16:15:35 +0000725 // Check for C++1y 'decltype(auto)'.
726 if (Tok.is(tok::kw_auto)) {
727 // No need to disambiguate here: an expression can't start with 'auto',
728 // because the typename-specifier in a function-style cast operation can't
729 // be 'auto'.
730 Diag(Tok.getLocation(),
731 getLangOpts().CPlusPlus1y
732 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
733 : diag::ext_decltype_auto_type_specifier);
734 ConsumeToken();
735 } else {
736 // Parse the expression
David Blaikie42d6d0c2011-12-04 05:04:18 +0000737
Richard Smitha2c36462013-04-26 16:15:35 +0000738 // C++11 [dcl.type.simple]p4:
739 // The operand of the decltype specifier is an unevaluated operand.
740 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
741 0, /*IsDecltype=*/true);
742 Result = ParseExpression();
743 if (Result.isInvalid()) {
744 DS.SetTypeSpecError();
Alexey Bataev8fe24752013-11-18 08:17:37 +0000745 if (SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch)) {
Richard Smitha2c36462013-04-26 16:15:35 +0000746 EndLoc = ConsumeParen();
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000747 } else {
Richard Smitha2c36462013-04-26 16:15:35 +0000748 if (PP.isBacktrackEnabled() && Tok.is(tok::semi)) {
749 // Backtrack to get the location of the last token before the semi.
750 PP.RevertCachedTokens(2);
751 ConsumeToken(); // the semi.
752 EndLoc = ConsumeAnyToken();
753 assert(Tok.is(tok::semi));
754 } else {
755 EndLoc = Tok.getLocation();
756 }
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000757 }
Richard Smitha2c36462013-04-26 16:15:35 +0000758 return EndLoc;
Argyrios Kyrtzidis1e584692012-10-26 22:53:44 +0000759 }
Richard Smitha2c36462013-04-26 16:15:35 +0000760
761 Result = Actions.ActOnDecltypeExpression(Result.take());
David Blaikie42d6d0c2011-12-04 05:04:18 +0000762 }
763
764 // Match the ')'
765 T.consumeClose();
766 if (T.getCloseLocation().isInvalid()) {
767 DS.SetTypeSpecError();
768 // FIXME: this should return the location of the last token
769 // that was consumed (by "consumeClose()")
770 return T.getCloseLocation();
771 }
772
Richard Smith76f3f692012-02-22 02:04:18 +0000773 if (Result.isInvalid()) {
774 DS.SetTypeSpecError();
775 return T.getCloseLocation();
776 }
777
David Blaikie42d6d0c2011-12-04 05:04:18 +0000778 EndLoc = T.getCloseLocation();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000779 }
Richard Smitha2c36462013-04-26 16:15:35 +0000780 assert(!Result.isInvalid());
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000782 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000783 unsigned DiagID;
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000784 // Check for duplicate type specifiers (e.g. "int decltype(a)").
Richard Smitha2c36462013-04-26 16:15:35 +0000785 if (Result.get()
786 ? DS.SetTypeSpecType(DeclSpec::TST_decltype, StartLoc, PrevSpec,
787 DiagID, Result.release())
788 : DS.SetTypeSpecType(DeclSpec::TST_decltype_auto, StartLoc, PrevSpec,
789 DiagID)) {
John McCallfec54012009-08-03 20:12:06 +0000790 Diag(StartLoc, DiagID) << PrevSpec;
David Blaikie42d6d0c2011-12-04 05:04:18 +0000791 DS.SetTypeSpecError();
792 }
793 return EndLoc;
794}
795
796void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec& DS,
797 SourceLocation StartLoc,
798 SourceLocation EndLoc) {
799 // make sure we have a token we can turn into an annotation token
800 if (PP.isBacktrackEnabled())
801 PP.RevertCachedTokens(1);
802 else
803 PP.EnterToken(Tok);
804
805 Tok.setKind(tok::annot_decltype);
Richard Smitha2c36462013-04-26 16:15:35 +0000806 setExprAnnotation(Tok,
807 DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr() :
808 DS.getTypeSpecType() == TST_decltype_auto ? ExprResult() :
809 ExprError());
David Blaikie42d6d0c2011-12-04 05:04:18 +0000810 Tok.setAnnotationEndLoc(EndLoc);
811 Tok.setLocation(StartLoc);
812 PP.AnnotateCachedTokens(Tok);
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000813}
814
Sean Huntdb5d44b2011-05-19 05:37:45 +0000815void Parser::ParseUnderlyingTypeSpecifier(DeclSpec &DS) {
816 assert(Tok.is(tok::kw___underlying_type) &&
817 "Not an underlying type specifier");
818
819 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000820 BalancedDelimiterTracker T(*this, tok::l_paren);
821 if (T.expectAndConsume(diag::err_expected_lparen_after,
822 "__underlying_type", tok::r_paren)) {
Sean Huntdb5d44b2011-05-19 05:37:45 +0000823 return;
824 }
825
826 TypeResult Result = ParseTypeName();
827 if (Result.isInvalid()) {
Alexey Bataev8fe24752013-11-18 08:17:37 +0000828 SkipUntil(tok::r_paren, StopAtSemi);
Sean Huntdb5d44b2011-05-19 05:37:45 +0000829 return;
830 }
831
832 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000833 T.consumeClose();
834 if (T.getCloseLocation().isInvalid())
Sean Huntdb5d44b2011-05-19 05:37:45 +0000835 return;
836
837 const char *PrevSpec = 0;
838 unsigned DiagID;
Sean Huntca63c202011-05-24 22:41:36 +0000839 if (DS.SetTypeSpecType(DeclSpec::TST_underlyingType, StartLoc, PrevSpec,
Sean Huntdb5d44b2011-05-19 05:37:45 +0000840 DiagID, Result.release()))
841 Diag(StartLoc, DiagID) << PrevSpec;
Enea Zaffanella2d776342013-07-06 18:54:58 +0000842 DS.setTypeofParensRange(T.getRange());
Sean Huntdb5d44b2011-05-19 05:37:45 +0000843}
844
David Blaikie09048df2011-10-25 15:01:20 +0000845/// ParseBaseTypeSpecifier - Parse a C++ base-type-specifier which is either a
846/// class name or decltype-specifier. Note that we only check that the result
847/// names a type; semantic analysis will need to verify that the type names a
848/// class. The result is either a type or null, depending on whether a type
849/// name was found.
Douglas Gregor42a552f2008-11-05 20:51:48 +0000850///
Richard Smith05321402013-02-19 23:47:15 +0000851/// base-type-specifier: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000852/// class-or-decltype
Richard Smith05321402013-02-19 23:47:15 +0000853/// class-or-decltype: [C++11 class.derived]
David Blaikie09048df2011-10-25 15:01:20 +0000854/// nested-name-specifier[opt] class-name
855/// decltype-specifier
Richard Smith05321402013-02-19 23:47:15 +0000856/// class-name: [C++ class.name]
Douglas Gregor42a552f2008-11-05 20:51:48 +0000857/// identifier
Douglas Gregor7f43d672009-02-25 23:52:28 +0000858/// simple-template-id
Mike Stump1eb44332009-09-09 15:08:12 +0000859///
Richard Smith05321402013-02-19 23:47:15 +0000860/// In C++98, instead of base-type-specifier, we have:
861///
862/// ::[opt] nested-name-specifier[opt] class-name
David Blaikie22216eb2011-10-25 17:10:12 +0000863Parser::TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
864 SourceLocation &EndLocation) {
David Blaikie7fe38782011-10-25 18:46:41 +0000865 // Ignore attempts to use typename
866 if (Tok.is(tok::kw_typename)) {
867 Diag(Tok, diag::err_expected_class_name_not_template)
868 << FixItHint::CreateRemoval(Tok.getLocation());
869 ConsumeToken();
870 }
871
David Blaikie152aa4b2011-10-25 18:17:58 +0000872 // Parse optional nested-name-specifier
873 CXXScopeSpec SS;
874 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
875
876 BaseLoc = Tok.getLocation();
877
David Blaikie22216eb2011-10-25 17:10:12 +0000878 // Parse decltype-specifier
David Blaikie42d6d0c2011-12-04 05:04:18 +0000879 // tok == kw_decltype is just error recovery, it can only happen when SS
880 // isn't empty
881 if (Tok.is(tok::kw_decltype) || Tok.is(tok::annot_decltype)) {
David Blaikie152aa4b2011-10-25 18:17:58 +0000882 if (SS.isNotEmpty())
883 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
884 << FixItHint::CreateRemoval(SS.getRange());
David Blaikie22216eb2011-10-25 17:10:12 +0000885 // Fake up a Declarator to use with ActOnTypeName.
886 DeclSpec DS(AttrFactory);
887
David Blaikieb5777572011-12-08 04:53:15 +0000888 EndLocation = ParseDecltypeSpecifier(DS);
David Blaikie22216eb2011-10-25 17:10:12 +0000889
890 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
891 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
892 }
893
Douglas Gregor7f43d672009-02-25 23:52:28 +0000894 // Check whether we have a template-id that names a type.
895 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +0000896 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +0000897 if (TemplateId->Kind == TNK_Type_template ||
898 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +0000899 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f43d672009-02-25 23:52:28 +0000900
901 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +0000902 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor7f43d672009-02-25 23:52:28 +0000903 EndLocation = Tok.getAnnotationEndLoc();
904 ConsumeToken();
Douglas Gregor31a19b62009-04-01 21:51:26 +0000905
906 if (Type)
907 return Type;
908 return true;
Douglas Gregor7f43d672009-02-25 23:52:28 +0000909 }
910
911 // Fall through to produce an error below.
912 }
913
Douglas Gregor42a552f2008-11-05 20:51:48 +0000914 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000915 Diag(Tok, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000916 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000917 }
918
Douglas Gregor84d0a192010-01-12 21:28:44 +0000919 IdentifierInfo *Id = Tok.getIdentifierInfo();
920 SourceLocation IdLoc = ConsumeToken();
921
922 if (Tok.is(tok::less)) {
923 // It looks the user intended to write a template-id here, but the
924 // template-name was wrong. Try to fix that.
925 TemplateNameKind TNK = TNK_Type_template;
926 TemplateTy Template;
Douglas Gregor23c94db2010-07-02 17:43:08 +0000927 if (!Actions.DiagnoseUnknownTemplateName(*Id, IdLoc, getCurScope(),
Douglas Gregor059101f2011-03-02 00:47:37 +0000928 &SS, Template, TNK)) {
Douglas Gregor84d0a192010-01-12 21:28:44 +0000929 Diag(IdLoc, diag::err_unknown_template_name)
930 << Id;
931 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000932
Serge Pavlov62f675c2013-08-10 05:54:47 +0000933 if (!Template) {
934 TemplateArgList TemplateArgs;
935 SourceLocation LAngleLoc, RAngleLoc;
936 ParseTemplateIdAfterTemplateName(TemplateTy(), IdLoc, SS,
937 true, LAngleLoc, TemplateArgs, RAngleLoc);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000938 return true;
Serge Pavlov62f675c2013-08-10 05:54:47 +0000939 }
Douglas Gregor84d0a192010-01-12 21:28:44 +0000940
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000941 // Form the template name
Douglas Gregor84d0a192010-01-12 21:28:44 +0000942 UnqualifiedId TemplateName;
943 TemplateName.setIdentifier(Id, IdLoc);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000944
Douglas Gregor84d0a192010-01-12 21:28:44 +0000945 // Parse the full template-id, then turn it into a type.
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000946 if (AnnotateTemplateIdToken(Template, TNK, SS, SourceLocation(),
947 TemplateName, true))
Douglas Gregor84d0a192010-01-12 21:28:44 +0000948 return true;
949 if (TNK == TNK_Dependent_template_name)
Douglas Gregor059101f2011-03-02 00:47:37 +0000950 AnnotateTemplateIdTokenAsType();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000951
Douglas Gregor84d0a192010-01-12 21:28:44 +0000952 // If we didn't end up with a typename token, there's nothing more we
953 // can do.
954 if (Tok.isNot(tok::annot_typename))
955 return true;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000956
Douglas Gregor84d0a192010-01-12 21:28:44 +0000957 // Retrieve the type from the annotation token, consume that token, and
958 // return.
959 EndLocation = Tok.getAnnotationEndLoc();
John McCallb3d87482010-08-24 05:47:05 +0000960 ParsedType Type = getTypeAnnotation(Tok);
Douglas Gregor84d0a192010-01-12 21:28:44 +0000961 ConsumeToken();
962 return Type;
963 }
964
Douglas Gregor42a552f2008-11-05 20:51:48 +0000965 // We have an identifier; check whether it is actually a type.
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000966 IdentifierInfo *CorrectedII = 0;
Douglas Gregor059101f2011-03-02 00:47:37 +0000967 ParsedType Type = Actions.getTypeName(*Id, IdLoc, getCurScope(), &SS, true,
Douglas Gregor9e876872011-03-01 18:12:44 +0000968 false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +0000969 /*IsCtorOrDtorName=*/false,
Kaelyn Uhrainc1fb5422012-06-22 23:37:05 +0000970 /*NonTrivialTypeSourceInfo=*/true,
971 &CorrectedII);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +0000972 if (!Type) {
Douglas Gregor124b8782010-02-16 19:09:40 +0000973 Diag(IdLoc, diag::err_expected_class_name);
Douglas Gregor31a19b62009-04-01 21:51:26 +0000974 return true;
Douglas Gregor42a552f2008-11-05 20:51:48 +0000975 }
976
977 // Consume the identifier.
Douglas Gregor84d0a192010-01-12 21:28:44 +0000978 EndLocation = IdLoc;
Nick Lewycky56062202010-07-26 16:56:01 +0000979
980 // Fake up a Declarator to use with ActOnTypeName.
John McCall0b7e6782011-03-24 11:26:52 +0000981 DeclSpec DS(AttrFactory);
Nick Lewycky56062202010-07-26 16:56:01 +0000982 DS.SetRangeStart(IdLoc);
983 DS.SetRangeEnd(EndLocation);
Douglas Gregor059101f2011-03-02 00:47:37 +0000984 DS.getTypeSpecScope() = SS;
Nick Lewycky56062202010-07-26 16:56:01 +0000985
986 const char *PrevSpec = 0;
987 unsigned DiagID;
988 DS.SetTypeSpecType(TST_typename, IdLoc, PrevSpec, DiagID, Type);
989
990 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
991 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor42a552f2008-11-05 20:51:48 +0000992}
993
John McCallc052dbb2012-05-22 21:28:12 +0000994void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
995 while (Tok.is(tok::kw___single_inheritance) ||
996 Tok.is(tok::kw___multiple_inheritance) ||
997 Tok.is(tok::kw___virtual_inheritance)) {
998 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
999 SourceLocation AttrNameLoc = ConsumeToken();
Aaron Ballman624421f2013-08-31 01:11:41 +00001000 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0,
1001 AttributeList::AS_GNU);
John McCallc052dbb2012-05-22 21:28:12 +00001002 }
1003}
1004
Richard Smithc9f35172012-06-25 21:37:02 +00001005/// Determine whether the following tokens are valid after a type-specifier
1006/// which could be a standalone declaration. This will conservatively return
1007/// true if there's any doubt, and is appropriate for insert-';' fixits.
Richard Smith139be702012-07-02 19:14:01 +00001008bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
Richard Smithc9f35172012-06-25 21:37:02 +00001009 // This switch enumerates the valid "follow" set for type-specifiers.
1010 switch (Tok.getKind()) {
1011 default: break;
1012 case tok::semi: // struct foo {...} ;
1013 case tok::star: // struct foo {...} * P;
1014 case tok::amp: // struct foo {...} & R = ...
Richard Smithba65f502013-01-19 03:48:05 +00001015 case tok::ampamp: // struct foo {...} && R = ...
Richard Smithc9f35172012-06-25 21:37:02 +00001016 case tok::identifier: // struct foo {...} V ;
1017 case tok::r_paren: //(struct foo {...} ) {4}
1018 case tok::annot_cxxscope: // struct foo {...} a:: b;
1019 case tok::annot_typename: // struct foo {...} a ::b;
1020 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1021 case tok::l_paren: // struct foo {...} ( x);
1022 case tok::comma: // __builtin_offsetof(struct foo{...} ,
Richard Smithba65f502013-01-19 03:48:05 +00001023 case tok::kw_operator: // struct foo operator ++() {...}
Richard Smithc9f35172012-06-25 21:37:02 +00001024 return true;
Richard Smith139be702012-07-02 19:14:01 +00001025 case tok::colon:
1026 return CouldBeBitfield; // enum E { ... } : 2;
Richard Smithc9f35172012-06-25 21:37:02 +00001027 // Type qualifiers
1028 case tok::kw_const: // struct foo {...} const x;
1029 case tok::kw_volatile: // struct foo {...} volatile x;
1030 case tok::kw_restrict: // struct foo {...} restrict x;
Richard Smithba65f502013-01-19 03:48:05 +00001031 // Function specifiers
1032 // Note, no 'explicit'. An explicit function must be either a conversion
1033 // operator or a constructor. Either way, it can't have a return type.
1034 case tok::kw_inline: // struct foo inline f();
1035 case tok::kw_virtual: // struct foo virtual f();
1036 case tok::kw_friend: // struct foo friend f();
Richard Smithc9f35172012-06-25 21:37:02 +00001037 // Storage-class specifiers
1038 case tok::kw_static: // struct foo {...} static x;
1039 case tok::kw_extern: // struct foo {...} extern x;
1040 case tok::kw_typedef: // struct foo {...} typedef x;
1041 case tok::kw_register: // struct foo {...} register x;
1042 case tok::kw_auto: // struct foo {...} auto x;
1043 case tok::kw_mutable: // struct foo {...} mutable x;
Richard Smithba65f502013-01-19 03:48:05 +00001044 case tok::kw_thread_local: // struct foo {...} thread_local x;
Richard Smithc9f35172012-06-25 21:37:02 +00001045 case tok::kw_constexpr: // struct foo {...} constexpr x;
1046 // As shown above, type qualifiers and storage class specifiers absolutely
1047 // can occur after class specifiers according to the grammar. However,
1048 // almost no one actually writes code like this. If we see one of these,
1049 // it is much more likely that someone missed a semi colon and the
1050 // type/storage class specifier we're seeing is part of the *next*
1051 // intended declaration, as in:
1052 //
1053 // struct foo { ... }
1054 // typedef int X;
1055 //
1056 // We'd really like to emit a missing semicolon error instead of emitting
1057 // an error on the 'int' saying that you can't have two type specifiers in
1058 // the same declaration of X. Because of this, we look ahead past this
1059 // token to see if it's a type specifier. If so, we know the code is
1060 // otherwise invalid, so we can produce the expected semi error.
1061 if (!isKnownToBeTypeSpecifier(NextToken()))
1062 return true;
1063 break;
1064 case tok::r_brace: // struct bar { struct foo {...} }
1065 // Missing ';' at end of struct is accepted as an extension in C mode.
1066 if (!getLangOpts().CPlusPlus)
1067 return true;
1068 break;
Richard Smithba65f502013-01-19 03:48:05 +00001069 // C++11 attributes
1070 case tok::l_square: // enum E [[]] x
1071 // Note, no tok::kw_alignas here; alignas cannot appertain to a type.
1072 return getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
Richard Smith8338a9d2013-01-29 04:13:32 +00001073 case tok::greater:
1074 // template<class T = class X>
1075 return getLangOpts().CPlusPlus;
Richard Smithc9f35172012-06-25 21:37:02 +00001076 }
1077 return false;
1078}
1079
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001080/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
1081/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
1082/// until we reach the start of a definition or see a token that
Richard Smith69730c12012-03-12 07:56:15 +00001083/// cannot start a definition.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001084///
1085/// class-specifier: [C++ class]
1086/// class-head '{' member-specification[opt] '}'
1087/// class-head '{' member-specification[opt] '}' attributes[opt]
1088/// class-head:
1089/// class-key identifier[opt] base-clause[opt]
1090/// class-key nested-name-specifier identifier base-clause[opt]
1091/// class-key nested-name-specifier[opt] simple-template-id
1092/// base-clause[opt]
1093/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001094/// [GNU] class-key attributes[opt] nested-name-specifier
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001095/// identifier base-clause[opt]
Mike Stump1eb44332009-09-09 15:08:12 +00001096/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001097/// simple-template-id base-clause[opt]
1098/// class-key:
1099/// 'class'
1100/// 'struct'
1101/// 'union'
1102///
1103/// elaborated-type-specifier: [C++ dcl.type.elab]
Mike Stump1eb44332009-09-09 15:08:12 +00001104/// class-key ::[opt] nested-name-specifier[opt] identifier
1105/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
1106/// simple-template-id
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001107///
1108/// Note that the C++ class-specifier and elaborated-type-specifier,
1109/// together, subsume the C99 struct-or-union-specifier:
1110///
1111/// struct-or-union-specifier: [C99 6.7.2.1]
1112/// struct-or-union identifier[opt] '{' struct-contents '}'
1113/// struct-or-union identifier
1114/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
1115/// '}' attributes[opt]
1116/// [GNU] struct-or-union attributes[opt] identifier
1117/// struct-or-union:
1118/// 'struct'
1119/// 'union'
Chris Lattner4c97d762009-04-12 21:49:30 +00001120void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1121 SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001122 const ParsedTemplateInfo &TemplateInfo,
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001123 AccessSpecifier AS,
Michael Han2e397132012-11-26 22:54:45 +00001124 bool EnteringContext, DeclSpecContext DSC,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001125 ParsedAttributesWithRange &Attributes) {
Joao Matos17d35c32012-08-31 22:18:20 +00001126 DeclSpec::TST TagType;
1127 if (TagTokKind == tok::kw_struct)
1128 TagType = DeclSpec::TST_struct;
1129 else if (TagTokKind == tok::kw___interface)
1130 TagType = DeclSpec::TST_interface;
1131 else if (TagTokKind == tok::kw_class)
1132 TagType = DeclSpec::TST_class;
1133 else {
Chris Lattner4c97d762009-04-12 21:49:30 +00001134 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1135 TagType = DeclSpec::TST_union;
1136 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001137
Douglas Gregor374929f2009-09-18 15:37:17 +00001138 if (Tok.is(tok::code_completion)) {
1139 // Code completion for a struct, class, or union name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001140 Actions.CodeCompleteTag(getCurScope(), TagType);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001141 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00001142 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001143
Chandler Carruth926c4b42010-06-28 08:39:25 +00001144 // C++03 [temp.explicit] 14.7.2/8:
1145 // The usual access checking rules do not apply to names used to specify
1146 // explicit instantiations.
1147 //
1148 // As an extension we do not perform access checking on the names used to
1149 // specify explicit specializations either. This is important to allow
1150 // specializing traits classes for private types.
John McCall13489672012-05-07 06:16:58 +00001151 //
1152 // Note that we don't suppress if this turns out to be an elaborated
1153 // type specifier.
1154 bool shouldDelayDiagsInTag =
1155 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
1156 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
1157 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Chandler Carruth926c4b42010-06-28 08:39:25 +00001158
Sean Hunt2edf0a22012-06-23 05:07:58 +00001159 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001160 // If attributes exist after tag, parse them.
Richard Smithdf1cce52013-10-24 01:21:09 +00001161 MaybeParseGNUAttributes(attrs);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001162
Steve Narofff59e17e2008-12-24 20:59:21 +00001163 // If declspecs exist after tag, parse them.
John McCallb1d397c2010-08-05 17:13:11 +00001164 while (Tok.is(tok::kw___declspec))
John McCall7f040a92010-12-24 02:08:15 +00001165 ParseMicrosoftDeclSpec(attrs);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001166
John McCallc052dbb2012-05-22 21:28:12 +00001167 // Parse inheritance specifiers.
1168 if (Tok.is(tok::kw___single_inheritance) ||
1169 Tok.is(tok::kw___multiple_inheritance) ||
1170 Tok.is(tok::kw___virtual_inheritance))
Richard Smithdf1cce52013-10-24 01:21:09 +00001171 ParseMicrosoftInheritanceClassAttributes(attrs);
John McCallc052dbb2012-05-22 21:28:12 +00001172
Sean Huntbbd37c62009-11-21 08:43:09 +00001173 // If C++0x attributes exist here, parse them.
1174 // FIXME: Are we consistent with the ordering of parsing of different
1175 // styles of attributes?
Richard Smith4e24f0f2013-01-02 12:01:23 +00001176 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Michael Han07fc1ba2013-01-07 16:57:11 +00001178 // Source location used by FIXIT to insert misplaced
1179 // C++11 attributes
1180 SourceLocation AttrFixitLoc = Tok.getLocation();
1181
John Wiegley20c0da72011-04-27 23:09:49 +00001182 if (TagType == DeclSpec::TST_struct &&
Douglas Gregorb467cda2011-04-29 15:31:39 +00001183 !Tok.is(tok::identifier) &&
1184 Tok.getIdentifierInfo() &&
1185 (Tok.is(tok::kw___is_arithmetic) ||
1186 Tok.is(tok::kw___is_convertible) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001187 Tok.is(tok::kw___is_empty) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001188 Tok.is(tok::kw___is_floating_point) ||
1189 Tok.is(tok::kw___is_function) ||
John Wiegley20c0da72011-04-27 23:09:49 +00001190 Tok.is(tok::kw___is_fundamental) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001191 Tok.is(tok::kw___is_integral) ||
1192 Tok.is(tok::kw___is_member_function_pointer) ||
1193 Tok.is(tok::kw___is_member_pointer) ||
1194 Tok.is(tok::kw___is_pod) ||
1195 Tok.is(tok::kw___is_pointer) ||
1196 Tok.is(tok::kw___is_same) ||
Douglas Gregor877222e2011-04-29 01:38:03 +00001197 Tok.is(tok::kw___is_scalar) ||
Douglas Gregorb467cda2011-04-29 15:31:39 +00001198 Tok.is(tok::kw___is_signed) ||
1199 Tok.is(tok::kw___is_unsigned) ||
1200 Tok.is(tok::kw___is_void))) {
Douglas Gregor68876142011-07-30 07:01:49 +00001201 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
Douglas Gregorb467cda2011-04-29 15:31:39 +00001202 // name of struct templates, but some are keywords in GCC >= 4.3
1203 // and Clang. Therefore, when we see the token sequence "struct
1204 // X", make X into a normal identifier rather than a keyword, to
1205 // allow libstdc++ 4.2 and libc++ to work properly.
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001206 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
Douglas Gregorb117a602009-09-04 05:53:02 +00001207 Tok.setKind(tok::identifier);
1208 }
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001210 // Parse the (optional) nested-name-specifier.
John McCallaa87d332009-12-12 11:40:51 +00001211 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00001212 if (getLangOpts().CPlusPlus) {
Chris Lattner08d92ec2009-12-10 00:32:41 +00001213 // "FOO : BAR" is not a potential typo for "FOO::BAR".
1214 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001215
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001216 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext))
John McCall207014e2010-07-30 06:26:29 +00001217 DS.SetTypeSpecError();
John McCall9ba61662010-02-26 08:45:28 +00001218 if (SS.isSet())
Chris Lattner08d92ec2009-12-10 00:32:41 +00001219 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
1220 Diag(Tok, diag::err_expected_ident);
1221 }
Douglas Gregorcc636682009-02-17 23:15:12 +00001222
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001223 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1224
Douglas Gregorcc636682009-02-17 23:15:12 +00001225 // Parse the (optional) class name or simple-template-id.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001226 IdentifierInfo *Name = 0;
1227 SourceLocation NameLoc;
Douglas Gregor39a8de12009-02-25 19:37:18 +00001228 TemplateIdAnnotation *TemplateId = 0;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001229 if (Tok.is(tok::identifier)) {
1230 Name = Tok.getIdentifierInfo();
1231 NameLoc = ConsumeToken();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001232
David Blaikie4e4d0842012-03-11 07:00:24 +00001233 if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001234 // The name was supposed to refer to a template, but didn't.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001235 // Eat the template argument list and try to continue parsing this as
1236 // a class (or template thereof).
1237 TemplateArgList TemplateArgs;
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001238 SourceLocation LAngleLoc, RAngleLoc;
Douglas Gregor059101f2011-03-02 00:47:37 +00001239 if (ParseTemplateIdAfterTemplateName(TemplateTy(), NameLoc, SS,
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001240 true, LAngleLoc,
Douglas Gregor314b97f2009-11-10 19:49:08 +00001241 TemplateArgs, RAngleLoc)) {
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001242 // We couldn't parse the template argument list at all, so don't
1243 // try to give any location information for the list.
1244 LAngleLoc = RAngleLoc = SourceLocation();
1245 }
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001246
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001247 Diag(NameLoc, diag::err_explicit_spec_non_template)
Joao Matos17d35c32012-08-31 22:18:20 +00001248 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1249 << (TagType == DeclSpec::TST_class? 0
1250 : TagType == DeclSpec::TST_struct? 1
Richard Smith0464e092013-11-08 21:51:24 +00001251 : TagType == DeclSpec::TST_union? 2
Joao Matos17d35c32012-08-31 22:18:20 +00001252 : 3)
1253 << Name
1254 << SourceRange(LAngleLoc, RAngleLoc);
1255
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001256 // Strip off the last template parameter list if it was empty, since
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001257 // we've removed its template argument list.
1258 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1259 if (TemplateParams && TemplateParams->size() > 1) {
1260 TemplateParams->pop_back();
1261 } else {
1262 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001263 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001264 = ParsedTemplateInfo::NonTemplate;
1265 }
1266 } else if (TemplateInfo.Kind
1267 == ParsedTemplateInfo::ExplicitInstantiation) {
1268 // Pretend this is just a forward declaration.
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001269 TemplateParams = 0;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001270 const_cast<ParsedTemplateInfo&>(TemplateInfo).Kind
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001271 = ParsedTemplateInfo::NonTemplate;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001272 const_cast<ParsedTemplateInfo&>(TemplateInfo).TemplateLoc
Douglas Gregorc78c06d2009-10-30 22:09:44 +00001273 = SourceLocation();
1274 const_cast<ParsedTemplateInfo&>(TemplateInfo).ExternLoc
1275 = SourceLocation();
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001276 }
Douglas Gregor2cc782f2009-10-30 21:46:58 +00001277 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001278 } else if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001279 TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001280 NameLoc = ConsumeToken();
Douglas Gregorcc636682009-02-17 23:15:12 +00001281
Douglas Gregor059101f2011-03-02 00:47:37 +00001282 if (TemplateId->Kind != TNK_Type_template &&
1283 TemplateId->Kind != TNK_Dependent_template_name) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001284 // The template-name in the simple-template-id refers to
1285 // something other than a class template. Give an appropriate
1286 // error message and skip to the ';'.
1287 SourceRange Range(NameLoc);
1288 if (SS.isNotEmpty())
1289 Range.setBegin(SS.getBeginLoc());
Douglas Gregorcc636682009-02-17 23:15:12 +00001290
Douglas Gregor39a8de12009-02-25 19:37:18 +00001291 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
Richard Trieu6e91f4b2013-06-19 22:25:01 +00001292 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Douglas Gregor39a8de12009-02-25 19:37:18 +00001294 DS.SetTypeSpecError();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001295 SkipUntil(tok::semi, StopBeforeMatch);
Douglas Gregor39a8de12009-02-25 19:37:18 +00001296 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001297 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001298 }
1299
Richard Smith7796eb52012-03-12 08:56:40 +00001300 // There are four options here.
1301 // - If we are in a trailing return type, this is always just a reference,
1302 // and we must not try to parse a definition. For instance,
1303 // [] () -> struct S { };
1304 // does not define a type.
1305 // - If we have 'struct foo {...', 'struct foo :...',
1306 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1307 // - If we have 'struct foo;', then this is either a forward declaration
1308 // or a friend declaration, which have to be treated differently.
1309 // - Otherwise we have something like 'struct foo xyz', a reference.
Michael Han2e397132012-11-26 22:54:45 +00001310 //
1311 // We also detect these erroneous cases to provide better diagnostic for
1312 // C++11 attributes parsing.
1313 // - attributes follow class name:
1314 // struct foo [[]] {};
1315 // - attributes appear before or after 'final':
1316 // struct foo [[]] final [[]] {};
1317 //
Richard Smith69730c12012-03-12 07:56:15 +00001318 // However, in type-specifier-seq's, things look like declarations but are
1319 // just references, e.g.
1320 // new struct s;
Sebastian Redld9bafa72010-02-03 21:21:43 +00001321 // or
Richard Smith69730c12012-03-12 07:56:15 +00001322 // &T::operator struct s;
1323 // For these, DSC is DSC_type_specifier.
Michael Han2e397132012-11-26 22:54:45 +00001324
1325 // If there are attributes after class name, parse them.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001326 MaybeParseCXX11Attributes(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00001327
John McCallf312b1e2010-08-26 23:41:50 +00001328 Sema::TagUseKind TUK;
Richard Smith7796eb52012-03-12 08:56:40 +00001329 if (DSC == DSC_trailing)
1330 TUK = Sema::TUK_Reference;
1331 else if (Tok.is(tok::l_brace) ||
1332 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001333 (isCXX11FinalKeyword() &&
David Blaikie6f426692012-03-12 15:39:49 +00001334 (NextToken().is(tok::l_brace) || NextToken().is(tok::colon)))) {
Douglas Gregord85bea22009-09-26 06:47:28 +00001335 if (DS.isFriendSpecified()) {
1336 // C++ [class.friend]p2:
1337 // A class shall not be defined in a friend declaration.
Richard Smithbdad7a22012-01-10 01:33:14 +00001338 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
Douglas Gregord85bea22009-09-26 06:47:28 +00001339 << SourceRange(DS.getFriendSpecLoc());
1340
1341 // Skip everything up to the semicolon, so that this looks like a proper
1342 // friend class (or template thereof) declaration.
Alexey Bataev8fe24752013-11-18 08:17:37 +00001343 SkipUntil(tok::semi, StopBeforeMatch);
John McCallf312b1e2010-08-26 23:41:50 +00001344 TUK = Sema::TUK_Friend;
Douglas Gregord85bea22009-09-26 06:47:28 +00001345 } else {
1346 // Okay, this is a class definition.
John McCallf312b1e2010-08-26 23:41:50 +00001347 TUK = Sema::TUK_Definition;
Douglas Gregord85bea22009-09-26 06:47:28 +00001348 }
Richard Smith150d8532013-02-22 06:46:23 +00001349 } else if (isCXX11FinalKeyword() && (NextToken().is(tok::l_square) ||
1350 NextToken().is(tok::kw_alignas))) {
Michael Han2e397132012-11-26 22:54:45 +00001351 // We can't tell if this is a definition or reference
1352 // until we skipped the 'final' and C++11 attribute specifiers.
1353 TentativeParsingAction PA(*this);
1354
1355 // Skip the 'final' keyword.
1356 ConsumeToken();
1357
1358 // Skip C++11 attribute specifiers.
1359 while (true) {
1360 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1361 ConsumeBracket();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001362 if (!SkipUntil(tok::r_square, StopAtSemi))
Michael Han2e397132012-11-26 22:54:45 +00001363 break;
Richard Smith150d8532013-02-22 06:46:23 +00001364 } else if (Tok.is(tok::kw_alignas) && NextToken().is(tok::l_paren)) {
Michael Han2e397132012-11-26 22:54:45 +00001365 ConsumeToken();
1366 ConsumeParen();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001367 if (!SkipUntil(tok::r_paren, StopAtSemi))
Michael Han2e397132012-11-26 22:54:45 +00001368 break;
1369 } else {
1370 break;
1371 }
1372 }
1373
1374 if (Tok.is(tok::l_brace) || Tok.is(tok::colon))
1375 TUK = Sema::TUK_Definition;
1376 else
1377 TUK = Sema::TUK_Reference;
1378
1379 PA.Revert();
Richard Smithc9f35172012-06-25 21:37:02 +00001380 } else if (DSC != DSC_type_specifier &&
1381 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00001382 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(false)))) {
John McCallf312b1e2010-08-26 23:41:50 +00001383 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
Joao Matos17d35c32012-08-31 22:18:20 +00001384 if (Tok.isNot(tok::semi)) {
1385 // A semicolon was missing after this declaration. Diagnose and recover.
1386 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1387 DeclSpec::getSpecifierName(TagType));
1388 PP.EnterToken(Tok);
1389 Tok.setKind(tok::semi);
1390 }
Richard Smithc9f35172012-06-25 21:37:02 +00001391 } else
John McCallf312b1e2010-08-26 23:41:50 +00001392 TUK = Sema::TUK_Reference;
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001393
Michael Han2e397132012-11-26 22:54:45 +00001394 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1395 // to caller to handle.
Michael Han07fc1ba2013-01-07 16:57:11 +00001396 if (TUK != Sema::TUK_Reference) {
1397 // If this is not a reference, then the only possible
1398 // valid place for C++11 attributes to appear here
1399 // is between class-key and class-name. If there are
1400 // any attributes after class-name, we try a fixit to move
1401 // them to the right place.
1402 SourceRange AttrRange = Attributes.Range;
1403 if (AttrRange.isValid()) {
1404 Diag(AttrRange.getBegin(), diag::err_attributes_not_allowed)
1405 << AttrRange
1406 << FixItHint::CreateInsertionFromRange(AttrFixitLoc,
1407 CharSourceRange(AttrRange, true))
1408 << FixItHint::CreateRemoval(AttrRange);
1409
1410 // Recover by adding misplaced attributes to the attribute list
1411 // of the class so they can be applied on the class later.
1412 attrs.takeAllFrom(Attributes);
1413 }
1414 }
Michael Han2e397132012-11-26 22:54:45 +00001415
John McCall13489672012-05-07 06:16:58 +00001416 // If this is an elaborated type specifier, and we delayed
1417 // diagnostics before, just merge them into the current pool.
1418 if (shouldDelayDiagsInTag) {
1419 diagsFromTag.done();
1420 if (TUK == Sema::TUK_Reference)
1421 diagsFromTag.redelay();
1422 }
1423
John McCall207014e2010-07-30 06:26:29 +00001424 if (!Name && !TemplateId && (DS.getTypeSpecType() == DeclSpec::TST_error ||
John McCallf312b1e2010-08-26 23:41:50 +00001425 TUK != Sema::TUK_Definition)) {
John McCall207014e2010-07-30 06:26:29 +00001426 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1427 // We have a declaration or reference to an anonymous class.
1428 Diag(StartLoc, diag::err_anon_type_definition)
1429 << DeclSpec::getSpecifierName(TagType);
1430 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001431
Alexey Bataev8fe24752013-11-18 08:17:37 +00001432 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001433 return;
1434 }
1435
Douglas Gregorddc29e12009-02-06 22:42:48 +00001436 // Create the tag portion of the class or class template.
John McCalld226f652010-08-21 09:40:31 +00001437 DeclResult TagOrTempResult = true; // invalid
1438 TypeResult TypeResult = true; // invalid
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001439
Douglas Gregor402abb52009-05-28 23:31:59 +00001440 bool Owned = false;
John McCallf1bbbb42009-09-04 01:14:41 +00001441 if (TemplateId) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001442 // Explicit specialization, class template partial specialization,
1443 // or explicit instantiation.
Benjamin Kramer5354e772012-08-23 23:38:35 +00001444 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
Douglas Gregor39a8de12009-02-25 19:37:18 +00001445 TemplateId->NumArgs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001446 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001447 TUK == Sema::TUK_Declaration) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001448 // This is an explicit instantiation of a class template.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001449 ProhibitAttributes(attrs);
1450
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001451 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001452 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001453 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001454 TemplateInfo.TemplateLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001455 TagType,
Mike Stump1eb44332009-09-09 15:08:12 +00001456 StartLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001457 SS,
John McCall2b5289b2010-08-23 07:28:44 +00001458 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001459 TemplateId->TemplateNameLoc,
1460 TemplateId->LAngleLoc,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001461 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001462 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001463 attrs.getList());
John McCall74256f52010-04-14 00:24:33 +00001464
1465 // Friend template-ids are treated as references unless
1466 // they have template headers, in which case they're ill-formed
1467 // (FIXME: "template <class T> friend class A<T>::B<int>;").
1468 // We diagnose this error in ActOnClassTemplateSpecialization.
John McCallf312b1e2010-08-26 23:41:50 +00001469 } else if (TUK == Sema::TUK_Reference ||
1470 (TUK == Sema::TUK_Friend &&
John McCall74256f52010-04-14 00:24:33 +00001471 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001472 ProhibitAttributes(attrs);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001473 TypeResult = Actions.ActOnTagTemplateIdType(TUK, TagType, StartLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001474 TemplateId->SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001475 TemplateId->TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00001476 TemplateId->Template,
1477 TemplateId->TemplateNameLoc,
1478 TemplateId->LAngleLoc,
1479 TemplateArgsPtr,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00001480 TemplateId->RAngleLoc);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001481 } else {
1482 // This is an explicit specialization or a class template
1483 // partial specialization.
1484 TemplateParameterLists FakedParamLists;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001485 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1486 // This looks like an explicit instantiation, because we have
1487 // something like
1488 //
1489 // template class Foo<X>
1490 //
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001491 // but it actually has a definition. Most likely, this was
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001492 // meant to be an explicit specialization, but the user forgot
1493 // the '<>' after 'template'.
Richard Smith61dfea92013-11-08 19:03:29 +00001494 // It this is friend declaration however, since it cannot have a
1495 // template header, it is most likely that the user meant to
1496 // remove the 'template' keyword.
Larisse Voufo49854292013-06-22 13:56:11 +00001497 assert((TUK == Sema::TUK_Definition || TUK == Sema::TUK_Friend) &&
Richard Smith61dfea92013-11-08 19:03:29 +00001498 "Expected a definition here");
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001499
Richard Smith61dfea92013-11-08 19:03:29 +00001500 if (TUK == Sema::TUK_Friend) {
1501 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
1502 TemplateParams = 0;
1503 } else {
1504 SourceLocation LAngleLoc =
1505 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1506 Diag(TemplateId->TemplateNameLoc,
1507 diag::err_explicit_instantiation_with_definition)
1508 << SourceRange(TemplateInfo.TemplateLoc)
1509 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1510
1511 // Create a fake template parameter list that contains only
1512 // "template<>", so that we treat this construct as a class
1513 // template specialization.
1514 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
1515 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0,
1516 LAngleLoc));
1517 TemplateParams = &FakedParamLists;
1518 }
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001519 }
1520
1521 // Build the class template specialization.
1522 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001523 = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK,
Douglas Gregord023aec2011-09-09 20:53:38 +00001524 StartLoc, DS.getModulePrivateSpecLoc(), SS,
John McCall2b5289b2010-08-23 07:28:44 +00001525 TemplateId->Template,
Mike Stump1eb44332009-09-09 15:08:12 +00001526 TemplateId->TemplateNameLoc,
1527 TemplateId->LAngleLoc,
Douglas Gregor39a8de12009-02-25 19:37:18 +00001528 TemplateArgsPtr,
Mike Stump1eb44332009-09-09 15:08:12 +00001529 TemplateId->RAngleLoc,
John McCall7f040a92010-12-24 02:08:15 +00001530 attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001531 MultiTemplateParamsArg(
Douglas Gregorcc636682009-02-17 23:15:12 +00001532 TemplateParams? &(*TemplateParams)[0] : 0,
1533 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001534 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001535 } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
John McCallf312b1e2010-08-26 23:41:50 +00001536 TUK == Sema::TUK_Declaration) {
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001537 // Explicit instantiation of a member of a class template
1538 // specialization, e.g.,
1539 //
1540 // template struct Outer<int>::Inner;
1541 //
Sean Hunt2edf0a22012-06-23 05:07:58 +00001542 ProhibitAttributes(attrs);
1543
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001544 TagOrTempResult
Douglas Gregor23c94db2010-07-02 17:43:08 +00001545 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor45f96552009-09-04 06:33:52 +00001546 TemplateInfo.ExternLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001547 TemplateInfo.TemplateLoc,
1548 TagType, StartLoc, SS, Name,
John McCall7f040a92010-12-24 02:08:15 +00001549 NameLoc, attrs.getList());
John McCall9a34edb2010-10-19 01:40:49 +00001550 } else if (TUK == Sema::TUK_Friend &&
1551 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001552 ProhibitAttributes(attrs);
1553
John McCall9a34edb2010-10-19 01:40:49 +00001554 TagOrTempResult =
1555 Actions.ActOnTemplatedFriendTag(getCurScope(), DS.getFriendSpecLoc(),
1556 TagType, StartLoc, SS,
John McCall7f040a92010-12-24 02:08:15 +00001557 Name, NameLoc, attrs.getList(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001558 MultiTemplateParamsArg(
John McCall9a34edb2010-10-19 01:40:49 +00001559 TemplateParams? &(*TemplateParams)[0] : 0,
1560 TemplateParams? TemplateParams->size() : 0));
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001561 } else {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001562 if (TUK != Sema::TUK_Declaration && TUK != Sema::TUK_Definition)
1563 ProhibitAttributes(attrs);
Richard Smith61dfea92013-11-08 19:03:29 +00001564
Larisse Voufo7c64ef02013-06-21 00:08:46 +00001565 if (TUK == Sema::TUK_Definition &&
1566 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1567 // If the declarator-id is not a template-id, issue a diagnostic and
1568 // recover by ignoring the 'template' keyword.
1569 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1570 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
Larisse Voufo49854292013-06-22 13:56:11 +00001571 TemplateParams = 0;
Larisse Voufo7c64ef02013-06-21 00:08:46 +00001572 }
Sean Hunt2edf0a22012-06-23 05:07:58 +00001573
John McCallc4e70192009-09-11 04:59:25 +00001574 bool IsDependent = false;
1575
John McCalla25c4082010-10-19 18:40:57 +00001576 // Don't pass down template parameter lists if this is just a tag
1577 // reference. For example, we don't need the template parameters here:
1578 // template <class T> class A *makeA(T t);
1579 MultiTemplateParamsArg TParams;
1580 if (TUK != Sema::TUK_Reference && TemplateParams)
1581 TParams =
1582 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
1583
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001584 // Declaration or definition of a class type
John McCall9a34edb2010-10-19 01:40:49 +00001585 TagOrTempResult = Actions.ActOnTag(getCurScope(), TagType, TUK, StartLoc,
John McCall7f040a92010-12-24 02:08:15 +00001586 SS, Name, NameLoc, attrs.getList(), AS,
Douglas Gregore7612302011-09-09 19:05:14 +00001587 DS.getModulePrivateSpecLoc(),
Richard Smithbdad7a22012-01-10 01:33:14 +00001588 TParams, Owned, IsDependent,
1589 SourceLocation(), false,
1590 clang::TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00001591
1592 // If ActOnTag said the type was dependent, try again with the
1593 // less common call.
John McCall9a34edb2010-10-19 01:40:49 +00001594 if (IsDependent) {
1595 assert(TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001596 TypeResult = Actions.ActOnDependentTag(getCurScope(), TagType, TUK,
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001597 SS, Name, StartLoc, NameLoc);
John McCall9a34edb2010-10-19 01:40:49 +00001598 }
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001599 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001600
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001601 // If there is a body, parse it and inform the actions module.
John McCallf312b1e2010-08-26 23:41:50 +00001602 if (TUK == Sema::TUK_Definition) {
John McCallbd0dfa52009-12-19 21:48:58 +00001603 assert(Tok.is(tok::l_brace) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001604 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
Richard Smith4e24f0f2013-01-02 12:01:23 +00001605 isCXX11FinalKeyword());
David Blaikie4e4d0842012-03-11 07:00:24 +00001606 if (getLangOpts().CPlusPlus)
Michael Han07fc1ba2013-01-07 16:57:11 +00001607 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs, TagType,
1608 TagOrTempResult.get());
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00001609 else
Douglas Gregor212e81c2009-03-25 00:13:59 +00001610 ParseStructUnionBody(StartLoc, TagType, TagOrTempResult.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001611 }
1612
John McCallb3d87482010-08-24 05:47:05 +00001613 const char *PrevSpec = 0;
1614 unsigned DiagID;
1615 bool Result;
John McCallc4e70192009-09-11 04:59:25 +00001616 if (!TypeResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001617 Result = DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
1618 NameLoc.isValid() ? NameLoc : StartLoc,
John McCallb3d87482010-08-24 05:47:05 +00001619 PrevSpec, DiagID, TypeResult.get());
John McCallc4e70192009-09-11 04:59:25 +00001620 } else if (!TagOrTempResult.isInvalid()) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00001621 Result = DS.SetTypeSpecType(TagType, StartLoc,
1622 NameLoc.isValid() ? NameLoc : StartLoc,
1623 PrevSpec, DiagID, TagOrTempResult.get(), Owned);
John McCallc4e70192009-09-11 04:59:25 +00001624 } else {
Douglas Gregorddc29e12009-02-06 22:42:48 +00001625 DS.SetTypeSpecError();
Anders Carlsson66e99772009-05-11 22:27:47 +00001626 return;
1627 }
Mike Stump1eb44332009-09-09 15:08:12 +00001628
John McCallb3d87482010-08-24 05:47:05 +00001629 if (Result)
John McCallfec54012009-08-03 20:12:06 +00001630 Diag(StartLoc, DiagID) << PrevSpec;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00001631
Chris Lattner4ed5d912010-02-02 01:23:29 +00001632 // At this point, we've successfully parsed a class-specifier in 'definition'
1633 // form (e.g. "struct foo { int x; }". While we could just return here, we're
1634 // going to look at what comes after it to improve error recovery. If an
1635 // impossible token occurs next, we assume that the programmer forgot a ; at
1636 // the end of the declaration and recover that way.
1637 //
Richard Smithc9f35172012-06-25 21:37:02 +00001638 // Also enforce C++ [temp]p3:
1639 // In a template-declaration which defines a class, no declarator
1640 // is permitted.
Joao Matos17d35c32012-08-31 22:18:20 +00001641 if (TUK == Sema::TUK_Definition &&
1642 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(false))) {
Argyrios Kyrtzidis7d033b22012-12-17 20:10:43 +00001643 if (Tok.isNot(tok::semi)) {
1644 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
1645 DeclSpec::getSpecifierName(TagType));
1646 // Push this token back into the preprocessor and change our current token
1647 // to ';' so that the rest of the code recovers as though there were an
1648 // ';' after the definition.
1649 PP.EnterToken(Tok);
1650 Tok.setKind(tok::semi);
1651 }
Chris Lattner4ed5d912010-02-02 01:23:29 +00001652 }
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001653}
1654
Mike Stump1eb44332009-09-09 15:08:12 +00001655/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001656///
1657/// base-clause : [C++ class.derived]
1658/// ':' base-specifier-list
1659/// base-specifier-list:
1660/// base-specifier '...'[opt]
1661/// base-specifier-list ',' base-specifier '...'[opt]
John McCalld226f652010-08-21 09:40:31 +00001662void Parser::ParseBaseClause(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001663 assert(Tok.is(tok::colon) && "Not a base clause");
1664 ConsumeToken();
1665
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001666 // Build up an array of parsed base specifiers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001667 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001668
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001669 while (true) {
1670 // Parse a base-specifier.
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001671 BaseResult Result = ParseBaseSpecifier(ClassDecl);
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001672 if (Result.isInvalid()) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001673 // Skip the rest of this base specifier, up until the comma or
1674 // opening brace.
Alexey Bataev8fe24752013-11-18 08:17:37 +00001675 SkipUntil(tok::comma, tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001676 } else {
1677 // Add this to our array of base specifiers.
Douglas Gregor5ac8aff2009-01-26 22:44:13 +00001678 BaseInfo.push_back(Result.get());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001679 }
1680
1681 // If the next token is a comma, consume it and keep reading
1682 // base-specifiers.
1683 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001685 // Consume the comma.
1686 ConsumeToken();
1687 }
Douglas Gregorf8268ae2008-10-22 17:49:05 +00001688
1689 // Attach the base specifiers
Jay Foadbeaaccd2009-05-21 09:52:38 +00001690 Actions.ActOnBaseSpecifiers(ClassDecl, BaseInfo.data(), BaseInfo.size());
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001691}
1692
1693/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
1694/// one entry in the base class list of a class specifier, for example:
1695/// class foo : public bar, virtual private baz {
1696/// 'public bar' and 'virtual private baz' are each base-specifiers.
1697///
1698/// base-specifier: [C++ class.derived]
Richard Smith05321402013-02-19 23:47:15 +00001699/// attribute-specifier-seq[opt] base-type-specifier
1700/// attribute-specifier-seq[opt] 'virtual' access-specifier[opt]
1701/// base-type-specifier
1702/// attribute-specifier-seq[opt] access-specifier 'virtual'[opt]
1703/// base-type-specifier
John McCalld226f652010-08-21 09:40:31 +00001704Parser::BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001705 bool IsVirtual = false;
1706 SourceLocation StartLoc = Tok.getLocation();
1707
Richard Smith05321402013-02-19 23:47:15 +00001708 ParsedAttributesWithRange Attributes(AttrFactory);
1709 MaybeParseCXX11Attributes(Attributes);
1710
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001711 // Parse the 'virtual' keyword.
1712 if (Tok.is(tok::kw_virtual)) {
1713 ConsumeToken();
1714 IsVirtual = true;
1715 }
1716
Richard Smith05321402013-02-19 23:47:15 +00001717 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1718
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001719 // Parse an (optional) access specifier.
1720 AccessSpecifier Access = getAccessSpecifierIfPresent();
John McCall92f88312010-01-23 00:46:32 +00001721 if (Access != AS_none)
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001722 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001723
Richard Smith05321402013-02-19 23:47:15 +00001724 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1725
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001726 // Parse the 'virtual' keyword (again!), in case it came after the
1727 // access specifier.
1728 if (Tok.is(tok::kw_virtual)) {
1729 SourceLocation VirtualLoc = ConsumeToken();
1730 if (IsVirtual) {
1731 // Complain about duplicate 'virtual'
Chris Lattner1ab3b962008-11-18 07:48:38 +00001732 Diag(VirtualLoc, diag::err_dup_virtual)
Douglas Gregor849b2432010-03-31 17:46:05 +00001733 << FixItHint::CreateRemoval(VirtualLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001734 }
1735
1736 IsVirtual = true;
1737 }
1738
Richard Smith05321402013-02-19 23:47:15 +00001739 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
1740
Douglas Gregor42a552f2008-11-05 20:51:48 +00001741 // Parse the class-name.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001742 SourceLocation EndLocation;
David Blaikie22216eb2011-10-25 17:10:12 +00001743 SourceLocation BaseLoc;
1744 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001745 if (BaseType.isInvalid())
Douglas Gregor42a552f2008-11-05 20:51:48 +00001746 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001747
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001748 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
1749 // actually part of the base-specifier-list grammar productions, but we
1750 // parse it here for convenience.
1751 SourceLocation EllipsisLoc;
1752 if (Tok.is(tok::ellipsis))
1753 EllipsisLoc = ConsumeToken();
1754
Mike Stump1eb44332009-09-09 15:08:12 +00001755 // Find the complete source range for the base-specifier.
Douglas Gregor7f43d672009-02-25 23:52:28 +00001756 SourceRange Range(StartLoc, EndLocation);
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001758 // Notify semantic analysis that we have parsed a complete
1759 // base-specifier.
Richard Smith05321402013-02-19 23:47:15 +00001760 return Actions.ActOnBaseSpecifier(ClassDecl, Range, Attributes, IsVirtual,
1761 Access, BaseType.get(), BaseLoc,
1762 EllipsisLoc);
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001763}
1764
1765/// getAccessSpecifierIfPresent - Determine whether the next token is
1766/// a C++ access-specifier.
1767///
1768/// access-specifier: [C++ class.derived]
1769/// 'private'
1770/// 'protected'
1771/// 'public'
Mike Stump1eb44332009-09-09 15:08:12 +00001772AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001773 switch (Tok.getKind()) {
1774 default: return AS_none;
1775 case tok::kw_private: return AS_private;
1776 case tok::kw_protected: return AS_protected;
1777 case tok::kw_public: return AS_public;
1778 }
1779}
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001780
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001781/// \brief If the given declarator has any parts for which parsing has to be
Richard Smitha058fd42012-05-02 22:22:32 +00001782/// delayed, e.g., default arguments, create a late-parsed method declaration
1783/// record to handle the parsing at the end of the class definition.
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001784void Parser::HandleMemberFunctionDeclDelays(Declarator& DeclaratorInfo,
1785 Decl *ThisDecl) {
Eli Friedmand33133c2009-07-22 21:45:50 +00001786 // We just declared a member function. If this member function
Richard Smitha058fd42012-05-02 22:22:32 +00001787 // has any default arguments, we'll need to parse them later.
Eli Friedmand33133c2009-07-22 21:45:50 +00001788 LateParsedMethodDeclaration *LateMethod = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001789 DeclaratorChunk::FunctionTypeInfo &FTI
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001790 = DeclaratorInfo.getFunctionTypeInfo();
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001791
Eli Friedmand33133c2009-07-22 21:45:50 +00001792 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumArgs; ++ParamIdx) {
1793 if (LateMethod || FTI.ArgInfo[ParamIdx].DefaultArgTokens) {
1794 if (!LateMethod) {
1795 // Push this method onto the stack of late-parsed method
1796 // declarations.
Douglas Gregord54eb442010-10-12 16:25:54 +00001797 LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
1798 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001799 LateMethod->TemplateScope = getCurScope()->isTemplateParamScope();
Eli Friedmand33133c2009-07-22 21:45:50 +00001800
1801 // Add all of the parameters prior to this one (they don't
1802 // have default arguments).
1803 LateMethod->DefaultArgs.reserve(FTI.NumArgs);
1804 for (unsigned I = 0; I < ParamIdx; ++I)
1805 LateMethod->DefaultArgs.push_back(
Douglas Gregor8f8210c2010-03-02 01:29:43 +00001806 LateParsedDefaultArgument(FTI.ArgInfo[I].Param));
Eli Friedmand33133c2009-07-22 21:45:50 +00001807 }
1808
Douglas Gregor74e2fc32012-04-16 18:27:27 +00001809 // Add this parameter to the list of parameters (it may or may
Eli Friedmand33133c2009-07-22 21:45:50 +00001810 // not have a default argument).
1811 LateMethod->DefaultArgs.push_back(
1812 LateParsedDefaultArgument(FTI.ArgInfo[ParamIdx].Param,
1813 FTI.ArgInfo[ParamIdx].DefaultArgTokens));
1814 }
1815 }
1816}
1817
Richard Smith4e24f0f2013-01-02 12:01:23 +00001818/// isCXX11VirtSpecifier - Determine whether the given token is a C++11
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001819/// virt-specifier.
1820///
1821/// virt-specifier:
1822/// override
1823/// final
Richard Smith4e24f0f2013-01-02 12:01:23 +00001824VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001825 if (!getLangOpts().CPlusPlus)
Anders Carlssoncc54d592011-01-22 16:56:46 +00001826 return VirtSpecifiers::VS_None;
1827
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001828 if (Tok.is(tok::identifier)) {
1829 IdentifierInfo *II = Tok.getIdentifierInfo();
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001830
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001831 // Initialize the contextual keywords.
1832 if (!Ident_final) {
1833 Ident_final = &PP.getIdentifierTable().get("final");
David Majnemer7121bdb2013-10-18 00:33:31 +00001834 if (getLangOpts().MicrosoftExt)
1835 Ident_sealed = &PP.getIdentifierTable().get("sealed");
Anders Carlsson7eeb4ec2011-01-20 03:47:08 +00001836 Ident_override = &PP.getIdentifierTable().get("override");
1837 }
1838
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001839 if (II == Ident_override)
1840 return VirtSpecifiers::VS_Override;
1841
David Majnemer7121bdb2013-10-18 00:33:31 +00001842 if (II == Ident_sealed)
1843 return VirtSpecifiers::VS_Sealed;
1844
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001845 if (II == Ident_final)
1846 return VirtSpecifiers::VS_Final;
1847 }
1848
1849 return VirtSpecifiers::VS_None;
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001850}
1851
Richard Smith4e24f0f2013-01-02 12:01:23 +00001852/// ParseOptionalCXX11VirtSpecifierSeq - Parse a virt-specifier-seq.
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001853///
1854/// virt-specifier-seq:
1855/// virt-specifier
1856/// virt-specifier-seq virt-specifier
Richard Smith4e24f0f2013-01-02 12:01:23 +00001857void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
John McCalle402e722012-09-25 07:32:39 +00001858 bool IsInterface) {
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001859 while (true) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001860 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001861 if (Specifier == VirtSpecifiers::VS_None)
1862 return;
1863
1864 // C++ [class.mem]p8:
1865 // A virt-specifier-seq shall contain at most one of each virt-specifier.
Anders Carlssoncc54d592011-01-22 16:56:46 +00001866 const char *PrevSpec = 0;
Anders Carlsson46127a92011-01-22 15:58:16 +00001867 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001868 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
1869 << PrevSpec
1870 << FixItHint::CreateRemoval(Tok.getLocation());
1871
David Majnemer7121bdb2013-10-18 00:33:31 +00001872 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
1873 Specifier == VirtSpecifiers::VS_Sealed)) {
John McCalle402e722012-09-25 07:32:39 +00001874 Diag(Tok.getLocation(), diag::err_override_control_interface)
1875 << VirtSpecifiers::getSpecifierName(Specifier);
David Majnemer7121bdb2013-10-18 00:33:31 +00001876 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
1877 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
John McCalle402e722012-09-25 07:32:39 +00001878 } else {
David Majnemer7121bdb2013-10-18 00:33:31 +00001879 Diag(Tok.getLocation(),
1880 getLangOpts().CPlusPlus11
1881 ? diag::warn_cxx98_compat_override_control_keyword
1882 : diag::ext_override_control_keyword)
1883 << VirtSpecifiers::getSpecifierName(Specifier);
John McCalle402e722012-09-25 07:32:39 +00001884 }
Anders Carlssonb971dbd2011-01-17 03:05:47 +00001885 ConsumeToken();
1886 }
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001887}
1888
Richard Smith4e24f0f2013-01-02 12:01:23 +00001889/// isCXX11FinalKeyword - Determine whether the next token is a C++11
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001890/// contextual 'final' keyword.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001891bool Parser::isCXX11FinalKeyword() const {
David Blaikie4e4d0842012-03-11 07:00:24 +00001892 if (!getLangOpts().CPlusPlus)
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001893 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001894
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001895 if (!Tok.is(tok::identifier))
1896 return false;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001897
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001898 // Initialize the contextual keywords.
1899 if (!Ident_final) {
1900 Ident_final = &PP.getIdentifierTable().get("final");
David Majnemer7121bdb2013-10-18 00:33:31 +00001901 if (getLangOpts().MicrosoftExt)
1902 Ident_sealed = &PP.getIdentifierTable().get("sealed");
Anders Carlsson8a29ba02011-03-25 14:53:29 +00001903 Ident_override = &PP.getIdentifierTable().get("override");
1904 }
David Majnemer7121bdb2013-10-18 00:33:31 +00001905
1906 return Tok.getIdentifierInfo() == Ident_final ||
1907 Tok.getIdentifierInfo() == Ident_sealed;
Anders Carlssoncc54d592011-01-22 16:56:46 +00001908}
1909
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001910/// ParseCXXClassMemberDeclaration - Parse a C++ class member declaration.
1911///
1912/// member-declaration:
1913/// decl-specifier-seq[opt] member-declarator-list[opt] ';'
1914/// function-definition ';'[opt]
1915/// ::[opt] nested-name-specifier template[opt] unqualified-id ';'[TODO]
1916/// using-declaration [TODO]
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001917/// [C++0x] static_assert-declaration
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00001918/// template-declaration
Chris Lattnerbc8d5642008-12-18 01:12:00 +00001919/// [GNU] '__extension__' member-declaration
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001920///
1921/// member-declarator-list:
1922/// member-declarator
1923/// member-declarator-list ',' member-declarator
1924///
1925/// member-declarator:
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001926/// declarator virt-specifier-seq[opt] pure-specifier[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001927/// declarator constant-initializer[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00001928/// [C++11] declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001929/// identifier[opt] ':' constant-expression
1930///
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001931/// virt-specifier-seq:
1932/// virt-specifier
1933/// virt-specifier-seq virt-specifier
1934///
1935/// virt-specifier:
1936/// override
1937/// final
David Majnemer7121bdb2013-10-18 00:33:31 +00001938/// [MS] sealed
Anders Carlsson1f3b6fd2011-01-16 23:56:42 +00001939///
Sebastian Redle2b68332009-04-12 17:16:29 +00001940/// pure-specifier:
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00001941/// '= 0'
1942///
1943/// constant-initializer:
1944/// '=' constant-expression
1945///
Douglas Gregor37b372b2009-08-20 22:52:58 +00001946void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00001947 AttributeList *AccessAttrs,
John McCallc9068d72010-07-16 08:13:16 +00001948 const ParsedTemplateInfo &TemplateInfo,
1949 ParsingDeclRAIIObject *TemplateDiags) {
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001950 if (Tok.is(tok::at)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001951 if (getLangOpts().ObjC1 && NextToken().isObjCAtKeyword(tok::objc_defs))
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001952 Diag(Tok, diag::err_at_defs_cxx);
1953 else
1954 Diag(Tok, diag::err_at_in_class);
Richard Smithb3104392013-11-09 04:52:51 +00001955
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001956 ConsumeToken();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001957 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregor8a9013d2011-04-14 17:21:19 +00001958 return;
1959 }
Richard Smithb3104392013-11-09 04:52:51 +00001960
John McCall60fa3cf2009-12-11 02:10:03 +00001961 // Access declarations.
Richard Smith83a22ec2012-05-09 08:23:23 +00001962 bool MalformedTypeSpec = false;
John McCall60fa3cf2009-12-11 02:10:03 +00001963 if (!TemplateInfo.Kind &&
Richard Smith83a22ec2012-05-09 08:23:23 +00001964 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon))) {
1965 if (TryAnnotateCXXScopeToken())
1966 MalformedTypeSpec = true;
1967
1968 bool isAccessDecl;
1969 if (Tok.isNot(tok::annot_cxxscope))
1970 isAccessDecl = false;
1971 else if (NextToken().is(tok::identifier))
John McCall60fa3cf2009-12-11 02:10:03 +00001972 isAccessDecl = GetLookAheadToken(2).is(tok::semi);
1973 else
1974 isAccessDecl = NextToken().is(tok::kw_operator);
1975
1976 if (isAccessDecl) {
1977 // Collect the scope specifier token we annotated earlier.
1978 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00001979 ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
1980 /*EnteringContext=*/false);
John McCall60fa3cf2009-12-11 02:10:03 +00001981
1982 // Try to parse an unqualified-id.
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001983 SourceLocation TemplateKWLoc;
John McCall60fa3cf2009-12-11 02:10:03 +00001984 UnqualifiedId Name;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001985 if (ParseUnqualifiedId(SS, false, true, true, ParsedType(),
1986 TemplateKWLoc, Name)) {
John McCall60fa3cf2009-12-11 02:10:03 +00001987 SkipUntil(tok::semi);
1988 return;
1989 }
1990
1991 // TODO: recover from mistakenly-qualified operator declarations.
1992 if (ExpectAndConsume(tok::semi,
1993 diag::err_expected_semi_after,
1994 "access declaration",
1995 tok::semi))
1996 return;
1997
Douglas Gregor23c94db2010-07-02 17:43:08 +00001998 Actions.ActOnUsingDeclaration(getCurScope(), AS,
Enea Zaffanella8d030c72013-07-22 10:54:09 +00001999 /* HasUsingKeyword */ false,
2000 SourceLocation(),
John McCall60fa3cf2009-12-11 02:10:03 +00002001 SS, Name,
2002 /* AttrList */ 0,
Enea Zaffanella8d030c72013-07-22 10:54:09 +00002003 /* HasTypenameKeyword */ false,
John McCall60fa3cf2009-12-11 02:10:03 +00002004 SourceLocation());
2005 return;
2006 }
2007 }
2008
Anders Carlsson511d7ab2009-03-11 16:27:10 +00002009 // static_assert-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00002010 if (Tok.is(tok::kw_static_assert) || Tok.is(tok::kw__Static_assert)) {
Douglas Gregor37b372b2009-08-20 22:52:58 +00002011 // FIXME: Check for templates
Chris Lattner97144fc2009-04-02 04:16:50 +00002012 SourceLocation DeclEnd;
2013 ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00002014 return;
2015 }
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Chris Lattner682bf922009-03-29 16:50:03 +00002017 if (Tok.is(tok::kw_template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002018 assert(!TemplateInfo.TemplateParams &&
Douglas Gregor37b372b2009-08-20 22:52:58 +00002019 "Nested template improperly parsed?");
Chris Lattner97144fc2009-04-02 04:16:50 +00002020 SourceLocation DeclEnd;
Mike Stump1eb44332009-09-09 15:08:12 +00002021 ParseDeclarationStartingWithTemplate(Declarator::MemberContext, DeclEnd,
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002022 AS, AccessAttrs);
Chris Lattner682bf922009-03-29 16:50:03 +00002023 return;
2024 }
Anders Carlsson5aeccdb2009-03-26 00:52:18 +00002025
Chris Lattnerbc8d5642008-12-18 01:12:00 +00002026 // Handle: member-declaration ::= '__extension__' member-declaration
2027 if (Tok.is(tok::kw___extension__)) {
2028 // __extension__ silences extension warnings in the subexpression.
2029 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2030 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002031 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2032 TemplateInfo, TemplateDiags);
Chris Lattnerbc8d5642008-12-18 01:12:00 +00002033 }
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002034
Chris Lattner4ed5d912010-02-02 01:23:29 +00002035 // Don't parse FOO:BAR as if it were a typo for FOO::BAR, in this context it
2036 // is a bitfield.
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002037 ColonProtectionRAIIObject X(*this);
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002038
John McCall0b7e6782011-03-24 11:26:52 +00002039 ParsedAttributesWithRange attrs(AttrFactory);
Michael Han52b501c2012-11-28 23:17:40 +00002040 ParsedAttributesWithRange FnAttrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00002041 // Optional C++11 attribute-specifier
2042 MaybeParseCXX11Attributes(attrs);
Michael Han52b501c2012-11-28 23:17:40 +00002043 // We need to keep these attributes for future diagnostic
2044 // before they are taken over by declaration specifier.
2045 FnAttrs.addAll(attrs.getList());
2046 FnAttrs.Range = attrs.Range;
2047
John McCall7f040a92010-12-24 02:08:15 +00002048 MaybeParseMicrosoftAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002049
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002050 if (Tok.is(tok::kw_using)) {
John McCall7f040a92010-12-24 02:08:15 +00002051 ProhibitAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002052
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002053 // Eat 'using'.
2054 SourceLocation UsingLoc = ConsumeToken();
2055
2056 if (Tok.is(tok::kw_namespace)) {
2057 Diag(UsingLoc, diag::err_using_namespace_in_class);
Alexey Bataev8fe24752013-11-18 08:17:37 +00002058 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattnerae50d502010-02-02 00:43:15 +00002059 } else {
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002060 SourceLocation DeclEnd;
Richard Smith3e4c6c42011-05-05 21:57:07 +00002061 // Otherwise, it must be a using-declaration or an alias-declaration.
John McCall78b81052010-11-10 02:40:36 +00002062 ParseUsingDeclaration(Declarator::MemberContext, TemplateInfo,
2063 UsingLoc, DeclEnd, AS);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002064 }
2065 return;
2066 }
2067
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002068 // Hold late-parsed attributes so we can attach a Decl to them later.
2069 LateParsedAttrList CommonLateParsedAttrs;
2070
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002071 // decl-specifier-seq:
2072 // Parse the common declaration-specifiers piece.
John McCallc9068d72010-07-16 08:13:16 +00002073 ParsingDeclSpec DS(*this, TemplateDiags);
John McCall7f040a92010-12-24 02:08:15 +00002074 DS.takeAttributesFrom(attrs);
Richard Smith83a22ec2012-05-09 08:23:23 +00002075 if (MalformedTypeSpec)
2076 DS.SetTypeSpecError();
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002077 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC_class,
2078 &CommonLateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002079
Benjamin Kramer5354e772012-08-23 23:38:35 +00002080 MultiTemplateParamsArg TemplateParams(
John McCalldd4a3b02009-09-16 22:47:08 +00002081 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data() : 0,
2082 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2083
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002084 if (Tok.is(tok::semi)) {
2085 ConsumeToken();
Michael Han52b501c2012-11-28 23:17:40 +00002086
2087 if (DS.isFriendSpecified())
2088 ProhibitAttributes(FnAttrs);
2089
John McCalld226f652010-08-21 09:40:31 +00002090 Decl *TheDecl =
Chandler Carruth0f4be742011-05-03 18:35:10 +00002091 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS, DS, TemplateParams);
John McCallc9068d72010-07-16 08:13:16 +00002092 DS.complete(TheDecl);
John McCall67d1a672009-08-06 02:15:43 +00002093 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002094 }
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002095
John McCall54abf7d2009-11-04 02:18:39 +00002096 ParsingDeclarator DeclaratorInfo(*this, DS, Declarator::MemberContext);
Nico Weber48673472011-01-28 06:07:34 +00002097 VirtSpecifiers VS;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002098
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002099 // Hold late-parsed attributes so we can attach a Decl to them later.
2100 LateParsedAttrList LateParsedAttrs;
2101
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002102 SourceLocation EqualLoc;
2103 bool HasInitializer = false;
2104 ExprResult Init;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002105 if (Tok.isNot(tok::colon)) {
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002106 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2107 ColonProtectionRAIIObject X(*this);
2108
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002109 // Parse the first declarator.
2110 ParseDeclarator(DeclaratorInfo);
Richard Smitha058fd42012-05-02 22:22:32 +00002111 // Error parsing the declarator?
Douglas Gregor10bd3682008-11-17 22:58:34 +00002112 if (!DeclaratorInfo.hasName()) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002113 // If so, skip until the semi-colon or a }.
Alexey Bataev8fe24752013-11-18 08:17:37 +00002114 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002115 if (Tok.is(tok::semi))
2116 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002117 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002118 }
2119
Richard Smith4e24f0f2013-01-02 12:01:23 +00002120 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Nico Weber48673472011-01-28 06:07:34 +00002121
John Thompson1b2fc0f2009-11-25 22:58:06 +00002122 // If attributes exist after the declarator, but before an '{', parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002123 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
John Thompson1b2fc0f2009-11-25 22:58:06 +00002124
Francois Pichet6a247472011-05-11 02:14:46 +00002125 // MSVC permits pure specifier on inline functions declared at class scope.
2126 // Hence check for =0 before checking for function definition.
David Blaikie4e4d0842012-03-11 07:00:24 +00002127 if (getLangOpts().MicrosoftExt && Tok.is(tok::equal) &&
Francois Pichet6a247472011-05-11 02:14:46 +00002128 DeclaratorInfo.isFunctionDeclarator() &&
2129 NextToken().is(tok::numeric_constant)) {
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002130 EqualLoc = ConsumeToken();
Francois Pichet6a247472011-05-11 02:14:46 +00002131 Init = ParseInitializer();
2132 if (Init.isInvalid())
Alexey Bataev8fe24752013-11-18 08:17:37 +00002133 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002134 else
2135 HasInitializer = true;
Francois Pichet6a247472011-05-11 02:14:46 +00002136 }
2137
Douglas Gregor45fa5602011-11-07 20:56:01 +00002138 FunctionDefinitionKind DefinitionKind = FDK_Declaration;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002139 // function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002140 //
2141 // In C++11, a non-function declarator followed by an open brace is a
2142 // braced-init-list for an in-class member initialization, not an
2143 // erroneous function definition.
Richard Smith80ad52f2013-01-02 11:42:31 +00002144 if (Tok.is(tok::l_brace) && !getLangOpts().CPlusPlus11) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002145 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002146 } else if (DeclaratorInfo.isFunctionDeclarator()) {
Richard Smith7a614d82011-06-11 17:19:42 +00002147 if (Tok.is(tok::l_brace) || Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
Douglas Gregor45fa5602011-11-07 20:56:01 +00002148 DefinitionKind = FDK_Definition;
Sean Hunte4246a62011-05-12 06:15:49 +00002149 } else if (Tok.is(tok::equal)) {
2150 const Token &KW = NextToken();
Douglas Gregor45fa5602011-11-07 20:56:01 +00002151 if (KW.is(tok::kw_default))
2152 DefinitionKind = FDK_Defaulted;
2153 else if (KW.is(tok::kw_delete))
2154 DefinitionKind = FDK_Deleted;
Sean Hunte4246a62011-05-12 06:15:49 +00002155 }
2156 }
2157
Michael Han52b501c2012-11-28 23:17:40 +00002158 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2159 // to a friend declaration, that declaration shall be a definition.
2160 if (DeclaratorInfo.isFunctionDeclarator() &&
2161 DefinitionKind != FDK_Definition && DS.isFriendSpecified()) {
2162 // Diagnose attributes that appear before decl specifier:
2163 // [[]] friend int foo();
2164 ProhibitAttributes(FnAttrs);
2165 }
2166
Douglas Gregor45fa5602011-11-07 20:56:01 +00002167 if (DefinitionKind) {
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002168 if (!DeclaratorInfo.isFunctionDeclarator()) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002169 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002170 ConsumeBrace();
Alexey Bataev8fe24752013-11-18 08:17:37 +00002171 SkipUntil(tok::r_brace);
Michael Han52b501c2012-11-28 23:17:40 +00002172
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002173 // Consume the optional ';'
2174 if (Tok.is(tok::semi))
2175 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002176 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002177 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002178
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002179 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
Richard Trieu65ba9482012-01-21 02:59:18 +00002180 Diag(DeclaratorInfo.getIdentifierLoc(),
2181 diag::err_function_declared_typedef);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002182
Richard Smith6f9a4452012-11-15 22:54:20 +00002183 // Recover by treating the 'typedef' as spurious.
2184 DS.ClearStorageClassSpecs();
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002185 }
2186
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002187 Decl *FunDecl =
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002188 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
Douglas Gregor45fa5602011-11-07 20:56:01 +00002189 VS, DefinitionKind, Init);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002190
David Majnemerfcbe2082013-08-01 04:22:55 +00002191 if (FunDecl) {
2192 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2193 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2194 }
2195 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2196 LateParsedAttrs[i]->addDecl(FunDecl);
2197 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002198 }
2199 LateParsedAttrs.clear();
Sean Hunte4246a62011-05-12 06:15:49 +00002200
2201 // Consume the ';' - it's optional unless we have a delete or default
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002202 if (Tok.is(tok::semi))
Richard Smitheab9d6f2012-07-23 05:45:25 +00002203 ConsumeExtraSemi(AfterMemberFunctionDefinition);
Douglas Gregor9ea416e2011-01-19 16:41:58 +00002204
Chris Lattner682bf922009-03-29 16:50:03 +00002205 return;
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002206 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002207 }
2208
2209 // member-declarator-list:
2210 // member-declarator
2211 // member-declarator-list ',' member-declarator
2212
Chris Lattner5f9e2722011-07-23 10:55:15 +00002213 SmallVector<Decl *, 8> DeclsInGroup;
John McCall60d7b3a2010-08-24 06:29:42 +00002214 ExprResult BitfieldSize;
Richard Smith1c94c162012-01-09 22:31:44 +00002215 bool ExpectSemi = true;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002216
2217 while (1) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002218 // member-declarator:
2219 // declarator pure-specifier[opt]
Richard Smith7a614d82011-06-11 17:19:42 +00002220 // declarator brace-or-equal-initializer[opt]
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002221 // identifier[opt] ':' constant-expression
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002222 if (Tok.is(tok::colon)) {
2223 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002224 BitfieldSize = ParseConstantExpression();
2225 if (BitfieldSize.isInvalid())
Alexey Bataev8fe24752013-11-18 08:17:37 +00002226 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002227 }
Mike Stump1eb44332009-09-09 15:08:12 +00002228
Chris Lattnere6563252010-06-13 05:34:18 +00002229 // If a simple-asm-expr is present, parse it.
2230 if (Tok.is(tok::kw_asm)) {
2231 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +00002232 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Chris Lattnere6563252010-06-13 05:34:18 +00002233 if (AsmLabel.isInvalid())
Alexey Bataev8fe24752013-11-18 08:17:37 +00002234 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Chris Lattnere6563252010-06-13 05:34:18 +00002235
2236 DeclaratorInfo.setAsmLabel(AsmLabel.release());
2237 DeclaratorInfo.SetRangeEnd(Loc);
2238 }
2239
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002240 // If attributes exist after the declarator, parse them.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002241 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002242
Richard Smith7a614d82011-06-11 17:19:42 +00002243 // FIXME: When g++ adds support for this, we'll need to check whether it
2244 // goes before or after the GNU attributes and __asm__.
Richard Smith4e24f0f2013-01-02 12:01:23 +00002245 ParseOptionalCXX11VirtSpecifierSeq(VS, getCurrentClass().IsInterface);
Richard Smith7a614d82011-06-11 17:19:42 +00002246
Richard Smithca523302012-06-10 03:12:00 +00002247 InClassInitStyle HasInClassInit = ICIS_NoInit;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002248 if ((Tok.is(tok::equal) || Tok.is(tok::l_brace)) && !HasInitializer) {
Richard Smith7a614d82011-06-11 17:19:42 +00002249 if (BitfieldSize.get()) {
2250 Diag(Tok, diag::err_bitfield_member_init);
Alexey Bataev8fe24752013-11-18 08:17:37 +00002251 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Richard Smith7a614d82011-06-11 17:19:42 +00002252 } else {
Douglas Gregor147545d2011-10-10 14:49:18 +00002253 HasInitializer = true;
Richard Smithca523302012-06-10 03:12:00 +00002254 if (!DeclaratorInfo.isDeclarationOfFunction() &&
2255 DeclaratorInfo.getDeclSpec().getStorageClassSpec()
Richard Smithca523302012-06-10 03:12:00 +00002256 != DeclSpec::SCS_typedef)
2257 HasInClassInit = Tok.is(tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
Richard Smith7a614d82011-06-11 17:19:42 +00002258 }
2259 }
2260
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002261 // NOTE: If Sema is the Action module and declarator is an instance field,
Chris Lattner682bf922009-03-29 16:50:03 +00002262 // this call will *not* return the created decl; It will return null.
Argyrios Kyrtzidis07952322008-07-01 10:37:29 +00002263 // See Sema::ActOnCXXMemberDeclarator for details.
John McCall67d1a672009-08-06 02:15:43 +00002264
Rafael Espindolafc35cbc2013-01-08 20:44:06 +00002265 NamedDecl *ThisDecl = 0;
John McCall67d1a672009-08-06 02:15:43 +00002266 if (DS.isFriendSpecified()) {
Michael Han52b501c2012-11-28 23:17:40 +00002267 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
2268 // to a friend declaration, that declaration shall be a definition.
2269 //
2270 // Diagnose attributes appear after friend member function declarator:
2271 // foo [[]] ();
2272 SmallVector<SourceRange, 4> Ranges;
2273 DeclaratorInfo.getCXX11AttributeRanges(Ranges);
2274 if (!Ranges.empty()) {
Craig Topper09d19ef2013-07-04 03:08:24 +00002275 for (SmallVectorImpl<SourceRange>::iterator I = Ranges.begin(),
Michael Han52b501c2012-11-28 23:17:40 +00002276 E = Ranges.end(); I != E; ++I) {
2277 Diag((*I).getBegin(), diag::err_attributes_not_allowed)
2278 << *I;
2279 }
2280 }
2281
John McCallbbbcdd92009-09-11 21:02:39 +00002282 // TODO: handle initializers, bitfields, 'delete'
Douglas Gregor23c94db2010-07-02 17:43:08 +00002283 ThisDecl = Actions.ActOnFriendFunctionDecl(getCurScope(), DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002284 TemplateParams);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002285 } else {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002286 ThisDecl = Actions.ActOnCXXMemberDeclarator(getCurScope(), AS,
John McCall67d1a672009-08-06 02:15:43 +00002287 DeclaratorInfo,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002288 TemplateParams,
John McCall67d1a672009-08-06 02:15:43 +00002289 BitfieldSize.release(),
Richard Smithca523302012-06-10 03:12:00 +00002290 VS, HasInClassInit);
Larisse Voufoef4579c2013-08-06 01:03:05 +00002291
2292 if (VarTemplateDecl *VT =
2293 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) : 0)
2294 // Re-direct this decl to refer to the templated decl so that we can
2295 // initialize it.
2296 ThisDecl = VT->getTemplatedDecl();
2297
David Majnemerfcbe2082013-08-01 04:22:55 +00002298 if (ThisDecl && AccessAttrs)
Richard Smith4a97b8e2013-08-29 00:47:48 +00002299 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
Douglas Gregor37b372b2009-08-20 22:52:58 +00002300 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002301
Douglas Gregor147545d2011-10-10 14:49:18 +00002302 // Handle the initializer.
David Blaikie1d87fba2013-01-30 01:22:18 +00002303 if (HasInClassInit != ICIS_NoInit &&
2304 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2305 DeclSpec::SCS_static) {
Douglas Gregor147545d2011-10-10 14:49:18 +00002306 // The initializer was deferred; parse it and cache the tokens.
David Majnemerfcbe2082013-08-01 04:22:55 +00002307 Diag(Tok, getLangOpts().CPlusPlus11
2308 ? diag::warn_cxx98_compat_nonstatic_member_init
2309 : diag::ext_nonstatic_member_init);
Richard Smith7fe62082011-10-15 05:09:34 +00002310
Richard Smith7a614d82011-06-11 17:19:42 +00002311 if (DeclaratorInfo.isArrayOfUnknownBound()) {
Richard Smithca523302012-06-10 03:12:00 +00002312 // C++11 [dcl.array]p3: An array bound may also be omitted when the
2313 // declarator is followed by an initializer.
Richard Smith7a614d82011-06-11 17:19:42 +00002314 //
2315 // A brace-or-equal-initializer for a member-declarator is not an
David Blaikie3164c142012-02-14 09:00:46 +00002316 // initializer in the grammar, so this is ill-formed.
Richard Smith7a614d82011-06-11 17:19:42 +00002317 Diag(Tok, diag::err_incomplete_array_member_init);
Alexey Bataev8fe24752013-11-18 08:17:37 +00002318 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
David Majnemerfcbe2082013-08-01 04:22:55 +00002319
2320 // Avoid later warnings about a class member of incomplete type.
David Blaikie3164c142012-02-14 09:00:46 +00002321 if (ThisDecl)
David Blaikie3164c142012-02-14 09:00:46 +00002322 ThisDecl->setInvalidDecl();
Richard Smith7a614d82011-06-11 17:19:42 +00002323 } else
2324 ParseCXXNonStaticMemberInitializer(ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002325 } else if (HasInitializer) {
2326 // Normal initializer.
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002327 if (!Init.isUsable())
David Majnemerfcbe2082013-08-01 04:22:55 +00002328 Init = ParseCXXMemberInitializer(
2329 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
2330
Douglas Gregor147545d2011-10-10 14:49:18 +00002331 if (Init.isInvalid())
Alexey Bataev8fe24752013-11-18 08:17:37 +00002332 SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch);
Douglas Gregor147545d2011-10-10 14:49:18 +00002333 else if (ThisDecl)
Sebastian Redl33deb352012-02-22 10:50:08 +00002334 Actions.AddInitializerToDecl(ThisDecl, Init.get(), EqualLoc.isInvalid(),
Richard Smitha2c36462013-04-26 16:15:35 +00002335 DS.containsPlaceholderType());
David Majnemerfcbe2082013-08-01 04:22:55 +00002336 } else if (ThisDecl && DS.getStorageClassSpec() == DeclSpec::SCS_static)
Douglas Gregor147545d2011-10-10 14:49:18 +00002337 // No initializer.
Richard Smitha2c36462013-04-26 16:15:35 +00002338 Actions.ActOnUninitializedDecl(ThisDecl, DS.containsPlaceholderType());
David Majnemerfcbe2082013-08-01 04:22:55 +00002339
Douglas Gregor147545d2011-10-10 14:49:18 +00002340 if (ThisDecl) {
David Majnemerfcbe2082013-08-01 04:22:55 +00002341 if (!ThisDecl->isInvalidDecl()) {
2342 // Set the Decl for any late parsed attributes
2343 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
2344 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
2345
2346 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
2347 LateParsedAttrs[i]->addDecl(ThisDecl);
2348 }
Douglas Gregor147545d2011-10-10 14:49:18 +00002349 Actions.FinalizeDeclaration(ThisDecl);
2350 DeclsInGroup.push_back(ThisDecl);
David Majnemerfcbe2082013-08-01 04:22:55 +00002351
2352 if (DeclaratorInfo.isFunctionDeclarator() &&
2353 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
2354 DeclSpec::SCS_typedef)
2355 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
Douglas Gregor147545d2011-10-10 14:49:18 +00002356 }
David Majnemerfcbe2082013-08-01 04:22:55 +00002357 LateParsedAttrs.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002358
2359 DeclaratorInfo.complete(ThisDecl);
Richard Smith7a614d82011-06-11 17:19:42 +00002360
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002361 // If we don't have a comma, it is either the end of the list (a ';')
2362 // or an error, bail out.
2363 if (Tok.isNot(tok::comma))
2364 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002365
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002366 // Consume the comma.
Richard Smith1c94c162012-01-09 22:31:44 +00002367 SourceLocation CommaLoc = ConsumeToken();
2368
2369 if (Tok.isAtStartOfLine() &&
2370 !MightBeDeclarator(Declarator::MemberContext)) {
2371 // This comma was followed by a line-break and something which can't be
2372 // the start of a declarator. The comma was probably a typo for a
2373 // semicolon.
2374 Diag(CommaLoc, diag::err_expected_semi_declaration)
2375 << FixItHint::CreateReplacement(CommaLoc, ";");
2376 ExpectSemi = false;
2377 break;
2378 }
Mike Stump1eb44332009-09-09 15:08:12 +00002379
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002380 // Parse the next declarator.
2381 DeclaratorInfo.clear();
Nico Weber48673472011-01-28 06:07:34 +00002382 VS.clear();
Douglas Gregor147545d2011-10-10 14:49:18 +00002383 BitfieldSize = true;
Douglas Gregora2b4e5d2011-10-17 17:09:53 +00002384 Init = true;
2385 HasInitializer = false;
Richard Smith7984de32012-01-12 23:53:29 +00002386 DeclaratorInfo.setCommaLoc(CommaLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002387
Bill Wendlingad017fa2012-12-20 19:22:21 +00002388 // Attributes are only allowed on the second declarator.
John McCall7f040a92010-12-24 02:08:15 +00002389 MaybeParseGNUAttributes(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002390
Argyrios Kyrtzidis3a9fdb42008-06-28 08:10:48 +00002391 if (Tok.isNot(tok::colon))
2392 ParseDeclarator(DeclaratorInfo);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002393 }
2394
Richard Smith1c94c162012-01-09 22:31:44 +00002395 if (ExpectSemi &&
2396 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
Chris Lattnerae50d502010-02-02 00:43:15 +00002397 // Skip to end of block or statement.
Alexey Bataev8fe24752013-11-18 08:17:37 +00002398 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattnerae50d502010-02-02 00:43:15 +00002399 // If we stopped at a ';', eat it.
2400 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +00002401 return;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002402 }
2403
Rafael Espindola4549d7f2013-07-09 12:05:01 +00002404 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002405}
2406
Richard Smith7a614d82011-06-11 17:19:42 +00002407/// ParseCXXMemberInitializer - Parse the brace-or-equal-initializer or
2408/// pure-specifier. Also detect and reject any attempted defaulted/deleted
2409/// function definition. The location of the '=', if any, will be placed in
2410/// EqualLoc.
2411///
2412/// pure-specifier:
2413/// '= 0'
Sebastian Redl33deb352012-02-22 10:50:08 +00002414///
Richard Smith7a614d82011-06-11 17:19:42 +00002415/// brace-or-equal-initializer:
2416/// '=' initializer-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002417/// braced-init-list
2418///
Richard Smith7a614d82011-06-11 17:19:42 +00002419/// initializer-clause:
2420/// assignment-expression
Sebastian Redl33deb352012-02-22 10:50:08 +00002421/// braced-init-list
2422///
Richard Smithb3104392013-11-09 04:52:51 +00002423/// defaulted/deleted function-definition:
Richard Smith7a614d82011-06-11 17:19:42 +00002424/// '=' 'default'
2425/// '=' 'delete'
2426///
2427/// Prior to C++0x, the assignment-expression in an initializer-clause must
2428/// be a constant-expression.
Douglas Gregor552e2992012-02-21 02:22:07 +00002429ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
Richard Smith7a614d82011-06-11 17:19:42 +00002430 SourceLocation &EqualLoc) {
2431 assert((Tok.is(tok::equal) || Tok.is(tok::l_brace))
2432 && "Data member initializer not starting with '=' or '{'");
2433
Douglas Gregor552e2992012-02-21 02:22:07 +00002434 EnterExpressionEvaluationContext Context(Actions,
2435 Sema::PotentiallyEvaluated,
2436 D);
Richard Smith7a614d82011-06-11 17:19:42 +00002437 if (Tok.is(tok::equal)) {
2438 EqualLoc = ConsumeToken();
2439 if (Tok.is(tok::kw_delete)) {
2440 // In principle, an initializer of '= delete p;' is legal, but it will
2441 // never type-check. It's better to diagnose it as an ill-formed expression
2442 // than as an ill-formed deleted non-function member.
2443 // An initializer of '= delete p, foo' will never be parsed, because
2444 // a top-level comma always ends the initializer expression.
2445 const Token &Next = NextToken();
2446 if (IsFunction || Next.is(tok::semi) || Next.is(tok::comma) ||
2447 Next.is(tok::eof)) {
2448 if (IsFunction)
2449 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2450 << 1 /* delete */;
2451 else
2452 Diag(ConsumeToken(), diag::err_deleted_non_function);
2453 return ExprResult();
2454 }
2455 } else if (Tok.is(tok::kw_default)) {
Richard Smith7a614d82011-06-11 17:19:42 +00002456 if (IsFunction)
2457 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
2458 << 0 /* default */;
2459 else
2460 Diag(ConsumeToken(), diag::err_default_special_members);
2461 return ExprResult();
2462 }
2463
Sebastian Redl33deb352012-02-22 10:50:08 +00002464 }
2465 return ParseInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00002466}
2467
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002468/// ParseCXXMemberSpecification - Parse the class definition.
2469///
2470/// member-specification:
2471/// member-declaration member-specification[opt]
2472/// access-specifier ':' member-specification[opt]
2473///
Joao Matos17d35c32012-08-31 22:18:20 +00002474void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
Michael Han07fc1ba2013-01-07 16:57:11 +00002475 SourceLocation AttrFixitLoc,
Richard Smith05321402013-02-19 23:47:15 +00002476 ParsedAttributesWithRange &Attrs,
Joao Matos17d35c32012-08-31 22:18:20 +00002477 unsigned TagType, Decl *TagDecl) {
2478 assert((TagType == DeclSpec::TST_struct ||
2479 TagType == DeclSpec::TST_interface ||
2480 TagType == DeclSpec::TST_union ||
2481 TagType == DeclSpec::TST_class) && "Invalid TagType!");
2482
John McCallf312b1e2010-08-26 23:41:50 +00002483 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2484 "parsing struct/union/class body");
Mike Stump1eb44332009-09-09 15:08:12 +00002485
Douglas Gregor26997fd2010-01-16 20:52:59 +00002486 // Determine whether this is a non-nested class. Note that local
2487 // classes are *not* considered to be nested classes.
2488 bool NonNestedClass = true;
2489 if (!ClassStack.empty()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002490 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00002491 if (S->isClassScope()) {
2492 // We're inside a class scope, so this is a nested class.
2493 NonNestedClass = false;
John McCalle402e722012-09-25 07:32:39 +00002494
2495 // The Microsoft extension __interface does not permit nested classes.
2496 if (getCurrentClass().IsInterface) {
2497 Diag(RecordLoc, diag::err_invalid_member_in_interface)
2498 << /*ErrorType=*/6
2499 << (isa<NamedDecl>(TagDecl)
2500 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
2501 : "<anonymous>");
2502 }
Douglas Gregor26997fd2010-01-16 20:52:59 +00002503 break;
2504 }
2505
2506 if ((S->getFlags() & Scope::FnScope)) {
2507 // If we're in a function or function template declared in the
2508 // body of a class, then this is a local class rather than a
2509 // nested class.
2510 const Scope *Parent = S->getParent();
2511 if (Parent->isTemplateParamScope())
2512 Parent = Parent->getParent();
2513 if (Parent->isClassScope())
2514 break;
2515 }
2516 }
2517 }
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002518
2519 // Enter a scope for the class.
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002520 ParseScope ClassScope(this, Scope::ClassScope|Scope::DeclScope);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002521
Douglas Gregor6569d682009-05-27 23:11:45 +00002522 // Note that we are parsing a new (potentially-nested) class definition.
John McCalle402e722012-09-25 07:32:39 +00002523 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
2524 TagType == DeclSpec::TST_interface);
Douglas Gregor6569d682009-05-27 23:11:45 +00002525
Douglas Gregorddc29e12009-02-06 22:42:48 +00002526 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002527 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002528
Anders Carlssonb184a182011-03-25 14:46:08 +00002529 SourceLocation FinalLoc;
David Majnemer7121bdb2013-10-18 00:33:31 +00002530 bool IsFinalSpelledSealed = false;
Anders Carlssonb184a182011-03-25 14:46:08 +00002531
2532 // Parse the optional 'final' keyword.
David Blaikie4e4d0842012-03-11 07:00:24 +00002533 if (getLangOpts().CPlusPlus && Tok.is(tok::identifier)) {
David Majnemer7121bdb2013-10-18 00:33:31 +00002534 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2535 assert((Specifier == VirtSpecifiers::VS_Final ||
2536 Specifier == VirtSpecifiers::VS_Sealed) &&
2537 "not a class definition");
Richard Smith8b11b5e2011-10-15 04:21:46 +00002538 FinalLoc = ConsumeToken();
David Majnemer7121bdb2013-10-18 00:33:31 +00002539 IsFinalSpelledSealed = Specifier == VirtSpecifiers::VS_Sealed;
Anders Carlssonb184a182011-03-25 14:46:08 +00002540
David Majnemer7121bdb2013-10-18 00:33:31 +00002541 if (TagType == DeclSpec::TST_interface)
John McCalle402e722012-09-25 07:32:39 +00002542 Diag(FinalLoc, diag::err_override_control_interface)
David Majnemer7121bdb2013-10-18 00:33:31 +00002543 << VirtSpecifiers::getSpecifierName(Specifier);
2544 else if (Specifier == VirtSpecifiers::VS_Final)
2545 Diag(FinalLoc, getLangOpts().CPlusPlus11
2546 ? diag::warn_cxx98_compat_override_control_keyword
2547 : diag::ext_override_control_keyword)
2548 << VirtSpecifiers::getSpecifierName(Specifier);
2549 else if (Specifier == VirtSpecifiers::VS_Sealed)
2550 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
Michael Han2e397132012-11-26 22:54:45 +00002551
Michael Han07fc1ba2013-01-07 16:57:11 +00002552 // Parse any C++11 attributes after 'final' keyword.
2553 // These attributes are not allowed to appear here,
2554 // and the only possible place for them to appertain
2555 // to the class would be between class-key and class-name.
Richard Smith05321402013-02-19 23:47:15 +00002556 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
Anders Carlssonb184a182011-03-25 14:46:08 +00002557 }
Anders Carlssoncc54d592011-01-22 16:56:46 +00002558
John McCallbd0dfa52009-12-19 21:48:58 +00002559 if (Tok.is(tok::colon)) {
2560 ParseBaseClause(TagDecl);
2561
2562 if (!Tok.is(tok::l_brace)) {
2563 Diag(Tok, diag::err_expected_lbrace_after_base_specifiers);
John McCalldb7bb4a2010-03-17 00:38:33 +00002564
2565 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002566 Actions.ActOnTagDefinitionError(getCurScope(), TagDecl);
John McCallbd0dfa52009-12-19 21:48:58 +00002567 return;
2568 }
2569 }
2570
2571 assert(Tok.is(tok::l_brace));
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002572 BalancedDelimiterTracker T(*this, tok::l_brace);
2573 T.consumeOpen();
John McCallbd0dfa52009-12-19 21:48:58 +00002574
John McCall42a4f662010-05-28 08:11:17 +00002575 if (TagDecl)
Anders Carlsson2c3ee542011-03-25 14:31:08 +00002576 Actions.ActOnStartCXXMemberDeclarations(getCurScope(), TagDecl, FinalLoc,
David Majnemer7121bdb2013-10-18 00:33:31 +00002577 IsFinalSpelledSealed,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002578 T.getOpenLocation());
John McCallf9368152009-12-20 07:58:13 +00002579
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002580 // C++ 11p3: Members of a class defined with the keyword class are private
2581 // by default. Members of a class defined with the keywords struct or union
2582 // are public by default.
2583 AccessSpecifier CurAS;
2584 if (TagType == DeclSpec::TST_class)
2585 CurAS = AS_private;
2586 else
2587 CurAS = AS_public;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002588 ParsedAttributes AccessAttrs(AttrFactory);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002589
Douglas Gregor07976d22010-06-21 22:31:09 +00002590 if (TagDecl) {
2591 // While we still have something to read, read the member-declarations.
2592 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
2593 // Each iteration of this loop reads one member-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002594
David Blaikie4e4d0842012-03-11 07:00:24 +00002595 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet563a6452011-05-25 10:19:49 +00002596 Tok.is(tok::kw___if_not_exists))) {
2597 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
2598 continue;
2599 }
2600
Douglas Gregor07976d22010-06-21 22:31:09 +00002601 // Check for extraneous top-level semicolon.
2602 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00002603 ConsumeExtraSemi(InsideStruct, TagType);
Douglas Gregor07976d22010-06-21 22:31:09 +00002604 continue;
2605 }
2606
Eli Friedmanaa5ab262012-02-23 23:47:16 +00002607 if (Tok.is(tok::annot_pragma_vis)) {
2608 HandlePragmaVisibility();
2609 continue;
2610 }
2611
2612 if (Tok.is(tok::annot_pragma_pack)) {
2613 HandlePragmaPack();
2614 continue;
2615 }
2616
Argyrios Kyrtzidisf4deaef2012-10-12 17:39:59 +00002617 if (Tok.is(tok::annot_pragma_align)) {
2618 HandlePragmaAlign();
2619 continue;
2620 }
2621
Alexey Bataevc6400582013-03-22 06:34:35 +00002622 if (Tok.is(tok::annot_pragma_openmp)) {
2623 ParseOpenMPDeclarativeDirective();
2624 continue;
2625 }
2626
Richard Smithb3104392013-11-09 04:52:51 +00002627 // If we see a namespace here, a close brace was missing somewhere.
2628 if (Tok.is(tok::kw_namespace)) {
Richard Smith7faf81f2013-11-15 23:00:02 +00002629 DiagnoseUnexpectedNamespace(cast<NamedDecl>(TagDecl));
Richard Smithb3104392013-11-09 04:52:51 +00002630 break;
2631 }
2632
Douglas Gregor07976d22010-06-21 22:31:09 +00002633 AccessSpecifier AS = getAccessSpecifierIfPresent();
2634 if (AS != AS_none) {
2635 // Current token is a C++ access specifier.
2636 CurAS = AS;
2637 SourceLocation ASLoc = Tok.getLocation();
David Blaikie13f8daf2011-10-13 06:08:43 +00002638 unsigned TokLength = Tok.getLength();
Douglas Gregor07976d22010-06-21 22:31:09 +00002639 ConsumeToken();
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002640 AccessAttrs.clear();
2641 MaybeParseGNUAttributes(AccessAttrs);
2642
David Blaikie13f8daf2011-10-13 06:08:43 +00002643 SourceLocation EndLoc;
2644 if (Tok.is(tok::colon)) {
2645 EndLoc = Tok.getLocation();
2646 ConsumeToken();
2647 } else if (Tok.is(tok::semi)) {
2648 EndLoc = Tok.getLocation();
2649 ConsumeToken();
2650 Diag(EndLoc, diag::err_expected_colon)
2651 << FixItHint::CreateReplacement(EndLoc, ":");
2652 } else {
2653 EndLoc = ASLoc.getLocWithOffset(TokLength);
2654 Diag(EndLoc, diag::err_expected_colon)
2655 << FixItHint::CreateInsertion(EndLoc, ":");
2656 }
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002657
John McCalle402e722012-09-25 07:32:39 +00002658 // The Microsoft extension __interface does not permit non-public
2659 // access specifiers.
2660 if (TagType == DeclSpec::TST_interface && CurAS != AS_public) {
2661 Diag(ASLoc, diag::err_access_specifier_interface)
2662 << (CurAS == AS_protected);
2663 }
2664
Erik Verbruggenc35cba42011-10-17 09:54:52 +00002665 if (Actions.ActOnAccessSpecifier(AS, ASLoc, EndLoc,
2666 AccessAttrs.getList())) {
2667 // found another attribute than only annotations
2668 AccessAttrs.clear();
2669 }
2670
Douglas Gregor07976d22010-06-21 22:31:09 +00002671 continue;
2672 }
2673
Douglas Gregor07976d22010-06-21 22:31:09 +00002674 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00002675 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002676 }
2677
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002678 T.consumeClose();
Douglas Gregor07976d22010-06-21 22:31:09 +00002679 } else {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002680 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002681 }
Mike Stump1eb44332009-09-09 15:08:12 +00002682
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002683 // If attributes exist after class contents, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002684 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002685 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002686
John McCall42a4f662010-05-28 08:11:17 +00002687 if (TagDecl)
Douglas Gregor23c94db2010-07-02 17:43:08 +00002688 Actions.ActOnFinishCXXMemberSpecification(getCurScope(), RecordLoc, TagDecl,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002689 T.getOpenLocation(),
2690 T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002691 attrs.getList());
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002692
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002693 // C++11 [class.mem]p2:
2694 // Within the class member-specification, the class is regarded as complete
Richard Smitha058fd42012-05-02 22:22:32 +00002695 // within function bodies, default arguments, and
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002696 // brace-or-equal-initializers for non-static data members (including such
2697 // things in nested classes).
Douglas Gregor07976d22010-06-21 22:31:09 +00002698 if (TagDecl && NonNestedClass) {
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002699 // We are not inside a nested class. This class and its nested classes
Douglas Gregor72b505b2008-12-16 21:30:33 +00002700 // are complete and we can parse the delayed portions of method
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002701 // declarations and the lexed inline method definitions, along with any
2702 // delayed attributes.
Douglas Gregore0cc0472010-06-16 23:45:56 +00002703 SourceLocation SavedPrevTokLocation = PrevTokLocation;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00002704 ParseLexedAttributes(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002705 ParseLexedMethodDeclarations(getCurrentClass());
Richard Smitha4156b82012-04-21 18:42:51 +00002706
2707 // We've finished with all pending member declarations.
2708 Actions.ActOnFinishCXXMemberDecls();
2709
Richard Smith7a614d82011-06-11 17:19:42 +00002710 ParseLexedMemberInitializers(getCurrentClass());
Douglas Gregor6569d682009-05-27 23:11:45 +00002711 ParseLexedMethodDefs(getCurrentClass());
Douglas Gregore0cc0472010-06-16 23:45:56 +00002712 PrevTokLocation = SavedPrevTokLocation;
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002713 }
2714
John McCall42a4f662010-05-28 08:11:17 +00002715 if (TagDecl)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002716 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2717 T.getCloseLocation());
John McCalldb7bb4a2010-03-17 00:38:33 +00002718
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002719 // Leave the class scope.
Douglas Gregor6569d682009-05-27 23:11:45 +00002720 ParsingDef.Pop();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002721 ClassScope.Exit();
Argyrios Kyrtzidis4cc18a42008-06-24 22:12:16 +00002722}
Douglas Gregor7ad83902008-11-05 04:29:56 +00002723
Richard Smith7faf81f2013-11-15 23:00:02 +00002724void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
Richard Smithb3104392013-11-09 04:52:51 +00002725 assert(Tok.is(tok::kw_namespace));
2726
2727 // FIXME: Suggest where the close brace should have gone by looking
2728 // at indentation changes within the definition body.
Richard Smith7faf81f2013-11-15 23:00:02 +00002729 Diag(D->getLocation(),
2730 diag::err_missing_end_of_definition) << D;
Richard Smithb3104392013-11-09 04:52:51 +00002731 Diag(Tok.getLocation(),
Richard Smith7faf81f2013-11-15 23:00:02 +00002732 diag::note_missing_end_of_definition_before) << D;
Richard Smithb3104392013-11-09 04:52:51 +00002733
2734 // Push '};' onto the token stream to recover.
2735 PP.EnterToken(Tok);
2736
2737 Tok.startToken();
2738 Tok.setLocation(PP.getLocForEndOfToken(PrevTokLocation));
2739 Tok.setKind(tok::semi);
2740 PP.EnterToken(Tok);
2741
2742 Tok.setKind(tok::r_brace);
2743}
2744
Douglas Gregor7ad83902008-11-05 04:29:56 +00002745/// ParseConstructorInitializer - Parse a C++ constructor initializer,
2746/// which explicitly initializes the members or base classes of a
2747/// class (C++ [class.base.init]). For example, the three initializers
2748/// after the ':' in the Derived constructor below:
2749///
2750/// @code
2751/// class Base { };
2752/// class Derived : Base {
2753/// int x;
2754/// float f;
2755/// public:
2756/// Derived(float f) : Base(), x(17), f(f) { }
2757/// };
2758/// @endcode
2759///
Mike Stump1eb44332009-09-09 15:08:12 +00002760/// [C++] ctor-initializer:
2761/// ':' mem-initializer-list
Douglas Gregor7ad83902008-11-05 04:29:56 +00002762///
Mike Stump1eb44332009-09-09 15:08:12 +00002763/// [C++] mem-initializer-list:
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00002764/// mem-initializer ...[opt]
2765/// mem-initializer ...[opt] , mem-initializer-list
John McCalld226f652010-08-21 09:40:31 +00002766void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002767 assert(Tok.is(tok::colon) && "Constructor initializer always starts with ':'");
2768
John Wiegley28bbe4b2011-04-28 01:08:34 +00002769 // Poison the SEH identifiers so they are flagged as illegal in constructor initializers
2770 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002771 SourceLocation ColonLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002772
Chris Lattner5f9e2722011-07-23 10:55:15 +00002773 SmallVector<CXXCtorInitializer*, 4> MemInitializers;
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002774 bool AnyErrors = false;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00002775
Douglas Gregor7ad83902008-11-05 04:29:56 +00002776 do {
Douglas Gregor0133f522010-08-28 00:00:50 +00002777 if (Tok.is(tok::code_completion)) {
Dmitri Gribenko572cf582013-06-23 22:58:02 +00002778 Actions.CodeCompleteConstructorInitializer(ConstructorDecl,
2779 MemInitializers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002780 return cutOffParsing();
Douglas Gregor0133f522010-08-28 00:00:50 +00002781 } else {
2782 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
2783 if (!MemInit.isInvalid())
2784 MemInitializers.push_back(MemInit.get());
2785 else
2786 AnyErrors = true;
2787 }
2788
Douglas Gregor7ad83902008-11-05 04:29:56 +00002789 if (Tok.is(tok::comma))
2790 ConsumeToken();
2791 else if (Tok.is(tok::l_brace))
2792 break;
Douglas Gregorb1f6fa42010-09-07 14:35:10 +00002793 // If the next token looks like a base or member initializer, assume that
2794 // we're just missing a comma.
Douglas Gregor751f6922010-09-07 14:51:08 +00002795 else if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon)) {
2796 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2797 Diag(Loc, diag::err_ctor_init_missing_comma)
2798 << FixItHint::CreateInsertion(Loc, ", ");
2799 } else {
Douglas Gregor7ad83902008-11-05 04:29:56 +00002800 // Skip over garbage, until we get to '{'. Don't eat the '{'.
Sebastian Redld3a413d2009-04-26 20:35:05 +00002801 Diag(Tok.getLocation(), diag::err_expected_lbrace_or_comma);
Alexey Bataev8fe24752013-11-18 08:17:37 +00002802 SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002803 break;
2804 }
2805 } while (true);
2806
David Blaikie93c86172013-01-17 05:26:25 +00002807 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInitializers,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +00002808 AnyErrors);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002809}
2810
2811/// ParseMemInitializer - Parse a C++ member initializer, which is
2812/// part of a constructor initializer that explicitly initializes one
2813/// member or base class (C++ [class.base.init]). See
2814/// ParseConstructorInitializer for an example.
2815///
2816/// [C++] mem-initializer:
2817/// mem-initializer-id '(' expression-list[opt] ')'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002818/// [C++0x] mem-initializer-id braced-init-list
Mike Stump1eb44332009-09-09 15:08:12 +00002819///
Douglas Gregor7ad83902008-11-05 04:29:56 +00002820/// [C++] mem-initializer-id:
2821/// '::'[opt] nested-name-specifier[opt] class-name
2822/// identifier
John McCalld226f652010-08-21 09:40:31 +00002823Parser::MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
Fariborz Jahanianbcfad542009-06-30 23:26:25 +00002824 // parse '::'[opt] nested-name-specifier[opt]
2825 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002826 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), /*EnteringContext=*/false);
John McCallb3d87482010-08-24 05:47:05 +00002827 ParsedType TemplateTypeTy;
Fariborz Jahanian96174332009-07-01 19:21:19 +00002828 if (Tok.is(tok::annot_template_id)) {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002829 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregord9b600c2010-01-12 17:52:59 +00002830 if (TemplateId->Kind == TNK_Type_template ||
2831 TemplateId->Kind == TNK_Dependent_template_name) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002832 AnnotateTemplateIdTokenAsType();
Fariborz Jahanian96174332009-07-01 19:21:19 +00002833 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
John McCallb3d87482010-08-24 05:47:05 +00002834 TemplateTypeTy = getTypeAnnotation(Tok);
Fariborz Jahanian96174332009-07-01 19:21:19 +00002835 }
Fariborz Jahanian96174332009-07-01 19:21:19 +00002836 }
David Blaikief2116622012-01-24 06:03:59 +00002837 // Uses of decltype will already have been converted to annot_decltype by
2838 // ParseOptionalCXXScopeSpecifier at this point.
2839 if (!TemplateTypeTy && Tok.isNot(tok::identifier)
2840 && Tok.isNot(tok::annot_decltype)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002841 Diag(Tok, diag::err_expected_member_or_base_name);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002842 return true;
2843 }
Mike Stump1eb44332009-09-09 15:08:12 +00002844
David Blaikief2116622012-01-24 06:03:59 +00002845 IdentifierInfo *II = 0;
2846 DeclSpec DS(AttrFactory);
2847 SourceLocation IdLoc = Tok.getLocation();
2848 if (Tok.is(tok::annot_decltype)) {
2849 // Get the decltype expression, if there is one.
2850 ParseDecltypeSpecifier(DS);
2851 } else {
2852 if (Tok.is(tok::identifier))
2853 // Get the identifier. This may be a member name or a class name,
2854 // but we'll let the semantic analysis determine which it is.
2855 II = Tok.getIdentifierInfo();
2856 ConsumeToken();
2857 }
2858
Douglas Gregor7ad83902008-11-05 04:29:56 +00002859
2860 // Parse the '('.
Richard Smith80ad52f2013-01-02 11:42:31 +00002861 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Richard Smith7fe62082011-10-15 05:09:34 +00002862 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2863
Sebastian Redl6df65482011-09-24 17:48:25 +00002864 ExprResult InitList = ParseBraceInitializer();
2865 if (InitList.isInvalid())
2866 return true;
2867
2868 SourceLocation EllipsisLoc;
2869 if (Tok.is(tok::ellipsis))
2870 EllipsisLoc = ConsumeToken();
2871
2872 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002873 TemplateTypeTy, DS, IdLoc,
2874 InitList.take(), EllipsisLoc);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002875 } else if(Tok.is(tok::l_paren)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002876 BalancedDelimiterTracker T(*this, tok::l_paren);
2877 T.consumeOpen();
Douglas Gregor7ad83902008-11-05 04:29:56 +00002878
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002879 // Parse the optional expression-list.
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002880 ExprVector ArgExprs;
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002881 CommaLocsTy CommaLocs;
2882 if (Tok.isNot(tok::r_paren) && ParseExpressionList(ArgExprs, CommaLocs)) {
Alexey Bataev8fe24752013-11-18 08:17:37 +00002883 SkipUntil(tok::r_paren, StopAtSemi);
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002884 return true;
2885 }
2886
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002887 T.consumeClose();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002888
2889 SourceLocation EllipsisLoc;
2890 if (Tok.is(tok::ellipsis))
2891 EllipsisLoc = ConsumeToken();
2892
2893 return Actions.ActOnMemInitializer(ConstructorDecl, getCurScope(), SS, II,
David Blaikief2116622012-01-24 06:03:59 +00002894 TemplateTypeTy, DS, IdLoc,
Dmitri Gribenkoa36bbac2013-05-09 23:51:52 +00002895 T.getOpenLocation(), ArgExprs,
2896 T.getCloseLocation(), EllipsisLoc);
Douglas Gregor7ad83902008-11-05 04:29:56 +00002897 }
2898
Richard Smith80ad52f2013-01-02 11:42:31 +00002899 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::err_expected_lparen_or_lbrace
Sebastian Redldbef1bb2011-06-05 12:23:16 +00002900 : diag::err_expected_lparen);
2901 return true;
Douglas Gregor7ad83902008-11-05 04:29:56 +00002902}
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002903
Sebastian Redl7acafd02011-03-05 14:45:16 +00002904/// \brief Parse a C++ exception-specification if present (C++0x [except.spec]).
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002905///
Douglas Gregora4745612008-12-01 18:00:20 +00002906/// exception-specification:
Sebastian Redl7acafd02011-03-05 14:45:16 +00002907/// dynamic-exception-specification
2908/// noexcept-specification
2909///
2910/// noexcept-specification:
2911/// 'noexcept'
2912/// 'noexcept' '(' constant-expression ')'
2913ExceptionSpecificationType
Richard Smitha058fd42012-05-02 22:22:32 +00002914Parser::tryParseExceptionSpecification(
Douglas Gregor74e2fc32012-04-16 18:27:27 +00002915 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002916 SmallVectorImpl<ParsedType> &DynamicExceptions,
2917 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00002918 ExprResult &NoexceptExpr) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00002919 ExceptionSpecificationType Result = EST_None;
2920
2921 // See if there's a dynamic specification.
2922 if (Tok.is(tok::kw_throw)) {
2923 Result = ParseDynamicExceptionSpecification(SpecificationRange,
2924 DynamicExceptions,
2925 DynamicExceptionRanges);
2926 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
2927 "Produced different number of exception types and ranges.");
2928 }
2929
2930 // If there's no noexcept specification, we're done.
2931 if (Tok.isNot(tok::kw_noexcept))
2932 return Result;
2933
Richard Smith841804b2011-10-17 23:06:20 +00002934 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
2935
Sebastian Redl7acafd02011-03-05 14:45:16 +00002936 // If we already had a dynamic specification, parse the noexcept for,
2937 // recovery, but emit a diagnostic and don't store the results.
2938 SourceRange NoexceptRange;
2939 ExceptionSpecificationType NoexceptType = EST_None;
2940
2941 SourceLocation KeywordLoc = ConsumeToken();
2942 if (Tok.is(tok::l_paren)) {
2943 // There is an argument.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002944 BalancedDelimiterTracker T(*this, tok::l_paren);
2945 T.consumeOpen();
Sebastian Redl7acafd02011-03-05 14:45:16 +00002946 NoexceptType = EST_ComputedNoexcept;
2947 NoexceptExpr = ParseConstantExpression();
Sebastian Redl60618fa2011-03-12 11:50:43 +00002948 // The argument must be contextually convertible to bool. We use
2949 // ActOnBooleanCondition for this purpose.
2950 if (!NoexceptExpr.isInvalid())
2951 NoexceptExpr = Actions.ActOnBooleanCondition(getCurScope(), KeywordLoc,
2952 NoexceptExpr.get());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002953 T.consumeClose();
2954 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
Sebastian Redl7acafd02011-03-05 14:45:16 +00002955 } else {
2956 // There is no argument.
2957 NoexceptType = EST_BasicNoexcept;
2958 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
2959 }
2960
2961 if (Result == EST_None) {
2962 SpecificationRange = NoexceptRange;
2963 Result = NoexceptType;
2964
2965 // If there's a dynamic specification after a noexcept specification,
2966 // parse that and ignore the results.
2967 if (Tok.is(tok::kw_throw)) {
2968 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2969 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
2970 DynamicExceptionRanges);
2971 }
2972 } else {
2973 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
2974 }
2975
2976 return Result;
2977}
2978
Richard Smith79f4bb72013-06-13 02:02:51 +00002979static void diagnoseDynamicExceptionSpecification(
2980 Parser &P, const SourceRange &Range, bool IsNoexcept) {
2981 if (P.getLangOpts().CPlusPlus11) {
2982 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
2983 P.Diag(Range.getBegin(), diag::warn_exception_spec_deprecated) << Range;
2984 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
2985 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
2986 }
2987}
2988
Sebastian Redl7acafd02011-03-05 14:45:16 +00002989/// ParseDynamicExceptionSpecification - Parse a C++
2990/// dynamic-exception-specification (C++ [except.spec]).
2991///
2992/// dynamic-exception-specification:
Douglas Gregora4745612008-12-01 18:00:20 +00002993/// 'throw' '(' type-id-list [opt] ')'
2994/// [MS] 'throw' '(' '...' ')'
Mike Stump1eb44332009-09-09 15:08:12 +00002995///
Douglas Gregora4745612008-12-01 18:00:20 +00002996/// type-id-list:
Douglas Gregora04426c2010-12-20 23:57:46 +00002997/// type-id ... [opt]
2998/// type-id-list ',' type-id ... [opt]
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002999///
Sebastian Redl7acafd02011-03-05 14:45:16 +00003000ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
3001 SourceRange &SpecificationRange,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003002 SmallVectorImpl<ParsedType> &Exceptions,
3003 SmallVectorImpl<SourceRange> &Ranges) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003004 assert(Tok.is(tok::kw_throw) && "expected throw");
Mike Stump1eb44332009-09-09 15:08:12 +00003005
Sebastian Redl7acafd02011-03-05 14:45:16 +00003006 SpecificationRange.setBegin(ConsumeToken());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003007 BalancedDelimiterTracker T(*this, tok::l_paren);
3008 if (T.consumeOpen()) {
Sebastian Redl7acafd02011-03-05 14:45:16 +00003009 Diag(Tok, diag::err_expected_lparen_after) << "throw";
3010 SpecificationRange.setEnd(SpecificationRange.getBegin());
Sebastian Redl60618fa2011-03-12 11:50:43 +00003011 return EST_DynamicNone;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003012 }
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003013
Douglas Gregora4745612008-12-01 18:00:20 +00003014 // Parse throw(...), a Microsoft extension that means "this function
3015 // can throw anything".
3016 if (Tok.is(tok::ellipsis)) {
3017 SourceLocation EllipsisLoc = ConsumeToken();
David Blaikie4e4d0842012-03-11 07:00:24 +00003018 if (!getLangOpts().MicrosoftExt)
Douglas Gregora4745612008-12-01 18:00:20 +00003019 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003020 T.consumeClose();
3021 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith79f4bb72013-06-13 02:02:51 +00003022 diagnoseDynamicExceptionSpecification(*this, SpecificationRange, false);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003023 return EST_MSAny;
Douglas Gregora4745612008-12-01 18:00:20 +00003024 }
3025
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003026 // Parse the sequence of type-ids.
Sebastian Redlef65f062009-05-29 18:02:33 +00003027 SourceRange Range;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003028 while (Tok.isNot(tok::r_paren)) {
Sebastian Redlef65f062009-05-29 18:02:33 +00003029 TypeResult Res(ParseTypeName(&Range));
Sebastian Redl7acafd02011-03-05 14:45:16 +00003030
Douglas Gregora04426c2010-12-20 23:57:46 +00003031 if (Tok.is(tok::ellipsis)) {
3032 // C++0x [temp.variadic]p5:
3033 // - In a dynamic-exception-specification (15.4); the pattern is a
3034 // type-id.
3035 SourceLocation Ellipsis = ConsumeToken();
Sebastian Redl7acafd02011-03-05 14:45:16 +00003036 Range.setEnd(Ellipsis);
Douglas Gregora04426c2010-12-20 23:57:46 +00003037 if (!Res.isInvalid())
3038 Res = Actions.ActOnPackExpansion(Res.get(), Ellipsis);
3039 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00003040
Sebastian Redlef65f062009-05-29 18:02:33 +00003041 if (!Res.isInvalid()) {
Sebastian Redl7dc81342009-04-29 17:30:04 +00003042 Exceptions.push_back(Res.get());
Sebastian Redlef65f062009-05-29 18:02:33 +00003043 Ranges.push_back(Range);
3044 }
Douglas Gregora04426c2010-12-20 23:57:46 +00003045
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003046 if (Tok.is(tok::comma))
3047 ConsumeToken();
Sebastian Redl7dc81342009-04-29 17:30:04 +00003048 else
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003049 break;
3050 }
3051
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003052 T.consumeClose();
3053 SpecificationRange.setEnd(T.getCloseLocation());
Richard Smith79f4bb72013-06-13 02:02:51 +00003054 diagnoseDynamicExceptionSpecification(*this, SpecificationRange,
3055 Exceptions.empty());
Sebastian Redl60618fa2011-03-12 11:50:43 +00003056 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003057}
Douglas Gregor6569d682009-05-27 23:11:45 +00003058
Douglas Gregordab60ad2010-10-01 18:44:50 +00003059/// ParseTrailingReturnType - Parse a trailing return type on a new-style
3060/// function declaration.
Douglas Gregorae7902c2011-08-04 15:30:47 +00003061TypeResult Parser::ParseTrailingReturnType(SourceRange &Range) {
Douglas Gregordab60ad2010-10-01 18:44:50 +00003062 assert(Tok.is(tok::arrow) && "expected arrow");
3063
3064 ConsumeToken();
3065
Richard Smith7796eb52012-03-12 08:56:40 +00003066 return ParseTypeName(&Range, Declarator::TrailingReturnContext);
Douglas Gregordab60ad2010-10-01 18:44:50 +00003067}
3068
Douglas Gregor6569d682009-05-27 23:11:45 +00003069/// \brief We have just started parsing the definition of a new class,
3070/// so push that class onto our stack of classes that is currently
3071/// being parsed.
John McCalleee1d542011-02-14 07:13:47 +00003072Sema::ParsingClassState
John McCalle402e722012-09-25 07:32:39 +00003073Parser::PushParsingClass(Decl *ClassDecl, bool NonNestedClass,
3074 bool IsInterface) {
Douglas Gregor26997fd2010-01-16 20:52:59 +00003075 assert((NonNestedClass || !ClassStack.empty()) &&
Douglas Gregor6569d682009-05-27 23:11:45 +00003076 "Nested class without outer class");
John McCalle402e722012-09-25 07:32:39 +00003077 ClassStack.push(new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
John McCalleee1d542011-02-14 07:13:47 +00003078 return Actions.PushParsingClass();
Douglas Gregor6569d682009-05-27 23:11:45 +00003079}
3080
3081/// \brief Deallocate the given parsed class and all of its nested
3082/// classes.
3083void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
Douglas Gregord54eb442010-10-12 16:25:54 +00003084 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
3085 delete Class->LateParsedDeclarations[I];
Douglas Gregor6569d682009-05-27 23:11:45 +00003086 delete Class;
3087}
3088
3089/// \brief Pop the top class of the stack of classes that are
3090/// currently being parsed.
3091///
3092/// This routine should be called when we have finished parsing the
3093/// definition of a class, but have not yet popped the Scope
3094/// associated with the class's definition.
John McCalleee1d542011-02-14 07:13:47 +00003095void Parser::PopParsingClass(Sema::ParsingClassState state) {
Douglas Gregor6569d682009-05-27 23:11:45 +00003096 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
Mike Stump1eb44332009-09-09 15:08:12 +00003097
John McCalleee1d542011-02-14 07:13:47 +00003098 Actions.PopParsingClass(state);
3099
Douglas Gregor6569d682009-05-27 23:11:45 +00003100 ParsingClass *Victim = ClassStack.top();
3101 ClassStack.pop();
3102 if (Victim->TopLevelClass) {
3103 // Deallocate all of the nested classes of this class,
3104 // recursively: we don't need to keep any of this information.
3105 DeallocateParsedClasses(Victim);
3106 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003107 }
Douglas Gregor6569d682009-05-27 23:11:45 +00003108 assert(!ClassStack.empty() && "Missing top-level class?");
3109
Douglas Gregord54eb442010-10-12 16:25:54 +00003110 if (Victim->LateParsedDeclarations.empty()) {
Douglas Gregor6569d682009-05-27 23:11:45 +00003111 // The victim is a nested class, but we will not need to perform
3112 // any processing after the definition of this class since it has
3113 // no members whose handling was delayed. Therefore, we can just
3114 // remove this nested class.
Douglas Gregord54eb442010-10-12 16:25:54 +00003115 DeallocateParsedClasses(Victim);
Douglas Gregor6569d682009-05-27 23:11:45 +00003116 return;
3117 }
3118
3119 // This nested class has some members that will need to be processed
3120 // after the top-level class is completely defined. Therefore, add
3121 // it to the list of nested classes within its parent.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003122 assert(getCurScope()->isClassScope() && "Nested class outside of class scope?");
Douglas Gregord54eb442010-10-12 16:25:54 +00003123 ClassStack.top()->LateParsedDeclarations.push_back(new LateParsedClass(this, Victim));
Douglas Gregor23c94db2010-07-02 17:43:08 +00003124 Victim->TemplateScope = getCurScope()->getParent()->isTemplateParamScope();
Douglas Gregor6569d682009-05-27 23:11:45 +00003125}
Sean Huntbbd37c62009-11-21 08:43:09 +00003126
Richard Smithc56298d2012-04-10 03:25:07 +00003127/// \brief Try to parse an 'identifier' which appears within an attribute-token.
3128///
3129/// \return the parsed identifier on success, and 0 if the next token is not an
3130/// attribute-token.
3131///
3132/// C++11 [dcl.attr.grammar]p3:
3133/// If a keyword or an alternative token that satisfies the syntactic
3134/// requirements of an identifier is contained in an attribute-token,
3135/// it is considered an identifier.
3136IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(SourceLocation &Loc) {
3137 switch (Tok.getKind()) {
3138 default:
3139 // Identifiers and keywords have identifier info attached.
3140 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
3141 Loc = ConsumeToken();
3142 return II;
3143 }
3144 return 0;
3145
3146 case tok::ampamp: // 'and'
3147 case tok::pipe: // 'bitor'
3148 case tok::pipepipe: // 'or'
3149 case tok::caret: // 'xor'
3150 case tok::tilde: // 'compl'
3151 case tok::amp: // 'bitand'
3152 case tok::ampequal: // 'and_eq'
3153 case tok::pipeequal: // 'or_eq'
3154 case tok::caretequal: // 'xor_eq'
3155 case tok::exclaim: // 'not'
3156 case tok::exclaimequal: // 'not_eq'
3157 // Alternative tokens do not have identifier info, but their spelling
3158 // starts with an alphabetical character.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003159 SmallString<8> SpellingBuf;
Richard Smithc56298d2012-04-10 03:25:07 +00003160 StringRef Spelling = PP.getSpelling(Tok.getLocation(), SpellingBuf);
Jordan Rose3f6f51e2013-02-08 22:30:41 +00003161 if (isLetter(Spelling[0])) {
Richard Smithc56298d2012-04-10 03:25:07 +00003162 Loc = ConsumeToken();
Benjamin Kramer0eb75262012-04-22 20:43:30 +00003163 return &PP.getIdentifierTable().get(Spelling);
Richard Smithc56298d2012-04-10 03:25:07 +00003164 }
3165 return 0;
3166 }
3167}
3168
Michael Han6880f492012-10-03 01:56:22 +00003169static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
3170 IdentifierInfo *ScopeName) {
3171 switch (AttributeList::getKind(AttrName, ScopeName,
3172 AttributeList::AS_CXX11)) {
3173 case AttributeList::AT_CarriesDependency:
3174 case AttributeList::AT_FallThrough:
Richard Smithcd8ab512013-01-17 01:30:42 +00003175 case AttributeList::AT_CXX11NoReturn: {
Michael Han6880f492012-10-03 01:56:22 +00003176 return true;
3177 }
3178
3179 default:
3180 return false;
3181 }
3182}
3183
Richard Smithc56298d2012-04-10 03:25:07 +00003184/// ParseCXX11AttributeSpecifier - Parse a C++11 attribute-specifier. Currently
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003185/// only parses standard attributes.
Sean Huntbbd37c62009-11-21 08:43:09 +00003186///
Richard Smith6ee326a2012-04-10 01:32:12 +00003187/// [C++11] attribute-specifier:
Sean Huntbbd37c62009-11-21 08:43:09 +00003188/// '[' '[' attribute-list ']' ']'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003189/// alignment-specifier
Sean Huntbbd37c62009-11-21 08:43:09 +00003190///
Richard Smith6ee326a2012-04-10 01:32:12 +00003191/// [C++11] attribute-list:
Sean Huntbbd37c62009-11-21 08:43:09 +00003192/// attribute[opt]
3193/// attribute-list ',' attribute[opt]
Richard Smithc56298d2012-04-10 03:25:07 +00003194/// attribute '...'
3195/// attribute-list ',' attribute '...'
Sean Huntbbd37c62009-11-21 08:43:09 +00003196///
Richard Smith6ee326a2012-04-10 01:32:12 +00003197/// [C++11] attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00003198/// attribute-token attribute-argument-clause[opt]
3199///
Richard Smith6ee326a2012-04-10 01:32:12 +00003200/// [C++11] attribute-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003201/// identifier
3202/// attribute-scoped-token
3203///
Richard Smith6ee326a2012-04-10 01:32:12 +00003204/// [C++11] attribute-scoped-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003205/// attribute-namespace '::' identifier
3206///
Richard Smith6ee326a2012-04-10 01:32:12 +00003207/// [C++11] attribute-namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +00003208/// identifier
3209///
Richard Smith6ee326a2012-04-10 01:32:12 +00003210/// [C++11] attribute-argument-clause:
Sean Huntbbd37c62009-11-21 08:43:09 +00003211/// '(' balanced-token-seq ')'
3212///
Richard Smith6ee326a2012-04-10 01:32:12 +00003213/// [C++11] balanced-token-seq:
Sean Huntbbd37c62009-11-21 08:43:09 +00003214/// balanced-token
3215/// balanced-token-seq balanced-token
3216///
Richard Smith6ee326a2012-04-10 01:32:12 +00003217/// [C++11] balanced-token:
Sean Huntbbd37c62009-11-21 08:43:09 +00003218/// '(' balanced-token-seq ')'
3219/// '[' balanced-token-seq ']'
3220/// '{' balanced-token-seq '}'
3221/// any token but '(', ')', '[', ']', '{', or '}'
Richard Smithc56298d2012-04-10 03:25:07 +00003222void Parser::ParseCXX11AttributeSpecifier(ParsedAttributes &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003223 SourceLocation *endLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003224 if (Tok.is(tok::kw_alignas)) {
Richard Smith41be6732011-10-14 20:48:27 +00003225 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00003226 ParseAlignmentSpecifier(attrs, endLoc);
3227 return;
3228 }
3229
Sean Huntbbd37c62009-11-21 08:43:09 +00003230 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)
Richard Smith6ee326a2012-04-10 01:32:12 +00003231 && "Not a C++11 attribute list");
Sean Huntbbd37c62009-11-21 08:43:09 +00003232
Richard Smith41be6732011-10-14 20:48:27 +00003233 Diag(Tok.getLocation(), diag::warn_cxx98_compat_attribute);
3234
Sean Huntbbd37c62009-11-21 08:43:09 +00003235 ConsumeBracket();
3236 ConsumeBracket();
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003237
Richard Smithcd8ab512013-01-17 01:30:42 +00003238 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
3239
Richard Smithc56298d2012-04-10 03:25:07 +00003240 while (Tok.isNot(tok::r_square)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003241 // attribute not present
3242 if (Tok.is(tok::comma)) {
3243 ConsumeToken();
3244 continue;
3245 }
3246
Richard Smithc56298d2012-04-10 03:25:07 +00003247 SourceLocation ScopeLoc, AttrLoc;
3248 IdentifierInfo *ScopeName = 0, *AttrName = 0;
3249
3250 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3251 if (!AttrName)
3252 // Break out to the "expected ']'" diagnostic.
3253 break;
Kovarththanan Rajaratnam19357542010-03-13 10:17:05 +00003254
Sean Huntbbd37c62009-11-21 08:43:09 +00003255 // scoped attribute
3256 if (Tok.is(tok::coloncolon)) {
3257 ConsumeToken();
3258
Richard Smithc56298d2012-04-10 03:25:07 +00003259 ScopeName = AttrName;
3260 ScopeLoc = AttrLoc;
3261
3262 AttrName = TryParseCXX11AttributeIdentifier(AttrLoc);
3263 if (!AttrName) {
Sean Huntbbd37c62009-11-21 08:43:09 +00003264 Diag(Tok.getLocation(), diag::err_expected_ident);
Alexey Bataev8fe24752013-11-18 08:17:37 +00003265 SkipUntil(tok::r_square, tok::comma, StopAtSemi | StopBeforeMatch);
Sean Huntbbd37c62009-11-21 08:43:09 +00003266 continue;
3267 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003268 }
3269
Michael Han6880f492012-10-03 01:56:22 +00003270 bool StandardAttr = IsBuiltInOrStandardCXX11Attribute(AttrName,ScopeName);
Sean Huntbbd37c62009-11-21 08:43:09 +00003271 bool AttrParsed = false;
Sean Huntbbd37c62009-11-21 08:43:09 +00003272
Richard Smithcd8ab512013-01-17 01:30:42 +00003273 if (StandardAttr &&
3274 !SeenAttrs.insert(std::make_pair(AttrName, AttrLoc)).second)
3275 Diag(AttrLoc, diag::err_cxx11_attribute_repeated)
3276 << AttrName << SourceRange(SeenAttrs[AttrName]);
3277
Michael Han6880f492012-10-03 01:56:22 +00003278 // Parse attribute arguments
3279 if (Tok.is(tok::l_paren)) {
3280 if (ScopeName && ScopeName->getName() == "gnu") {
3281 ParseGNUAttributeArgs(AttrName, AttrLoc, attrs, endLoc,
3282 ScopeName, ScopeLoc, AttributeList::AS_CXX11);
3283 AttrParsed = true;
3284 } else {
3285 if (StandardAttr)
3286 Diag(Tok.getLocation(), diag::err_cxx11_attribute_forbids_arguments)
3287 << AttrName->getName();
3288
3289 // FIXME: handle other formats of c++11 attribute arguments
3290 ConsumeParen();
Alexey Bataev8fe24752013-11-18 08:17:37 +00003291 SkipUntil(tok::r_paren);
Michael Han6880f492012-10-03 01:56:22 +00003292 }
3293 }
3294
3295 if (!AttrParsed)
Richard Smithe0d3b4c2012-05-03 18:27:39 +00003296 attrs.addNew(AttrName,
3297 SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc,
3298 AttrLoc),
Aaron Ballman624421f2013-08-31 01:11:41 +00003299 ScopeName, ScopeLoc, 0, 0, AttributeList::AS_CXX11);
Richard Smith6ee326a2012-04-10 01:32:12 +00003300
Richard Smithc56298d2012-04-10 03:25:07 +00003301 if (Tok.is(tok::ellipsis)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003302 ConsumeToken();
Michael Han6880f492012-10-03 01:56:22 +00003303
3304 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
3305 << AttrName->getName();
Richard Smithc56298d2012-04-10 03:25:07 +00003306 }
Sean Huntbbd37c62009-11-21 08:43:09 +00003307 }
3308
3309 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
Alexey Bataev8fe24752013-11-18 08:17:37 +00003310 SkipUntil(tok::r_square);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003311 if (endLoc)
3312 *endLoc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00003313 if (ExpectAndConsume(tok::r_square, diag::err_expected_rsquare))
Alexey Bataev8fe24752013-11-18 08:17:37 +00003314 SkipUntil(tok::r_square);
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003315}
Sean Huntbbd37c62009-11-21 08:43:09 +00003316
Sean Hunt2edf0a22012-06-23 05:07:58 +00003317/// ParseCXX11Attributes - Parse a C++11 attribute-specifier-seq.
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003318///
3319/// attribute-specifier-seq:
3320/// attribute-specifier-seq[opt] attribute-specifier
Richard Smithc56298d2012-04-10 03:25:07 +00003321void Parser::ParseCXX11Attributes(ParsedAttributesWithRange &attrs,
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003322 SourceLocation *endLoc) {
Richard Smith672edb02013-02-22 09:15:49 +00003323 assert(getLangOpts().CPlusPlus11);
3324
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003325 SourceLocation StartLoc = Tok.getLocation(), Loc;
3326 if (!endLoc)
3327 endLoc = &Loc;
3328
Douglas Gregor8828ee72011-10-07 20:35:25 +00003329 do {
Richard Smithc56298d2012-04-10 03:25:07 +00003330 ParseCXX11AttributeSpecifier(attrs, endLoc);
Richard Smith6ee326a2012-04-10 01:32:12 +00003331 } while (isCXX11AttributeSpecifier());
Peter Collingbourne3497fdf2011-09-29 18:04:05 +00003332
3333 attrs.Range = SourceRange(StartLoc, *endLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003334}
3335
Richard Smith5eed7e02013-10-15 01:34:54 +00003336void Parser::DiagnoseAndSkipCXX11Attributes() {
3337 if (!isCXX11AttributeSpecifier())
3338 return;
3339
3340 // Start and end location of an attribute or an attribute list.
3341 SourceLocation StartLoc = Tok.getLocation();
3342 SourceLocation EndLoc;
3343
3344 do {
3345 if (Tok.is(tok::l_square)) {
3346 BalancedDelimiterTracker T(*this, tok::l_square);
3347 T.consumeOpen();
3348 T.skipToEnd();
3349 EndLoc = T.getCloseLocation();
3350 } else {
3351 assert(Tok.is(tok::kw_alignas) && "not an attribute specifier");
3352 ConsumeToken();
3353 BalancedDelimiterTracker T(*this, tok::l_paren);
3354 if (!T.consumeOpen())
3355 T.skipToEnd();
3356 EndLoc = T.getCloseLocation();
3357 }
3358 } while (isCXX11AttributeSpecifier());
3359
3360 if (EndLoc.isValid()) {
3361 SourceRange Range(StartLoc, EndLoc);
3362 Diag(StartLoc, diag::err_attributes_not_allowed)
3363 << Range;
3364 }
3365}
3366
Francois Pichet334d47e2010-10-11 12:59:39 +00003367/// ParseMicrosoftAttributes - Parse a Microsoft attribute [Attr]
3368///
3369/// [MS] ms-attribute:
3370/// '[' token-seq ']'
3371///
3372/// [MS] ms-attribute-seq:
3373/// ms-attribute[opt]
3374/// ms-attribute ms-attribute-seq
John McCall7f040a92010-12-24 02:08:15 +00003375void Parser::ParseMicrosoftAttributes(ParsedAttributes &attrs,
3376 SourceLocation *endLoc) {
Francois Pichet334d47e2010-10-11 12:59:39 +00003377 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
3378
3379 while (Tok.is(tok::l_square)) {
Richard Smith6ee326a2012-04-10 01:32:12 +00003380 // FIXME: If this is actually a C++11 attribute, parse it as one.
Francois Pichet334d47e2010-10-11 12:59:39 +00003381 ConsumeBracket();
Alexey Bataev8fe24752013-11-18 08:17:37 +00003382 SkipUntil(tok::r_square, StopAtSemi | StopBeforeMatch);
John McCall7f040a92010-12-24 02:08:15 +00003383 if (endLoc) *endLoc = Tok.getLocation();
Francois Pichet334d47e2010-10-11 12:59:39 +00003384 ExpectAndConsume(tok::r_square, diag::err_expected_rsquare);
3385 }
3386}
Francois Pichet563a6452011-05-25 10:19:49 +00003387
3388void Parser::ParseMicrosoftIfExistsClassDeclaration(DeclSpec::TST TagType,
3389 AccessSpecifier& CurAS) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00003390 IfExistsCondition Result;
Francois Pichet563a6452011-05-25 10:19:49 +00003391 if (ParseMicrosoftIfExistsCondition(Result))
3392 return;
3393
Douglas Gregor3896fc52011-10-24 22:31:10 +00003394 BalancedDelimiterTracker Braces(*this, tok::l_brace);
3395 if (Braces.consumeOpen()) {
Francois Pichet563a6452011-05-25 10:19:49 +00003396 Diag(Tok, diag::err_expected_lbrace);
3397 return;
3398 }
Francois Pichet563a6452011-05-25 10:19:49 +00003399
Douglas Gregor3896fc52011-10-24 22:31:10 +00003400 switch (Result.Behavior) {
3401 case IEB_Parse:
3402 // Parse the declarations below.
3403 break;
3404
3405 case IEB_Dependent:
3406 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
3407 << Result.IsIfExists;
3408 // Fall through to skip.
3409
3410 case IEB_Skip:
3411 Braces.skipToEnd();
Francois Pichet563a6452011-05-25 10:19:49 +00003412 return;
3413 }
3414
Douglas Gregor3896fc52011-10-24 22:31:10 +00003415 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Francois Pichet563a6452011-05-25 10:19:49 +00003416 // __if_exists, __if_not_exists can nest.
3417 if ((Tok.is(tok::kw___if_exists) || Tok.is(tok::kw___if_not_exists))) {
3418 ParseMicrosoftIfExistsClassDeclaration((DeclSpec::TST)TagType, CurAS);
3419 continue;
3420 }
3421
3422 // Check for extraneous top-level semicolon.
3423 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003424 ConsumeExtraSemi(InsideStruct, TagType);
Francois Pichet563a6452011-05-25 10:19:49 +00003425 continue;
3426 }
3427
3428 AccessSpecifier AS = getAccessSpecifierIfPresent();
3429 if (AS != AS_none) {
3430 // Current token is a C++ access specifier.
3431 CurAS = AS;
3432 SourceLocation ASLoc = Tok.getLocation();
3433 ConsumeToken();
3434 if (Tok.is(tok::colon))
3435 Actions.ActOnAccessSpecifier(AS, ASLoc, Tok.getLocation());
3436 else
3437 Diag(Tok, diag::err_expected_colon);
3438 ConsumeToken();
3439 continue;
3440 }
3441
3442 // Parse all the comma separated declarators.
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003443 ParseCXXClassMemberDeclaration(CurAS, 0);
Francois Pichet563a6452011-05-25 10:19:49 +00003444 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00003445
3446 Braces.consumeClose();
Francois Pichet563a6452011-05-25 10:19:49 +00003447}